|
| 1 | +// Copyright 2026 The Erigon Authors |
| 2 | +// This file is part of Erigon. |
| 3 | +// |
| 4 | +// Erigon is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Erigon is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with Erigon. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package execmodule |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/erigontech/erigon/common" |
| 24 | + "github.com/erigontech/erigon/db/kv" |
| 25 | + "github.com/erigontech/erigon/db/kv/rawdbv3" |
| 26 | + "github.com/erigontech/erigon/db/rawdb" |
| 27 | + "github.com/erigontech/erigon/db/rawdb/rawtemporaldb" |
| 28 | + "github.com/erigontech/erigon/db/state/execctx" |
| 29 | + "github.com/erigontech/erigon/execution/stagedsync" |
| 30 | + "github.com/erigontech/erigon/execution/stagedsync/stages" |
| 31 | +) |
| 32 | + |
| 33 | +func getLatestBlockNumber(tx kv.Tx) (uint64, error) { |
| 34 | + forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx) |
| 35 | + if forkchoiceHeadHash != (common.Hash{}) { |
| 36 | + forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash) |
| 37 | + if forkchoiceHeadNum != nil { |
| 38 | + return *forkchoiceHeadNum, nil |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + blockNum, err := stages.GetStageProgress(tx, stages.Execution) |
| 43 | + if err != nil { |
| 44 | + return 0, fmt.Errorf("getting latest block number: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + return blockNum, nil |
| 48 | +} |
| 49 | + |
| 50 | +// SetHead rewinds the local chain to the specified block number by unwinding |
| 51 | +// all staged sync stages. This is the core implementation used by debug_setHead. |
| 52 | +func (e *ExecModule) SetHead(ctx context.Context, targetBlock uint64) error { |
| 53 | + if !e.semaphore.TryAcquire(1) { |
| 54 | + return fmt.Errorf("execution module is busy") |
| 55 | + } |
| 56 | + defer e.semaphore.Release(1) |
| 57 | + |
| 58 | + tx, err := e.db.BeginTemporalRw(ctx) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to begin rw transaction: %w", err) |
| 61 | + } |
| 62 | + defer tx.Rollback() |
| 63 | + |
| 64 | + // Get the current head block number |
| 65 | + currentHead, err := getLatestBlockNumber(tx) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to get current head: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + if targetBlock > currentHead { |
| 71 | + return fmt.Errorf("cannot set head to a future block: target %d, current head %d", targetBlock, currentHead) |
| 72 | + } |
| 73 | + |
| 74 | + if targetBlock == currentHead { |
| 75 | + return nil // already at the target |
| 76 | + } |
| 77 | + |
| 78 | + // Check if we can unwind that far back |
| 79 | + minUnwindableBlock, err := rawtemporaldb.CanUnwindToBlockNum(tx) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to check minimum unwindable block: %w", err) |
| 82 | + } |
| 83 | + if targetBlock < minUnwindableBlock { |
| 84 | + return fmt.Errorf("cannot unwind to block %d: minimum unwindable block is %d", targetBlock, minUnwindableBlock) |
| 85 | + } |
| 86 | + |
| 87 | + // Verify the target block exists in the canonical chain |
| 88 | + targetHash, ok, err := e.blockReader.CanonicalHash(ctx, tx, targetBlock) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to get canonical hash for block %d: %w", targetBlock, err) |
| 91 | + } |
| 92 | + if !ok { |
| 93 | + return fmt.Errorf("block %d not found in canonical chain", targetBlock) |
| 94 | + } |
| 95 | + |
| 96 | + // Create SharedDomains context for the unwind |
| 97 | + sd, err := execctx.NewSharedDomains(ctx, tx, e.logger) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to create shared domains: %w", err) |
| 100 | + } |
| 101 | + defer sd.Close() |
| 102 | + |
| 103 | + // Set the unwind point and run the unwind |
| 104 | + if err := e.executionPipeline.UnwindTo(targetBlock, stagedsync.StagedUnwind, tx); err != nil { |
| 105 | + return fmt.Errorf("failed to set unwind point: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := e.hook.BeforeRun(tx, true); err != nil { |
| 109 | + return fmt.Errorf("hook BeforeRun failed: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + if err := e.executionPipeline.RunUnwind(sd, tx); err != nil { |
| 113 | + return fmt.Errorf("failed to run unwind: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + // Truncate TxNums above the target block |
| 117 | + if err := rawdbv3.TxNums.Truncate(tx, targetBlock+1); err != nil { |
| 118 | + return fmt.Errorf("failed to truncate tx nums: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + // Update the head block hash |
| 122 | + rawdb.WriteHeadBlockHash(tx, targetHash) |
| 123 | + |
| 124 | + // Update stage progress for headers and bodies |
| 125 | + if err := stages.SaveStageProgress(tx, stages.Headers, targetBlock); err != nil { |
| 126 | + return fmt.Errorf("failed to save headers stage progress: %w", err) |
| 127 | + } |
| 128 | + if err := stages.SaveStageProgress(tx, stages.Bodies, targetBlock); err != nil { |
| 129 | + return fmt.Errorf("failed to save bodies stage progress: %w", err) |
| 130 | + } |
| 131 | + if err := stages.SaveStageProgress(tx, stages.BlockHashes, targetBlock); err != nil { |
| 132 | + return fmt.Errorf("failed to save block hashes stage progress: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + // Flush and commit |
| 136 | + if err := sd.Flush(ctx, tx); err != nil { |
| 137 | + return fmt.Errorf("failed to flush shared domains: %w", err) |
| 138 | + } |
| 139 | + sd.Close() |
| 140 | + |
| 141 | + if err := tx.Commit(); err != nil { |
| 142 | + return fmt.Errorf("failed to commit transaction: %w", err) |
| 143 | + } |
| 144 | + |
| 145 | + e.logger.Info("SetHead: successfully rewound chain", "targetBlock", targetBlock, "previousHead", currentHead) |
| 146 | + return nil |
| 147 | +} |
0 commit comments