cmd, eth: implement flag delete-all-bad-blocks

This commit is contained in:
wit 2025-11-17 18:09:41 +08:00
parent 5e6f7374de
commit dff7741049
5 changed files with 31 additions and 1 deletions

View file

@ -51,6 +51,7 @@ func main() {
utils.HoodiFlag, utils.HoodiFlag,
utils.BlsyncApiFlag, utils.BlsyncApiFlag,
utils.BlsyncJWTSecretFlag, utils.BlsyncJWTSecretFlag,
utils.DeleteAllBadBlocksFlag,
}, },
debug.Flags, debug.Flags,
) )

View file

@ -669,6 +669,11 @@ var (
} }
// MISC settings // MISC settings
DeleteAllBadBlocksFlag = &cli.BoolFlag{
Name: "delete-all-bad-blocks",
Usage: "Delete all bad blocks in the database",
Category: flags.MiscCategory,
}
SyncTargetFlag = &cli.StringFlag{ SyncTargetFlag = &cli.StringFlag{
Name: "synctarget", Name: "synctarget",
Usage: `Hash of the block to full sync to (dev testing feature)`, Usage: `Hash of the block to full sync to (dev testing feature)`,
@ -1606,7 +1611,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// Avoid conflicting network flags, don't allow network id override on preset networks // Avoid conflicting network flags, don't allow network id override on preset networks
flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, NetworkIdFlag, OverrideGenesisFlag) flags.CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, HoodiFlag, NetworkIdFlag, OverrideGenesisFlag)
flags.CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer flags.CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
if ctx.Bool(DeleteAllBadBlocksFlag.Name) {
cfg.DeleteAllBadBlocks = true
}
// Set configurations from CLI flags // Set configurations from CLI flags
setEtherbase(ctx, cfg) setEtherbase(ctx, cfg)
setGPO(ctx, &cfg.GPO) setGPO(ctx, &cfg.GPO)

View file

@ -827,6 +827,13 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
} }
} }
// DeleteBadBlocks deletes all the bad blocks from the database
func DeleteBadBlocks(db ethdb.KeyValueWriter) {
if err := db.Delete(badBlockKey); err != nil {
log.Crit("Failed to delete bad blocks", "err", err)
}
}
// ReadHeadHeader returns the current canonical head header. // ReadHeadHeader returns the current canonical head header.
func ReadHeadHeader(db ethdb.Reader) *types.Header { func ReadHeadHeader(db ethdb.Reader) *types.Header {
headHeaderHash := ReadHeadHeaderHash(db) headHeaderHash := ReadHeadHeaderHash(db)

View file

@ -220,6 +220,20 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion) rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
} }
} }
badBlocks := rawdb.ReadAllBadBlocks(chainDb)
log.Info("Bad blocks in db", "count", len(badBlocks))
for i, block := range badBlocks {
log.Info("Bad block in db", "i", i, "number", block.Number(), "hash", block.Hash().Hex())
}
if config.DeleteAllBadBlocks {
if len(badBlocks) == 0 {
log.Warn("No bad blocks in db to delete")
} else {
rawdb.DeleteBadBlocks(chainDb)
log.Info(fmt.Sprintf("Deleted %d bad blocks in db", len(badBlocks)))
}
}
var ( var (
options = &core.BlockChainConfig{ options = &core.BlockChainConfig{
TrieCleanLimit: config.TrieCleanCache, TrieCleanLimit: config.TrieCleanCache,

View file

@ -120,6 +120,7 @@ type Config struct {
// Database options // Database options
SkipBcVersionCheck bool `toml:"-"` SkipBcVersionCheck bool `toml:"-"`
DeleteAllBadBlocks bool `toml:"-"`
DatabaseHandles int `toml:"-"` DatabaseHandles int `toml:"-"`
DatabaseCache int DatabaseCache int
DatabaseFreezer string DatabaseFreezer string