disable "auto switch to snap sync" (#813)

don't switch to snapsync
This commit is contained in:
HAOYUatHZ 2024-06-13 19:36:36 +08:00 committed by GitHub
parent ef2ac7ee8c
commit 1f58af2d5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 40 deletions

View file

@ -151,22 +151,26 @@ func newHandler(config *handlerConfig) (*handler, error) {
handlerStartCh: make(chan struct{}), handlerStartCh: make(chan struct{}),
} }
if config.Sync == downloader.FullSync { if config.Sync == downloader.FullSync {
// The database seems empty as the current block is the genesis. Yet the snap // Currently in Scroll we only support full sync,
// block is ahead, so snap sync was enabled for this node at a certain point. // so we should never switch to snap sync.
// The scenarios where this can happen is // TODO: revert this change when we support snap sync.
// * if the user manually (or via a bad block) rolled back a snap sync node
// below the sync point. // // The database seems empty as the current block is the genesis. Yet the snap
// * the last snap sync is not finished while user specifies a full sync this // // block is ahead, so snap sync was enabled for this node at a certain point.
// time. But we don't have any recent state for full sync. // // The scenarios where this can happen is
// In these cases however it's safe to reenable snap sync. // // * if the user manually (or via a bad block) rolled back a snap sync node
fullBlock, snapBlock := h.chain.CurrentBlock(), h.chain.CurrentSnapBlock() // // below the sync point.
if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 { // // * the last snap sync is not finished while user specifies a full sync this
h.snapSync.Store(true) // // time. But we don't have any recent state for full sync.
log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete") // // In these cases however it's safe to reenable snap sync.
} else if !h.chain.HasState(fullBlock.Root) { // fullBlock, snapBlock := h.chain.CurrentBlock(), h.chain.CurrentSnapBlock()
h.snapSync.Store(true) // if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 {
log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing") // h.snapSync.Store(true)
} // log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete")
// } else if !h.chain.HasState(fullBlock.Root) {
// h.snapSync.Store(true)
// log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing")
// }
} else { } else {
head := h.chain.CurrentBlock() head := h.chain.CurrentBlock()
if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) { if head.Number.Uint64() > 0 && h.chain.HasState(head.Root) {

View file

@ -189,32 +189,35 @@ func peerToSyncOp(mode downloader.SyncMode, p *eth.Peer) *chainSyncOp {
return &chainSyncOp{mode: mode, peer: p, td: peerTD, head: peerHead} return &chainSyncOp{mode: mode, peer: p, td: peerTD, head: peerHead}
} }
// Currently in Scroll we only support full sync,
// so we should never use snap sync.
// TODO: revert this change when we support snap sync.
func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, *big.Int) { func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, *big.Int) {
// If we're in snap sync mode, return that directly // // If we're in snap sync mode, return that directly
if cs.handler.snapSync.Load() { // if cs.handler.snapSync.Load() {
block := cs.handler.chain.CurrentSnapBlock() // block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64()) // td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
return downloader.SnapSync, td // return downloader.SnapSync, td
} // }
// We are probably in full sync, but we might have rewound to before the // // We are probably in full sync, but we might have rewound to before the
// snap sync pivot, check if we should re-enable snap sync. // // snap sync pivot, check if we should re-enable snap sync.
head := cs.handler.chain.CurrentBlock() head := cs.handler.chain.CurrentBlock()
if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil { // if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
if head.Number.Uint64() < *pivot { // if head.Number.Uint64() < *pivot {
block := cs.handler.chain.CurrentSnapBlock() // block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64()) // td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
return downloader.SnapSync, td // return downloader.SnapSync, td
} // }
} // }
// We are in a full sync, but the associated head state is missing. To complete // // We are in a full sync, but the associated head state is missing. To complete
// the head state, forcefully rerun the snap sync. Note it doesn't mean the // // the head state, forcefully rerun the snap sync. Note it doesn't mean the
// persistent state is corrupted, just mismatch with the head block. // // persistent state is corrupted, just mismatch with the head block.
if !cs.handler.chain.HasState(head.Root) { // if !cs.handler.chain.HasState(head.Root) {
block := cs.handler.chain.CurrentSnapBlock() // block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64()) // td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
log.Info("Reenabled snap sync as chain is stateless") // log.Info("Reenabled snap sync as chain is stateless")
return downloader.SnapSync, td // return downloader.SnapSync, td
} // }
// Nope, we're really full syncing // Nope, we're really full syncing
td := cs.handler.chain.GetTd(head.Hash(), head.Number.Uint64()) td := cs.handler.chain.GetTd(head.Hash(), head.Number.Uint64())
return downloader.FullSync, td return downloader.FullSync, td