Skip to content

Commit

Permalink
Add EXTCODE contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Aug 23, 2024
1 parent 641f4fc commit 276b723
Show file tree
Hide file tree
Showing 4 changed files with 1,456 additions and 223 deletions.
95 changes: 50 additions & 45 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,95 +1,100 @@
LibStringTest:testAddressToHexStringZeroRightPadded(address) (runs: 256, μ: 3384, ~: 3384)
ExtcodeTest:testExtcodeInitCodes() (gas: 129646)
ExtcodeTest:testExtcodecopy(uint256,uint256) (runs: 257, μ: 28153, ~: 11129)
ExtcodeTest:testExtcodehash() (gas: 5811)
ExtcodeTest:testExtcodesize() (gas: 5799)
ExtcodeTest:test__codesize() (gas: 4946)
LibStringTest:testAddressToHexStringZeroRightPadded(address) (runs: 257, μ: 3384, ~: 3384)
LibStringTest:testBytesToHexString() (gas: 6422)
LibStringTest:testBytesToHexString(bytes) (runs: 256, μ: 690707, ~: 594542)
LibStringTest:testBytesToHexString(bytes) (runs: 257, μ: 686305, ~: 598437)
LibStringTest:testBytesToHexStringNoPrefix() (gas: 6039)
LibStringTest:testBytesToHexStringNoPrefix(bytes) (runs: 256, μ: 809930, ~: 594334)
LibStringTest:testBytesToHexStringNoPrefix(bytes) (runs: 257, μ: 673912, ~: 598089)
LibStringTest:testContains() (gas: 43812)
LibStringTest:testFromAddressToHexString() (gas: 3765)
LibStringTest:testFromAddressToHexStringChecksummed() (gas: 40345)
LibStringTest:testFromAddressToHexStringChecksummedDifferential(uint256) (runs: 256, μ: 692226, ~: 583789)
LibStringTest:testFromAddressToHexStringChecksummedDifferential(uint256) (runs: 257, μ: 664371, ~: 587753)
LibStringTest:testFromAddressToHexStringWithLeadingZeros() (gas: 3741)
LibStringTest:testHexStringNoPrefixVariants(uint256,uint256) (runs: 256, μ: 641350, ~: 593918)
LibStringTest:testHexStringNoPrefixVariants(uint256,uint256) (runs: 257, μ: 677842, ~: 597628)
LibStringTest:testNormalizeSmallString() (gas: 1349)
LibStringTest:testNormalizeSmallString(bytes32) (runs: 256, μ: 3105, ~: 4183)
LibStringTest:testNormalizeSmallString(bytes32) (runs: 257, μ: 2929, ~: 4183)
LibStringTest:testStringConcat() (gas: 6735)
LibStringTest:testStringConcat(string,string) (runs: 256, μ: 672637, ~: 575375)
LibStringTest:testStringConcat(string,string) (runs: 257, μ: 674038, ~: 579223)
LibStringTest:testStringConcatOriginal() (gas: 7994)
LibStringTest:testStringDirectReturn() (gas: 8186)
LibStringTest:testStringDirectReturn(string) (runs: 256, μ: 3585, ~: 3426)
LibStringTest:testStringDirectReturn(string) (runs: 257, μ: 3584, ~: 3426)
LibStringTest:testStringEndsWith() (gas: 2798)
LibStringTest:testStringEndsWith(uint256) (runs: 256, μ: 828826, ~: 608504)
LibStringTest:testStringEq(string,string) (runs: 256, μ: 1576, ~: 1577)
LibStringTest:testStringEndsWith(uint256) (runs: 257, μ: 699712, ~: 611578)
LibStringTest:testStringEq(string,string) (runs: 257, μ: 1576, ~: 1577)
LibStringTest:testStringEqs() (gas: 2149)
LibStringTest:testStringEscapeHTML() (gas: 11844)
LibStringTest:testStringEscapeHTML(uint256) (runs: 256, μ: 779111, ~: 625012)
LibStringTest:testStringEscapeHTML(uint256) (runs: 257, μ: 762467, ~: 617437)
LibStringTest:testStringEscapeJSON() (gas: 53068)
LibStringTest:testStringEscapeJSONHexEncode() (gas: 736800)
LibStringTest:testStringEscapeJSONHexEncode() (gas: 740891)
LibStringTest:testStringIndexOf() (gas: 17348)
LibStringTest:testStringIndexOf(uint256) (runs: 256, μ: 838879, ~: 619190)
LibStringTest:testStringIndexOf(uint256) (runs: 257, μ: 712791, ~: 618244)
LibStringTest:testStringIndicesOf() (gas: 11642)
LibStringTest:testStringIndicesOf(uint256) (runs: 256, μ: 747502, ~: 617517)
LibStringTest:testStringIndicesOf(uint256) (runs: 257, μ: 743540, ~: 621591)
LibStringTest:testStringIs7BitASCII() (gas: 203501)
LibStringTest:testStringIs7BitASCIIDifferential(bytes) (runs: 256, μ: 708983, ~: 574187)
LibStringTest:testStringIs7BitASCIIDifferential(bytes) (runs: 257, μ: 668440, ~: 578410)
LibStringTest:testStringLastIndexOf() (gas: 23952)
LibStringTest:testStringLastIndexOf(uint256) (runs: 256, μ: 808848, ~: 615043)
LibStringTest:testStringLowerDifferential() (gas: 3618133)
LibStringTest:testStringLowerDifferential(string) (runs: 256, μ: 8873, ~: 8506)
LibStringTest:testStringLastIndexOf(uint256) (runs: 257, μ: 752180, ~: 621949)
LibStringTest:testStringLowerDifferential() (gas: 3212201)
LibStringTest:testStringLowerDifferential(string) (runs: 257, μ: 8857, ~: 8506)
LibStringTest:testStringLowerOriginal() (gas: 1771)
LibStringTest:testStringPackAndUnpackOne() (gas: 724714)
LibStringTest:testStringPackAndUnpackOne(string) (runs: 256, μ: 700607, ~: 573980)
LibStringTest:testStringPackAndUnpackOneDifferential(string) (runs: 256, μ: 688368, ~: 573268)
LibStringTest:testStringPackAndUnpackTwo() (gas: 885198)
LibStringTest:testStringPackAndUnpackTwo(string,string) (runs: 256, μ: 661591, ~: 575678)
LibStringTest:testStringPackAndUnpackTwoDifferential(string,string) (runs: 256, μ: 673562, ~: 574227)
LibStringTest:testStringPackAndUnpackOne() (gas: 729214)
LibStringTest:testStringPackAndUnpackOne(string) (runs: 257, μ: 743821, ~: 577878)
LibStringTest:testStringPackAndUnpackOneDifferential(string) (runs: 257, μ: 714090, ~: 577233)
LibStringTest:testStringPackAndUnpackTwo() (gas: 4584957)
LibStringTest:testStringPackAndUnpackTwo(string,string) (runs: 257, μ: 757240, ~: 579613)
LibStringTest:testStringPackAndUnpackTwoDifferential(string,string) (runs: 257, μ: 710280, ~: 578104)
LibStringTest:testStringRepeat() (gas: 8445)
LibStringTest:testStringRepeat(string,uint256) (runs: 256, μ: 693370, ~: 576818)
LibStringTest:testStringRepeat(string,uint256) (runs: 257, μ: 670577, ~: 580884)
LibStringTest:testStringRepeatOriginal() (gas: 13538)
LibStringTest:testStringReplace(uint256) (runs: 256, μ: 667960, ~: 622556)
LibStringTest:testStringReplace(uint256) (runs: 257, μ: 722961, ~: 623450)
LibStringTest:testStringReplaceLong() (gas: 9615)
LibStringTest:testStringReplaceMedium() (gas: 8368)
LibStringTest:testStringReplaceShort() (gas: 18628)
LibStringTest:testStringRuneCount() (gas: 2975992)
LibStringTest:testStringRuneCountDifferential(string) (runs: 256, μ: 6043, ~: 5821)
LibStringTest:testStringRuneCount() (gas: 3024248)
LibStringTest:testStringRuneCountDifferential(string) (runs: 257, μ: 6031, ~: 5810)
LibStringTest:testStringSlice() (gas: 17049)
LibStringTest:testStringSlice(uint256) (runs: 256, μ: 682270, ~: 618818)
LibStringTest:testStringSlice(uint256) (runs: 257, μ: 753718, ~: 617411)
LibStringTest:testStringSplit() (gas: 19683)
LibStringTest:testStringSplit(uint256) (runs: 256, μ: 718602, ~: 617835)
LibStringTest:testStringSplit(uint256) (runs: 257, μ: 717316, ~: 622500)
LibStringTest:testStringStartsWith() (gas: 2558)
LibStringTest:testStringStartsWith(uint256) (runs: 256, μ: 708358, ~: 605784)
LibStringTest:testStringUpperDifferential() (gas: 3549675)
LibStringTest:testStringUpperDifferential(string) (runs: 256, μ: 8874, ~: 8507)
LibStringTest:testStringStartsWith(uint256) (runs: 257, μ: 746754, ~: 609420)
LibStringTest:testStringUpperDifferential() (gas: 3392177)
LibStringTest:testStringUpperDifferential(string) (runs: 257, μ: 8858, ~: 8507)
LibStringTest:testStringUpperOriginal() (gas: 1769)
LibStringTest:testToHexStringFixedLengthInsufficientLength() (gas: 3390)
LibStringTest:testToHexStringFixedLengthPositiveNumberLong() (gas: 4446)
LibStringTest:testToHexStringFixedLengthPositiveNumberShort() (gas: 1494)
LibStringTest:testToHexStringFixedLengthUint256Max() (gas: 4491)
LibStringTest:testToHexStringFixedLengthZeroRightPadded(uint256,uint256) (runs: 256, μ: 8101, ~: 4897)
LibStringTest:testToHexStringFixedLengthZeroRightPadded(uint256,uint256) (runs: 257, μ: 7929, ~: 5508)
LibStringTest:testToHexStringPositiveNumber() (gas: 1399)
LibStringTest:testToHexStringUint256Max() (gas: 4210)
LibStringTest:testToHexStringZero() (gas: 1336)
LibStringTest:testToHexStringZeroRightPadded(uint256) (runs: 256, μ: 2020, ~: 1337)
LibStringTest:testToHexStringZeroRightPadded(uint256) (runs: 257, μ: 1943, ~: 1420)
LibStringTest:testToMinimalHexStringNoPrefixPositiveNumber() (gas: 5959)
LibStringTest:testToMinimalHexStringNoPrefixUint256Max() (gas: 4054)
LibStringTest:testToMinimalHexStringNoPrefixZero() (gas: 1340)
LibStringTest:testToMinimalHexStringNoPrefixZeroRightPadded(uint256) (runs: 256, μ: 2011, ~: 1335)
LibStringTest:testToMinimalHexStringNoPrefixZeroRightPadded(uint256) (runs: 257, μ: 1940, ~: 1418)
LibStringTest:testToMinimalHexStringPositiveNumber() (gas: 6092)
LibStringTest:testToMinimalHexStringUint256Max() (gas: 4237)
LibStringTest:testToMinimalHexStringZero() (gas: 1383)
LibStringTest:testToMinimalHexStringZeroRightPadded(uint256) (runs: 256, μ: 2044, ~: 1361)
LibStringTest:testToMinimalHexStringZeroRightPadded(uint256) (runs: 257, μ: 1968, ~: 1444)
LibStringTest:testToSmallString() (gas: 4420)
LibStringTest:testToStringPositiveNumber() (gas: 1461)
LibStringTest:testToStringPositiveNumberBrutalized() (gas: 1444820)
LibStringTest:testToStringSignedDifferential(int256) (runs: 256, μ: 649797, ~: 574779)
LibStringTest:testToStringPositiveNumberBrutalized() (gas: 577888)
LibStringTest:testToStringSignedDifferential(int256) (runs: 257, μ: 769568, ~: 581076)
LibStringTest:testToStringSignedGas() (gas: 7212)
LibStringTest:testToStringSignedMemory(int256) (runs: 256, μ: 709754, ~: 573844)
LibStringTest:testToStringSignedMemory(int256) (runs: 257, μ: 787297, ~: 578799)
LibStringTest:testToStringSignedOriginalGas() (gas: 9760)
LibStringTest:testToStringUint256Max() (gas: 7432)
LibStringTest:testToStringUint256MaxBrutalized() (gas: 586571)
LibStringTest:testToStringUint256MaxBrutalized() (gas: 590215)
LibStringTest:testToStringZero() (gas: 1186)
LibStringTest:testToStringZeroBrutalized() (gas: 573986)
LibStringTest:testToStringZeroRightPadded(uint256) (runs: 256, μ: 689385, ~: 573834)
LibStringTest:test__codesize() (gas: 42004)
LibTTest:testLibT(bytes32,uint256) (runs: 256, μ: 1003, ~: 1003)
LibStringTest:testToStringZeroBrutalized() (gas: 577629)
LibStringTest:testToStringZeroRightPadded(uint256) (runs: 257, μ: 687580, ~: 577653)
LibStringTest:test__codesize() (gas: 42154)
LibTTest:testLibT(bytes32,uint256) (runs: 257, μ: 1003, ~: 1003)
LibTTest:test__codesize() (gas: 1805)
ReentrancyGuardTest:testRecursiveDirectUnguardedCall() (gas: 32203)
ReentrancyGuardTest:testRecursiveIndirectUnguardedCall() (gas: 45600)
Expand Down
81 changes: 81 additions & 0 deletions test/Extcode.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./utils/SoladyTest.sol";

contract ExtcodeTest is SoladyTest {
address internal _extcodecopy;
address internal _extcodesize;
address internal _extcodehash;

function setUp() public {
_extcodecopy = address(uint160(uint256(keccak256("extcodecopy"))));
_extcodehash = address(uint160(uint256(keccak256("extcodehash"))));
_extcodesize = address(uint160(uint256(keccak256("extcodesize"))));
vm.etch(_extcodecopy, hex"604035806020353d3d353c3df3");
vm.etch(_extcodesize, hex"3d353b3d52593df3");
vm.etch(_extcodehash, hex"3d353f3d52593df3");
}

function testExtcodeInitCodes() public {
bytes memory extcodecopyInitCode = hex"6c604035806020353d3d353c3df33d52600d6013f3";
_testInitCode(extcodecopyInitCode, _extcodecopy);
emit LogBytes32("extcodecopyInitCodeHash", keccak256(extcodecopyInitCode));

bytes memory extcodesizeInitCode = hex"673d353b3d52593df33d5260086018f3";
_testInitCode(extcodesizeInitCode, _extcodesize);
emit LogBytes32("extcodesizeInitCodeHash", keccak256(extcodesizeInitCode));

bytes memory extcodehashInitCode = hex"673d353f3d52593df33d5260086018f3";
_testInitCode(extcodehashInitCode, _extcodehash);
emit LogBytes32("extcodehashInitCodeHash", keccak256(extcodehashInitCode));
}

function _testInitCode(bytes memory initCode, address etched) internal {
address instance;
/// @solidity memory-safe-assembly
assembly {
instance := create(0, add(0x20, initCode), mload(initCode))
}
assertEq(instance.code, etched.code);
}

function testExtcodehash() public {
bytes32 expected;
/// @solidity memory-safe-assembly
assembly {
expected := extcodehash(address())
}
(bool success, bytes memory result) = _extcodehash.call(abi.encode(address(this)));
assertEq(abi.decode(result, (bytes32)), expected);
assertTrue(success);
}

function testExtcodesize() public {
bytes32 expected;
/// @solidity memory-safe-assembly
assembly {
expected := extcodesize(address())
}
(bool success, bytes memory result) = _extcodesize.call(abi.encode(address(this)));
assertEq(abi.decode(result, (bytes32)), expected);
assertTrue(success);
}

function testExtcodecopy(uint256 offset, uint256 length) public {
offset = _bound(offset, 0, 0xff);
length = _bound(length, 0, 0xffff);
(bool success, bytes memory result) =
_extcodecopy.call(abi.encode(address(this), offset, length));
bytes memory expected;
/// @solidity memory-safe-assembly
assembly {
expected := mload(0x40)
mstore(expected, length)
extcodecopy(address(), add(expected, 0x20), offset, length)
mstore(0x40, add(add(expected, 0x20), length))
}
assertEq(result.length, length);
assertTrue(success);
}
}
Loading

0 comments on commit 276b723

Please sign in to comment.