Skip to content

Commit ed47f29

Browse files
committed
eth: enforce chain above CHT before accepting txs into the pool
1 parent 3873a73 commit ed47f29

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎eth/handler.go‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,30 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
181181
return blockchain.CurrentBlock().NumberU64()
182182
}
183183
inserter := func(blocks types.Blocks) (int, error) {
184-
// If fast sync is running, deny importing weird blocks
184+
// If sync hasn't reached the checkpoint yet, deny importing weird blocks.
185+
//
186+
// Ideally we would also compare the head block's timestamp and similarly reject
187+
// the propagated block if the head is too old. Unfortunately there is a corner
188+
// case when starting new networks, where the genesis might be ancient (0 unix)
189+
// which would prevent full nodes from accepting it.
190+
if manager.blockchain.CurrentBlock().NumberU64() < manager.checkpointNumber {
191+
log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
192+
return 0, nil
193+
}
194+
// If fast sync is running, deny importing weird blocks. This is a problematic
195+
// clause when starting up a new network, because fast-syncing miners might not
196+
// accept each others' blocks until a restart. Unfortunately we haven't figured
197+
// out a way yet where nodes can decide unilaterally whether the network is new
198+
// or not. This should be fixed if we figure out a solution.
185199
if atomic.LoadUint32(&manager.fastSync) == 1 {
186-
log.Warn("Discarded bad propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
200+
log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
187201
return 0, nil
188202
}
189-
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
190-
return manager.blockchain.InsertChain(blocks)
203+
n, err := manager.blockchain.InsertChain(blocks)
204+
if err == nil {
205+
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
206+
}
207+
return n, err
191208
}
192209
manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
193210

‎eth/sync.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,17 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
202202
log.Info("Fast sync complete, auto disabling")
203203
atomic.StoreUint32(&pm.fastSync, 0)
204204
}
205-
atomic.StoreUint32(&pm.acceptTxs, 1) // Mark initial sync done
206-
if head := pm.blockchain.CurrentBlock(); head.NumberU64() > 0 {
205+
// If we've successfully finished a sync cycle and passed any required checkpoint,
206+
// enable accepting transactions from the network.
207+
head := pm.blockchain.CurrentBlock()
208+
if head.NumberU64() >= pm.checkpointNumber {
209+
// Checkpoint passed, sanity check the timestamp to have a fallback mechanism
210+
// for non-checkpointed (number = 0) private networks.
211+
if head.Time() >= uint64(time.Now().AddDate(0, -1, 0).Unix()) {
212+
atomic.StoreUint32(&pm.acceptTxs, 1)
213+
}
214+
}
215+
if head.NumberU64() > 0 {
207216
// We've completed a sync cycle, notify all peers of new state. This path is
208217
// essential in star-topology networks where a gateway node needs to notify
209218
// all its out-of-date peers of the availability of a new block. This failure

0 commit comments

Comments
 (0)