From dc98fecbecfd2272a66b7187f591817b071300d4 Mon Sep 17 00:00:00 2001 From: Matthieu Riou Date: Sat, 27 Feb 2016 14:09:33 -0800 Subject: [PATCH] Introducing a blockchain interface instead of passing the implementation directly to the protocol manager. Allows for better mocking and improves reusability of ProtocolManager.. --- eth/handler.go | 4 ++-- eth/protocol.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/eth/handler.go b/eth/handler.go index 2c5cae479f..1db87998d5 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -60,7 +60,7 @@ type ProtocolManager struct { fastSync bool txpool txPool - blockchain *core.BlockChain + blockchain blockChain chaindb ethdb.Database downloader *downloader.Downloader @@ -86,7 +86,7 @@ type ProtocolManager struct { // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable // with the ethereum network. -func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) { +func NewProtocolManager(fastSync bool, networkId int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain blockChain, chaindb ethdb.Database) (*ProtocolManager, error) { // Figure out whether to allow fast sync or not if fastSync && blockchain.CurrentBlock().NumberU64() > 0 { glog.V(logger.Info).Infof("blockchain not empty, fast sync disabled") diff --git a/eth/protocol.go b/eth/protocol.go index 808ac06011..9b10a5245c 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -123,6 +123,30 @@ type chainManager interface { Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) } +// proxy interface to core.BlockChain +type blockChain interface { + Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) + Genesis() *types.Block + CurrentHeader() *types.Header + CurrentBlock() *types.Block + CurrentFastBlock() *types.Block + HasHeader(hash common.Hash) bool + HasBlock(hash common.Hash) bool + HasBlockAndState(hash common.Hash) bool + GetHeader(hash common.Hash) *types.Header + GetHeaderByNumber(number uint64) *types.Header + GetBlock(hash common.Hash) *types.Block + GetBlockByNumber(number uint64) *types.Block + GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash + GetBodyRLP(hash common.Hash) rlp.RawValue + FastSyncCommitHead(hash common.Hash) error + GetTd(hash common.Hash) *big.Int + InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) + InsertChain(chain types.Blocks) (int, error) + InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) + Rollback(chain []common.Hash) +} + // statusData is the network packet for the status message. type statusData struct { ProtocolVersion uint32