Skip to content

Commit 785d441

Browse files
authored
Merge pull request #2059 from 0xPolygon/feat/force-pmt-regen-startup-2.64
Feat/force pmt regen startup 2.64
2 parents 98ac507 + 62ebc58 commit 785d441

File tree

10 files changed

+66
-9
lines changed

10 files changed

+66
-9
lines changed

cmd/utils/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,11 @@ var (
988988
Usage: "Only use SMT v2 for state changes",
989989
Value: true,
990990
}
991+
ForcePMTInterhashesRegenOnRestart = cli.BoolFlag{
992+
Name: "zkevm.force-pmt-interhashes-regen-on-restart",
993+
Usage: "Force regeneration of PMT interhashes on node restart",
994+
Value: false,
995+
}
991996
SequencerBlockGasLimit = cli.Uint64Flag{
992997
Name: "zkevm.sequencer-block-gas-limit",
993998
Usage: "The gas limit of the sequencer block. Default (0) means no limit.",

core/state/intra_block_state_zkevm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package state
22

33
import (
44
"errors"
5+
"github.com/erigontech/erigon-lib/log/v3"
56

67
"github.com/erigontech/erigon-lib/chain"
78
libcommon "github.com/erigontech/erigon-lib/common"
@@ -135,6 +136,7 @@ func (sdb *IntraBlockState) SyncerPreExecuteStateSet(
135136
//save ger with l1blockhash - but only in the case that the l1 info tree index hasn't been
136137
// re-used. If it has been re-used we never write this to the contract storage
137138
if !reUsedL1InfoTreeIndex && blockGer != nil && *blockGer != emptyHash {
139+
log.Info("[Pre-Execute] [SR-DEBUG] Writing Global Exit Root L1 block hash to state DB", "ger", blockGer.String(), "l1BlockHash", l1BlockHash.String())
138140
sdb.WriteGerManagerL1BlockHash(*blockGer, *l1BlockHash)
139141
}
140142
} else {
@@ -148,6 +150,7 @@ func (sdb *IntraBlockState) SyncerPreExecuteStateSet(
148150

149151
for _, ger := range *gerUpdates {
150152
//save ger
153+
log.Info("[Pre-Execute] [SR-DEBUG] Writing Global Exit Root timestamp to state DB", "ger", ger.GlobalExitRoot.String(), "timestamp", ger.Timestamp)
151154
sdb.WriteGlobalExitRootTimestamp(ger.GlobalExitRoot, ger.Timestamp)
152155
}
153156

eth/ethconfig/config_zkevm.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ type Zk struct {
131131
L2InfoTreeUpdatesBatchSize uint64
132132
L2InfoTreeUpdatesURL string
133133

134-
InjectGers bool
135-
HonourChainspec bool `yaml:"zkevm.honour-chainspec"`
136-
SimultaneousPmtAndSmt bool
137-
SkipSmt bool
138-
OnlySmtV2 bool
139-
SequencerBlockGasLimit uint64
140-
PessimisticForkNumber uint64
134+
InjectGers bool
135+
HonourChainspec bool `yaml:"zkevm.honour-chainspec"`
136+
SimultaneousPmtAndSmt bool
137+
SkipSmt bool
138+
OnlySmtV2 bool
139+
ForcePMTInterhashesRegenOnRestart bool
140+
SequencerBlockGasLimit uint64
141+
PessimisticForkNumber uint64
141142
}
142143

143144
func (c *Zk) ShouldCountersBeUnlimited(l1Recovery bool) bool {

eth/stagedsync/stage_interhashes.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/binary"
77
"fmt"
88
"math/bits"
9+
"runtime"
910
"slices"
1011
"sync/atomic"
1112

@@ -596,6 +597,11 @@ func IncrementIntermediateHashes(logPrefix string, s *StageState, db kv.RwTx, to
596597
return trie.EmptyRoot, err
597598
}
598599
}
600+
601+
var memStats runtime.MemStats
602+
runtime.ReadMemStats(&memStats)
603+
logger.Info(fmt.Sprintf("[%s] [SR-DEBUG] PMT Increment intermediate hashes finished promotion", logPrefix), "retainListSize", rl.Len(), "allocMB", memStats.Alloc/1024/1024, "headAllocMB", memStats.HeapAlloc/1024/1024, "heapObjects", memStats.HeapObjects, "numGC", memStats.NumGC)
604+
599605
accTrieCollector := etl.NewCollector(logPrefix, cfg.tmpDir, etl.NewSortableBuffer(etl.BufferOptimalSize), logger)
600606
defer accTrieCollector.Close()
601607
accTrieCollectorFunc := accountTrieCollector(accTrieCollector)
@@ -605,6 +611,7 @@ func IncrementIntermediateHashes(logPrefix string, s *StageState, db kv.RwTx, to
605611
stTrieCollectorFunc := storageTrieCollector(stTrieCollector)
606612

607613
loader := trie.NewFlatDBTrieLoader(logPrefix, rl, accTrieCollectorFunc, stTrieCollectorFunc, false)
614+
608615
hash, err := loader.CalcTrieRoot(db, quit)
609616
if err != nil {
610617
return trie.EmptyRoot, err

turbo/cli/default_flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ var DefaultFlags = []cli.Flag{
349349
&utils.InjectGers,
350350
&utils.SkipSmt,
351351
&utils.OnlySmtV2,
352+
&utils.ForcePMTInterhashesRegenOnRestart,
352353
&utils.SequencerBlockGasLimit,
353354
&utils.PessimisticForkNumber,
354355
}

turbo/cli/flags_zkevm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
341341
InjectGers: ctx.Bool(utils.InjectGers.Name),
342342
SkipSmt: ctx.Bool(utils.SkipSmt.Name),
343343
OnlySmtV2: ctx.Bool(utils.OnlySmtV2.Name),
344+
ForcePMTInterhashesRegenOnRestart: ctx.Bool(utils.ForcePMTInterhashesRegenOnRestart.Name),
344345
SequencerBlockGasLimit: ctx.Uint64(utils.SequencerBlockGasLimit.Name),
345346
PessimisticForkNumber: ctx.Uint64(utils.PessimisticForkNumber.Name),
346347
}

zk/stages/stage_sequence_execute.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"github.com/erigontech/erigon/consensus/misc"
8+
"sync"
89
"time"
910

1011
"github.com/erigontech/erigon-lib/common"
@@ -34,6 +35,8 @@ type TxYielder interface {
3435
Cleanup()
3536
}
3637

38+
var regenPmtOnce sync.Once
39+
3740
func SpawnSequencingStage(
3841
s *stagedsync.StageState,
3942
u stagedsync.Unwinder,
@@ -127,6 +130,19 @@ func sequencingBatchStep(
127130
return err
128131
}
129132

133+
if cfg.zk.ForcePMTInterhashesRegenOnRestart {
134+
if cfg.chainConfig.IsPmtEnabled(executionAt) {
135+
log.Info(fmt.Sprintf("[%s] [SR-DEBUG] Forcing PMT interhashes regeneration as per configuration", logPrefix))
136+
regenPmtOnce.Do(func() {
137+
if err = sequencerRegentIntermediateHashesPMT(ctx, s, sdb.tx, cfg); err != nil {
138+
panic(fmt.Sprintf("failed to regen PMT interhashes: %v", err))
139+
}
140+
})
141+
} else {
142+
log.Warn(fmt.Sprintf("[%s] [SR-DEBUG] Not regenerating PMT as zkevm.force-pmt-interhashes-regen-on-restart is set but PMT is not being used", logPrefix))
143+
}
144+
}
145+
130146
lastBatch, err := stages.GetStageProgress(sdb.tx, stages.HighestSeenBatchNumber)
131147
if err != nil {
132148
return err

zk/stages/stage_sequence_execute_blocks.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func handleStateForNewBlockStarting(
6161
l1BlockHash := ibs.ReadGerManagerL1BlockHash(l1info.GER)
6262
if l1BlockHash == (common.Hash{}) {
6363
// not in the contract so let's write it!
64+
log.Info(fmt.Sprintf("[%s] Writing GER manager L1 block hash to Intra Block State", batchContext.s.LogPrefix()), "ger", l1info.GER.String(), "l1BlockHash", l1info.ParentHash.String())
6465
ibs.WriteGerManagerL1BlockHash(l1info.GER, l1info.ParentHash)
6566
if err := hermezDb.WriteLatestUsedGer(blockNumber, l1info.GER); err != nil {
6667
return err
@@ -200,16 +201,21 @@ func finaliseBlock(
200201
var commitmentToLog string
201202
if batchContext.cfg.chainConfig.IsPmtEnabled(newHeader.Number.Uint64()) {
202203
commitmentToLog = "pmt"
203-
if err = stagedsync.HashStateFromTo(batchContext.s.LogPrefix(), batchContext.sdb.tx, batchContext.cfg.hashStateCfg, newHeader.Number.Uint64()-1, newHeader.Number.Uint64(), batchContext.ctx, log.Root()); err != nil {
204+
logger := log.New()
205+
log.Info(fmt.Sprintf("[%s] [SR-DEBUG] Hashing State for the PMT", batchContext.s.LogPrefix()), "startingBlock", newHeader.Number.Uint64()-1, "endingBlock", newHeader.Number.Uint64())
206+
if err = stagedsync.HashStateFromTo(batchContext.s.LogPrefix(), batchContext.sdb.tx, batchContext.cfg.hashStateCfg, newHeader.Number.Uint64()-1, newHeader.Number.Uint64(), batchContext.ctx, logger); err != nil {
204207
return nil, err
205208
}
206209

207-
newRoot, err = stagedsync.IncrementIntermediateHashes(batchContext.s.LogPrefix(), batchContext.s, batchContext.sdb.tx, thisBlockNumber, trieConfigSequencer(batchContext.cfg.intersCfg), common.Hash{}, quit, log.Root())
210+
log.Info(fmt.Sprintf("[%s] [SR-DEBUG] IncrementIntermediateHashes for the PMT", batchContext.s.LogPrefix()), "startingBlock", batchContext.s.BlockNumber, "endingBlock", thisBlockNumber)
211+
newRoot, err = stagedsync.IncrementIntermediateHashes(batchContext.s.LogPrefix(), batchContext.s, batchContext.sdb.tx, thisBlockNumber, trieConfigSequencer(batchContext.cfg.intersCfg), common.Hash{}, quit, logger)
208212
} else {
213+
log.Info(fmt.Sprintf("[%s] [SR-DEBUG] IncrementIntermediateHashes for the SMT", batchContext.s.LogPrefix()), "startingBlock", newHeader.Number.Uint64()-1, "endingBlock", newHeader.Number.Uint64())
209214
commitmentToLog = "smt"
210215
newRoot, err = zkIncrementIntermediateHashes_v2_Forwards(batchContext.ctx, batchContext.cfg.dirs.Tmp, batchContext.s.LogPrefix(), batchContext.s, batchContext.sdb.tx, newHeader.Number.Uint64()-1, newHeader.Number.Uint64())
211216
}
212217
if err != nil {
218+
log.Error(fmt.Sprintf("[%s] [SR-DEBUG] IncrementIntermediateHashes failed", batchContext.s.LogPrefix()), "err", err)
213219
batchContext.sdb.eridb.RollbackBatch()
214220
return nil, err
215221
}

zk/stages/stage_sequence_execute_utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,18 @@ func sequencerRegenIntermediateHashes(ctx context.Context, s *stagedsync.StageSt
697697
}
698698
return tx.Commit()
699699
}
700+
701+
// sequencerRegentIntermediateHashesPMT will build the intermediate hashes PMT for the given block number.
702+
// This will run when flag `zkevm.force-pmt-interhashes-regen-on-restart` is enabled to forcefully regenerate the PMT if there is any issues there.
703+
func sequencerRegentIntermediateHashesPMT(ctx context.Context, s *stagedsync.StageState, tx kv.RwTx, cfg SequenceBlockCfg) error {
704+
timeStart := time.Now()
705+
_, err := stagedsync.RegenerateIntermediateHashes(s.LogPrefix(), tx, trieConfigSequencer(cfg.intersCfg), common.Hash{}, ctx, log.New())
706+
if err != nil {
707+
log.Error(fmt.Sprintf("[%s] [SR-DEBUG] Failed to regenerate intermediate hashes PMT: %v", s.LogPrefix(), err))
708+
return err
709+
}
710+
timeFinish := time.Now()
711+
log.Info(fmt.Sprintf("[%s] [SR-DEBUG] Regenerated intermediate hashes PMT in %s", s.LogPrefix(), timeFinish.Sub(timeStart)))
712+
713+
return nil
714+
}

zk/utils/global_exit_root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"encoding/binary"
55
"errors"
6+
"github.com/erigontech/erigon-lib/log/v3"
67
"math/big"
78

89
"github.com/erigontech/erigon-lib/common"
@@ -57,6 +58,7 @@ func WriteGlobalExitRoot(stateReader state.StateReader, stateWriter state.Writer
5758
}
5859

5960
// write global exit root to state
61+
log.Info("[SR-DEBUG] Writing Global Exit Root to state", "ger", ger.String(), "timestamp", timestamp)
6062
if err := stateWriter.WriteAccountStorage(addr, uint64(1), &gerp, emptyUint256, headerTime); err != nil {
6163
return err
6264
}

0 commit comments

Comments
 (0)