github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/libraries/Hashing.t.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity 0.8.15; 3 4 // Testing utilities 5 import { CommonTest } from "test/setup/CommonTest.sol"; 6 7 // Libraries 8 import { Types } from "src/libraries/Types.sol"; 9 import { Encoding } from "src/libraries/Encoding.sol"; 10 import { LegacyCrossDomainUtils } from "src/libraries/LegacyCrossDomainUtils.sol"; 11 12 // Target contract 13 import { Hashing } from "src/libraries/Hashing.sol"; 14 15 contract Hashing_hashDepositSource_Test is CommonTest { 16 /// @notice Tests that hashDepositSource returns the correct hash in a simple case. 17 function test_hashDepositSource_succeeds() external { 18 assertEq( 19 Hashing.hashDepositSource(0xd25df7858efc1778118fb133ac561b138845361626dfb976699c5287ed0f4959, 0x1), 20 0xf923fb07134d7d287cb52c770cc619e17e82606c21a875c92f4c63b65280a5cc 21 ); 22 } 23 } 24 25 contract Hashing_hashCrossDomainMessage_Test is CommonTest { 26 /// @notice Tests that hashCrossDomainMessage returns the correct hash in a simple case. 27 function testDiff_hashCrossDomainMessage_succeeds( 28 uint240 _nonce, 29 uint16 _version, 30 address _sender, 31 address _target, 32 uint256 _value, 33 uint256 _gasLimit, 34 bytes memory _data 35 ) 36 external 37 { 38 // Ensure the version is valid. 39 uint16 version = uint16(bound(uint256(_version), 0, 1)); 40 uint256 nonce = Encoding.encodeVersionedNonce(_nonce, version); 41 42 assertEq( 43 Hashing.hashCrossDomainMessage(nonce, _sender, _target, _value, _gasLimit, _data), 44 ffi.hashCrossDomainMessage(nonce, _sender, _target, _value, _gasLimit, _data) 45 ); 46 } 47 48 /// @notice Tests that hashCrossDomainMessageV0 matches the hash of the legacy encoding. 49 function testFuzz_hashCrossDomainMessageV0_matchesLegacy_succeeds( 50 address _target, 51 address _sender, 52 bytes memory _message, 53 uint256 _messageNonce 54 ) 55 external 56 { 57 assertEq( 58 keccak256(LegacyCrossDomainUtils.encodeXDomainCalldata(_target, _sender, _message, _messageNonce)), 59 Hashing.hashCrossDomainMessageV0(_target, _sender, _message, _messageNonce) 60 ); 61 } 62 } 63 64 contract Hashing_hashWithdrawal_Test is CommonTest { 65 /// @notice Tests that hashWithdrawal returns the correct hash in a simple case. 66 function testDiff_hashWithdrawal_succeeds( 67 uint256 _nonce, 68 address _sender, 69 address _target, 70 uint256 _value, 71 uint256 _gasLimit, 72 bytes memory _data 73 ) 74 external 75 { 76 assertEq( 77 Hashing.hashWithdrawal(Types.WithdrawalTransaction(_nonce, _sender, _target, _value, _gasLimit, _data)), 78 ffi.hashWithdrawal(_nonce, _sender, _target, _value, _gasLimit, _data) 79 ); 80 } 81 } 82 83 contract Hashing_hashOutputRootProof_Test is CommonTest { 84 /// @notice Tests that hashOutputRootProof returns the correct hash in a simple case. 85 function testDiff_hashOutputRootProof_succeeds( 86 bytes32 _stateRoot, 87 bytes32 _messagePasserStorageRoot, 88 bytes32 _latestBlockhash 89 ) 90 external 91 { 92 bytes32 version = 0; 93 assertEq( 94 Hashing.hashOutputRootProof( 95 Types.OutputRootProof({ 96 version: version, 97 stateRoot: _stateRoot, 98 messagePasserStorageRoot: _messagePasserStorageRoot, 99 latestBlockhash: _latestBlockhash 100 }) 101 ), 102 ffi.hashOutputRootProof(version, _stateRoot, _messagePasserStorageRoot, _latestBlockhash) 103 ); 104 } 105 } 106 107 contract Hashing_hashDepositTransaction_Test is CommonTest { 108 /// @notice Tests that hashDepositTransaction returns the correct hash in a simple case. 109 function testDiff_hashDepositTransaction_succeeds( 110 address _from, 111 address _to, 112 uint256 _mint, 113 uint256 _value, 114 uint64 _gas, 115 bytes memory _data, 116 uint64 _logIndex 117 ) 118 external 119 { 120 assertEq( 121 Hashing.hashDepositTransaction( 122 Types.UserDepositTransaction( 123 _from, 124 _to, 125 false, // isCreate 126 _value, 127 _mint, 128 _gas, 129 _data, 130 bytes32(uint256(0)), 131 _logIndex 132 ) 133 ), 134 ffi.hashDepositTransaction(_from, _to, _mint, _value, _gas, _data, _logIndex) 135 ); 136 } 137 }