From 782fc61dc10f6aa02254e25ca0d011b0e688f227 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 22 Dec 2016 12:34:51 -0800 Subject: [PATCH] lnwallet+lnrpc: convert GetCurrentHeight to GetBestBlock in BlockChainIO This commit expands the data returned by the current GetCurrentHeight to also return the current best block hash, expanding the method into GetBestBlock. Additionally, the current best BlockHash is also now displayed within the GetInfo RPC call. --- lnrpc/rpc.proto | 14 +++++++------ lnwallet/btcwallet/blockchain.go | 13 ++++-------- lnwallet/interface.go | 35 ++++++++++++++++---------------- rpcserver.go | 5 +++-- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/lnrpc/rpc.proto b/lnrpc/rpc.proto index 297a877b..d5cdbe78 100644 --- a/lnrpc/rpc.proto +++ b/lnrpc/rpc.proto @@ -256,16 +256,18 @@ message ListPeersResponse { message GetInfoRequest{} message GetInfoResponse { string identity_pubkey = 1; + string alias = 2; - uint32 num_pending_channels = 2; - uint32 num_active_channels = 3; + uint32 num_pending_channels = 3; + uint32 num_active_channels = 4; - uint32 num_peers = 4; + uint32 num_peers = 5; - uint32 block_height = 5; + uint32 block_height = 6; + string block_hash = 8; - bool synced_to_chain = 6; - bool testnet = 7; + bool synced_to_chain = 9; + bool testnet = 10; } message ConfirmationUpdate { diff --git a/lnwallet/btcwallet/blockchain.go b/lnwallet/btcwallet/blockchain.go index 17078e35..5618d6e8 100644 --- a/lnwallet/btcwallet/blockchain.go +++ b/lnwallet/btcwallet/blockchain.go @@ -6,17 +6,12 @@ import ( "github.com/roasbeef/btcd/wire" ) -// GetCurrentHeight returns the current height of the known block within the -// main chain. +// GetBestBlock returns the current height and hash of the best known block +// within the main chain. // // This method is a part of the lnwallet.BlockChainIO interface. -func (b *BtcWallet) GetCurrentHeight() (int32, error) { - _, height, err := b.rpc.GetBestBlock() - if err != nil { - return 0, err - } - - return height, nil +func (b *BtcWallet) GetBestBlock() (*wire.ShaHash, int32, error) { + return b.rpc.GetBestBlock() } // GetTxOut returns the original output referenced by the passed outpoint. diff --git a/lnwallet/interface.go b/lnwallet/interface.go index 23ec6c22..eba2882d 100644 --- a/lnwallet/interface.go +++ b/lnwallet/interface.go @@ -114,7 +114,7 @@ type WalletController interface { ConfirmedBalance(confs int32, witness bool) (btcutil.Amount, error) // NewAddress returns the next external or internal address for the - // wallet dicatated by the value of the `change` paramter. If change is + // wallet dictated by the value of the `change` parameter. If change is // true, then an internal address should be used, otherwise an external // address should be returned. The type of address returned is dictated // by the wallet's capabilities, and may be of type: p2sh, p2pkh, @@ -162,7 +162,7 @@ type WalletController interface { LockOutpoint(o wire.OutPoint) // UnlockOutpoint unlocks an previously locked output, marking it - // eligible for coin seleciton. + // eligible for coin selection. UnlockOutpoint(o wire.OutPoint) // PublishTransaction performs cursory validation (dust checks, etc), @@ -201,23 +201,24 @@ type WalletController interface { // TODO(roasbeef): move to diff package perhaps? // TODO(roasbeef): move publish txn here? type BlockChainIO interface { - // GetCurrentHeight returns the current height of the valid most-work - // chain the implementation is aware of. - GetCurrentHeight() (int32, error) + // GetBestBlock returns the current height and block hash of the valid + // most-work chain the implementation is aware of. + GetBestBlock() (*wire.ShaHash, int32, error) // GetTxOut returns the original output referenced by the passed // outpoint. GetUtxo(txid *wire.ShaHash, index uint32) (*wire.TxOut, error) // GetTransaction returns the full transaction identified by the passed - // transaction ID. - GetTransaction(txid *wire.ShaHash) (*wire.MsgTx, error) + // transaction ID. + GetTransaction(txid *wire.ShaHash) (*wire.MsgTx, error) - // GetBlockHash returns the hash of the block in the best block chain at the - // given height. + // GetBlockHash returns the hash of the block in the best block chain + // at the given height. GetBlockHash(blockHeight int64) (*wire.ShaHash, error) - // GetBlock returns a block by the given hash. + // GetBlock returns the block in the main chain identified by the given + // hash. GetBlock(blockHash *wire.ShaHash) (*wire.MsgBlock, error) } @@ -278,14 +279,14 @@ type Signer interface { // transaction with the signature as defined within the passed // SignDescriptor. This method should be capable of generating the // proper input script for both regular p2wkh output and p2wkh outputs - // nested within a regualr p2sh output. + // nested within a regular p2sh output. ComputeInputScript(tx *wire.MsgTx, signDesc *SignDescriptor) (*InputScript, error) } // WalletDriver represents a "driver" for a particular concrete -// WalletController implementation. A driver is indentified by a globally -// unique string identifier along with a 'New()' method which is responsible -// for initializing a particular WalletController concrete implementation. +// WalletController implementation. A driver is identified by a globally unique +// string identifier along with a 'New()' method which is responsible for +// initializing a particular WalletController concrete implementation. type WalletDriver struct { // WalletType is a string which uniquely identifes the WalletController // that this driver, drives. @@ -293,8 +294,8 @@ type WalletDriver struct { // New creates a new instance of a concrete WalletController // implementation given a variadic set up arguments. The function takes - // a varidaic number of interface paramters in order to provide - // initialization flexibility, thereby accomodating several potential + // a varidaic number of interface parameters in order to provide + // initialization flexibility, thereby accommodating several potential // WalletController implementations. New func(args ...interface{}) (WalletController, error) } @@ -337,7 +338,7 @@ func RegisterWallet(driver *WalletDriver) error { return nil } -// SupportedWallets returns a slice of strings that represents the walelt +// SupportedWallets returns a slice of strings that represents the wallet // drivers that have been registered and are therefore supported. // // NOTE: This function is safe for concurrent access. diff --git a/rpcserver.go b/rpcserver.go index c836ffce..57e4f9f1 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -422,7 +422,7 @@ func (r *rpcServer) GetInfo(ctx context.Context, pendingChannels := r.server.fundingMgr.NumPendingChannels() idPub := r.server.identityPriv.PubKey().SerializeCompressed() - currentHeight, err := r.server.bio.GetCurrentHeight() + bestHash, bestHeight, err := r.server.bio.GetBestBlock() if err != nil { return nil, err } @@ -437,7 +437,8 @@ func (r *rpcServer) GetInfo(ctx context.Context, NumPendingChannels: pendingChannels, NumActiveChannels: activeChannels, NumPeers: uint32(len(serverPeers)), - BlockHeight: uint32(currentHeight), + BlockHeight: uint32(bestHeight), + BlockHash: bestHash.String(), SyncedToChain: isSynced, Testnet: activeNetParams.Params == &chaincfg.TestNet3Params, }, nil