Skip to content

Commit

Permalink
Allow execution to short circuit to the last downloaded batch (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls authored Nov 8, 2024
1 parent 41a3543 commit 3f88803
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ var (
Usage: "The time to wait for data to arrive from the stream before reporting an error (0s doesn't check)",
Value: "3s",
}
L2ShortCircuitToVerifiedBatchFlag = cli.BoolFlag{
Name: "zkevm.l2-short-circuit-to-verified-batch",
Usage: "Short circuit block execution up to the batch after the latest verified batch (default: true). When disabled, the sequencer will execute all downloaded batches",
Value: true,
}
L1SyncStartBlock = cli.Uint64Flag{
Name: "zkevm.l1-sync-start-block",
Usage: "Designed for recovery of the network from the L1 batch data, slower mode of operation than the datastream. If set the datastream will not be used",
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 @@ -12,6 +12,7 @@ type Zk struct {
L2RpcUrl string
L2DataStreamerUrl string
L2DataStreamerTimeout time.Duration
L2ShortCircuitToVerifiedBatch bool
L1SyncStartBlock uint64
L1SyncStopBatch uint64
L1ChainId uint64
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_execute_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func getExecRange(cfg ExecuteBlockCfg, tx kv.RwTx, stageProgress, toBlock uint64
return to, total, nil
}

shouldShortCircuit, noProgressTo, err := utils.ShouldShortCircuitExecution(tx, logPrefix)
shouldShortCircuit, noProgressTo, err := utils.ShouldShortCircuitExecution(tx, logPrefix, cfg.zk.L2ShortCircuitToVerifiedBatch)
if err != nil {
return 0, 0, err
}
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 @@ -175,6 +175,7 @@ var DefaultFlags = []cli.Flag{
&utils.L2RpcUrlFlag,
&utils.L2DataStreamerUrlFlag,
&utils.L2DataStreamerTimeout,
&utils.L2ShortCircuitToVerifiedBatchFlag,
&utils.L1SyncStartBlock,
&utils.L1SyncStopBatch,
&utils.L1ChainIdFlag,
Expand Down
6 changes: 5 additions & 1 deletion turbo/cli/flags_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (

"time"

"strconv"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/zk/sequencer"
utils2 "github.com/ledgerwatch/erigon/zk/utils"
"github.com/urfave/cli/v2"
"strconv"
)

var DeprecatedFlags = map[string]string{
Expand Down Expand Up @@ -70,6 +71,8 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
panic(fmt.Sprintf("could not parse l2 datastreamer timeout value %s", l2DataStreamTimeoutVal))
}

l2ShortCircuitToVerifiedBatchVal := ctx.Bool(utils.L2ShortCircuitToVerifiedBatchFlag.Name)

sequencerBlockSealTimeVal := ctx.String(utils.SequencerBlockSealTime.Name)
sequencerBlockSealTime, err := time.ParseDuration(sequencerBlockSealTimeVal)
if err != nil {
Expand Down Expand Up @@ -133,6 +136,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
L2RpcUrl: ctx.String(utils.L2RpcUrlFlag.Name),
L2DataStreamerUrl: ctx.String(utils.L2DataStreamerUrlFlag.Name),
L2DataStreamerTimeout: l2DataStreamTimeout,
L2ShortCircuitToVerifiedBatch: l2ShortCircuitToVerifiedBatchVal,
L1SyncStartBlock: ctx.Uint64(utils.L1SyncStartBlock.Name),
L1SyncStopBatch: ctx.Uint64(utils.L1SyncStopBatch.Name),
L1ChainId: ctx.Uint64(utils.L1ChainIdFlag.Name),
Expand Down
10 changes: 5 additions & 5 deletions zk/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// if current sync is before verified batch - short circuit to verified batch, otherwise to enx of next batch
// if there is no new fully downloaded batch - do not short circuit
// returns (shouldShortCircuit, blockNumber, error)
func ShouldShortCircuitExecution(tx kv.RwTx, logPrefix string) (bool, uint64, error) {
func ShouldShortCircuitExecution(tx kv.RwTx, logPrefix string, l2ShortCircuitToVerifiedBatch bool) (bool, uint64, error) {
hermezDb := hermez_db.NewHermezDb(tx)

// get highest verified batch
Expand Down Expand Up @@ -48,18 +48,18 @@ func ShouldShortCircuitExecution(tx kv.RwTx, logPrefix string) (bool, uint64, er
var shortCircuitBatch, shortCircuitBlock, cycle uint64

// this is so empty batches work
for shortCircuitBlock == 0 {
for shortCircuitBlock == 0 || (!l2ShortCircuitToVerifiedBatch && executedBatch+cycle < downloadedBatch) {
cycle++
// if executed lower than verified, short curcuit up to verified
if executedBatch < highestVerifiedBatchNo {
// if executed lower than verified, short circuit up to verified (only if l2ShortCircuitToVerifiedBatch is true)
if executedBatch < highestVerifiedBatchNo && l2ShortCircuitToVerifiedBatch {
if downloadedBatch < highestVerifiedBatchNo {
shortCircuitBatch = downloadedBatch
} else {
shortCircuitBatch = highestVerifiedBatchNo
}
} else if executedBatch+cycle <= downloadedBatch { // else short circuit up to next downloaded batch
shortCircuitBatch = executedBatch + cycle
} else { // if we don't have at least one more full downlaoded batch, don't short circuit and just execute to latest block
} else { // if we don't have at least one more full downloaded batch, don't short circuit and just execute to latest block
return false, 0, nil
}

Expand Down

0 comments on commit 3f88803

Please sign in to comment.