From f27add660e23b46da61099f0908d67193e1e3b09 Mon Sep 17 00:00:00 2001 From: Niven Date: Tue, 9 Jul 2024 18:25:13 +0800 Subject: [PATCH] Add sequencer slowdown (#39) --- cmd/utils/flags_xlayer.go | 7 ++++++- eth/ethconfig/apollo_xlayer.go | 10 ++++++++++ eth/ethconfig/config_xlayer.go | 4 ++++ go.sum | 2 +- test/config/test.erigon.rpc.config.yaml | 1 + test/config/test.erigon.seq.config.yaml | 1 + turbo/cli/default_flags.go | 1 + turbo/cli/flags_xlayer.go | 3 ++- zk/stages/stage_sequence_execute.go | 3 +++ zk/stages/stage_sequence_execute_xlayer.go | 17 +++++++++++++++++ 10 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 zk/stages/stage_sequence_execute_xlayer.go diff --git a/cmd/utils/flags_xlayer.go b/cmd/utils/flags_xlayer.go index 272629672b4..0cf6bd8d1cf 100644 --- a/cmd/utils/flags_xlayer.go +++ b/cmd/utils/flags_xlayer.go @@ -12,7 +12,7 @@ import ( ) var ( - // XLayer apollo + // X Layer apollo ApolloEnableFlag = cli.BoolFlag{ Name: "zkevm.apollo-enabled", Usage: "Apollo enable flag.", @@ -152,6 +152,11 @@ var ( Usage: "Used to determine whether pending tx has reached the threshold for congestion", Value: 0, } + SequencerBatchSleepDuration = cli.DurationFlag{ + Name: "zkevm.sequencer-batch-sleep-duration", + Usage: "Full batch sleep duration is the time the sequencer sleeps between each full batch iteration.", + Value: 0 * time.Second, + } ) func setGPOXLayer(ctx *cli.Context, cfg *gaspricecfg.XLayerConfig) { diff --git a/eth/ethconfig/apollo_xlayer.go b/eth/ethconfig/apollo_xlayer.go index be64be70a70..4b1d650fae6 100644 --- a/eth/ethconfig/apollo_xlayer.go +++ b/eth/ethconfig/apollo_xlayer.go @@ -3,6 +3,7 @@ package ethconfig import ( "fmt" "sync" + "time" ) // ApolloConfig is the apollo eth backend dynamic config @@ -41,3 +42,12 @@ func (c *ApolloConfig) Enable() bool { defer c.RUnlock() return c.EnableApollo } + +func GetFullBatchSleepDuration(localDuration time.Duration) time.Duration { + conf, err := GetApolloConfig() + if err != nil { + return localDuration + } else { + return conf.Zk.XLayer.SequencerBatchSleepDuration + } +} diff --git a/eth/ethconfig/config_xlayer.go b/eth/ethconfig/config_xlayer.go index e2f0422cea3..6105b8e3b48 100644 --- a/eth/ethconfig/config_xlayer.go +++ b/eth/ethconfig/config_xlayer.go @@ -1,10 +1,14 @@ package ethconfig +import "time" + // XLayerConfig is the X Layer config used on the eth backend type XLayerConfig struct { Apollo ApolloClientConfig Nacos NacosConfig EnableInnerTx bool + // Sequencer + SequencerBatchSleepDuration time.Duration } var DefaultXLayerConfig = &XLayerConfig{} diff --git a/go.sum b/go.sum index 6b9f2f263f7..d1ec78f3b71 100644 --- a/go.sum +++ b/go.sum @@ -1435,9 +1435,9 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/test/config/test.erigon.rpc.config.yaml b/test/config/test.erigon.rpc.config.yaml index bb48b3721fc..91faabf2b03 100644 --- a/test/config/test.erigon.rpc.config.yaml +++ b/test/config/test.erigon.rpc.config.yaml @@ -30,6 +30,7 @@ zkevm.sequencer-initial-fork-id: 9 zkevm.sequencer-block-seal-time: "6s" zkevm.sequencer-batch-seal-time: "12s" zkevm.sequencer-non-empty-batch-seal-time: "3s" +zkevm.sequencer-batch-sleep-duration: "0s" zkevm.data-stream-host: "localhost" zkevm.data-stream-port: 6900 diff --git a/test/config/test.erigon.seq.config.yaml b/test/config/test.erigon.seq.config.yaml index 79047aa0757..3c602a3b4da 100644 --- a/test/config/test.erigon.seq.config.yaml +++ b/test/config/test.erigon.seq.config.yaml @@ -30,6 +30,7 @@ zkevm.sequencer-initial-fork-id: 9 zkevm.sequencer-block-seal-time: "6s" zkevm.sequencer-batch-seal-time: "12s" zkevm.sequencer-non-empty-batch-seal-time: "3s" +zkevm.sequencer-batch-sleep-duration: "0s" zkevm.data-stream-host: "localhost" zkevm.data-stream-port: 6900 diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 33c175e540b..34daa1c6b9e 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -250,4 +250,5 @@ var DefaultFlags = []cli.Flag{ &utils.TxPoolEnableWhitelistFlag, &utils.TxPoolWhiteList, &utils.TxPoolBlockedList, + &utils.SequencerBatchSleepDuration, } diff --git a/turbo/cli/flags_xlayer.go b/turbo/cli/flags_xlayer.go index 37819fd13bf..601363c42ed 100644 --- a/turbo/cli/flags_xlayer.go +++ b/turbo/cli/flags_xlayer.go @@ -20,6 +20,7 @@ func ApplyFlagsForXLayerConfig(ctx *cli.Context, cfg *ethconfig.Config) { ApplicationName: ctx.String(utils.NacosApplicationNameFlag.Name), ExternalListenAddr: ctx.String(utils.NacosExternalListenAddrFlag.Name), }, - EnableInnerTx: ctx.Bool(utils.AllowInternalTransactions.Name), + EnableInnerTx: ctx.Bool(utils.AllowInternalTransactions.Name), + SequencerBatchSleepDuration: ctx.Duration(utils.SequencerBatchSleepDuration.Name), } } diff --git a/zk/stages/stage_sequence_execute.go b/zk/stages/stage_sequence_execute.go index 61290cf7561..d337b611314 100644 --- a/zk/stages/stage_sequence_execute.go +++ b/zk/stages/stage_sequence_execute.go @@ -452,6 +452,9 @@ func SpawnSequencingStage( } } + // X Layer handle + tryToSleepSequencer(cfg.zk.XLayer.SequencerBatchSleepDuration, logPrefix) + return nil } diff --git a/zk/stages/stage_sequence_execute_xlayer.go b/zk/stages/stage_sequence_execute_xlayer.go new file mode 100644 index 00000000000..36e6273d8b1 --- /dev/null +++ b/zk/stages/stage_sequence_execute_xlayer.go @@ -0,0 +1,17 @@ +package stages + +import ( + "fmt" + "time" + + "github.com/ledgerwatch/erigon/eth/ethconfig" + "github.com/ledgerwatch/log/v3" +) + +func tryToSleepSequencer(localDuration time.Duration, logPrefix string) { + fullBatchSleepDuration := ethconfig.GetFullBatchSleepDuration(localDuration) + if fullBatchSleepDuration > 0 { + log.Info(fmt.Sprintf("[%s] Slow down sequencer: %v", logPrefix, fullBatchSleepDuration)) + time.Sleep(fullBatchSleepDuration) + } +}