aboutsummaryrefslogtreecommitdiffstats
path: root/eth/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'eth/handler.go')
-rw-r--r--eth/handler.go48
1 files changed, 17 insertions, 31 deletions
diff --git a/eth/handler.go b/eth/handler.go
index ade8f7719..fb8a0fd57 100644
--- a/eth/handler.go
+++ b/eth/handler.go
@@ -27,6 +27,8 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/consensus"
+ "github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
@@ -37,7 +39,6 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rlp"
)
@@ -61,8 +62,8 @@ func errResp(code errCode, format string, v ...interface{}) error {
type ProtocolManager struct {
networkId int
- fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
- synced uint32 // Flag whether we're considered synchronised (enables transaction processing)
+ fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
+ acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
txpool txPool
blockchain *core.BlockChain
@@ -91,13 +92,11 @@ type ProtocolManager struct {
// wait group is used for graceful shutdowns during downloading
// and processing
wg sync.WaitGroup
-
- badBlockReportingEnabled bool
}
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
// with the ethereum network.
-func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int, maxPeers int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
+func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkId int, maxPeers int, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
// Create the protocol manager with the base fields
manager := &ProtocolManager{
networkId: networkId,
@@ -114,18 +113,18 @@ func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int
quitSync: make(chan struct{}),
}
// Figure out whether to allow fast sync or not
- if fastSync && blockchain.CurrentBlock().NumberU64() > 0 {
+ if mode == downloader.FastSync && blockchain.CurrentBlock().NumberU64() > 0 {
log.Warn("Blockchain not empty, fast sync disabled")
- fastSync = false
+ mode = downloader.FullSync
}
- if fastSync {
+ if mode == downloader.FastSync {
manager.fastSync = uint32(1)
}
// Initiate a sub-protocol for every implemented version we can handle
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
for i, version := range ProtocolVersions {
// Skip protocol version if incompatible with the mode of operation
- if fastSync && version < eth63 {
+ if mode == downloader.FastSync && version < eth63 {
continue
}
// Compatible; initialise the sub-protocol
@@ -160,39 +159,26 @@ func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int
return nil, errIncompatibleConfig
}
// Construct the different synchronisation mechanisms
- manager.downloader = downloader.New(downloader.FullSync, chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash,
+ manager.downloader = downloader.New(mode, chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash,
blockchain.GetBlockByHash, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead,
- blockchain.GetTdByHash, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
+ blockchain.GetTdByHash, blockchain.InsertHeaderChain, manager.blockchain.InsertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
manager.removePeer)
- validator := func(block *types.Block, parent *types.Block) error {
- return core.ValidateHeader(config, pow, block.Header(), parent.Header(), true, false)
+ validator := func(header *types.Header) error {
+ return engine.VerifyHeader(blockchain, header, true)
}
heighter := func() uint64 {
return blockchain.CurrentBlock().NumberU64()
}
inserter := func(blocks types.Blocks) (int, error) {
- atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
- return manager.insertChain(blocks)
+ atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
+ return manager.blockchain.InsertChain(blocks)
}
manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
- if blockchain.Genesis().Hash().Hex() == defaultGenesisHash && networkId == 1 {
- log.Debug("Bad block reporting is enabled")
- manager.badBlockReportingEnabled = true
- }
-
return manager, nil
}
-func (pm *ProtocolManager) insertChain(blocks types.Blocks) (i int, err error) {
- i, err = pm.blockchain.InsertChain(blocks)
- if pm.badBlockReportingEnabled && core.IsValidationErr(err) && i < len(blocks) {
- go sendBadBlockReport(blocks[i], err)
- }
- return i, err
-}
-
func (pm *ProtocolManager) removePeer(id string) {
// Short circuit if the peer was already removed
peer := pm.peers.Peer(id)
@@ -448,7 +434,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
p.forkDrop = nil
// Validate the header and either drop the peer or continue
- if err := core.ValidateDAOHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
+ if err := misc.VerifyDAOHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
p.Log().Debug("Verified to be on the other side of the DAO fork, dropping")
return err
}
@@ -657,7 +643,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
case msg.Code == TxMsg:
// Transactions arrived, make sure we have a valid and fresh chain to handle them
- if atomic.LoadUint32(&pm.synced) == 0 {
+ if atomic.LoadUint32(&pm.acceptTxs) == 0 {
break
}
// Transactions can be processed, parse all of them and deliver to the pool