Skip to content
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

Merged
merged 6 commits into from
May 17, 2023
Merged

feat: decoder return l1<->l2 msgs #597

merged 6 commits into from
May 17, 2023

Conversation

LHerskind
Copy link
Contributor

@LHerskind LHerskind commented May 16, 2023

Description

Useful for #523.

The decoder will now return a list of l1 -> l2 and l2 -> l1 messages to the rollup. These can be used for batch insertsion into inbox and outboxes.

Notice that this will be returning zero values for empty messages, so the batch insertion should be altered to skip when "empty" messages are received. This is safe under the assumption that no-one have a preimage that hashes to 0.

Checklist:

  • I have reviewed my diff in github, line by line.
  • Every change is related to the PR description.
  • I have linked 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.

@LHerskind LHerskind marked this pull request as ready for review May 16, 2023 17:04
Copy link
Member

@Maddiaa0 Maddiaa0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, all comments are nits

}

/**
* 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)
Copy link
Member

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

function _computePublicInputsHash(bytes calldata _l2Block)
internal
pure
returns (bytes32, bytes32[] memory l2ToL1Msgs, bytes32[] memory l1ToL2Msgs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some named return values while others arent

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);
Copy link
Member

Choose a reason for hiding this comment

The 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?

@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood

internal
pure
returns (bytes32, bytes32)
returns (bytes32, bytes32[] memory, bytes32[] memory, bytes32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
returns (bytes32, bytes32[] memory, bytes32[] memory, bytes32)
returns (bytes32, bytes32, bytes32[] memory, bytes32[] memory)

id probably prefer the hashes to be together and the arrays together

@@ -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"
Copy link
Member

@Maddiaa0 Maddiaa0 May 16, 2023

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 our use, even if I like foundry more, TS seems like a better fit to have the tests because we will have so much encoding and stuff to deal with 😬 I only really see our foundry tests right now as useful for better debugging when building and for checking stuff without all the encoding/decoding.

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);
publicInputHash = bytes32(uint256(sha256(temp)) % P);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to be annoying again but this asm can be in its own function with the diffRoot and msgHash passed in

@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood

Copy link
Contributor

@rahul-kothari rahul-kothari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM - 1 nit on naming

@@ -341,25 +351,23 @@ contract Decoder {
}

bytes32 diffRoot = _computeRoot(baseLeafs);

bytes32[] memory l1ToL2Messages;
bytes32 messagesHash;
{
uint256 messagesHashPreimageSize = 0x20 * L1_TO_L2_MESSAGES_PER_ROLLUP;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should messagesHashPreimageSize be renamed to L1ToL2MesssagesHashPreimageSize for consistency?
Same for messagesHash. It is unclear to me if it is for L1toL2 or L2toL1 or does it also include other kind of messages. In the natspec, you call this L1ToL2MessagesHash

@LHerskind LHerskind merged commit 83a0f63 into master May 17, 2023
@LHerskind LHerskind deleted the lh/decoder-l1-l2-msgs branch May 17, 2023 09:48
ludamad pushed a commit that referenced this pull request Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants