Skip to content

Commit

Permalink
do not build empty blocks on detecting an overflow
Browse files Browse the repository at this point in the history
also stop processing transactions when the block timer has ticked to
give more consistent block times
  • Loading branch information
hexoscott committed Nov 29, 2024
1 parent ba0a89e commit 340ea85
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
12 changes: 12 additions & 0 deletions zk/debug_tools/test-contracts/contracts/GasBurner.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

contract GasBurner {
constructor() {
//dynamic array
uint[] memory a = new uint[](12000);
for (uint i = 0; i < 2000; i++) {
a[i%10000] = i;
}
}
}
3 changes: 2 additions & 1 deletion zk/debug_tools/test-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"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",
"keccak:local": "npx hardhat compile && npx hardhat run scripts/keccak-loop.js --network local"
"keccak:local": "npx hardhat compile && npx hardhat run scripts/keccak-loop.js --network local",
"gasBurner:local": "npx hardhat compile && npx hardhat run scripts/gas-burner.js --network local"
},
"keywords": [],
"author": "",
Expand Down
26 changes: 26 additions & 0 deletions zk/debug_tools/test-contracts/scripts/gas-burner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
async function main() {
try {
// Get the ContractFactory of your BigLoopContract
const GasBurnerContract = await hre.ethers.getContractFactory("GasBurner");

// Deploy the contract
const contract = await GasBurnerContract.deploy();
// Wait for the deployment transaction to be mined
await contract.waitForDeployment();

console.log(`GasBurner 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);
});
44 changes: 36 additions & 8 deletions zk/stages/stage_sequence_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,22 @@ func sequencingBatchStep(
log.Info(fmt.Sprintf("[%s] Waiting for txs from the pool...", logPrefix))
}

LOOP_TRANSACTIONS:
innerBreak := false
emptyBlockOverflow := false

OuterLoopTransactions:
for {
if innerBreak {
break
}
select {
case <-logTicker.C:
if !batchState.isAnyRecovery() {
log.Info(fmt.Sprintf("[%s] Waiting some more for txs from the pool...", logPrefix))
}
case <-blockTicker.C:
if !batchState.isAnyRecovery() {
break LOOP_TRANSACTIONS
break OuterLoopTransactions
}
case <-batchTicker.C:
if !batchState.isAnyRecovery() {
Expand Down Expand Up @@ -380,7 +386,19 @@ func sequencingBatchStep(

badTxHashes := make([]common.Hash, 0)
minedTxHashes := make([]common.Hash, 0)

InnerLoopTransactions:
for i, transaction := range batchState.blockState.transactionsForInclusion {
// quick check if we should stop handling transactions
select {
case <-blockTicker.C:
if !batchState.isAnyRecovery() {
innerBreak = true
break InnerLoopTransactions
}
default:
}

txHash := transaction.Hash()
effectiveGas := batchState.blockState.getL1EffectiveGases(cfg, i)

Expand Down Expand Up @@ -457,7 +475,10 @@ func sequencingBatchStep(
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
if len(batchState.blockState.builtBlockElements.transactions) == 0 {
emptyBlockOverflow = true
}
break OuterLoopTransactions
}
}

Expand All @@ -474,7 +495,7 @@ func sequencingBatchStep(
}
log.Info(fmt.Sprintf("[%s] gas overflowed adding transaction to block", logPrefix), "block", blockNumber, "tx-hash", txHash)
runLoopBlocks = false
break LOOP_TRANSACTIONS
break OuterLoopTransactions
case overflowNone:
}

Expand All @@ -494,12 +515,12 @@ func sequencingBatchStep(
if len(batchState.blockState.transactionsForInclusion) == 0 {
// We need to jump to the next block here if there are no transactions in current block
batchState.resequenceBatchJob.UpdateLastProcessedTx(batchState.resequenceBatchJob.CurrentBlock().L2Blockhash)
break LOOP_TRANSACTIONS
break OuterLoopTransactions
}

if batchState.resequenceBatchJob.AtNewBlockBoundary() {
// We need to jump to the next block here if we are at the end of the current block
break LOOP_TRANSACTIONS
break OuterLoopTransactions
} else {
if cfg.zk.SequencerResequenceStrict {
return fmt.Errorf("strict mode enabled, but resequenced batch %d has transactions that overflowed counters or failed transactions", batchState.batchNumber)
Expand Down Expand Up @@ -533,16 +554,23 @@ func sequencingBatchStep(
log.Info(fmt.Sprintf("[%s] L1 recovery no more transactions to recover", logPrefix))
}

break LOOP_TRANSACTIONS
break OuterLoopTransactions
}

if batchState.isLimboRecovery() {
runLoopBlocks = false
break LOOP_TRANSACTIONS
break OuterLoopTransactions
}
}
}

// we do not want to commit this block if it has no transactions and we detected an overflow - essentially the batch is too
// full to get any more transactions in it and we don't want to commit an empty block
if emptyBlockOverflow {
log.Info(fmt.Sprintf("[%s] Block %d overflow detected with no transactions added, skipping block for next batch", logPrefix, blockNumber))
break
}

if block, err = doFinishBlockAndUpdateState(batchContext, ibs, header, parentBlock, batchState, ger, l1BlockHash, l1TreeUpdateIndex, infoTreeIndexProgress, batchCounters); err != nil {
return err
}
Expand Down

0 comments on commit 340ea85

Please sign in to comment.