From 8cc6d54a7b5f4605cee1ecfe5c4ebc871ba8ccea Mon Sep 17 00:00:00 2001 From: Scott Fairclough <70711990+hexoscott@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:43:50 +0000 Subject: [PATCH] immediate batch seal flag for counter overflows (#1417) # Conflicts: # eth/ethconfig/config_zkevm.go # turbo/cli/default_flags.go # turbo/cli/flags_zkevm.go # zk/debug_tools/test-contracts/package.json # zk/stages/stage_sequence_execute.go --- cmd/utils/flags.go | 5 ++++ eth/ethconfig/config_zkevm.go | 9 ++++--- turbo/cli/default_flags.go | 1 + turbo/cli/flags_zkevm.go | 1 + .../test-contracts/contracts/KeccakLoop.sol | 10 +++++++ zk/debug_tools/test-contracts/package.json | 3 ++- .../test-contracts/scripts/keccak-loop.js | 26 +++++++++++++++++++ zk/stages/stage_sequence_execute.go | 4 +-- 8 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 zk/debug_tools/test-contracts/contracts/KeccakLoop.sol create mode 100644 zk/debug_tools/test-contracts/scripts/keccak-loop.js diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 45290535b50..96fe7d2042f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -748,6 +748,11 @@ var ( Usage: "The interval at which the sequencer checks the L1 for new GER information", Value: 1 * time.Minute, } + SealBatchImmediatelyOnOverflow = cli.BoolFlag{ + Name: "zkevm.seal-batch-immediately-on-overflow", + Usage: "Seal the batch immediately when detecting a counter overflow", + Value: false, + } ACLPrintHistory = cli.IntFlag{ Name: "acl.print-history", Usage: "Number of entries to print from the ACL history on node start up", diff --git a/eth/ethconfig/config_zkevm.go b/eth/ethconfig/config_zkevm.go index 89ff70fc810..8cd78e64753 100644 --- a/eth/ethconfig/config_zkevm.go +++ b/eth/ethconfig/config_zkevm.go @@ -87,10 +87,11 @@ type Zk struct { TxPoolRejectSmartContractDeployments bool - InitialBatchCfgFile string - ACLPrintHistory int - InfoTreeUpdateInterval time.Duration - BadBatches []uint64 + InitialBatchCfgFile string + ACLPrintHistory int + InfoTreeUpdateInterval time.Duration + BadBatches []uint64 + SealBatchImmediatelyOnOverflow bool } var DefaultZkConfig = &Zk{} diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 682bbbf2bec..b8dd0f6463e 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -286,4 +286,5 @@ var DefaultFlags = []cli.Flag{ &utils.ACLPrintHistory, &utils.InfoTreeUpdateInterval, + &utils.SealBatchImmediatelyOnOverflow, } diff --git a/turbo/cli/flags_zkevm.go b/turbo/cli/flags_zkevm.go index 02d773dc363..56ec8ec0bee 100644 --- a/turbo/cli/flags_zkevm.go +++ b/turbo/cli/flags_zkevm.go @@ -204,6 +204,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) { InitialBatchCfgFile: ctx.String(utils.InitialBatchCfgFile.Name), ACLPrintHistory: ctx.Int(utils.ACLPrintHistory.Name), InfoTreeUpdateInterval: ctx.Duration(utils.InfoTreeUpdateInterval.Name), + SealBatchImmediatelyOnOverflow: ctx.Bool(utils.SealBatchImmediatelyOnOverflow.Name), } utils2.EnableTimer(cfg.DebugTimers) diff --git a/zk/debug_tools/test-contracts/contracts/KeccakLoop.sol b/zk/debug_tools/test-contracts/contracts/KeccakLoop.sol new file mode 100644 index 00000000000..23c1349fb25 --- /dev/null +++ b/zk/debug_tools/test-contracts/contracts/KeccakLoop.sol @@ -0,0 +1,10 @@ +pragma solidity >=0.8.10; + +// 1616 for legacy, 226 for erigon -> 1198 -> 246 +contract KeccakLoop { + constructor () { + for(uint256 i = 0; i < 200; i++) { + keccak256(new bytes(i)); + } + } +} \ No newline at end of file diff --git a/zk/debug_tools/test-contracts/package.json b/zk/debug_tools/test-contracts/package.json index 06514e69be1..7022ac7abf5 100644 --- a/zk/debug_tools/test-contracts/package.json +++ b/zk/debug_tools/test-contracts/package.json @@ -20,7 +20,8 @@ "erc20Revert:sepolia": "npx hardhat compile && npx hardhat run scripts/ERC20-revert.js --network sepolia", "chainCall:local": "npx hardhat compile && npx hardhat run scripts/chain-call.js --network local", "chainCall:sepolia": "npx hardhat compile && npx hardhat run scripts/chain-call.js --network sepolia", - "create:local": "npx hardhat compile && npx hardhat run scripts/create.js --network local" + "create:local": "npx hardhat compile && npx hardhat run scripts/create.js --network local", + "keccak:local": "npx hardhat compile && npx hardhat run scripts/keccak-loop.js --network local" }, "keywords": [], "author": "", diff --git a/zk/debug_tools/test-contracts/scripts/keccak-loop.js b/zk/debug_tools/test-contracts/scripts/keccak-loop.js new file mode 100644 index 00000000000..5fe60b34bc3 --- /dev/null +++ b/zk/debug_tools/test-contracts/scripts/keccak-loop.js @@ -0,0 +1,26 @@ +async function main() { +try { + // Get the ContractFactory of your KeccakLoopContract + const KeccakLoopContract = await hre.ethers.getContractFactory("KeccakLoop"); + + // Deploy the contract + const contract = await KeccakLoopContract.deploy(); + // Wait for the deployment transaction to be mined + await contract.waitForDeployment(); + + console.log(`KeccakLoop deployed to: ${await contract.getAddress()}`); + + // const result = await contract.bigLoop(10000); + // console.log(result); + } catch (error) { + console.error(error); + process.exit(1); + } +} + +main() + .then(() => process.exit(0)) + .catch(error => { + console.error(error); + process.exit(1); + }); \ No newline at end of file diff --git a/zk/stages/stage_sequence_execute.go b/zk/stages/stage_sequence_execute.go index 5387b30fe91..e55f17e41f6 100644 --- a/zk/stages/stage_sequence_execute.go +++ b/zk/stages/stage_sequence_execute.go @@ -440,8 +440,8 @@ func sequencingBatchStep( ocs, _ := batchCounters.CounterStats(l1TreeUpdateIndex != 0) // was not included in this batch because it overflowed: counter x, counter y log.Info(transactionNotAddedText, "Counters context:", ocs, "overflow transactions", batchState.overflowTransactions) - if batchState.reachedOverflowTransactionLimit() { - log.Info(fmt.Sprintf("[%s] closing batch due to counters", logPrefix), "counters: ", batchState.overflowTransactions) + if batchState.reachedOverflowTransactionLimit() || cfg.zk.SealBatchImmediatelyOnOverflow { + log.Info(fmt.Sprintf("[%s] closing batch due to counters", logPrefix), "counters: ", batchState.overflowTransactions, "immediate", cfg.zk.SealBatchImmediatelyOnOverflow) runLoopBlocks = false break LOOP_TRANSACTIONS }