-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: decoder return l1<->l2 msgs #597
Changes from 2 commits
2da9899
9358979
567866f
cc1e3a9
1826835
6e29297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -89,23 +89,29 @@ contract Decoder { | |||||
uint256 l2BlockNumber, | ||||||
bytes32 startStateHash, | ||||||
bytes32 endStateHash, | ||||||
bytes32 publicInputHash | ||||||
bytes32 publicInputHash, | ||||||
bytes32[] memory l2ToL1Msgs, | ||||||
bytes32[] memory l1ToL2Msgs | ||||||
) | ||||||
{ | ||||||
l2BlockNumber = _getL2BlockNumber(_l2Block); | ||||||
// Note, for startStateHash to match the storage, the l2 block number must be new - 1. | ||||||
// Only jumping 1 block at a time. | ||||||
startStateHash = _computeStateHash(l2BlockNumber - 1, 0x4, _l2Block); | ||||||
endStateHash = _computeStateHash(l2BlockNumber, 0x120, _l2Block); | ||||||
publicInputHash = _computePublicInputsHash(_l2Block); | ||||||
(publicInputHash, l2ToL1Msgs, l1ToL2Msgs) = _computePublicInputsHash(_l2Block); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Computes a hash of the public inputs from the calldata | ||||||
* @param _l2Block - The L2 block calldata. | ||||||
* @return sha256(header[0x4: 0x23c], diffRoot, l1Tol2MessagesHash) | ||||||
*/ | ||||||
function _computePublicInputsHash(bytes calldata _l2Block) internal pure returns (bytes32) { | ||||||
function _computePublicInputsHash(bytes calldata _l2Block) | ||||||
internal | ||||||
pure | ||||||
returns (bytes32, bytes32[] memory l2ToL1Msgs, bytes32[] memory l1ToL2Msgs) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some named return values while others arent |
||||||
{ | ||||||
// header size - block number size + one value for the diffRoot + one value for l1ToL2MessagesHash | ||||||
uint256 size = 0x23c - 0x04 + 0x20 + 0x20; | ||||||
|
||||||
|
@@ -115,15 +121,16 @@ contract Decoder { | |||||
calldatacopy(add(temp, 0x20), add(_l2Block.offset, 0x04), size) | ||||||
} | ||||||
|
||||||
// Diff root | ||||||
(bytes32 diffRoot, bytes32 l1ToL2messagesHash) = _computeDiffRootAndMessagesHash(_l2Block); | ||||||
bytes32 diffRoot; | ||||||
bytes32 l1ToL2messagesHash; | ||||||
(diffRoot, l2ToL1Msgs, l1ToL2Msgs, l1ToL2messagesHash) = _computeConsumables(_l2Block); | ||||||
assembly { | ||||||
let endOfTreesData := sub(0x23c, 0x04) | ||||||
mstore(add(temp, add(0x20, endOfTreesData)), diffRoot) | ||||||
mstore(add(temp, add(0x40, endOfTreesData)), l1ToL2messagesHash) | ||||||
} | ||||||
|
||||||
return bytes32(uint256(sha256(temp)) % P); | ||||||
return (bytes32(uint256(sha256(temp)) % P), l2ToL1Msgs, l1ToL2Msgs); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may as well make the mod p variable named on its own then you can spare the return statement. Having a calc in the return statement while others are pre calculated is a bit messy can we be consistent? |
||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -185,13 +192,17 @@ contract Decoder { | |||||
} | ||||||
|
||||||
/** | ||||||
* @notice Creates a "diff" tree and compute its root | ||||||
* @notice Computes the consumables for the block | ||||||
* @param _l2Block - The L2 block calldata. | ||||||
* @return diffRoot - The root of the diff tree (new commitments, nullifiers etc) | ||||||
* @return l2ToL1Msgs - The L2 to L1 messages of the block | ||||||
* @return l1ToL2Msgs - The L1 to L2 messages of the block | ||||||
* @return l1ToL2messagesHash - The hash of the L1 to L2 messages | ||||||
*/ | ||||||
function _computeDiffRootAndMessagesHash(bytes calldata _l2Block) | ||||||
function _computeConsumables(bytes calldata _l2Block) | ||||||
internal | ||||||
pure | ||||||
returns (bytes32, bytes32) | ||||||
returns (bytes32, bytes32[] memory, bytes32[] memory, bytes32) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
id probably prefer the hashes to be together and the arrays together |
||||||
{ | ||||||
// Find the lengths of the different inputs | ||||||
// TOOD: Naming / getting the messages root within this function is a bit weird | ||||||
|
@@ -225,6 +236,9 @@ contract Decoder { | |||||
bytes32[] memory baseLeafs = new bytes32[]( | ||||||
lengths.commitmentCount / (COMMITMENTS_PER_KERNEL * 2) | ||||||
); | ||||||
bytes32[] memory l2ToL1Msgs = new bytes32[]( | ||||||
lengths.l2ToL1MessagesCount | ||||||
); | ||||||
|
||||||
// Data starts after header. Look at L2 Block Data specification at the top of this file. | ||||||
{ | ||||||
|
@@ -236,6 +250,15 @@ contract Decoder { | |||||
offsets.contractDataOffset = offsets.contractOffset + lengths.contractCount * 0x20; | ||||||
offsets.l1ToL2MessagesOffset = offsets.contractDataOffset + 0x4 + lengths.contractCount * 0x34; | ||||||
|
||||||
// load the l2 to l1 msgs | ||||||
assembly { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this array copy be moved down to where the other one is performed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using that we just computer the offset values. We will be updating them every loop so was a nice way to have it right away. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. understood |
||||||
calldatacopy( | ||||||
add(l2ToL1Msgs, 0x20), | ||||||
add(_l2Block.offset, mload(add(offsets, 0x60))), | ||||||
mul(mload(add(lengths, 0x60)), 0x20) | ||||||
) | ||||||
} | ||||||
|
||||||
for (uint256 i = 0; i < baseLeafs.length; i++) { | ||||||
/** | ||||||
* Compute the leaf to insert. | ||||||
|
@@ -341,25 +364,23 @@ contract Decoder { | |||||
} | ||||||
|
||||||
bytes32 diffRoot = _computeRoot(baseLeafs); | ||||||
|
||||||
bytes32[] memory l1ToL2Messages; | ||||||
bytes32 messagesHash; | ||||||
{ | ||||||
uint256 messagesHashPreimageSize = 0x20 * L1_TO_L2_MESSAGES_PER_ROLLUP; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should |
||||||
bytes memory messagesHashPreimage = new bytes( | ||||||
messagesHashPreimageSize | ||||||
); | ||||||
l1ToL2Messages = new bytes32[](L1_TO_L2_MESSAGES_PER_ROLLUP); | ||||||
assembly { | ||||||
calldatacopy( | ||||||
add(messagesHashPreimage, 0x20), | ||||||
add(l1ToL2Messages, 0x20), | ||||||
add(_l2Block.offset, mload(add(offsets, 0xc0))), | ||||||
messagesHashPreimageSize | ||||||
) | ||||||
} | ||||||
|
||||||
messagesHash = sha256(messagesHashPreimage); | ||||||
messagesHash = sha256(abi.encodePacked(l1ToL2Messages)); | ||||||
} | ||||||
|
||||||
return (diffRoot, messagesHash); | ||||||
return (diffRoot, l2ToL1Msgs, l1ToL2Messages, messagesHash); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,8 +42,14 @@ contract DecoderTest is Test { | |
"Invalid messages hash" | ||
); | ||
|
||
(uint256 l2BlockNumber, bytes32 startStateHash, bytes32 endStateHash, bytes32 publicInputsHash) | ||
= helper.decode(block_empty_1); | ||
( | ||
uint256 l2BlockNumber, | ||
bytes32 startStateHash, | ||
bytes32 endStateHash, | ||
bytes32 publicInputsHash, | ||
bytes32[] memory l2ToL1Msgs, | ||
bytes32[] memory l1ToL2Msgs | ||
) = helper.decode(block_empty_1); | ||
|
||
assertEq(l2BlockNumber, 1, "Invalid block number"); | ||
assertEq( | ||
|
@@ -64,6 +70,13 @@ contract DecoderTest is Test { | |
|
||
rollup.process(bytes(""), block_empty_1); | ||
|
||
for (uint256 i = 0; i < l2ToL1Msgs.length; i++) { | ||
assertEq(l2ToL1Msgs[i], bytes32(0), "Invalid l2ToL1Msgs"); | ||
} | ||
for (uint256 i = 0; i < l1ToL2Msgs.length; i++) { | ||
assertEq(l1ToL2Msgs[i], bytes32(0), "Invalid l1ToL2Msgs"); | ||
} | ||
|
||
assertEq(rollup.rollupStateHash(), endStateHash, "Invalid rollup state hash"); | ||
} | ||
|
||
|
@@ -82,8 +95,14 @@ contract DecoderTest is Test { | |
"Invalid messages hash" | ||
); | ||
|
||
(uint256 l2BlockNumber, bytes32 startStateHash, bytes32 endStateHash, bytes32 publicInputsHash) | ||
= helper.decode(block_mixed_1); | ||
( | ||
uint256 l2BlockNumber, | ||
bytes32 startStateHash, | ||
bytes32 endStateHash, | ||
bytes32 publicInputsHash, | ||
bytes32[] memory l2ToL1Msgs, | ||
bytes32[] memory l1ToL2Msgs | ||
) = helper.decode(block_mixed_1); | ||
|
||
assertEq(l2BlockNumber, 1, "Invalid block number"); | ||
assertEq( | ||
|
@@ -102,6 +121,15 @@ contract DecoderTest is Test { | |
"Invalid public input hash" | ||
); | ||
|
||
for (uint256 i = 0; i < l2ToL1Msgs.length; i++) { | ||
assertEq( | ||
l2ToL1Msgs[i], bytes32(uint256(0x300 + 32 * (1 + i / 2) + i % 2)), "Invalid l2ToL1Msgs" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you lift the value calc up a line so its a bit more readable (ps. bruh we gotta automate test generation) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be adding the test in the integration test for L1 publisher when adding in rollup, then tested in there for the automatic. |
||
); | ||
} | ||
for (uint256 i = 0; i < l1ToL2Msgs.length; i++) { | ||
assertEq(l1ToL2Msgs[i], bytes32(uint256(0x401 + i)), "Invalid l1ToL2Msgs"); | ||
} | ||
|
||
rollup.process(bytes(""), block_mixed_1); | ||
|
||
assertEq(rollup.rollupStateHash(), endStateHash, "Invalid rollup state hash"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this doesnt make sense anymore if its also returning the message arrays. Comment natspec also needs updated