Skip to content

Commit

Permalink
configurable witness unwind limit (#1617)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott authored Jan 9, 2025
1 parent fa2664b commit bf076dd
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
6 changes: 6 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions eth/ethconfig/config_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Zk struct {
ExecutorRequestTimeout time.Duration
DatastreamNewBlockTimeout time.Duration
WitnessMemdbSize datasize.ByteSize
WitnessUnwindLimit uint64
ExecutorMaxConcurrentRequests int
Limbo bool
AllowFreeTransactions bool
Expand Down
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ var DefaultFlags = []cli.Flag{
&utils.ExecutorRequestTimeout,
&utils.DatastreamNewBlockTimeout,
&utils.WitnessMemdbSize,
&utils.WitnessUnwindLimit,
&utils.ExecutorMaxConcurrentRequests,
&utils.Limbo,
&utils.AllowFreeTransactions,
Expand Down
2 changes: 2 additions & 0 deletions turbo/cli/flags_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions turbo/jsonrpc/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
43 changes: 22 additions & 21 deletions zk/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit bf076dd

Please sign in to comment.