-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
foundry_test/modules/utils/L2CompressorHuffReadExecute.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import "foundry_test/base/AdvTest.sol"; | ||
|
||
import "forge-std/console.sol"; | ||
import "forge-std/console2.sol"; | ||
|
||
import { HuffConfig } from "foundry-huff/HuffConfig.sol"; | ||
import { HuffDeployer } from "foundry-huff/HuffDeployer.sol"; | ||
|
||
import "contracts/modules/commons/interfaces/IModuleCalls.sol"; | ||
|
||
uint256 constant FMS = 0xa0; | ||
|
||
import "./L2CompressorEncoder.sol"; | ||
|
||
contract L2CompressorHuffReadExecuteTest is AdvTest { | ||
address public imp; | ||
|
||
function setUp() public { | ||
imp = address( | ||
HuffDeployer | ||
.config() | ||
.with_evm_version("paris") | ||
.deploy("imps/L2CompressorReadExecute") | ||
); | ||
} | ||
|
||
function test_read_simple_execute( | ||
IModuleCalls.Transaction[] calldata _txs, | ||
uint160 _space, | ||
uint96 _nonce, | ||
bytes calldata _signature | ||
) external { | ||
vm.assume(_txs.length != 0 && _txs.length <= type(uint8).max); | ||
|
||
bytes32 packedNonce = abi.decode(abi.encodePacked(_space, _nonce), (bytes32)); | ||
|
||
bytes memory encoded = abi.encodePacked( | ||
encodeWord(_space), encodeWord(_nonce), | ||
uint8(_txs.length) | ||
); | ||
|
||
for (uint256 i = 0; i < _txs.length; i++) { | ||
IModuleCalls.Transaction memory t = _txs[i]; | ||
|
||
encoded = abi.encodePacked( | ||
encoded, | ||
build_flag(t.delegateCall, t.revertOnError, t.gasLimit != 0, t.value != 0, t.data.length != 0), | ||
t.gasLimit != 0 ? encodeWord(t.gasLimit) : bytes(""), | ||
encode_raw_address(t.target), | ||
t.value != 0 ? encodeWord(t.value) : bytes(""), | ||
t.data.length != 0 ? encode_bytes_n(t.data) : bytes("") | ||
); | ||
} | ||
|
||
encoded = abi.encodePacked( | ||
encoded, | ||
encode_bytes_n(_signature) | ||
); | ||
|
||
(bool s, bytes memory r) = imp.staticcall(encoded); | ||
|
||
assertTrue(s); | ||
(uint256 rindex, uint256 windex, bytes memory res) = abi.decode(r, (uint256, uint256, bytes)); | ||
|
||
assertEq(rindex, encoded.length); | ||
assertEq(windex, res.length + FMS); | ||
|
||
// Encode using solidity | ||
bytes memory solidityEncoded = abi.encodeWithSelector(IModuleCalls.execute.selector, _txs, packedNonce, _signature); | ||
assertEq(solidityEncoded, res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "../L2Compressor.huff" | ||
|
||
#define constant FMS = 0xa0 | ||
|
||
// Function Dispatching | ||
#define macro MAIN() = takes (1) returns (1) { | ||
// readAdvanced with whatever calldata is passed | ||
// first 32 bytes returns the new rindex and the next 32 bytes returns the new windex | ||
|
||
0x00 // [rindex] | ||
[FMS] // [windex, rindex] | ||
|
||
READ_EXECUTE_STANDALONE() // [windex, rindex] | ||
|
||
[FMS] // [0xa0, windex, rindex] | ||
dup2 // [windex, 0xa0, windex, rindex] | ||
sub // [len, windex, rindex] | ||
|
||
swap2 // [rindex, windex, len] | ||
|
||
0x80 [FMS] sub mstore // [windex, len] | ||
0x60 [FMS] sub mstore // [len] | ||
|
||
0x60 0x40 [FMS] sub mstore // [len] | ||
dup1 0x20 [FMS] sub mstore // [len] | ||
|
||
0x80 add // [len + 0x80] | ||
|
||
0x80 [FMS] sub return | ||
} |