From 7bedd7dc0a64b7394bcee41d3d5aae80eb748bcb Mon Sep 17 00:00:00 2001 From: Ryan Staudt Date: Mon, 6 Sep 2021 07:27:34 -0500 Subject: [PATCH] mempool: Add HeaderByHash to Config. This adds a HeaderByHash method to the mempool Config. This will be used as part of the automatic ticket revocations changes in a subsequent commit. --- internal/mempool/mempool.go | 5 +++++ internal/mempool/mempool_test.go | 13 +++++++++++++ server.go | 1 + 3 files changed, 19 insertions(+) diff --git a/internal/mempool/mempool.go b/internal/mempool/mempool.go index c2eb0218..be7db8eb 100644 --- a/internal/mempool/mempool.go +++ b/internal/mempool/mempool.go @@ -106,6 +106,11 @@ type Config struct { // the current best chain. BestHeight func() int64 + // HeaderByHash returns the block header identified by the given hash or an + // error if it doesn't exist. Note that this will return headers from both + // the main chain and any side chains. + HeaderByHash func(hash *chainhash.Hash) (wire.BlockHeader, error) + // PastMedianTime defines the function to use in order to access the // median time calculated from the point-of-view of the current chain // tip within the best chain. diff --git a/internal/mempool/mempool_test.go b/internal/mempool/mempool_test.go index 4829381e..7da6e85d 100644 --- a/internal/mempool/mempool_test.go +++ b/internal/mempool/mempool_test.go @@ -157,6 +157,18 @@ func (s *fakeChain) SetHeight(height int64) { s.Unlock() } +// HeaderByHash returns the header for the block with the given hash from the +// fake chain instance. Blocks can be added to the instance with the AddBlock +// function. +func (s *fakeChain) HeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error) { + block, ok := s.blocks[*hash] + if !ok { + return wire.BlockHeader{}, fmt.Errorf("unable to find block %v in fake "+ + "chain", hash) + } + return block.MsgBlock().Header, nil +} + // PastMedianTime returns the current median time associated with the fake chain // instance. func (s *fakeChain) PastMedianTime() time.Time { @@ -818,6 +830,7 @@ func newPoolHarness(chainParams *chaincfg.Params) (*poolHarness, []spendableOutp BlockByHash: chain.BlockByHash, BestHash: chain.BestHash, BestHeight: chain.BestHeight, + HeaderByHash: chain.HeaderByHash, PastMedianTime: chain.PastMedianTime, CalcSequenceLock: chain.CalcSequenceLock, TSpendMinedOnAncestor: chain.TSpendMinedOnAncestor, diff --git a/server.go b/server.go index f0341566..650b7ccf 100644 --- a/server.go +++ b/server.go @@ -3558,6 +3558,7 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB, BlockByHash: s.chain.BlockByHash, BestHash: func() *chainhash.Hash { return &s.chain.BestSnapshot().Hash }, BestHeight: func() int64 { return s.chain.BestSnapshot().Height }, + HeaderByHash: s.chain.HeaderByHash, CalcSequenceLock: s.chain.CalcSequenceLock, SubsidyCache: s.subsidyCache, SigCache: s.sigCache,