Skip to content

Commit

Permalink
feat: add l2 block utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
cheethas committed May 2, 2023
1 parent df5d344 commit 999200f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions l1-contracts/src/core/Decoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pragma solidity >=0.8.18;
* | 0x22c + x + y + z | 0x04 | len(l1ToL2Messages) denoted w
* | 0x230 + x + y + z | w | l1ToL2Messages
* |--- |--- | ---
* TODO(rebase): update unknown values - they are not insync
* TODO: a,b,c,d are number of elements and not bytes, need to be multiplied by the length of the elements.
*/
contract Decoder {
Expand Down
21 changes: 20 additions & 1 deletion yarn-project/types/src/l2_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,18 @@ export class L2Block {
this.startTreeOfHistoricPrivateDataTreeRootsSnapshot,
this.startTreeOfHistoricContractTreeRootsSnapshot,
this.startPublicDataTreeRoot,
this.startL1ToL2MessageTreeSnapshot,
this.startTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
this.endPrivateDataTreeSnapshot,
this.endNullifierTreeSnapshot,
this.endContractTreeSnapshot,
this.endTreeOfHistoricPrivateDataTreeRootsSnapshot,
this.endTreeOfHistoricContractTreeRootsSnapshot,
this.endPublicDataTreeRoot,
// TODO(sean rebase): make sure getMessagesHash is included in here too!
this.endL1ToL2MessageTreeSnapshot,
this.endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
this.getCalldataHash(),
this.getL1ToL2MessagesHash(),
);
const temp = toBigIntBE(sha256(buf));
// Prime order of BN254 curve
Expand All @@ -308,6 +312,8 @@ export class L2Block {
this.startContractTreeSnapshot,
this.startTreeOfHistoricPrivateDataTreeRootsSnapshot,
this.startTreeOfHistoricContractTreeRootsSnapshot,
this.startL1ToL2MessageTreeSnapshot,
this.startTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
this.startPublicDataTreeRoot,
);
return sha256(inputValue);
Expand All @@ -325,6 +331,8 @@ export class L2Block {
this.endContractTreeSnapshot,
this.endTreeOfHistoricPrivateDataTreeRootsSnapshot,
this.endTreeOfHistoricContractTreeRootsSnapshot,
this.endL1ToL2MessageTreeSnapshot,
this.endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot,
this.endPublicDataTreeRoot,
);
return sha256(inputValue);
Expand Down Expand Up @@ -394,6 +402,17 @@ export class L2Block {
return computeRoot(leafs);
}

/**
* Compute the hash of all of this blocks l1 to l2 messages,
* The hash is also calculated within the contract when the block is submitted
* @returns
*/
getL1ToL2MessagesHash() {
// Create a long buffer of all of the l1 to l2 messages
const l1ToL2Messages = Buffer.concat(this.newL1ToL2Messages.map(message => message.toBuffer()));
return sha256(l1ToL2Messages);
}

/**
* Get the ith transaction in an L2 block.
* @param txIndex - The index of the tx in the block.
Expand Down

0 comments on commit 999200f

Please sign in to comment.