rpcserver: Update getblockchaininfo best header.

This modifies the getblockchaininfo RPC handler to use the newly-exposed
best header that is not known to be invalid from chain versus the height
of the current best chain tip.
This commit is contained in:
Dave Collins 2020-12-23 15:44:36 -06:00
parent b29315a2c2
commit f3890ca2cc
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
3 changed files with 15 additions and 1 deletions

View File

@ -236,6 +236,10 @@ type Chain interface {
// treated as immutable since it is shared by all callers.
BestSnapshot() *blockchain.BestState
// BestHeader returns the header with the most cumulative work that is NOT
// known to be invalid.
BestHeader() (chainhash.Hash, int64)
// BlockByHash returns the block for the given hash, regardless of whether the
// block is part of the main chain or not.
BlockByHash(hash *chainhash.Hash) (*dcrutil.Block, error)

View File

@ -2018,6 +2018,7 @@ func handleGetBlock(_ context.Context, s *Server, cmd interface{}) (interface{},
func handleGetBlockchainInfo(_ context.Context, s *Server, cmd interface{}) (interface{}, error) {
chain := s.cfg.Chain
best := chain.BestSnapshot()
_, bestHeaderHeight := chain.BestHeader()
// Fetch the current chain work using the the best block hash.
chainWork, err := chain.ChainWork(&best.Hash)
@ -2087,7 +2088,7 @@ func handleGetBlockchainInfo(_ context.Context, s *Server, cmd interface{}) (int
response := types.GetBlockChainInfoResult{
Chain: params.Name,
Blocks: best.Height,
Headers: best.Height,
Headers: bestHeaderHeight,
SyncHeight: syncHeight,
ChainWork: fmt.Sprintf("%064x", chainWork),
InitialBlockDownload: !chain.IsCurrent(),

View File

@ -127,6 +127,8 @@ type tspendVotes struct {
// testRPCChain provides a mock block chain by implementing the Chain interface.
type testRPCChain struct {
bestSnapshot *blockchain.BestState
bestHeaderHash chainhash.Hash
bestHeaderHeight int64
blockByHash *dcrutil.Block
blockByHashErr error
blockByHeight *dcrutil.Block
@ -193,6 +195,11 @@ func (c *testRPCChain) BestSnapshot() *blockchain.BestState {
return c.bestSnapshot
}
// BestHeader returns a mocked best header hash and height.
func (c *testRPCChain) BestHeader() (chainhash.Hash, int64) {
return c.bestHeaderHash, c.bestHeaderHeight
}
// BlockByHash returns a mocked block for the given hash.
func (c *testRPCChain) BlockByHash(hash *chainhash.Hash) (*dcrutil.Block, error) {
return c.blockByHash, c.blockByHashErr
@ -3476,6 +3483,8 @@ func TestHandleGetBlockchainInfo(t *testing.T) {
Hash: *hash,
PrevHash: *prevHash,
}
chain.bestHeaderHash = *hash
chain.bestHeaderHeight = 463073
chain.chainWork = big.NewInt(0).SetBytes([]byte{0x11, 0x5d, 0x28, 0x33, 0x84,
0x90, 0x90, 0xb0, 0x02, 0x65, 0x06})
chain.isCurrent = false