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{}),
}
if config.Sync == downloader.FullSync {
// The database seems empty as the current block is the genesis. Yet the snap
// block is ahead, so snap sync was enabled for this node at a certain point.
// The scenarios where this can happen is
// * if the user manually (or via a bad block) rolled back a snap sync node
// below the sync point.
// * the last snap sync is not finished while user specifies a full sync this
// time. But we don't have any recent state for full sync.
// In these cases however it's safe to reenable snap sync.
fullBlock, snapBlock := h.chain.CurrentBlock(), h.chain.CurrentSnapBlock()
if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 {
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")
}
// Currently in Scroll we only support full sync,
// so we should never switch to snap sync.
// TODO: revert this change when we support snap sync.
// // The database seems empty as the current block is the genesis. Yet the snap
// // block is ahead, so snap sync was enabled for this node at a certain point.
// // The scenarios where this can happen is
// // * if the user manually (or via a bad block) rolled back a snap sync node
// // below the sync point.
// // * the last snap sync is not finished while user specifies a full sync this
// // time. But we don't have any recent state for full sync.
// // In these cases however it's safe to reenable snap sync.
// fullBlock, snapBlock := h.chain.CurrentBlock(), h.chain.CurrentSnapBlock()
// if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 {
// 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 {
head := h.chain.CurrentBlock()
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}
}
// 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) {
// If we're in snap sync mode, return that directly
if cs.handler.snapSync.Load() {
block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
return downloader.SnapSync, td
}
// 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.
// // If we're in snap sync mode, return that directly
// if cs.handler.snapSync.Load() {
// block := cs.handler.chain.CurrentSnapBlock()
// td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
// return downloader.SnapSync, td
// }
// // 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.
head := cs.handler.chain.CurrentBlock()
if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
if head.Number.Uint64() < *pivot {
block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
return downloader.SnapSync, td
}
}
// 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
// persistent state is corrupted, just mismatch with the head block.
if !cs.handler.chain.HasState(head.Root) {
block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
log.Info("Reenabled snap sync as chain is stateless")
return downloader.SnapSync, td
}
// if pivot := rawdb.ReadLastPivotNumber(cs.handler.database); pivot != nil {
// if head.Number.Uint64() < *pivot {
// block := cs.handler.chain.CurrentSnapBlock()
// td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
// return downloader.SnapSync, td
// }
// }
// // 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
// // persistent state is corrupted, just mismatch with the head block.
// if !cs.handler.chain.HasState(head.Root) {
// block := cs.handler.chain.CurrentSnapBlock()
// td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
// log.Info("Reenabled snap sync as chain is stateless")
// return downloader.SnapSync, td
// }
// Nope, we're really full syncing
td := cs.handler.chain.GetTd(head.Hash(), head.Number.Uint64())
return downloader.FullSync, td