Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ var (
Usage: "Download snapshots up to the given block number (exclusive). Disabled by default. Useful for testing and shadow forks.",
Aliases: []string{"shadow.fork.block"},
}
SnapDownloadToBlockWithRebuildCommitmentFlag = cli.BoolFlag{
Name: "snap.download.to.block.with.rebuild.commitment",
Usage: "Rebuild commitment domain files based on storage/acc/code. Only used with snap.download.to.block. Needed when all downloaded commitment files are purified.",
Value: false,
}
TorrentVerbosityFlag = cli.IntFlag{
Name: "torrent.verbosity",
Value: 1,
Expand Down
7 changes: 5 additions & 2 deletions db/snapshotsync/snapshotsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func SyncSnapshots(
continue
}

if filterToBlock(p.Name, toBlock, toStep, headerchain) {
if filterToBlock(p.Name, toBlock, toStep, headerchain, syncCfg.SnapshotDownloadToBlockWithRebuildCommitment) {
continue
}

Expand Down Expand Up @@ -512,7 +512,7 @@ func SyncSnapshots(
return nil
}

func filterToBlock(name string, toBlock uint64, toStep uint64, headerchain bool) bool {
func filterToBlock(name string, toBlock uint64, toStep uint64, headerchain bool, rebuildCommitment bool) bool {
if toBlock == 0 {
return false // toBlock filtering is not enabled
}
Expand All @@ -526,6 +526,9 @@ func filterToBlock(name string, toBlock uint64, toStep uint64, headerchain bool)
if strings.HasPrefix(name, "caplin/") {
return false // not applicable, caplin files are slot-based
}
if rebuildCommitment && strings.Contains(name, kv.CommitmentDomain.String()) && strings.HasPrefix(name, "domain") {
return true
}
if stateFile {
return fileInfo.To > toStep
}
Expand Down
18 changes: 9 additions & 9 deletions db/state/squeeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/erigontech/erigon/db/version"
"math"
"os"
"path/filepath"
Expand All @@ -28,6 +27,7 @@ import (
downloadertype "github.com/erigontech/erigon/db/snaptype"
"github.com/erigontech/erigon/db/state/execctx"
"github.com/erigontech/erigon/db/state/statecfg"
"github.com/erigontech/erigon/db/version"
"github.com/erigontech/erigon/execution/commitment/commitmentdb"
)

Expand Down Expand Up @@ -336,8 +336,8 @@ func CheckCommitmentForPrint(ctx context.Context, rwDb kv.TemporalRwDB) (string,
// RebuildCommitmentFiles recreates commitment files from existing accounts and storage kv files
// If some commitment exists, they will be accepted as correct and next kv range will be processed.
// DB expected to be empty, committed into db keys will be not processed.
func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsReader *rawdbv3.TxNumsReader, logger log.Logger, squeeze bool) (latestRoot []byte, err error) {
a := rwDb.(HasAgg).Agg().(*Aggregator)
func RebuildCommitmentFiles(ctx context.Context, db kv.TemporalRoDB, txNumsReader *rawdbv3.TxNumsReader, logger log.Logger, squeeze bool) (latestRoot []byte, err error) {
a := db.(HasAgg).Agg().(*Aggregator)

// disable hard alignment; allowing commitment and storage/account to have
// different visibleFiles
Expand Down Expand Up @@ -468,22 +468,22 @@ func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsRea
return true, k
}

rwTx, err := rwDb.BeginTemporalRw(ctx)
tx, err := db.BeginTemporalRo(ctx)
if err != nil {
return nil, err
}
defer rwTx.Rollback()
defer tx.Rollback()

domains, err := execctx.NewSharedDomains(ctx, rwTx, log.New())
domains, err := execctx.NewSharedDomains(ctx, tx, log.New())
if err != nil {
return nil, err
}

domains.SetBlockNum(blockNum)
domains.SetTxNum(lastTxnumInShard - 1)
domains.GetCommitmentCtx().SetLimitedHistoryStateReader(rwTx, lastTxnumInShard) // this helps to read state from correct file during commitment
domains.GetCommitmentCtx().SetLimitedHistoryStateReader(tx, lastTxnumInShard) // this helps to read state from correct file during commitment

rebuiltCommit, err = rebuildCommitmentShard(ctx, domains, rwTx, nextKey, &rebuiltCommitment{
rebuiltCommit, err = rebuildCommitmentShard(ctx, domains, tx, nextKey, &rebuiltCommitment{
StepFrom: shardFrom,
StepTo: shardTo,
TxnFrom: rangeFromTxNum,
Expand All @@ -503,7 +503,7 @@ func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsRea
a.dirtyFilesLock.Lock()
a.recalcVisibleFiles(a.dirtyFilesEndTxNumMinimax())
a.dirtyFilesLock.Unlock()
rwTx.Rollback()
tx.Rollback()

if shardTo+shardStepsSize > lastShard && shardStepsSize > 1 {
shardStepsSize /= 2
Expand Down
11 changes: 11 additions & 0 deletions execution/stagedsync/stage_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ func DownloadAndIndexSnapshotsIfNeed(s *StageState, ctx context.Context, tx kv.R
temporal.ForceReopenAggCtx() // otherwise next stages will not see just-indexed-files
}

if cfg.syncConfig.SnapshotDownloadToBlockWithRebuildCommitment && s.BlockNumber == 0 {
txNumsReader := cfg.blockReader.TxnumReader(ctx)
_, err = state.RebuildCommitmentFiles(ctx, cfg.db, &txNumsReader, logger, true /*squeeze*/)
if err != nil {
return fmt.Errorf("failed to rebuild commitment files: %w", err)
}
if temporal, ok := tx.(*temporal.RwTx); ok {
temporal.ForceReopenAggCtx() // otherwise next stages will not see new files
}
}

// It's ok to notify before tx.Commit(), because RPCDaemon does read list of files by gRPC (not by reading from db)
if cfg.notifier.Events != nil {
cfg.notifier.Events.OnNewSnapshot()
Expand Down
1 change: 1 addition & 0 deletions node/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var DefaultFlags = []cli.Flag{
&utils.SnapStateStopFlag,
&utils.SnapSkipStateSnapshotDownloadFlag,
&utils.SnapDownloadToBlockFlag,
&utils.SnapDownloadToBlockWithRebuildCommitmentFlag,
&utils.DbPageSizeFlag,
&utils.DbSizeLimitFlag,
&utils.DbWriteMapFlag,
Expand Down
1 change: 1 addition & 0 deletions node/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func ApplyFlagsForEthConfig(ctx *cli.Context, cfg *ethconfig.Config, logger log.

if ctx.IsSet(utils.SnapDownloadToBlockFlag.Name) {
cfg.Sync.SnapshotDownloadToBlock = ctx.Uint64(utils.SnapDownloadToBlockFlag.Name)
cfg.Sync.SnapshotDownloadToBlockWithRebuildCommitment = ctx.Bool(utils.SnapDownloadToBlockWithRebuildCommitmentFlag.Name)
}

if stage := ctx.String(SyncLoopBreakAfterFlag.Name); len(stage) > 0 {
Expand Down
4 changes: 3 additions & 1 deletion node/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,7 @@ type Sync struct {
MaxReorgDepth uint64
KeepExecutionProofs bool
PersistReceiptsCacheV2 bool
SnapshotDownloadToBlock uint64 // exclusive [0,toBlock)

SnapshotDownloadToBlock uint64 // exclusive [0,toBlock)
SnapshotDownloadToBlockWithRebuildCommitment bool
}
Loading