From fd357f034d80a602181c9e33d756f03710b46307 Mon Sep 17 00:00:00 2001
From: racytech <82003208+racytech@users.noreply.github.com>
Date: Wed, 1 Sep 2021 17:16:25 -0400
Subject: [PATCH] `bad.hash` flag added to exclude block by hash and not by
 number (#2612)

* BadHashFlag added

* bad.hash to bad.block
---
 eth/backend.go          | 16 +++++++++++-----
 eth/ethconfig/config.go |  6 +++---
 turbo/cli/flags.go      | 15 +++++++++++----
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/eth/backend.go b/eth/backend.go
index 59d80e82c3..1f0102e5a1 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -447,16 +447,22 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere
 	if err != nil {
 		return nil, err
 	}
-	if config.BadBlock != 0 {
-		var badHash common.Hash
+
+	emptyBadHash := config.BadBlockHash == common.Hash{}
+	if !emptyBadHash {
+		var badBlockHeader *types.Header
 		if err = chainKv.View(context.Background(), func(tx kv.Tx) error {
-			var hErr error
-			badHash, hErr = rawdb.ReadCanonicalHash(tx, config.BadBlock)
+			header, hErr := rawdb.ReadHeaderByHash(tx, config.BadBlockHash)
+			badBlockHeader = header
 			return hErr
 		}); err != nil {
 			return nil, err
 		}
-		backend.stagedSync.UnwindTo(config.BadBlock-1, badHash)
+
+		if badBlockHeader != nil {
+			unwindPoint := badBlockHeader.Number.Uint64() - 1
+			backend.stagedSync.UnwindTo(unwindPoint, config.BadBlockHash)
+		}
 	}
 
 	go txpropagate.BroadcastPendingTxsToNetwork(backend.downloadCtx, backend.txPool, backend.txPoolP2PServer.RecentPeers, backend.downloadServer)
diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go
index 503fdb9195..a116e27d48 100644
--- a/eth/ethconfig/config.go
+++ b/eth/ethconfig/config.go
@@ -137,9 +137,9 @@ type Config struct {
 
 	P2PEnabled bool
 
-	Prune     prune.Mode
-	BatchSize datasize.ByteSize // Batch size for execution stage
-	BadBlock  uint64            // Block marked as bad (for forced reorg)
+	Prune        prune.Mode
+	BatchSize    datasize.ByteSize // Batch size for execution stage
+	BadBlockHash common.Hash       // hash of the block marked as bad
 
 	Snapshot Snapshot
 
diff --git a/turbo/cli/flags.go b/turbo/cli/flags.go
index b023b06105..f1c86c56fc 100644
--- a/turbo/cli/flags.go
+++ b/turbo/cli/flags.go
@@ -8,7 +8,9 @@ import (
 	"github.com/c2h5oh/datasize"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon/cmd/utils"
+	"github.com/ledgerwatch/erigon/common"
 	"github.com/ledgerwatch/erigon/common/etl"
+	"github.com/ledgerwatch/erigon/common/hexutil"
 	"github.com/ledgerwatch/erigon/eth/ethconfig"
 	"github.com/ledgerwatch/erigon/ethdb/prune"
 	"github.com/ledgerwatch/erigon/node"
@@ -150,10 +152,10 @@ var (
 		Value: "",
 	}
 
-	BadBlockFlag = cli.IntFlag{
+	BadBlockFlag = cli.StringFlag{
 		Name:  "bad.block",
-		Usage: "Marks block with given number bad and forces initial reorg before normal staged sync",
-		Value: 0,
+		Usage: "Marks block with given hex string as bad and forces initial reorg before normal staged sync",
+		Value: "",
 	}
 )
 
@@ -207,7 +209,12 @@ func ApplyFlagsForEthConfig(ctx *cli.Context, cfg *ethconfig.Config) {
 		}
 		cfg.SyncLoopThrottle = syncLoopThrottle
 	}
-	cfg.BadBlock = uint64(ctx.GlobalInt(BadBlockFlag.Name))
+
+	bytes, err := hexutil.Decode(BadBlockFlag.Value)
+	if err != nil {
+		log.Warn("Error decoding hash %v: %v", BadBlockFlag.Value, err)
+	}
+	cfg.BadBlockHash = common.BytesToHash(bytes)
 }
 
 func ApplyFlagsForEthConfigCobra(f *pflag.FlagSet, cfg *ethconfig.Config) {
-- 
GitLab