From 5bd18771075790d6722808c6c4f17e2ade5bf783 Mon Sep 17 00:00:00 2001 From: Scott Fairclough Date: Thu, 9 Jan 2025 15:53:46 +0000 Subject: [PATCH] configurable witness unwind limit --- cmd/utils/flags.go | 6 +++++ eth/backend.go | 1 + eth/ethconfig/config_zkevm.go | 1 + turbo/cli/default_flags.go | 1 + turbo/cli/flags_zkevm.go | 2 ++ turbo/jsonrpc/zkevm_api.go | 6 +++-- zk/witness/witness.go | 43 ++++++++++++++++++----------------- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 90e0963ae86..1c5e071230a 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -607,6 +607,12 @@ var ( Usage: "A size of the memdb used on witness generation in format \"2GB\". Might fail generation for older batches if not enough for the unwind.", Value: datasizeFlagValue(2 * datasize.GB), } + WitnessUnwindLimit = cli.Uint64Flag{ + Name: "zkevm.witness-unwind-limit", + Usage: "The maximum number of blocks the witness generation can unwind", + Value: 500_000, + } + ExecutorMaxConcurrentRequests = cli.IntFlag{ Name: "zkevm.executor-max-concurrent-requests", Usage: "The maximum number of concurrent requests to the executor", diff --git a/eth/backend.go b/eth/backend.go index 147736267d9..c51eeac27c4 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -1120,6 +1120,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger backend.config.Zk, backend.engine, backend.config.WitnessContractInclusion, + backend.config.WitnessUnwindLimit, ) var legacyExecutors []*legacy_executor_verifier.Executor = make([]*legacy_executor_verifier.Executor, 0, len(cfg.ExecutorUrls)) diff --git a/eth/ethconfig/config_zkevm.go b/eth/ethconfig/config_zkevm.go index c7173832a5b..69703fc7961 100644 --- a/eth/ethconfig/config_zkevm.go +++ b/eth/ethconfig/config_zkevm.go @@ -51,6 +51,7 @@ type Zk struct { ExecutorRequestTimeout time.Duration DatastreamNewBlockTimeout time.Duration WitnessMemdbSize datasize.ByteSize + WitnessUnwindLimit uint64 ExecutorMaxConcurrentRequests int Limbo bool AllowFreeTransactions bool diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 4272fb1f300..31350eee8d3 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -217,6 +217,7 @@ var DefaultFlags = []cli.Flag{ &utils.ExecutorRequestTimeout, &utils.DatastreamNewBlockTimeout, &utils.WitnessMemdbSize, + &utils.WitnessUnwindLimit, &utils.ExecutorMaxConcurrentRequests, &utils.Limbo, &utils.AllowFreeTransactions, diff --git a/turbo/cli/flags_zkevm.go b/turbo/cli/flags_zkevm.go index ae01417fe3e..be95dd54b4b 100644 --- a/turbo/cli/flags_zkevm.go +++ b/turbo/cli/flags_zkevm.go @@ -115,6 +115,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) { } witnessMemSize := utils.DatasizeFlagValue(ctx, utils.WitnessMemdbSize.Name) + witnessUnwindLimit := ctx.Uint64(utils.WitnessUnwindLimit.Name) badBatchStrings := strings.Split(ctx.String(utils.BadBatches.Name), ",") badBatches := make([]uint64, 0) @@ -186,6 +187,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) { ExecutorRequestTimeout: ctx.Duration(utils.ExecutorRequestTimeout.Name), DatastreamNewBlockTimeout: ctx.Duration(utils.DatastreamNewBlockTimeout.Name), WitnessMemdbSize: *witnessMemSize, + WitnessUnwindLimit: witnessUnwindLimit, ExecutorMaxConcurrentRequests: ctx.Int(utils.ExecutorMaxConcurrentRequests.Name), Limbo: ctx.Bool(utils.Limbo.Name), AllowFreeTransactions: ctx.Bool(utils.AllowFreeTransactions.Name), diff --git a/turbo/jsonrpc/zkevm_api.go b/turbo/jsonrpc/zkevm_api.go index c7fa97bcf23..49acb4fadb8 100644 --- a/turbo/jsonrpc/zkevm_api.go +++ b/turbo/jsonrpc/zkevm_api.go @@ -16,12 +16,15 @@ import ( zktypes "github.com/ledgerwatch/erigon/zk/types" + "math" + "github.com/holiman/uint256" "github.com/ledgerwatch/erigon-lib/common/hexutil" "github.com/ledgerwatch/erigon-lib/kv/membatchwithdb" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/state" + "github.com/ledgerwatch/erigon/core/systemcontracts" eritypes "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types/accounts" "github.com/ledgerwatch/erigon/eth/ethconfig" @@ -45,8 +48,6 @@ import ( "github.com/ledgerwatch/erigon/zk/witness" "github.com/ledgerwatch/erigon/zkevm/hex" "github.com/ledgerwatch/erigon/zkevm/jsonrpc/client" - "github.com/ledgerwatch/erigon/core/systemcontracts" - "math" ) var sha3UncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") @@ -1021,6 +1022,7 @@ func (api *ZkEvmAPIImpl) buildGenerator(ctx context.Context, tx kv.Tx, witnessMo api.config.Zk, api.ethApi._engine, api.config.WitnessContractInclusion, + api.config.WitnessUnwindLimit, ) fullWitness := false diff --git a/zk/witness/witness.go b/zk/witness/witness.go index bbdb98e1a98..df84fbd3d56 100644 --- a/zk/witness/witness.go +++ b/zk/witness/witness.go @@ -43,21 +43,20 @@ import ( ) var ( - maxGetProofRewindBlockCount uint64 = 500_000 - ErrEndBeforeStart = errors.New("end block must be higher than start block") ) type Generator struct { - tx kv.Tx - dirs datadir.Dirs - historyV3 bool - agg *libstate.Aggregator - blockReader services.FullBlockReader - chainCfg *chain.Config - zkConfig *ethconfig.Zk - engine consensus.EngineReader - forcedContracts []libcommon.Address + tx kv.Tx + dirs datadir.Dirs + historyV3 bool + agg *libstate.Aggregator + blockReader services.FullBlockReader + chainCfg *chain.Config + zkConfig *ethconfig.Zk + engine consensus.EngineReader + forcedContracts []libcommon.Address + witnessUnwindLimit uint64 } func NewGenerator( @@ -69,16 +68,18 @@ func NewGenerator( zkConfig *ethconfig.Zk, engine consensus.EngineReader, forcedContracs []libcommon.Address, + witnessUnwindLimit uint64, ) *Generator { return &Generator{ - dirs: dirs, - historyV3: historyV3, - agg: agg, - blockReader: blockReader, - chainCfg: chainCfg, - zkConfig: zkConfig, - engine: engine, - forcedContracts: forcedContracs, + dirs: dirs, + historyV3: historyV3, + agg: agg, + blockReader: blockReader, + chainCfg: chainCfg, + zkConfig: zkConfig, + engine: engine, + forcedContracts: forcedContracs, + witnessUnwindLimit: witnessUnwindLimit, } } @@ -229,8 +230,8 @@ func (g *Generator) generateWitness(tx kv.Tx, ctx context.Context, batchNum uint } if startBlock-1 < latestBlock { - if latestBlock-startBlock > maxGetProofRewindBlockCount { - return nil, fmt.Errorf("requested block is too old, block must be within %d blocks of the head block number (currently %d)", maxGetProofRewindBlockCount, latestBlock) + if latestBlock-startBlock > g.witnessUnwindLimit { + return nil, fmt.Errorf("requested block is too old, block must be within %d blocks of the head block number (currently %d)", g.witnessUnwindLimit, latestBlock) } unwindState := &stagedsync.UnwindState{UnwindPoint: startBlock - 1}