Created
September 2, 2024 15:13
-
-
Save drgorillamd/050b43e3d3edf0abc41890b607d60438 to your computer and use it in GitHub Desktop.
mapping key creation vs solc
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
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.26; | |
import {Test, console} from "forge-std/Test.sol"; | |
// Ran 6 tests for test/Counter.t.sol:CounterTest | |
// [PASS] test_test1E2E(uint256) (runs: 258, μ: 31839, ~: 31839) | |
// [PASS] test_test1Set() (gas: 27750) | |
// [PASS] test_test1Test() (gas: 28762) | |
// [PASS] test_test2E2E(uint256) (runs: 258, μ: 31631, ~: 31631) | |
// [PASS] test_test2Set() (gas: 27672) | |
// [PASS] test_test2Test() (gas: 28599) | |
contract CounterTest is Test { | |
Test1 test1; | |
Test2 test2; | |
function setUp() public { | |
test1 = new Test1(); | |
test2 = new Test2(); | |
} | |
function test_test1Set() public { | |
test1.set(69); | |
} | |
function test_test1Test() public { | |
test1.set(69); | |
uint result = test1.test(69); | |
} | |
function test_test2Set() public { | |
test2.set(69); | |
} | |
function test_test2Test() public { | |
test2.set(69); | |
uint result = test2.test(69); | |
} | |
function test_test1E2E(uint _in) public { | |
test1.set(_in); | |
uint result = test1.test(_in); | |
assertEq(result, 90); | |
} | |
function test_test2E2E(uint _in) public { | |
test2.set(_in); | |
uint result = test2.test(_in); | |
assertEq(result, 90); | |
} | |
} | |
contract Test1 { | |
mapping(bytes32 => uint) foo; | |
function set(uint _in) external { | |
bytes32 id = keccak256(abi.encode(msg.sender, _in)); | |
foo[id] = 90; | |
} | |
function test(uint _in) public returns(uint){ | |
return foo[keccak256(abi.encode(msg.sender, _in))]; | |
} | |
} | |
contract Test2 { | |
mapping(address => mapping(uint => uint)) foo; | |
function set(uint _in) external { | |
foo[msg.sender][_in] = 90; | |
} | |
function test(uint _in) public returns(uint){ | |
return foo[msg.sender][_in]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment