|
| 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 stagedsync |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/erigontech/erigon/common/dbg" |
| 26 | + "github.com/erigontech/erigon/common/log/v3" |
| 27 | + "github.com/erigontech/erigon/db/kv" |
| 28 | + "github.com/erigontech/erigon/db/kv/dbcfg" |
| 29 | + "github.com/erigontech/erigon/db/kv/memdb" |
| 30 | + "github.com/erigontech/erigon/execution/stagedsync/stages" |
| 31 | +) |
| 32 | + |
| 33 | +// TestNoPruneSkipsAllPruneStages verifies that when --exec.no-prune is set |
| 34 | +// (dbg.NoPrune() == true), each staged-sync prune entrypoint is a no-op |
| 35 | +// against the MDBX tables it would otherwise delete rows from. |
| 36 | +func TestNoPruneSkipsAllPruneStages(t *testing.T) { |
| 37 | + orig := dbg.NoPrune() |
| 38 | + t.Cleanup(func() { dbg.SetNoPrune(orig) }) |
| 39 | + dbg.SetNoPrune(true) |
| 40 | + |
| 41 | + ctx := context.Background() |
| 42 | + logger := log.New() |
| 43 | + |
| 44 | + db := memdb.NewTestDB(t, dbcfg.ChainDB) |
| 45 | + tx, err := db.BeginRw(ctx) |
| 46 | + require.NoError(t, err) |
| 47 | + defer tx.Rollback() |
| 48 | + |
| 49 | + // Seed rows in every table any of the prune stages would target. |
| 50 | + type seedRow struct{ table, key, value string } |
| 51 | + seeds := []seedRow{ |
| 52 | + {kv.ChangeSets3, "k1", "v1"}, |
| 53 | + {kv.ChangeSets3, "k2", "v2"}, |
| 54 | + {kv.BlockAccessList, "b1", "ba1"}, |
| 55 | + {kv.BlockAccessList, "b2", "ba2"}, |
| 56 | + {kv.TxLookup, "t1", "tl1"}, |
| 57 | + {kv.BorWitnesses, "w1", "wit1"}, |
| 58 | + } |
| 59 | + for _, s := range seeds { |
| 60 | + require.NoError(t, tx.Put(s.table, []byte(s.key), []byte(s.value))) |
| 61 | + } |
| 62 | + countRows := func(t *testing.T, table string) int { |
| 63 | + c, err := tx.Cursor(table) |
| 64 | + require.NoError(t, err) |
| 65 | + defer c.Close() |
| 66 | + n := 0 |
| 67 | + for k, _, err := c.First(); k != nil; k, _, err = c.Next() { |
| 68 | + require.NoError(t, err) |
| 69 | + n++ |
| 70 | + } |
| 71 | + return n |
| 72 | + } |
| 73 | + tracked := []string{kv.ChangeSets3, kv.BlockAccessList, kv.TxLookup, kv.BorWitnesses} |
| 74 | + pre := map[string]int{} |
| 75 | + for _, table := range tracked { |
| 76 | + pre[table] = countRows(t, table) |
| 77 | + require.Greater(t, pre[table], 0, "expected seeded rows in %s", table) |
| 78 | + } |
| 79 | + |
| 80 | + // ForwardProgress is well past MaxReorgDepth so the inner rawdb.PruneTable / |
| 81 | + // PruneSmallBatches calls would normally fire. Each prune function |
| 82 | + // early-returns on dbg.NoPrune() before reading any cfg field, so zero-value |
| 83 | + // cfgs are safe. |
| 84 | + const forward uint64 = 10_000 |
| 85 | + require.NoError(t, PruneExecutionStage(ctx, &PruneState{ID: stages.Execution, ForwardProgress: forward}, tx, ExecuteBlockCfg{}, 0, logger)) |
| 86 | + require.NoError(t, PruneTxLookup(&PruneState{ID: stages.TxLookup, ForwardProgress: forward}, tx, TxLookupCfg{}, ctx, logger)) |
| 87 | + require.NoError(t, PruneWitnessProcessingStage(&PruneState{ID: stages.WitnessProcessing, ForwardProgress: forward}, tx, WitnessProcessingCfg{}, ctx, logger)) |
| 88 | + require.NoError(t, SnapshotsPrune(&PruneState{ID: stages.Snapshots, ForwardProgress: forward}, SnapshotsCfg{}, ctx, tx, logger)) |
| 89 | + |
| 90 | + for _, table := range tracked { |
| 91 | + require.Equal(t, pre[table], countRows(t, table), "table %s lost rows under --exec.no-prune", table) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// TestNoPruneFlagBookkeeping confirms each prune stage still records its |
| 96 | +// PruneProgress when skipping work — otherwise the staged-sync state machine |
| 97 | +// would re-enter the prune step on every cycle. |
| 98 | +func TestNoPruneFlagBookkeeping(t *testing.T) { |
| 99 | + orig := dbg.NoPrune() |
| 100 | + t.Cleanup(func() { dbg.SetNoPrune(orig) }) |
| 101 | + dbg.SetNoPrune(true) |
| 102 | + |
| 103 | + ctx := context.Background() |
| 104 | + logger := log.New() |
| 105 | + |
| 106 | + db := memdb.NewTestDB(t, dbcfg.ChainDB) |
| 107 | + tx, err := db.BeginRw(ctx) |
| 108 | + require.NoError(t, err) |
| 109 | + defer tx.Rollback() |
| 110 | + |
| 111 | + const forward uint64 = 12_345 |
| 112 | + require.NoError(t, PruneExecutionStage(ctx, &PruneState{ID: stages.Execution, ForwardProgress: forward}, tx, ExecuteBlockCfg{}, 0, logger)) |
| 113 | + require.NoError(t, PruneTxLookup(&PruneState{ID: stages.TxLookup, ForwardProgress: forward}, tx, TxLookupCfg{}, ctx, logger)) |
| 114 | + require.NoError(t, PruneWitnessProcessingStage(&PruneState{ID: stages.WitnessProcessing, ForwardProgress: forward}, tx, WitnessProcessingCfg{}, ctx, logger)) |
| 115 | + |
| 116 | + for _, id := range []stages.SyncStage{stages.Execution, stages.TxLookup, stages.WitnessProcessing} { |
| 117 | + got, err := stages.GetStagePruneProgress(tx, id) |
| 118 | + require.NoError(t, err) |
| 119 | + require.Equal(t, forward, got, "stage %s did not record PruneProgress under --exec.no-prune", id) |
| 120 | + } |
| 121 | +} |
0 commit comments