Skip to content

Commit

Permalink
fix: foundry profile and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Oct 19, 2022
1 parent 0f57cb2 commit e7b4621
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 109 deletions.
71 changes: 29 additions & 42 deletions contracts/TypedMemView.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ library TypedMemView {
// - - make sure to explicitly check for this with `notNull` or `assertType`
// - use `equal` for typed comparisons.


// The null view
bytes29 public constant NULL = hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
// Mask a low uint96
Expand Down Expand Up @@ -106,7 +105,9 @@ library TypedMemView {
if (i != 16) {
first <<= 16;
}
unchecked { i -= 1; }
unchecked {
i -= 1;
}
}

// abusing underflow here =_=
Expand All @@ -116,7 +117,9 @@ library TypedMemView {
if (i != 0) {
second <<= 16;
}
unchecked { i -= 1; }
unchecked {
i -= 1;
}
}
}

Expand All @@ -130,17 +133,17 @@ library TypedMemView {
v = _b;

// swap bytes
v = ((v >> 8) & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) |
((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
v = ((v >> 8) & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF)
| ((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
// swap 2-byte long pairs
v = ((v >> 16) & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) |
((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
v = ((v >> 16) & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF)
| ((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
// swap 4-byte long pairs
v = ((v >> 32) & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) |
((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
v = ((v >> 32) & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF)
| ((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
// swap 8-byte long pairs
v = ((v >> 64) & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) |
((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
v = ((v >> 64) & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF)
| ((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
// swap 16-byte long pairs
v = (v >> 128) | (v << 128);
}
Expand All @@ -154,10 +157,7 @@ library TypedMemView {
// ugly. redo without assembly?
assembly {
// solhint-disable-previous-line no-inline-assembly
mask := sar(
sub(_len, 1),
0x8000000000000000000000000000000000000000000000000000000000000000
)
mask := sar(sub(_len, 1), 0x8000000000000000000000000000000000000000000000000000000000000000)
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ library TypedMemView {
* @return ret - True if the view is valid
*/
function isValid(bytes29 memView) internal pure returns (bool ret) {
if (typeOf(memView) == 0xffffffffff) {return false;}
if (typeOf(memView) == 0xffffffffff) return false;
uint256 _end = end(memView);
assembly {
// solhint-disable-previous-line no-inline-assembly
Expand Down Expand Up @@ -234,14 +234,8 @@ library TypedMemView {
if (!isType(memView, _expected)) {
(, uint256 g) = encodeHex(uint256(typeOf(memView)));
(, uint256 e) = encodeHex(uint256(_expected));
string memory err = string(
abi.encodePacked(
"Type assertion failed. Got 0x",
uint80(g),
". Expected 0x",
uint80(e)
)
);
string memory err =
string(abi.encodePacked("Type assertion failed. Got 0x", uint80(g), ". Expected 0x", uint80(e)));
revert(err);
}
return memView;
Expand Down Expand Up @@ -300,9 +294,7 @@ library TypedMemView {
uint256 _end = _loc + _len;
assembly {
// solhint-disable-previous-line no-inline-assembly
if gt(_end, mload(0x40)) {
_end := 0
}
if gt(_end, mload(0x40)) { _end := 0 }
}
if (_end == 0) {
return NULL;
Expand All @@ -324,7 +316,7 @@ library TypedMemView {
uint256 _loc;
assembly {
// solhint-disable-previous-line no-inline-assembly
_loc := add(arr, 0x20) // our view is of the data, not the struct
_loc := add(arr, 0x20) // our view is of the data, not the struct
}

return build(newType, _loc, _len);
Expand Down Expand Up @@ -460,12 +452,11 @@ library TypedMemView {
* @param _slice The slice where the overrun occurred
* @return err - The err
*/
function indexErrOverrun(
uint256 _loc,
uint256 _len,
uint256 _index,
uint256 _slice
) internal pure returns (string memory err) {
function indexErrOverrun(uint256 _loc, uint256 _len, uint256 _index, uint256 _slice)
internal
pure
returns (string memory err)
{
(, uint256 a) = encodeHex(_loc);
(, uint256 b) = encodeHex(_len);
(, uint256 c) = encodeHex(_index);
Expand Down Expand Up @@ -496,7 +487,7 @@ library TypedMemView {
* @return result - The 32 byte result
*/
function index(bytes29 memView, uint256 _index, uint8 _bytes) internal pure returns (bytes32 result) {
if (_bytes == 0) {return bytes32(0);}
if (_bytes == 0) return bytes32(0);
if (_index + _bytes > len(memView)) {
revert(indexErrOverrun(loc(memView), len(memView), _index, uint256(_bytes)));
}
Expand Down Expand Up @@ -685,9 +676,7 @@ library TypedMemView {
// solhint-disable-previous-line no-inline-assembly
ptr := mload(0x40)
// revert if we're writing in occupied memory
if gt(ptr, _newLoc) {
revert(0x60, 0x20) // empty revert message
}
if gt(ptr, _newLoc) { revert(0x60, 0x20) } // empty revert message

// use the identity precompile to copy
res := staticcall(gas(), 4, _oldLoc, _len, _newLoc, _len)
Expand Down Expand Up @@ -737,13 +726,11 @@ library TypedMemView {
// solhint-disable-previous-line no-inline-assembly
let ptr := mload(0x40)
// revert if we're writing in occupied memory
if gt(ptr, _location) {
revert(0x60, 0x20) // empty revert message
}
if gt(ptr, _location) { revert(0x60, 0x20) } // empty revert message
}

uint256 _offset = 0;
for (uint256 i = 0; i < memViews.length; i ++) {
for (uint256 i = 0; i < memViews.length; i++) {
bytes29 memView = memViews[i];
unchecked {
unsafeCopyTo(memView, _location + _offset);
Expand Down
86 changes: 20 additions & 66 deletions contracts/test/TypedMemView.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {TypedMemView} from "../TypedMemView.sol";
import "ds-test/test.sol";

interface CheatCodes {
function expectRevert(bytes calldata) external;
function expectRevert(bytes calldata) external;
}

contract TestMemView is DSTest {
Expand Down Expand Up @@ -75,57 +75,33 @@ contract TestMemView is DSTest {
bytes29 v1 = TypedMemView.ref(one, 0);
bytes29 v2 = TypedMemView.ref(two, 0);

require(
v1.slice(0, 0, 0).equal(v2.slice(2, 0, 0)),
""
);
require(v1.slice(0, 0, 0).equal(v2.slice(2, 0, 0)), "");

require(
v1.slice(0, 2, 0).equal(v2.slice(2, 2, 0)),
"abcd"
);
require(v1.slice(0, 2, 0).equal(v2.slice(2, 2, 0)), "abcd");

require(
v1.slice(0, 4, 0).equal(v2.slice(2, 4, 0)),
"abcdffff"
);
require(v1.slice(0, 4, 0).equal(v2.slice(2, 4, 0)), "abcdffff");

require(
v1.slice(2, 4, 0).equal(v2.slice(4, 4, 0)),
"ffff1111"
);
require(v1.slice(2, 4, 0).equal(v2.slice(4, 4, 0)), "ffff1111");

require(
v1.slice(2, 6, 0).equal(v2.slice(4, 6, 0)),
"ffff1111ffff"
);
require(v1.slice(2, 6, 0).equal(v2.slice(4, 6, 0)), "ffff1111ffff");

require(
v1.postfix(8, 0).equal(v2.postfix(8, 0)),
"ffffffffffffffff"
);
require(v1.postfix(8, 0).equal(v2.postfix(8, 0)), "ffffffffffffffff");

require(
v1.prefix(14, 0).equal(v2.postfix(14, 0)),
"abcdffff1111ffffffffffffffff"
);
require(v1.prefix(14, 0).equal(v2.postfix(14, 0)), "abcdffff1111ffffffffffffffff");

require(
v1.postfix(2, 0).equal(v2.prefix(2, 0)),
"ffff"
);
require(v1.postfix(2, 0).equal(v2.prefix(2, 0)), "ffff");
}

function testSlicing() public view {
// 76 bytes - 3 words

// solhint-disable-next-line max-line-length
bytes memory one = hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b";
bytes memory one =
hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b";

bytes29 v1 = TypedMemView.ref(one, 1);
require(
v1.slice(1, 13, 0).keccak() == keccak256(hex"0102030405060708090a0b0c0d"),
"slice(1, 13) -- keccak mismatch"
v1.slice(1, 13, 0).keccak() == keccak256(hex"0102030405060708090a0b0c0d"), "slice(1, 13) -- keccak mismatch"
);

bytes29 emptySlice = v1.slice(0, 0, 0);
Expand All @@ -145,28 +121,15 @@ contract TestMemView is DSTest {
require(v2.keccak() == keccak256(hex""), "v2 slice not null");

bytes29 v3 = v1.slice(1, 25, 0).slice(13, 12, 0);
require(
v3.keccak() == keccak256(hex"0e0f10111213141516171819"),
"multiple slice hash mismatch"
);
require(
v3.sha2() == sha256(hex"0e0f10111213141516171819"),
"multiple slice sha2 mismatch"
);
require(v3.keccak() == keccak256(hex"0e0f10111213141516171819"), "multiple slice hash mismatch");
require(v3.sha2() == sha256(hex"0e0f10111213141516171819"), "multiple slice sha2 mismatch");

require(
keccak256(v3.clone()) == v3.keccak(),
"clone slice hash mismatch"
);
require(keccak256(v3.clone()) == v3.keccak(), "clone slice hash mismatch");

require(
sha256(v3.clone()) == v3.sha2(),
"clone slice sha2 mismatch"
);
require(sha256(v3.clone()) == v3.sha2(), "clone slice sha2 mismatch");

require(
v1.slice(25, 33, 0).slice(19, 14, 0) == v1.slice(20, 43, 0).slice(24, 14, 0),
"expected equivalent slices"
v1.slice(25, 33, 0).slice(19, 14, 0) == v1.slice(20, 43, 0).slice(24, 14, 0), "expected equivalent slices"
);

require(
Expand All @@ -179,20 +142,11 @@ contract TestMemView is DSTest {
"index mismatch 14 bytes"
);

require(
v1.indexUint(0, 14) == 0x000102030405060708090a0b0c0d,
"index mismatch 14 byte uint"
);
require(v1.indexUint(0, 14) == 0x000102030405060708090a0b0c0d, "index mismatch 14 byte uint");

require(
v1.indexLEUint(0, 14) == 0x0d0c0b0a09080706050403020100,
"index mismatch 14 byte uint le"
);
require(v1.indexLEUint(0, 14) == 0x0d0c0b0a09080706050403020100, "index mismatch 14 byte uint le");

require(
v1.indexAddress(12) == 0x0c0D0E0F101112131415161718191a1B1c1D1E1F,
"index mismatch address"
);
require(v1.indexAddress(12) == 0x0c0D0E0F101112131415161718191a1B1c1D1E1F, "index mismatch address");

require(v1.slice(0, 76, 1).equal(v1), "full slice not equal");
require(v1.slice(0, 77, 1).isNull(), "Non-null on slice overrun");
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[default]
[profile.default]
src = 'contracts'
out = 'out'
libs = ['lib']
Expand Down

0 comments on commit e7b4621

Please sign in to comment.