mempool: Break dependency on chain instance.

This modifies the config for the new mempool package such that it takes
a callback function to obtain the best chain height instead of requiring
a fully initialized blockchain.BlockChain instance.

This will make it much easier to test the mempool since the tests will
be able to provide their own height function to test various
functionality without having create and manipulate full blocks and chain
instances.
This commit is contained in:
Dave Collins 2016-08-21 00:04:43 -05:00
parent c57c18c8e2
commit 641182b2ad
No known key found for this signature in database
GPG key ID: B8904D9D9C93D1F2
2 changed files with 8 additions and 8 deletions

View file

@ -55,9 +55,9 @@ type Config struct {
// transaction output information.
FetchUtxoView func(*btcutil.Tx) (*blockchain.UtxoViewpoint, error)
// Chain defines the concurrent safe block chain instance which houses
// BestHeight defines the function to use to access the block height of
// the current best chain.
Chain *blockchain.BlockChain
BestHeight func() int32
// SigCache defines a signature cache to use.
SigCache *txscript.SigCache
@ -532,8 +532,8 @@ func (mp *TxPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool)
// Get the current height of the main chain. A standalone transaction
// will be mined into the next block at best, so its height is at least
// one more than the current height.
best := mp.cfg.Chain.BestSnapshot()
nextBlockHeight := best.Height + 1
bestHeight := mp.cfg.BestHeight()
nextBlockHeight := bestHeight + 1
// Don't allow non-standard transactions if the network parameters
// forbid their relaying.
@ -733,7 +733,7 @@ func (mp *TxPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool)
}
// Add to transaction pool.
mp.addTransaction(utxoView, tx, best.Height, txFee)
mp.addTransaction(utxoView, tx, bestHeight, txFee)
log.Debugf("Accepted transaction %v (pool size: %v)", txHash,
len(mp.pool))
@ -1007,7 +1007,7 @@ func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseRe
result := make(map[string]*btcjson.GetRawMempoolVerboseResult,
len(mp.pool))
best := mp.cfg.Chain.BestSnapshot()
bestHeight := mp.cfg.BestHeight()
for _, desc := range mp.pool {
// Calculate the current priority based on the inputs to
@ -1018,7 +1018,7 @@ func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseRe
utxos, err := mp.fetchInputUtxos(tx)
if err == nil {
currentPriority = CalcPriority(tx.MsgTx(), utxos,
best.Height+1)
bestHeight+1)
}
mpd := &btcjson.GetRawMempoolVerboseResult{

View file

@ -2527,7 +2527,7 @@ func newServer(listenAddrs []string, db database.DB, chainParams *chaincfg.Param
},
ChainParams: chainParams,
FetchUtxoView: s.blockManager.chain.FetchUtxoView,
Chain: s.blockManager.chain,
BestHeight: func() int32 { return bm.chain.BestSnapshot().Height },
SigCache: s.sigCache,
TimeSource: s.timeSource,
AddrIndex: s.addrIndex,