Introducing a blockchain interface instead of passing the implementation directly to the protocol manager. Allows for better mocking and improves reusability of ProtocolManager..

This commit is contained in:
Matthieu Riou 2016-02-27 14:09:33 -08:00
parent b3b110bc95
commit dc98fecbec
2 changed files with 26 additions and 2 deletions

View file

@ -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")

View file

@ -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