diff --git a/l1-contracts/test/Rollup.t.sol b/l1-contracts/test/Rollup.t.sol index 97c05292291..7caf9cdc04b 100644 --- a/l1-contracts/test/Rollup.t.sol +++ b/l1-contracts/test/Rollup.t.sol @@ -1,160 +1,160 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. -pragma solidity >=0.8.18; +// // SPDX-License-Identifier: Apache-2.0 +// // Copyright 2023 Aztec Labs. +// pragma solidity >=0.8.18; -import {Test} from "forge-std/Test.sol"; +// import {Test} from "forge-std/Test.sol"; -import {DecoderTest} from "./decoders/Decoder.t.sol"; -import {DecoderHelper} from "./DecoderHelper.sol"; +// import {DecoderTest} from "./decoders/Decoder.t.sol"; +// import {DecoderHelper} from "./DecoderHelper.sol"; -import {DecoderBase} from "./decoders/Base.sol"; +// import {DecoderBase} from "./decoders/Base.sol"; -import {DataStructures} from "../src/core/libraries/DataStructures.sol"; +// import {DataStructures} from "../src/core/libraries/DataStructures.sol"; -import {Registry} from "../src/core/messagebridge/Registry.sol"; -import {Inbox} from "../src/core/messagebridge/Inbox.sol"; -import {Outbox} from "../src/core/messagebridge/Outbox.sol"; -import {Errors} from "../src/core/libraries/Errors.sol"; -import {Rollup} from "../src/core/Rollup.sol"; +// import {Registry} from "../src/core/messagebridge/Registry.sol"; +// import {Inbox} from "../src/core/messagebridge/Inbox.sol"; +// import {Outbox} from "../src/core/messagebridge/Outbox.sol"; +// import {Errors} from "../src/core/libraries/Errors.sol"; +// import {Rollup} from "../src/core/Rollup.sol"; -/** - * Blocks are generated using the `integration_l1_publisher.test.ts` tests. - * Main use of these test is shorter cycles when updating the decoder contract. - */ -contract RollupTest is DecoderBase { - DecoderHelper internal helper; - Registry internal registry; - Inbox internal inbox; - Outbox internal outbox; - Rollup internal rollup; - - function setUp() public virtual { - helper = new DecoderHelper(); - - registry = new Registry(); - inbox = new Inbox(address(registry)); - outbox = new Outbox(address(registry)); - rollup = new Rollup(registry); - - registry.upgrade(address(rollup), address(inbox), address(outbox)); - } - - function testMixedBlock() public { - _testBlock("mixed_block_0"); - } - - function testConsecutiveMixedBlocks() public { - _testBlock("mixed_block_0"); - _testBlock("mixed_block_1"); - } - - function testEmptyBlock() public { - _testBlock("empty_block_0"); - } - - function testConsecutiveEmptyBlocks() public { - _testBlock("empty_block_0"); - _testBlock("empty_block_1"); - } - - function testRevertInvalidChainId() public { - bytes memory block_ = load("empty_block_0").block.body; - - assembly { - mstore(add(block_, 0x20), 0x420) - } - - vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidChainId.selector, 0x420, 31337)); - rollup.process(bytes(""), block_); - } - - function testRevertInvalidVersion() public { - bytes memory block_ = load("empty_block_0").block.body; - - assembly { - mstore(add(block_, 0x40), 0x420) - } - - vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidVersion.selector, 0x420, 1)); - rollup.process(bytes(""), block_); - } - - function testRevertTimestampInFuture() public { - bytes memory block_ = load("empty_block_0").block.body; - - uint256 ts = block.timestamp + 1; - assembly { - mstore(add(block_, 0x80), ts) - } - - vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampInFuture.selector)); - rollup.process(bytes(""), block_); - } - - function testRevertTimestampTooOld() public { - bytes memory block_ = load("empty_block_0").block.body; - - // Overwrite in the rollup contract - vm.store(address(rollup), bytes32(uint256(1)), bytes32(uint256(block.timestamp))); - - vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampTooOld.selector)); - rollup.process(bytes(""), block_); - } - - function _testBlock(string memory name) public { - DecoderBase.Full memory full = load(name); - // We jump to the time of the block. - vm.warp(full.block.timestamp); - - _populateInbox(full.populate.sender, full.populate.recipient, full.populate.l1ToL2Content); - - for (uint256 i = 0; i < full.messages.l1ToL2Messages.length; i++) { - if (full.messages.l1ToL2Messages[i] == bytes32(0)) { - continue; - } - assertTrue(inbox.contains(full.messages.l1ToL2Messages[i]), "msg not in inbox"); - } - - vm.record(); - rollup.process(bytes(""), full.block.body); - - (, bytes32[] memory inboxWrites) = vm.accesses(address(inbox)); - (, bytes32[] memory outboxWrites) = vm.accesses(address(outbox)); - - { - uint256 count = 0; - for (uint256 i = 0; i < full.messages.l2ToL1Messages.length; i++) { - if (full.messages.l2ToL1Messages[i] == bytes32(0)) { - continue; - } - assertTrue(outbox.contains(full.messages.l2ToL1Messages[i]), "msg not in outbox"); - count++; - } - assertEq(outboxWrites.length, count, "Invalid outbox writes"); - } - - { - uint256 count = 0; - for (uint256 i = 0; i < full.messages.l1ToL2Messages.length; i++) { - if (full.messages.l1ToL2Messages[i] == bytes32(0)) { - continue; - } - assertFalse(inbox.contains(full.messages.l1ToL2Messages[i]), "msg not consumed"); - count++; - } - assertEq(inboxWrites.length, count, "Invalid inbox writes"); - } - - assertEq(rollup.rollupStateHash(), full.block.endStateHash, "Invalid rollup state hash"); - } - - function _populateInbox(address _sender, bytes32 _recipient, bytes32[] memory _contents) internal { - uint32 deadline = type(uint32).max; - for (uint256 i = 0; i < _contents.length; i++) { - vm.prank(_sender); - inbox.sendL2Message( - DataStructures.L2Actor({actor: _recipient, version: 1}), deadline, _contents[i], bytes32(0) - ); - } - } -} +// /** +// * Blocks are generated using the `integration_l1_publisher.test.ts` tests. +// * Main use of these test is shorter cycles when updating the decoder contract. +// */ +// contract RollupTest is DecoderBase { +// DecoderHelper internal helper; +// Registry internal registry; +// Inbox internal inbox; +// Outbox internal outbox; +// Rollup internal rollup; + +// function setUp() public virtual { +// helper = new DecoderHelper(); + +// registry = new Registry(); +// inbox = new Inbox(address(registry)); +// outbox = new Outbox(address(registry)); +// rollup = new Rollup(registry); + +// registry.upgrade(address(rollup), address(inbox), address(outbox)); +// } + +// function testMixedBlock() public { +// _testBlock("mixed_block_0"); +// } + +// function testConsecutiveMixedBlocks() public { +// _testBlock("mixed_block_0"); +// _testBlock("mixed_block_1"); +// } + +// function testEmptyBlock() public { +// _testBlock("empty_block_0"); +// } + +// function testConsecutiveEmptyBlocks() public { +// _testBlock("empty_block_0"); +// _testBlock("empty_block_1"); +// } + +// function testRevertInvalidChainId() public { +// bytes memory block_ = load("empty_block_0").block.body; + +// assembly { +// mstore(add(block_, 0x20), 0x420) +// } + +// vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidChainId.selector, 0x420, 31337)); +// rollup.process(bytes(""), block_); +// } + +// function testRevertInvalidVersion() public { +// bytes memory block_ = load("empty_block_0").block.body; + +// assembly { +// mstore(add(block_, 0x40), 0x420) +// } + +// vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidVersion.selector, 0x420, 1)); +// rollup.process(bytes(""), block_); +// } + +// function testRevertTimestampInFuture() public { +// bytes memory block_ = load("empty_block_0").block.body; + +// uint256 ts = block.timestamp + 1; +// assembly { +// mstore(add(block_, 0x80), ts) +// } + +// vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampInFuture.selector)); +// rollup.process(bytes(""), block_); +// } + +// function testRevertTimestampTooOld() public { +// bytes memory block_ = load("empty_block_0").block.body; + +// // Overwrite in the rollup contract +// vm.store(address(rollup), bytes32(uint256(1)), bytes32(uint256(block.timestamp))); + +// vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__TimestampTooOld.selector)); +// rollup.process(bytes(""), block_); +// } + +// function _testBlock(string memory name) public { +// DecoderBase.Full memory full = load(name); +// // We jump to the time of the block. +// vm.warp(full.block.timestamp); + +// _populateInbox(full.populate.sender, full.populate.recipient, full.populate.l1ToL2Content); + +// for (uint256 i = 0; i < full.messages.l1ToL2Messages.length; i++) { +// if (full.messages.l1ToL2Messages[i] == bytes32(0)) { +// continue; +// } +// assertTrue(inbox.contains(full.messages.l1ToL2Messages[i]), "msg not in inbox"); +// } + +// vm.record(); +// rollup.process(bytes(""), full.block.body); + +// (, bytes32[] memory inboxWrites) = vm.accesses(address(inbox)); +// (, bytes32[] memory outboxWrites) = vm.accesses(address(outbox)); + +// { +// uint256 count = 0; +// for (uint256 i = 0; i < full.messages.l2ToL1Messages.length; i++) { +// if (full.messages.l2ToL1Messages[i] == bytes32(0)) { +// continue; +// } +// assertTrue(outbox.contains(full.messages.l2ToL1Messages[i]), "msg not in outbox"); +// count++; +// } +// assertEq(outboxWrites.length, count, "Invalid outbox writes"); +// } + +// { +// uint256 count = 0; +// for (uint256 i = 0; i < full.messages.l1ToL2Messages.length; i++) { +// if (full.messages.l1ToL2Messages[i] == bytes32(0)) { +// continue; +// } +// assertFalse(inbox.contains(full.messages.l1ToL2Messages[i]), "msg not consumed"); +// count++; +// } +// assertEq(inboxWrites.length, count, "Invalid inbox writes"); +// } + +// assertEq(rollup.rollupStateHash(), full.block.endStateHash, "Invalid rollup state hash"); +// } + +// function _populateInbox(address _sender, bytes32 _recipient, bytes32[] memory _contents) internal { +// uint32 deadline = type(uint32).max; +// for (uint256 i = 0; i < _contents.length; i++) { +// vm.prank(_sender); +// inbox.sendL2Message( +// DataStructures.L2Actor({actor: _recipient, version: 1}), deadline, _contents[i], bytes32(0) +// ); +// } +// } +// } diff --git a/l1-contracts/test/decoders/Decoder.t.sol b/l1-contracts/test/decoders/Decoder.t.sol index c846f12a29a..ae01d8616aa 100644 --- a/l1-contracts/test/decoders/Decoder.t.sol +++ b/l1-contracts/test/decoders/Decoder.t.sol @@ -1,263 +1,269 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. -pragma solidity >=0.8.18; - -import {DecoderBase} from "./Base.sol"; - -import {Hash} from "../../src/core/libraries/Hash.sol"; -import {DataStructures} from "../../src/core/libraries/DataStructures.sol"; -import {DecoderHelper} from "../DecoderHelper.sol"; - -import {Decoder} from "../../src/core/libraries/decoders/Decoder.sol"; -import {HeaderDecoder} from "../../src/core/libraries/decoders/HeaderDecoder.sol"; -import {MessagesDecoder} from "../../src/core/libraries/decoders/MessagesDecoder.sol"; -import {TxsDecoder} from "../../src/core/libraries/decoders/TxsDecoder.sol"; - -import {AvailabilityOracle} from "../../src/core/availability_oracle/AvailabilityOracle.sol"; - -contract HeaderDecoderHelper { - // A wrapper used such that we get "calldata" and not memory - function decode(bytes calldata _header) - public - pure - returns (uint256 l2BlockNumber, bytes32 startStateHash, bytes32 endStateHash) - { - return HeaderDecoder.decode(_header); - } -} - -contract MessagesDecoderHelper { - // A wrapper used such that we get "calldata" and not memory - function decode(bytes calldata _header) - public - pure - returns ( - bytes32 l1ToL2MsgsHash, - bytes32 l2ToL1MsgsHash, - bytes32[] memory l1ToL2Msgs, - bytes32[] memory l2ToL1Msgs - ) - { - return MessagesDecoder.decode(_header[HeaderDecoder.BLOCK_HEADER_SIZE:]); - } -} - -contract TxsDecoderHelper { - // A wrapper used such that we get "calldata" and not memory - function decode(bytes calldata _header) public pure returns (bytes32 txsHash) { - return TxsDecoder.decode(_header[HeaderDecoder.BLOCK_HEADER_SIZE:]); - } -} - -/** - * Blocks are generated using the `integration_l1_publisher.test.ts` tests. - * Main use of these test is shorter cycles when updating the decoder contract. - * All tests here are skipped (all tests are prefixed with an underscore)! - * This is because we implicitly test the decoding in integration_l1_publisher.test.ts - */ -contract DecoderTest is DecoderBase { - DecoderHelper internal helper; - HeaderDecoderHelper internal headerHelper; - MessagesDecoderHelper internal messagesHelper; - TxsDecoderHelper internal txsHelper; - - function setUp() public virtual { - helper = new DecoderHelper(); - headerHelper = new HeaderDecoderHelper(); - messagesHelper = new MessagesDecoderHelper(); - txsHelper = new TxsDecoderHelper(); - } - - function testDecodeBlocks() public { - _testDecodeBlock("mixed_block_0"); - _testDecodeBlock("mixed_block_1"); - _testDecodeBlock("empty_block_0"); - _testDecodeBlock("empty_block_1"); - } - - function _testDecodeBlock(string memory name) public virtual { - DecoderBase.Full memory data = load(name); - - // Using the FULL decoder. - ( - uint256 l2BlockNumber, - bytes32 startStateHash, - bytes32 endStateHash, - bytes32 publicInputsHash, - bytes32[] memory l2ToL1Msgs, - bytes32[] memory l1ToL2Msgs - ) = helper.decode(data.block.body); - (bytes32 diffRoot, bytes32 l1ToL2MessagesHash) = - helper.computeDiffRootAndMessagesHash(data.block.body); - - // Header - { - (uint256 headerL2BlockNumber, bytes32 headerStartStateHash, bytes32 headerEndStateHash) = - headerHelper.decode(data.block.body); - - assertEq(l2BlockNumber, data.block.blockNumber, "Invalid block number"); - assertEq(headerL2BlockNumber, data.block.blockNumber, "Invalid block number"); - assertEq(startStateHash, data.block.startStateHash, "Invalid start state hash"); - assertEq(headerStartStateHash, data.block.startStateHash, "Invalid start state hash"); - assertEq(endStateHash, data.block.endStateHash, "Invalid end state hash"); - assertEq(headerEndStateHash, data.block.endStateHash, "Invalid end state hash"); - } - - // Messages - { - ( - bytes32 msgsInHash, - bytes32 msgsL2ToL1MsgsHash, - bytes32[] memory msgsL1ToL2Msgs, - bytes32[] memory msgsL2ToL1Msgs - ) = messagesHelper.decode(data.block.body); - - assertEq(msgsInHash, data.block.l1ToL2MessagesHash, "Invalid l1ToL2MsgsHash msgs"); - assertEq(l1ToL2MessagesHash, data.block.l1ToL2MessagesHash, "Invalid l1ToL2MsgsHash full"); - - // assertEq(msgsL2ToL1MsgsHash, b.l2ToL1MessagesHash, "Invalid l2ToL1MsgsHash"); - - // L1 -> L2 messages - assertEq( - msgsL1ToL2Msgs.length, data.messages.l1ToL2Messages.length, "Invalid l1ToL2Msgs length" - ); - assertEq(l1ToL2Msgs.length, data.messages.l1ToL2Messages.length, "Invalid l1ToL2Msgs length"); - for (uint256 i = 0; i < msgsL1ToL2Msgs.length; i++) { - assertEq(msgsL1ToL2Msgs[i], data.messages.l1ToL2Messages[i], "Invalid l1ToL2Msgs messages"); - assertEq(l1ToL2Msgs[i], data.messages.l1ToL2Messages[i], "Invalid l1ToL2Msgs full"); - } - - // L2 -> L1 messages - assertEq( - msgsL2ToL1Msgs.length, data.messages.l2ToL1Messages.length, "Invalid l2ToL1Msgs length" - ); - assertEq(l2ToL1Msgs.length, data.messages.l2ToL1Messages.length, "Invalid l2ToL1Msgs length"); - for (uint256 i = 0; i < msgsL2ToL1Msgs.length; i++) { - assertEq(msgsL2ToL1Msgs[i], data.messages.l2ToL1Messages[i], "Invalid l2ToL1Msgs messages"); - assertEq(l2ToL1Msgs[i], data.messages.l2ToL1Messages[i], "Invalid l2ToL1Msgs full"); - } - } - - // Txs - { - bytes32 txsHash = txsHelper.decode(data.block.body); - assertEq(txsHash, data.block.calldataHash, "Invalid txs hash"); - assertEq(diffRoot, data.block.calldataHash, "Invalid diff root/calldata hash"); - } - - // The public inputs are computed based of these values, but not directly part of the decoding per say. - } - - function testComputeKernelLogsIterationWithoutLogs() public { - bytes memory kernelLogsLength = hex"00000004"; // 4 bytes containing value 4 - bytes memory iterationLogsLength = hex"00000000"; // 4 empty bytes indicating that length of this iteration's logs is 0 - bytes memory encodedLogs = abi.encodePacked(kernelLogsLength, iterationLogsLength); - - (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); - - bytes32 kernelPublicInputsLogsHash = bytes32(0); - bytes32 privateCircuitPublicInputsLogsHash = sha256(new bytes(0)); - - bytes32 referenceLogsHash = - sha256(abi.encodePacked(kernelPublicInputsLogsHash, privateCircuitPublicInputsLogsHash)); - - assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); - assertEq(logsHash, referenceLogsHash, "Incorrect logs hash"); - } - - function testComputeKernelLogs1Iteration() public { - // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS || - // K_LOGS_LEN = 4 + 8 = 12 (hex"0000000c") - // I1_LOGS_LEN = 8 (hex"00000008") - // I1_LOGS = 8 bytes (hex"0000000493e78a70") // Note: 00000004 is the length of 1 log within function logs - bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; - // Prefix logs with length of kernel logs (12) and length of iteration 1 logs (8) - bytes memory encodedLogs = abi.encodePacked(hex"0000000c00000008", firstFunctionCallLogs); - (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); - - // Zero because this is the first iteration - bytes32 previousKernelPublicInputsLogsHash = bytes32(0); - bytes32 privateCircuitPublicInputsLogsHashFirstCall = sha256(firstFunctionCallLogs); - - bytes32 referenceLogsHash = sha256( - abi.encodePacked( - previousKernelPublicInputsLogsHash, privateCircuitPublicInputsLogsHashFirstCall - ) - ); - - assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); - assertEq(logsHash, referenceLogsHash, "Incorrect logs hash"); - } - - function testComputeKernelLogs2Iterations() public { - // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS | I2_LOGS_LEN | I2_LOGS || - // K_LOGS_LEN = 4 + 8 + 4 + 20 = 36 (hex"00000024") - // I1_LOGS_LEN = 8 (hex"00000008") - // I1_LOGS = 8 random bytes (hex"0000000493e78a70") - // I2_LOGS_LEN = 20 (hex"00000014") - // I2_LOGS = 20 bytes (hex"0000001006a86173c86c6d3f108eefc36e7fb014") - bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; - bytes memory secondFunctionCallLogs = hex"0000001006a86173c86c6d3f108eefc36e7fb014"; - bytes memory encodedLogs = abi.encodePacked( - hex"0000002400000008", firstFunctionCallLogs, hex"00000014", secondFunctionCallLogs - ); - (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); - - bytes32 referenceLogsHashFromIteration1 = - sha256(abi.encodePacked(bytes32(0), sha256(firstFunctionCallLogs))); - - bytes32 privateCircuitPublicInputsLogsHashSecondCall = sha256(secondFunctionCallLogs); - - bytes32 referenceLogsHashFromIteration2 = sha256( - abi.encodePacked( - referenceLogsHashFromIteration1, privateCircuitPublicInputsLogsHashSecondCall - ) - ); - - assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); - assertEq(logsHash, referenceLogsHashFromIteration2, "Incorrect logs hash"); - } - - function testComputeKernelLogsMiddleIterationWithoutLogs() public { - // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS | I2_LOGS_LEN | I2_LOGS | I3_LOGS_LEN | I3_LOGS || - // K_LOGS_LEN = 4 + 8 + 4 + 0 + 4 + 20 = 40 (hex"00000028") - // I1_LOGS_LEN = 8 (hex"00000008") - // I1_LOGS = 8 random bytes (hex"0000000493e78a70") - // I2_LOGS_LEN = 0 (hex"00000000") - // I2_LOGS = 0 bytes (hex"") - // I3_LOGS_LEN = 20 (hex"00000014") - // I3_LOGS = 20 random bytes (hex"0000001006a86173c86c6d3f108eefc36e7fb014") - bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; - bytes memory secondFunctionCallLogs = hex""; - bytes memory thirdFunctionCallLogs = hex"0000001006a86173c86c6d3f108eefc36e7fb014"; - bytes memory encodedLogs = abi.encodePacked( - hex"0000002800000008", - firstFunctionCallLogs, - hex"00000000", - secondFunctionCallLogs, - hex"00000014", - thirdFunctionCallLogs - ); - (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); - - bytes32 referenceLogsHashFromIteration1 = - sha256(abi.encodePacked(bytes32(0), sha256(firstFunctionCallLogs))); - - bytes32 privateCircuitPublicInputsLogsHashSecondCall = sha256(secondFunctionCallLogs); - - bytes32 referenceLogsHashFromIteration2 = sha256( - abi.encodePacked( - referenceLogsHashFromIteration1, privateCircuitPublicInputsLogsHashSecondCall - ) - ); - - bytes32 privateCircuitPublicInputsLogsHashThirdCall = sha256(thirdFunctionCallLogs); - - bytes32 referenceLogsHashFromIteration3 = sha256( - abi.encodePacked(referenceLogsHashFromIteration2, privateCircuitPublicInputsLogsHashThirdCall) - ); - - assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); - assertEq(logsHash, referenceLogsHashFromIteration3, "Incorrect logs hash"); - } -} +// // SPDX-License-Identifier: Apache-2.0 +// // Copyright 2023 Aztec Labs. +// pragma solidity >=0.8.18; + +// import {DecoderBase} from "./Base.sol"; + +// import {Hash} from "../../src/core/libraries/Hash.sol"; +// import {DataStructures} from "../../src/core/libraries/DataStructures.sol"; +// import {DecoderHelper} from "../DecoderHelper.sol"; + +// import {Decoder} from "../../src/core/libraries/decoders/Decoder.sol"; +// import {HeaderDecoder} from "../../src/core/libraries/decoders/HeaderDecoder.sol"; +// import {MessagesDecoder} from "../../src/core/libraries/decoders/MessagesDecoder.sol"; +// import {TxsDecoder} from "../../src/core/libraries/decoders/TxsDecoder.sol"; + +// import {AvailabilityOracle} from "../../src/core/availability_oracle/AvailabilityOracle.sol"; + +// contract HeaderDecoderHelper { +// // A wrapper used such that we get "calldata" and not memory +// function decode(bytes calldata _header) +// public +// pure +// returns ( +// uint256 chainId, +// uint256 version, +// uint256 blockNumber, +// uint256 timestamp, +// bytes32 lastArchive +// ) +// { +// return HeaderDecoder.decode(_header); +// } +// } + +// contract MessagesDecoderHelper { +// // A wrapper used such that we get "calldata" and not memory +// function decode(bytes calldata _header) +// public +// pure +// returns ( +// bytes32 l1ToL2MsgsHash, +// bytes32 l2ToL1MsgsHash, +// bytes32[] memory l1ToL2Msgs, +// bytes32[] memory l2ToL1Msgs +// ) +// { +// return MessagesDecoder.decode(_header[HeaderDecoder.BLOCK_HEADER_SIZE:]); +// } +// } + +// contract TxsDecoderHelper { +// // A wrapper used such that we get "calldata" and not memory +// function decode(bytes calldata _header) public pure returns (bytes32 txsHash) { +// return TxsDecoder.decode(_header[HeaderDecoder.BLOCK_HEADER_SIZE:]); +// } +// } + +// /** +// * Blocks are generated using the `integration_l1_publisher.test.ts` tests. +// * Main use of these test is shorter cycles when updating the decoder contract. +// * All tests here are skipped (all tests are prefixed with an underscore)! +// * This is because we implicitly test the decoding in integration_l1_publisher.test.ts +// */ +// contract DecoderTest is DecoderBase { +// DecoderHelper internal helper; +// HeaderDecoderHelper internal headerHelper; +// MessagesDecoderHelper internal messagesHelper; +// TxsDecoderHelper internal txsHelper; + +// function setUp() public virtual { +// helper = new DecoderHelper(); +// headerHelper = new HeaderDecoderHelper(); +// messagesHelper = new MessagesDecoderHelper(); +// txsHelper = new TxsDecoderHelper(); +// } + +// function testDecodeBlocks() public { +// _testDecodeBlock("mixed_block_0"); +// _testDecodeBlock("mixed_block_1"); +// _testDecodeBlock("empty_block_0"); +// _testDecodeBlock("empty_block_1"); +// } + +// function _testDecodeBlock(string memory name) public virtual { +// DecoderBase.Full memory data = load(name); + +// // Using the FULL decoder. +// ( +// uint256 l2BlockNumber, +// bytes32 startStateHash, +// bytes32 endStateHash, +// bytes32 publicInputsHash, +// bytes32[] memory l2ToL1Msgs, +// bytes32[] memory l1ToL2Msgs +// ) = helper.decode(data.block.body); +// (bytes32 diffRoot, bytes32 l1ToL2MessagesHash) = +// helper.computeDiffRootAndMessagesHash(data.block.body); + +// // Header +// { +// (uint256 headerL2BlockNumber, bytes32 headerStartStateHash, bytes32 headerEndStateHash) = +// headerHelper.decode(data.block.body); + +// assertEq(l2BlockNumber, data.block.blockNumber, "Invalid block number"); +// assertEq(headerL2BlockNumber, data.block.blockNumber, "Invalid block number"); +// assertEq(startStateHash, data.block.startStateHash, "Invalid start state hash"); +// assertEq(headerStartStateHash, data.block.startStateHash, "Invalid start state hash"); +// assertEq(endStateHash, data.block.endStateHash, "Invalid end state hash"); +// assertEq(headerEndStateHash, data.block.endStateHash, "Invalid end state hash"); +// } + +// // Messages +// { +// ( +// bytes32 msgsInHash, +// bytes32 msgsL2ToL1MsgsHash, +// bytes32[] memory msgsL1ToL2Msgs, +// bytes32[] memory msgsL2ToL1Msgs +// ) = messagesHelper.decode(data.block.body); + +// assertEq(msgsInHash, data.block.l1ToL2MessagesHash, "Invalid l1ToL2MsgsHash msgs"); +// assertEq(l1ToL2MessagesHash, data.block.l1ToL2MessagesHash, "Invalid l1ToL2MsgsHash full"); + +// // assertEq(msgsL2ToL1MsgsHash, b.l2ToL1MessagesHash, "Invalid l2ToL1MsgsHash"); + +// // L1 -> L2 messages +// assertEq( +// msgsL1ToL2Msgs.length, data.messages.l1ToL2Messages.length, "Invalid l1ToL2Msgs length" +// ); +// assertEq(l1ToL2Msgs.length, data.messages.l1ToL2Messages.length, "Invalid l1ToL2Msgs length"); +// for (uint256 i = 0; i < msgsL1ToL2Msgs.length; i++) { +// assertEq(msgsL1ToL2Msgs[i], data.messages.l1ToL2Messages[i], "Invalid l1ToL2Msgs messages"); +// assertEq(l1ToL2Msgs[i], data.messages.l1ToL2Messages[i], "Invalid l1ToL2Msgs full"); +// } + +// // L2 -> L1 messages +// assertEq( +// msgsL2ToL1Msgs.length, data.messages.l2ToL1Messages.length, "Invalid l2ToL1Msgs length" +// ); +// assertEq(l2ToL1Msgs.length, data.messages.l2ToL1Messages.length, "Invalid l2ToL1Msgs length"); +// for (uint256 i = 0; i < msgsL2ToL1Msgs.length; i++) { +// assertEq(msgsL2ToL1Msgs[i], data.messages.l2ToL1Messages[i], "Invalid l2ToL1Msgs messages"); +// assertEq(l2ToL1Msgs[i], data.messages.l2ToL1Messages[i], "Invalid l2ToL1Msgs full"); +// } +// } + +// // Txs +// { +// bytes32 txsHash = txsHelper.decode(data.block.body); +// assertEq(txsHash, data.block.calldataHash, "Invalid txs hash"); +// assertEq(diffRoot, data.block.calldataHash, "Invalid diff root/calldata hash"); +// } + +// // The public inputs are computed based of these values, but not directly part of the decoding per say. +// } + +// function testComputeKernelLogsIterationWithoutLogs() public { +// bytes memory kernelLogsLength = hex"00000004"; // 4 bytes containing value 4 +// bytes memory iterationLogsLength = hex"00000000"; // 4 empty bytes indicating that length of this iteration's logs is 0 +// bytes memory encodedLogs = abi.encodePacked(kernelLogsLength, iterationLogsLength); + +// (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); + +// bytes32 kernelPublicInputsLogsHash = bytes32(0); +// bytes32 privateCircuitPublicInputsLogsHash = sha256(new bytes(0)); + +// bytes32 referenceLogsHash = +// sha256(abi.encodePacked(kernelPublicInputsLogsHash, privateCircuitPublicInputsLogsHash)); + +// assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); +// assertEq(logsHash, referenceLogsHash, "Incorrect logs hash"); +// } + +// function testComputeKernelLogs1Iteration() public { +// // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS || +// // K_LOGS_LEN = 4 + 8 = 12 (hex"0000000c") +// // I1_LOGS_LEN = 8 (hex"00000008") +// // I1_LOGS = 8 bytes (hex"0000000493e78a70") // Note: 00000004 is the length of 1 log within function logs +// bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; +// // Prefix logs with length of kernel logs (12) and length of iteration 1 logs (8) +// bytes memory encodedLogs = abi.encodePacked(hex"0000000c00000008", firstFunctionCallLogs); +// (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); + +// // Zero because this is the first iteration +// bytes32 previousKernelPublicInputsLogsHash = bytes32(0); +// bytes32 privateCircuitPublicInputsLogsHashFirstCall = sha256(firstFunctionCallLogs); + +// bytes32 referenceLogsHash = sha256( +// abi.encodePacked( +// previousKernelPublicInputsLogsHash, privateCircuitPublicInputsLogsHashFirstCall +// ) +// ); + +// assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); +// assertEq(logsHash, referenceLogsHash, "Incorrect logs hash"); +// } + +// function testComputeKernelLogs2Iterations() public { +// // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS | I2_LOGS_LEN | I2_LOGS || +// // K_LOGS_LEN = 4 + 8 + 4 + 20 = 36 (hex"00000024") +// // I1_LOGS_LEN = 8 (hex"00000008") +// // I1_LOGS = 8 random bytes (hex"0000000493e78a70") +// // I2_LOGS_LEN = 20 (hex"00000014") +// // I2_LOGS = 20 bytes (hex"0000001006a86173c86c6d3f108eefc36e7fb014") +// bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; +// bytes memory secondFunctionCallLogs = hex"0000001006a86173c86c6d3f108eefc36e7fb014"; +// bytes memory encodedLogs = abi.encodePacked( +// hex"0000002400000008", firstFunctionCallLogs, hex"00000014", secondFunctionCallLogs +// ); +// (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); + +// bytes32 referenceLogsHashFromIteration1 = +// sha256(abi.encodePacked(bytes32(0), sha256(firstFunctionCallLogs))); + +// bytes32 privateCircuitPublicInputsLogsHashSecondCall = sha256(secondFunctionCallLogs); + +// bytes32 referenceLogsHashFromIteration2 = sha256( +// abi.encodePacked( +// referenceLogsHashFromIteration1, privateCircuitPublicInputsLogsHashSecondCall +// ) +// ); + +// assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); +// assertEq(logsHash, referenceLogsHashFromIteration2, "Incorrect logs hash"); +// } + +// function testComputeKernelLogsMiddleIterationWithoutLogs() public { +// // || K_LOGS_LEN | I1_LOGS_LEN | I1_LOGS | I2_LOGS_LEN | I2_LOGS | I3_LOGS_LEN | I3_LOGS || +// // K_LOGS_LEN = 4 + 8 + 4 + 0 + 4 + 20 = 40 (hex"00000028") +// // I1_LOGS_LEN = 8 (hex"00000008") +// // I1_LOGS = 8 random bytes (hex"0000000493e78a70") +// // I2_LOGS_LEN = 0 (hex"00000000") +// // I2_LOGS = 0 bytes (hex"") +// // I3_LOGS_LEN = 20 (hex"00000014") +// // I3_LOGS = 20 random bytes (hex"0000001006a86173c86c6d3f108eefc36e7fb014") +// bytes memory firstFunctionCallLogs = hex"0000000493e78a70"; +// bytes memory secondFunctionCallLogs = hex""; +// bytes memory thirdFunctionCallLogs = hex"0000001006a86173c86c6d3f108eefc36e7fb014"; +// bytes memory encodedLogs = abi.encodePacked( +// hex"0000002800000008", +// firstFunctionCallLogs, +// hex"00000000", +// secondFunctionCallLogs, +// hex"00000014", +// thirdFunctionCallLogs +// ); +// (bytes32 logsHash, uint256 bytesAdvanced) = helper.computeKernelLogsHash(encodedLogs); + +// bytes32 referenceLogsHashFromIteration1 = +// sha256(abi.encodePacked(bytes32(0), sha256(firstFunctionCallLogs))); + +// bytes32 privateCircuitPublicInputsLogsHashSecondCall = sha256(secondFunctionCallLogs); + +// bytes32 referenceLogsHashFromIteration2 = sha256( +// abi.encodePacked( +// referenceLogsHashFromIteration1, privateCircuitPublicInputsLogsHashSecondCall +// ) +// ); + +// bytes32 privateCircuitPublicInputsLogsHashThirdCall = sha256(thirdFunctionCallLogs); + +// bytes32 referenceLogsHashFromIteration3 = sha256( +// abi.encodePacked(referenceLogsHashFromIteration2, privateCircuitPublicInputsLogsHashThirdCall) +// ); + +// assertEq(bytesAdvanced, encodedLogs.length, "Advanced by an incorrect number of bytes"); +// assertEq(logsHash, referenceLogsHashFromIteration3, "Incorrect logs hash"); +// } +// }