From 809ee311d1f28f8a7e1e96b63ac8ebc754988e12 Mon Sep 17 00:00:00 2001 From: Scott Fairclough <70711990+hexoscott@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:56:27 +0000 Subject: [PATCH] configurable witness unwind limit (#1617) --- 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 | 1 + zk/witness/witness.go | 43 ++++++++++++++++++----------------- 7 files changed, 34 insertions(+), 21 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 05dfe41fde0..9f0250535b2 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -622,6 +622,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 90781b5a559..7de5acabaf3 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -1132,6 +1132,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 95a469f6631..a7a756275cf 100644 --- a/eth/ethconfig/config_zkevm.go +++ b/eth/ethconfig/config_zkevm.go @@ -53,6 +53,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 8910f16c685..da3698ac19a 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -220,6 +220,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 e8cb32e4699..73edc0ece84 100644 --- a/turbo/cli/flags_zkevm.go +++ b/turbo/cli/flags_zkevm.go @@ -126,6 +126,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) @@ -214,6 +215,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 dfd8814026c..e9ee7d5ed03 100644 --- a/turbo/jsonrpc/zkevm_api.go +++ b/turbo/jsonrpc/zkevm_api.go @@ -1042,6 +1042,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 0b41dbb6c02..f764e1a0ad7 100644 --- a/zk/witness/witness.go +++ b/zk/witness/witness.go @@ -35,21 +35,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( @@ -61,16 +60,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, } } @@ -197,8 +198,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) } if err := UnwindForWitness(ctx, rwtx, startBlock, latestBlock, g.dirs, g.historyV3, g.agg); err != nil {