Skip to content

Commit ecb8e23

Browse files
committed
[release/1.4.6] eth: don't accept transactions until we sync up with the network
(cherry picked from commit 32559cc)
1 parent 058c5fe commit ecb8e23

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

eth/handler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type blockFetcherFn func([]common.Hash) error
5959
type ProtocolManager struct {
6060
networkId int
6161

62-
fastSync uint32
62+
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
63+
synced uint32 // Flag whether we're considered synchronised (enables transaction processing)
64+
6365
txpool txPool
6466
blockchain *core.BlockChain
6567
chaindb ethdb.Database
@@ -161,7 +163,11 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
161163
heighter := func() uint64 {
162164
return blockchain.CurrentBlock().NumberU64()
163165
}
164-
manager.fetcher = fetcher.New(blockchain.GetBlock, validator, manager.BroadcastBlock, heighter, manager.insertChain, manager.removePeer)
166+
inserter := func(blocks types.Blocks) (int, error) {
167+
atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
168+
return manager.insertChain(blocks)
169+
}
170+
manager.fetcher = fetcher.New(blockchain.GetBlock, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
165171

166172
if blockchain.Genesis().Hash().Hex() == defaultGenesisHash && networkId == 1 {
167173
glog.V(logger.Debug).Infoln("Bad Block Reporting is enabled")
@@ -698,8 +704,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
698704
}
699705

700706
case msg.Code == TxMsg:
701-
// Transactions arrived, make sure we have a valid chain to handle them
702-
if atomic.LoadUint32(&pm.fastSync) == 1 {
707+
// Transactions arrived, make sure we have a valid and fresh chain to handle them
708+
if atomic.LoadUint32(&pm.synced) == 0 {
703709
break
704710
}
705711
// Transactions can be processed, parse all of them and deliver to the pool

eth/protocol_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
9797
func testRecvTransactions(t *testing.T, protocol int) {
9898
txAdded := make(chan []*types.Transaction)
9999
pm := newTestProtocolManagerMust(t, false, 0, nil, txAdded)
100+
pm.synced = 1 // mark synced to accept transactions
100101
p, _ := newTestPeer("peer", protocol, pm, true)
101102
defer pm.Stop()
102103
defer p.close()

eth/sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
174174
if err := pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), mode); err != nil {
175175
return
176176
}
177+
atomic.StoreUint32(&pm.synced, 1) // Mark initial sync done
178+
177179
// If fast sync was enabled, and we synced up, disable it
178180
if atomic.LoadUint32(&pm.fastSync) == 1 {
179181
// Disable fast sync if we indeed have something in our chain

0 commit comments

Comments
 (0)