2022-06-07 18:59:14 +00:00
package migrations
import (
"context"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/rawdb/rawdbreset"
2022-07-04 12:43:46 +00:00
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
2022-06-07 18:59:14 +00:00
"github.com/ledgerwatch/erigon/node/nodecfg/datadir"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/snap"
"github.com/ledgerwatch/log/v3"
)
var resetBlocks = Migration {
Name : "reset_blocks_3" ,
Up : func ( db kv . RwDB , dirs datadir . Dirs , progress [ ] byte , BeforeCommit Callback ) ( err error ) {
tx , err := db . BeginRw ( context . Background ( ) )
if err != nil {
return err
}
defer tx . Rollback ( )
enabled , err := snap . Enabled ( tx )
if err != nil {
return err
}
if ! enabled {
if err := BeforeCommit ( tx , nil , true ) ; err != nil {
return err
}
2022-07-18 07:38:01 +00:00
return tx . Commit ( )
2022-06-07 18:59:14 +00:00
}
genesisBlock := rawdb . ReadHeaderByNumber ( tx , 0 )
if genesisBlock == nil {
if err := BeforeCommit ( tx , nil , true ) ; err != nil {
return err
}
2022-07-18 07:38:01 +00:00
return tx . Commit ( )
2022-06-07 18:59:14 +00:00
}
chainConfig , err := rawdb . ReadChainConfig ( tx , genesisBlock . Hash ( ) )
if err != nil {
return err
}
2022-07-04 12:43:46 +00:00
headersProgress , _ := stages . GetStageProgress ( tx , stages . Headers )
if headersProgress > 0 {
log . Warn ( "NOTE: this migration will remove recent blocks (and senders) to fix several recent bugs. Your node will re-download last ~400K blocks, should not take very long" )
}
2022-06-07 18:59:14 +00:00
if err := snap . RemoveNonPreverifiedFiles ( chainConfig . ChainName , dirs . Snap ) ; err != nil {
return err
}
if err := rawdbreset . ResetBlocks ( tx ) ; err != nil {
return err
}
if err := rawdbreset . ResetSenders ( tx ) ; err != nil {
return err
}
if err := rawdbreset . ResetTxLookup ( tx ) ; err != nil {
return err
}
// This migration is no-op, but it forces the migration mechanism to apply it and thus write the DB schema version info
if err := BeforeCommit ( tx , nil , true ) ; err != nil {
return err
}
return tx . Commit ( )
} ,
}