|
|
@ -22,6 +22,7 @@ import ( |
|
|
|
"math" |
|
|
|
"math" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
"sync/atomic" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
@ -58,7 +59,7 @@ type blockFetcherFn func([]common.Hash) error |
|
|
|
type ProtocolManager struct { |
|
|
|
type ProtocolManager struct { |
|
|
|
networkId int |
|
|
|
networkId int |
|
|
|
|
|
|
|
|
|
|
|
fastSync bool |
|
|
|
fastSync uint32 |
|
|
|
txpool txPool |
|
|
|
txpool txPool |
|
|
|
blockchain *core.BlockChain |
|
|
|
blockchain *core.BlockChain |
|
|
|
chaindb ethdb.Database |
|
|
|
chaindb ethdb.Database |
|
|
@ -87,15 +88,9 @@ type ProtocolManager struct { |
|
|
|
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
|
|
|
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
|
|
|
// with the ethereum network.
|
|
|
|
// with the ethereum network.
|
|
|
|
func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) { |
|
|
|
func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.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") |
|
|
|
|
|
|
|
fastSync = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Create the protocol manager with the base fields
|
|
|
|
// Create the protocol manager with the base fields
|
|
|
|
manager := &ProtocolManager{ |
|
|
|
manager := &ProtocolManager{ |
|
|
|
networkId: networkId, |
|
|
|
networkId: networkId, |
|
|
|
fastSync: fastSync, |
|
|
|
|
|
|
|
eventMux: mux, |
|
|
|
eventMux: mux, |
|
|
|
txpool: txpool, |
|
|
|
txpool: txpool, |
|
|
|
blockchain: blockchain, |
|
|
|
blockchain: blockchain, |
|
|
@ -106,6 +101,14 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, |
|
|
|
txsyncCh: make(chan *txsync), |
|
|
|
txsyncCh: make(chan *txsync), |
|
|
|
quitSync: make(chan struct{}), |
|
|
|
quitSync: make(chan struct{}), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 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") |
|
|
|
|
|
|
|
fastSync = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if fastSync { |
|
|
|
|
|
|
|
manager.fastSync = uint32(1) |
|
|
|
|
|
|
|
} |
|
|
|
// Initiate a sub-protocol for every implemented version we can handle
|
|
|
|
// Initiate a sub-protocol for every implemented version we can handle
|
|
|
|
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions)) |
|
|
|
manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions)) |
|
|
|
for i, version := range ProtocolVersions { |
|
|
|
for i, version := range ProtocolVersions { |
|
|
@ -678,7 +681,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case msg.Code == TxMsg: |
|
|
|
case msg.Code == TxMsg: |
|
|
|
// Transactions arrived, parse all of them and deliver to the pool
|
|
|
|
// Transactions arrived, make sure we have a valid chain to handle them
|
|
|
|
|
|
|
|
if atomic.LoadUint32(&pm.fastSync) == 1 { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Transactions can be processed, parse all of them and deliver to the pool
|
|
|
|
var txs []*types.Transaction |
|
|
|
var txs []*types.Transaction |
|
|
|
if err := msg.Decode(&txs); err != nil { |
|
|
|
if err := msg.Decode(&txs); err != nil { |
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err) |
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err) |
|
|
|