Skip to content

Commit 0938fe6

Browse files
committed
feat: configurable gas limits
1 parent 137c041 commit 0938fe6

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

.gas-snapshot

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ RollupPassageTest:test_exit() (gas: 22347)
2828
RollupPassageTest:test_exitToken() (gas: 50183)
2929
RollupPassageTest:test_fallback() (gas: 19883)
3030
RollupPassageTest:test_receive() (gas: 19844)
31-
TransactTest:test_enterTransact() (gas: 99486)
32-
TransactTest:test_setUp() (gas: 8481)
33-
TransactTest:test_transact() (gas: 97089)
34-
TransactTest:test_transact_defaultChain() (gas: 95892)
35-
TransactTest:test_transact_globalGasLimit() (gas: 99262)
36-
TransactTest:test_transact_perTransactGasLimit() (gas: 30687)
31+
TransactTest:test_configureGas() (gas: 22828)
32+
TransactTest:test_enterTransact() (gas: 103961)
33+
TransactTest:test_onlyGasAdmin() (gas: 8810)
34+
TransactTest:test_setUp() (gas: 17494)
35+
TransactTest:test_transact() (gas: 101431)
36+
TransactTest:test_transact_defaultChain() (gas: 100544)
37+
TransactTest:test_transact_globalGasLimit() (gas: 105063)
38+
TransactTest:test_transact_perTransactGasLimit() (gas: 32774)
3739
ZenithTest:test_addSequencer() (gas: 88121)
3840
ZenithTest:test_badSignature() (gas: 37241)
3941
ZenithTest:test_incorrectHostBlock() (gas: 35086)

script/Zenith.s.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import {HostOrders, RollupOrders} from "../src/Orders.sol";
99

1010
contract ZenithScript is Script {
1111
// deploy:
12-
// forge script ZenithScript --sig "deploy(uint256,address,address)" --rpc-url $RPC_URL --etherscan-api-key $ETHERSCAN_API_KEY --private-key $PRIVATE_KEY --broadcast --verify $ROLLUP_CHAIN_ID $WITHDRAWAL_ADMIN_ADDRESS $INITIAL_ENTER_TOKENS_ARRAY $SEQUENCER_ADMIN_ADDRESS
12+
// forge script ZenithScript --sig "deploy(uint256,address,address)" --rpc-url $RPC_URL --etherscan-api-key $ETHERSCAN_API_KEY --private-key $PRIVATE_KEY --broadcast --verify $ROLLUP_CHAIN_ID $WITHDRAWAL_ADMIN_ADDRESS $INITIAL_ENTER_TOKENS_ARRAY $SEQUENCER_AND_GAS_ADMIN_ADDRESS
1313
function deploy(
1414
uint256 defaultRollupChainId,
1515
address withdrawalAdmin,
1616
address[] memory initialEnterTokens,
17-
address sequencerAdmin
17+
address sequencerAndGasAdmin
1818
) public returns (Zenith z, Passage p, Transactor t, HostOrders m) {
1919
vm.startBroadcast();
20-
z = new Zenith(sequencerAdmin);
20+
z = new Zenith(sequencerAndGasAdmin);
2121
p = new Passage(defaultRollupChainId, withdrawalAdmin, initialEnterTokens);
22-
t = new Transactor(defaultRollupChainId, p);
22+
t = new Transactor(defaultRollupChainId, sequencerAndGasAdmin, p, 30_000_000, 5_000_000);
2323
m = new HostOrders();
2424
}
2525

src/Transact.sol

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import {Passage} from "./Passage.sol";
55

66
/// @notice A contract deployed to Host chain that enables transactions from L1 to be sent on an L2.
77
contract Transactor {
8-
/// @notice The sum of `transact` calls in a block cannot use more than 30M gas.
9-
uint256 public constant PER_BLOCK_TRANSACT_GAS_LIMIT = 30_000_000;
10-
11-
/// @notice Each `transact` call cannot use more than 5M gas.
12-
uint256 public constant PER_TRANSACT_GAS_LIMIT = 5_000_000;
13-
148
/// @notice The chainId of rollup that Ether will be sent to by default when entering the rollup via fallback() or receive().
159
uint256 public immutable defaultRollupChainId;
1610

11+
/// @notice The address that is allowed to configure `transact` gas limits.
12+
address public immutable gasAdmin;
13+
1714
/// @notice The address of the Passage contract, to enable transact + enter.
1815
Passage public immutable passage;
1916

17+
/// @notice The sum of `transact` calls in a block cannot use more than this limit.
18+
uint256 public perBlockGasLimit;
19+
20+
/// @notice Each `transact` call cannot use more than this limit.
21+
uint256 public perTransactGasLimit;
22+
2023
/// @notice The total gas used by `transact` so far in this block.
2124
/// rollupChainId => block number => `transasct` gasLimit used so far.
2225
mapping(uint256 => mapping(uint256 => uint256)) public transactGasUsed;
@@ -32,17 +35,37 @@ contract Transactor {
3235
uint256 maxFeePerGas
3336
);
3437

38+
/// @notice Emitted when the admin configures gas limits.
39+
event GasConfigured(uint256 perBlock, uint256 perTransact);
40+
3541
/// @notice Thrown when attempting to use more then the current global `transact` gasLimit for the block.
3642
error PerBlockTransactGasLimit();
3743

3844
/// @notice Thrown when attempting to use too much gas per single `transact` call.
3945
error PerTransactGasLimit();
4046

47+
/// @notice Thrown when attempting to configure gas if not the admin.
48+
error OnlyGasAdmin();
49+
4150
/// @param _defaultRollupChainId - the chainId of the rollup that Ether will be sent to by default
4251
/// when entering the rollup via fallback() or receive() fns.
43-
constructor(uint256 _defaultRollupChainId, Passage _passage) {
52+
constructor(
53+
uint256 _defaultRollupChainId,
54+
address _gasAdmin,
55+
Passage _passage,
56+
uint256 _perBlockGasLimit,
57+
uint256 _perTransactGasLimit
58+
) {
4459
defaultRollupChainId = _defaultRollupChainId;
60+
gasAdmin = _gasAdmin;
4561
passage = _passage;
62+
_configureGas(_perBlockGasLimit, _perTransactGasLimit);
63+
}
64+
65+
/// @notice Configure the `transact` gas limits.
66+
function configureGas(uint256 perBlock, uint256 perTransact) external {
67+
if (msg.sender != gasAdmin) revert OnlyGasAdmin();
68+
_configureGas(perBlock, perTransact);
4669
}
4770

4871
/// @notice Allows a special transaction to be sent to the rollup with sender == L1 msg.sender.
@@ -93,14 +116,21 @@ contract Transactor {
93116
}
94117

95118
// ensure per-transact gas limit is respected
96-
if (gas > PER_TRANSACT_GAS_LIMIT) revert PerTransactGasLimit();
119+
if (gas > perTransactGasLimit) revert PerTransactGasLimit();
97120

98121
// ensure global transact gas limit is respected
99122
uint256 gasUsed = transactGasUsed[rollupChainId][block.number];
100-
if (gasUsed + gas > PER_BLOCK_TRANSACT_GAS_LIMIT) revert PerBlockTransactGasLimit();
123+
if (gasUsed + gas > perBlockGasLimit) revert PerBlockTransactGasLimit();
101124
transactGasUsed[rollupChainId][block.number] = gasUsed + gas;
102125

103126
// emit Transact event
104127
emit Transact(rollupChainId, msg.sender, to, data, value, gas, maxFeePerGas);
105128
}
129+
130+
/// @notice Helper to configure gas limits on deploy & via admin function
131+
function _configureGas(uint256 perBlock, uint256 perTransact) internal {
132+
perBlockGasLimit = perBlock;
133+
perTransactGasLimit = perTransact;
134+
emit GasConfigured(perBlock, perTransact);
135+
}
106136
}

test/Transact.t.sol

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ contract TransactTest is Test {
2828
uint256 maxFeePerGas
2929
);
3030

31+
event GasConfigured(uint256 perBlock, uint256 perTransact);
32+
3133
// Passage event
3234
event Enter(uint256 indexed rollupChainId, address indexed rollupRecipient, uint256 amount);
3335

3436
function setUp() public {
3537
// deploy target
3638
passage = new Passage(block.chainid + 1, address(this), new address[](0));
37-
target = new Transactor(block.chainid + 1, passage);
39+
target = new Transactor(block.chainid + 1, address(this), passage, gas * 6, gas);
3840
}
3941

4042
function test_setUp() public {
4143
assertEq(target.defaultRollupChainId(), block.chainid + 1);
44+
assertEq(target.gasAdmin(), address(this));
4245
assertEq(address(target.passage()), address(passage));
46+
assertEq(target.perBlockGasLimit(), gas * 6);
47+
assertEq(target.perTransactGasLimit(), gas);
4348
}
4449

4550
function test_transact() public {
@@ -88,4 +93,23 @@ contract TransactTest is Test {
8893
vm.expectRevert(abi.encodeWithSelector(Transactor.PerBlockTransactGasLimit.selector));
8994
target.transact(to, data, value, 1, maxFeePerGas);
9095
}
96+
97+
function test_onlyGasAdmin() public {
98+
vm.startPrank(address(0x01));
99+
vm.expectRevert(Transactor.OnlyGasAdmin.selector);
100+
target.configureGas(0, 0);
101+
}
102+
103+
function test_configureGas() public {
104+
uint256 newPerBlock = 40_000_000;
105+
uint256 newPerTransact = 2_000_000;
106+
107+
// configure gas
108+
vm.expectEmit();
109+
emit GasConfigured(newPerBlock, newPerTransact);
110+
target.configureGas(newPerBlock, newPerTransact);
111+
112+
assertEq(target.perBlockGasLimit(), newPerBlock);
113+
assertEq(target.perTransactGasLimit(), newPerTransact);
114+
}
91115
}

0 commit comments

Comments
 (0)