@@ -22,6 +22,7 @@ import (
2222 "math"
2323 "math/big"
2424 "sync"
25+ "sync/atomic"
2526 "time"
2627
2728 "github.com/ethereum/go-ethereum/common"
@@ -58,7 +59,7 @@ type blockFetcherFn func([]common.Hash) error
5859type ProtocolManager struct {
5960 networkId int
6061
61- fastSync bool
62+ fastSync uint32
6263 txpool txPool
6364 blockchain * core.BlockChain
6465 chaindb ethdb.Database
@@ -87,15 +88,9 @@ type ProtocolManager struct {
8788// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
8889// with the ethereum network.
8990func NewProtocolManager (config * core.ChainConfig , fastSync bool , networkId int , mux * event.TypeMux , txpool txPool , pow pow.PoW , blockchain * core.BlockChain , chaindb ethdb.Database ) (* ProtocolManager , error ) {
90- // Figure out whether to allow fast sync or not
91- if fastSync && blockchain .CurrentBlock ().NumberU64 () > 0 {
92- glog .V (logger .Info ).Infof ("blockchain not empty, fast sync disabled" )
93- fastSync = false
94- }
9591 // Create the protocol manager with the base fields
9692 manager := & ProtocolManager {
9793 networkId : networkId ,
98- fastSync : fastSync ,
9994 eventMux : mux ,
10095 txpool : txpool ,
10196 blockchain : blockchain ,
@@ -106,6 +101,14 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
106101 txsyncCh : make (chan * txsync ),
107102 quitSync : make (chan struct {}),
108103 }
104+ // Figure out whether to allow fast sync or not
105+ if fastSync && blockchain .CurrentBlock ().NumberU64 () > 0 {
106+ glog .V (logger .Info ).Infof ("blockchain not empty, fast sync disabled" )
107+ fastSync = false
108+ }
109+ if fastSync {
110+ manager .fastSync = uint32 (1 )
111+ }
109112 // Initiate a sub-protocol for every implemented version we can handle
110113 manager .SubProtocols = make ([]p2p.Protocol , 0 , len (ProtocolVersions ))
111114 for i , version := range ProtocolVersions {
@@ -678,7 +681,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
678681 }
679682
680683 case msg .Code == TxMsg :
681- // Transactions arrived, parse all of them and deliver to the pool
684+ // Transactions arrived, make sure we have a valid chain to handle them
685+ if atomic .LoadUint32 (& pm .fastSync ) == 1 {
686+ break
687+ }
688+ // Transactions can be processed, parse all of them and deliver to the pool
682689 var txs []* types.Transaction
683690 if err := msg .Decode (& txs ); err != nil {
684691 return errResp (ErrDecode , "msg %v: %v" , msg , err )
0 commit comments