Skip to content

Commit

Permalink
rpcserver: Update getblockchaininfo best header.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davecgh committed Dec 31, 2020
1 parent c2f1441 commit 3e43709
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/rpcserver/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down
9 changes: 9 additions & 0 deletions internal/rpcserver/rpcserverhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3495,6 +3502,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
Expand Down

0 comments on commit 3e43709

Please sign in to comment.