Skip to content

Commit fd072c2

Browse files
rjl493456442karalabe
authored andcommitted
eth: fix sync bloom panic (#19757)
* eth: fix sync bloom panic * eth: delete useless test cases
1 parent 54fd263 commit fd072c2

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

eth/handler.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,28 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
121121
txsyncCh: make(chan *txsync),
122122
quitSync: make(chan struct{}),
123123
}
124-
// If fast sync was requested and our database is empty, grant it
125-
if mode == downloader.FastSync && blockchain.CurrentBlock().NumberU64() == 0 {
126-
manager.fastSync = uint32(1)
124+
if mode == downloader.FullSync {
125+
// The database seems empty as the current block is the genesis. Yet the fast
126+
// block is ahead, so fast sync was enabled for this node at a certain point.
127+
// The scenarios where this can happen is
128+
// * if the user manually (or via a bad block) rolled back a fast sync node
129+
// below the sync point.
130+
// * the last fast sync is not finished while user specifies a full sync this
131+
// time. But we don't have any recent state for full sync.
132+
// In these cases however it's safe to reenable fast sync.
133+
fullBlock, fastBlock := blockchain.CurrentBlock(), blockchain.CurrentFastBlock()
134+
if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 {
135+
manager.fastSync = uint32(1)
136+
log.Warn("Switch sync mode from full sync to fast sync")
137+
}
138+
} else {
139+
if blockchain.CurrentBlock().NumberU64() > 0 {
140+
// Print warning log if database is not empty to run fast sync.
141+
log.Warn("Switch sync mode from fast sync to full sync")
142+
} else {
143+
// If fast sync was requested and our database is empty, grant it
144+
manager.fastSync = uint32(1)
145+
}
127146
}
128147
// If we have trusted checkpoints, enforce them on the chain
129148
if checkpoint, ok := params.TrustedCheckpoints[blockchain.Genesis().Hash()]; ok {

eth/handler_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,27 +468,22 @@ func TestCheckpointChallenge(t *testing.T) {
468468
// If checkpointing is not enabled locally, don't challenge and don't drop
469469
{downloader.FullSync, false, false, false, false, false},
470470
{downloader.FastSync, false, false, false, false, false},
471-
{downloader.LightSync, false, false, false, false, false},
472471

473472
// If checkpointing is enabled locally and remote response is empty, only drop during fast sync
474473
{downloader.FullSync, true, false, true, false, false},
475474
{downloader.FastSync, true, false, true, false, true}, // Special case, fast sync, unsynced peer
476-
{downloader.LightSync, true, false, true, false, false},
477475

478476
// If checkpointing is enabled locally and remote response mismatches, always drop
479477
{downloader.FullSync, true, false, false, false, true},
480478
{downloader.FastSync, true, false, false, false, true},
481-
{downloader.LightSync, true, false, false, false, true},
482479

483480
// If checkpointing is enabled locally and remote response matches, never drop
484481
{downloader.FullSync, true, false, false, true, false},
485482
{downloader.FastSync, true, false, false, true, false},
486-
{downloader.LightSync, true, false, false, true, false},
487483

488484
// If checkpointing is enabled locally and remote times out, always drop
489485
{downloader.FullSync, true, true, false, true, true},
490486
{downloader.FastSync, true, true, false, true, true},
491-
{downloader.LightSync, true, true, false, true, true},
492487
}
493488
for _, tt := range tests {
494489
t.Run(fmt.Sprintf("sync %v checkpoint %v timeout %v empty %v match %v", tt.syncmode, tt.checkpoint, tt.timeout, tt.empty, tt.match), func(t *testing.T) {

eth/sync.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
179179
if atomic.LoadUint32(&pm.fastSync) == 1 {
180180
// Fast sync was explicitly requested, and explicitly granted
181181
mode = downloader.FastSync
182-
} else if currentBlock.NumberU64() == 0 && pm.blockchain.CurrentFastBlock().NumberU64() > 0 {
183-
// The database seems empty as the current block is the genesis. Yet the fast
184-
// block is ahead, so fast sync was enabled for this node at a certain point.
185-
// The only scenario where this can happen is if the user manually (or via a
186-
// bad block) rolled back a fast sync node below the sync point. In this case
187-
// however it's safe to reenable fast sync.
188-
atomic.StoreUint32(&pm.fastSync, 1)
189-
mode = downloader.FastSync
190182
}
191183
if mode == downloader.FastSync {
192184
// Make sure the peer's total difficulty we are synchronizing is higher.

0 commit comments

Comments
 (0)