Skip to content

Commit

Permalink
feat(contracts): use constants where possible (#1238)
Browse files Browse the repository at this point in the history
# Description
Unsure about the best name for `constantsPerBase` or the
`leafDataLengthsPerBase` but these reduce constants, don't increase gas
too much (increase of ~400) and are a way to avoid stack too deep error

# Checklist:

- [ ] I have reviewed my diff in github, line by line.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to the issue(s) that it resolves.
- [ ] There are no unexpected formatting changes, superfluous debug
logs, or commented-out code.
- [ ] The branch has been merged or rebased against the head of its
merge target.
- [ ] I'm happy for the PR to be merged at the reviewer's next
convenience.

---------

Co-authored-by: Maddiaa0 <[email protected]>
Co-authored-by: Jan Beneš <[email protected]>
Co-authored-by: Lasse Herskind <[email protected]>
Co-authored-by: LHerskind <[email protected]>
  • Loading branch information
5 people authored Jul 31, 2023
1 parent 77f33b8 commit 6c6a31b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 246 deletions.
11 changes: 3 additions & 8 deletions l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,10 @@ contract Rollup is IRollup {
}

function _constrainGlobals(bytes calldata _l2Block) internal view {
uint256 chainId;
uint256 version;
uint256 ts;
uint256 chainId = uint256(bytes32(_l2Block[:0x20]));
uint256 version = uint256(bytes32(_l2Block[0x20:0x40]));
uint256 ts = uint256(bytes32(_l2Block[0x60:0x80]));
// block number already constrained by start state hash
assembly {
chainId := calldataload(_l2Block.offset)
version := calldataload(add(_l2Block.offset, 0x20))
ts := calldataload(add(_l2Block.offset, 0x60))
}

if (block.chainid != chainId) {
revert Errors.Rollup__InvalidChainId(chainId, block.chainid);
Expand Down
21 changes: 20 additions & 1 deletion l1-contracts/src/core/libraries/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,24 @@ library Constants {
uint256 internal constant PUBLIC_DATA_WRITES_PER_TX = 4;
uint256 internal constant CONTRACTS_PER_TX = 1;
uint256 internal constant L2_TO_L1_MSGS_PER_TX = 2;
uint256 internal constant L1_TO_L2_MSGS_PER_ROLLUP = 16;
uint256 internal constant L1_TO_L2_MSGS_PER_BASE_ROLLUP = 16;
uint256 internal constant KERNELS_PER_BASE_ROLLUP = 2;

// number of bytes taken up:
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * COMMITMENTS_PER_TX * 0x20;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * NULLIFIERS_PER_TX * 0x20;
uint256 internal constant PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * PUBLIC_DATA_WRITES_PER_TX * 0x40;
uint256 internal constant CONTRACTS_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * CONTRACTS_PER_TX * 0x20;
uint256 internal constant CONTRACT_DATA_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * CONTRACTS_PER_TX * 0x40; //aztec address + eth address (padded to 0x20)
uint256 internal constant CONTRACT_DATA_NUM_BYTES_PER_BASE_ROLLUP_UNPADDED =
KERNELS_PER_BASE_ROLLUP * CONTRACTS_PER_TX * 0x34; // same as prev except doesn't pad eth address. So 0x20 (aztec address) + 0x14 (eth address)
uint256 internal constant L2_TO_L1_MSGS_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * L2_TO_L1_MSGS_PER_TX * 0x20;
uint256 internal constant LOGS_HASHES_NUM_BYTES_PER_BASE_ROLLUP =
KERNELS_PER_BASE_ROLLUP * 2 * 0x20; // encrypted and unencrypted log types per tx
}
Loading

0 comments on commit 6c6a31b

Please sign in to comment.