Skip to content

Commit

Permalink
feat: Make decoder a library instead (#675)
Browse files Browse the repository at this point in the history
* fix: add linter

* chore: remove yarn.lock

* feat: Make decoder a library instead

* fix: import ordering
  • Loading branch information
LHerskind authored May 23, 2023
1 parent ea5d863 commit e808315
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
6 changes: 3 additions & 3 deletions l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {IOutbox} from "@aztec/core/interfaces/messagebridge/IOutbox.sol";
import {IRegistry} from "@aztec/core/interfaces/messagebridge/IRegistry.sol";

// Libraries
import {Decoder} from "@aztec/core/libraries/Decoder.sol";
import {Errors} from "@aztec/core/libraries/Errors.sol";
import {Decoder} from "./Decoder.sol";

// Contracts
import {MockVerifier} from "@aztec/mock/MockVerifier.sol";
Expand All @@ -21,7 +21,7 @@ import {MockVerifier} from "@aztec/mock/MockVerifier.sol";
* @notice Rollup contract that are concerned about readability and velocity of development
* not giving a damn about gas costs.
*/
contract Rollup is IRollup, Decoder {
contract Rollup is IRollup {
MockVerifier public immutable VERIFIER;
IRegistry public immutable REGISTRY;

Expand All @@ -45,7 +45,7 @@ contract Rollup is IRollup, Decoder {
bytes32 publicInputHash,
bytes32[] memory l2ToL1Msgs,
bytes32[] memory l1ToL2Msgs
) = _decode(_l2Block);
) = Decoder.decode(_l2Block);

// @todo @LHerskind Proper genesis state. If the state is empty, we allow anything for now.
if (rollupStateHash != bytes32(0) && rollupStateHash != oldStateHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Constants} from "@aztec/core/libraries/Constants.sol";
import {Hash} from "@aztec/core/libraries/Hash.sol";

/**
* @title Decoder
* @title Decoder Library
* @author Aztec Labs
* @notice Decoding a L2 block, concerned with readability and velocity of development
* not giving a damn about gas costs.
Expand Down Expand Up @@ -70,7 +70,7 @@ import {Hash} from "@aztec/core/libraries/Hash.sol";
* | K + f * 0x20 + 0x04 + g + 0x04 | h | newUnencryptedLogs
* |--- |--- | ---
*/
contract Decoder {
library Decoder {
struct ArrayLengths {
uint256 commitmentCount;
uint256 nullifierCount;
Expand Down Expand Up @@ -115,7 +115,7 @@ contract Decoder {
* @return l2ToL1Msgs - The L2 to L1 messages
* @return l1ToL2Msgs - The L1 to L2 messages
*/
function _decode(bytes calldata _l2Block)
function decode(bytes calldata _l2Block)
internal
pure
returns (
Expand All @@ -127,16 +127,16 @@ contract Decoder {
bytes32[] memory l1ToL2Msgs
)
{
l2BlockNumber = _getL2BlockNumber(_l2Block);
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);
startStateHash = computeStateHash(l2BlockNumber - 1, 0x4, _l2Block);
endStateHash = computeStateHash(l2BlockNumber, 0x120, _l2Block);

bytes32 diffRoot;
bytes32 l1ToL2MsgsHash;
(diffRoot, l1ToL2MsgsHash, l2ToL1Msgs, l1ToL2Msgs) = _computeConsumables(_l2Block);
publicInputHash = _computePublicInputHash(_l2Block, diffRoot, l1ToL2MsgsHash);
(diffRoot, l1ToL2MsgsHash, l2ToL1Msgs, l1ToL2Msgs) = computeConsumables(_l2Block);
publicInputHash = computePublicInputHash(_l2Block, diffRoot, l1ToL2MsgsHash);
}

/**
Expand All @@ -147,7 +147,7 @@ contract Decoder {
* @param _l1ToL2MsgsHash - The hash of the L1 to L2 messages
* @return publicInputHash - The hash of the public inputs (sha256 to field)
*/
function _computePublicInputHash(
function computePublicInputHash(
bytes calldata _l2Block,
bytes32 _diffRoot,
bytes32 _l1ToL2MsgsHash
Expand All @@ -168,7 +168,7 @@ contract Decoder {
* @param _l2Block - The L2 block calldata
* @return l2BlockNumber - The L2 block number
*/
function _getL2BlockNumber(bytes calldata _l2Block) internal pure returns (uint256 l2BlockNumber) {
function getL2BlockNumber(bytes calldata _l2Block) internal pure returns (uint256 l2BlockNumber) {
assembly {
l2BlockNumber := and(shr(224, calldataload(_l2Block.offset)), 0xffffffff)
}
Expand All @@ -184,7 +184,7 @@ contract Decoder {
* the block number, snapshots of all the trees and the root of the public data tree. This function
* copies all of these to memory and then hashes them.
*/
function _computeStateHash(uint256 _l2BlockNumber, uint256 _offset, bytes calldata _l2Block)
function computeStateHash(uint256 _l2BlockNumber, uint256 _offset, bytes calldata _l2Block)
internal
pure
returns (bytes32)
Expand Down Expand Up @@ -214,7 +214,7 @@ contract Decoder {
* @return l2ToL1Msgs - The L2 to L1 messages of the block
* @return l1ToL2Msgs - The L1 to L2 messages of the block
*/
function _computeConsumables(bytes calldata _l2Block)
function computeConsumables(bytes calldata _l2Block)
internal
pure
returns (bytes32, bytes32, bytes32[] memory, bytes32[] memory)
Expand Down Expand Up @@ -405,7 +405,7 @@ contract Decoder {
}
}

bytes32 diffRoot = _computeRoot(vars.baseLeaves);
bytes32 diffRoot = computeRoot(vars.baseLeaves);
bytes32[] memory l1ToL2Msgs;
bytes32 l1ToL2MsgsHash;
{
Expand Down Expand Up @@ -451,7 +451,7 @@ contract Decoder {
* @dev Link to a relevant discussion:
* https://discourse.aztec.network/t/proposal-forcing-the-sequencer-to-actually-submit-data-to-l1/426/9
*/
function _computeKernelLogsHash(uint256 _offset, bytes calldata /* _l2Block */ )
function computeKernelLogsHash(uint256 _offset, bytes calldata /* _l2Block */ )
internal
pure
returns (bytes32, uint256)
Expand Down Expand Up @@ -509,7 +509,7 @@ contract Decoder {
* @param _leafs - The 32 bytes leafs to build the tree of.
* @return The root of the Merkle tree.
*/
function _computeRoot(bytes32[] memory _leafs) internal pure returns (bytes32) {
function computeRoot(bytes32[] memory _leafs) internal pure returns (bytes32) {
// @todo Must pad the tree
uint256 treeDepth = 0;
while (2 ** treeDepth < _leafs.length) {
Expand Down
1 change: 0 additions & 1 deletion l1-contracts/test/Decoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity >=0.8.18;

import {Test} from "forge-std/Test.sol";

import {Decoder} from "@aztec/core/Decoder.sol";
import {Hash} from "@aztec/core/libraries/Hash.sol";
import {Constants} from "@aztec/core/libraries/Constants.sol";
import {DataStructures} from "@aztec/core/libraries/DataStructures.sol";
Expand Down
11 changes: 6 additions & 5 deletions l1-contracts/test/DecoderHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;

import {Decoder} from "@aztec/core/Decoder.sol";
import {Decoder} from "@aztec/core/libraries/Decoder.sol";
import {Rollup} from "@aztec/core/Rollup.sol";

contract DecoderHelper is Decoder {
contract DecoderHelper {
function decode(bytes calldata _l2Block)
external
pure
returns (uint256, bytes32, bytes32, bytes32, bytes32[] memory, bytes32[] memory)
{
return _decode(_l2Block);
return Decoder.decode(_l2Block);
}

function computeDiffRootAndMessagesHash(bytes calldata _l2Block)
external
pure
returns (bytes32, bytes32)
{
(bytes32 diffRoot, bytes32 l1ToL2MessagesHash,,) = _computeConsumables(_l2Block);
(bytes32 diffRoot, bytes32 l1ToL2MessagesHash,,) = Decoder.computeConsumables(_l2Block);
return (diffRoot, l1ToL2MessagesHash);
}

Expand All @@ -33,7 +33,8 @@ contract DecoderHelper is Decoder {
offsetInCalldata := _kernelLogs.offset
}

(bytes32 logsHash, uint256 offset) = _computeKernelLogsHash(offsetInCalldata, _kernelLogs);
(bytes32 logsHash, uint256 offset) =
Decoder.computeKernelLogsHash(offsetInCalldata, _kernelLogs);
uint256 bytesAdvanced = offset - offsetInCalldata;

return (logsHash, bytesAdvanced);
Expand Down
1 change: 0 additions & 1 deletion l1-contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {Registry} from "@aztec/core/messagebridge/Registry.sol";
import {Inbox} from "@aztec/core/messagebridge/Inbox.sol";
import {Outbox} from "@aztec/core/messagebridge/Outbox.sol";

import {Decoder} from "@aztec/core/Decoder.sol";
import {Rollup} from "@aztec/core/Rollup.sol";

/**
Expand Down

0 comments on commit e808315

Please sign in to comment.