Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(store): clean up Utils, add tests #1149

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/store/src/Memory.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { Utils } from "./Utils.sol";
import { leftMask } from "./Utils.sol";

library Memory {
function load(uint256 memoryPointer) internal pure returns (bytes32 data) {
Expand Down Expand Up @@ -49,7 +49,7 @@ library Memory {
)
}
} else {
uint256 mask = Utils.leftMask(length);
uint256 mask = leftMask(length);
assembly {
mstore(
toPointer,
Expand Down
10 changes: 5 additions & 5 deletions packages/store/src/Storage.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { Utils } from "./Utils.sol";
import { leftMask } from "./Utils.sol";

/**
* TODO Probably not fully optimized
Expand Down Expand Up @@ -45,7 +45,7 @@ library Storage {
wordRemainder = 32 - offset;
}

uint256 mask = Utils.leftMask(length);
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
// Load data from memory and offset it to match storage
Expand Down Expand Up @@ -91,7 +91,7 @@ library Storage {

// For the last partial word, apply a mask to the end
if (length > 0) {
uint256 mask = Utils.leftMask(length);
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
sstore(
Expand Down Expand Up @@ -151,7 +151,7 @@ library Storage {
wordRemainder = 32 - offset;
}

uint256 mask = Utils.leftMask(wordRemainder);
uint256 mask = leftMask(wordRemainder);
/// @solidity memory-safe-assembly
assembly {
// Load data from storage and offset it to match memory
Expand Down Expand Up @@ -195,7 +195,7 @@ library Storage {

// For the last partial word, apply a mask to the end
if (length > 0) {
uint256 mask = Utils.leftMask(length);
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
mstore(
Expand Down
1 change: 0 additions & 1 deletion packages/store/src/StoreCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Slice, SliceLib } from "./Slice.sol";
import { StoreMetadata, Hooks, HooksTableId } from "./codegen/Tables.sol";
import { IStoreErrors } from "./IStoreErrors.sol";
import { IStoreHook } from "./IStore.sol";
import { Utils } from "./Utils.sol";
import { TableId } from "./TableId.sol";

library StoreCore {
Expand Down
38 changes: 16 additions & 22 deletions packages/store/src/Utils.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

library Utils {
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b + (a % b == 0 ? 0 : 1);
}

/**
* Adapted from https://github.com/dk1a/solidity-stringutils/blob/main/src/utils/mem.sol#L149-L167
* @dev Left-aligned byte mask (e.g. for partial mload/mstore).
* For byteLength >= 32 returns type(uint256).max
*
* length 0: 0x000000...000000
* length 1: 0xff0000...000000
* length 2: 0xffff00...000000
* ...
* length 30: 0xffffff...ff0000
* length 31: 0xffffff...ffff00
* length 32+: 0xffffff...ffffff
*/
function leftMask(uint256 byteLength) internal pure returns (uint256) {
unchecked {
return ~(type(uint256).max >> (byteLength * 8));
}
/**
* Adapted from https://github.com/dk1a/solidity-stringutils/blob/main/src/utils/mem.sol#L149-L167
* @dev Left-aligned byte mask (e.g. for partial mload/mstore).
* For byteLength >= 32 returns type(uint256).max
*
* length 0: 0x000000...000000
* length 1: 0xff0000...000000
* length 2: 0xffff00...000000
* ...
* length 30: 0xffffff...ff0000
* length 31: 0xffffff...ffff00
* length 32+: 0xffffff...ffffff
*/
function leftMask(uint256 byteLength) pure returns (uint256) {
unchecked {
return ~(type(uint256).max >> (byteLength * 8));
}
}
1 change: 0 additions & 1 deletion packages/store/test/Storage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity >=0.8.0;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Storage } from "../src/Storage.sol";
import { Utils } from "../src/Utils.sol";
import { Bytes } from "../src/Bytes.sol";

contract StorageTest is Test, GasReporter {
Expand Down
1 change: 0 additions & 1 deletion packages/store/test/StoreCore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity >=0.8.0;
import "forge-std/Test.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { StoreCore, StoreCoreInternal } from "../src/StoreCore.sol";
import { Utils } from "../src/Utils.sol";
import { Bytes } from "../src/Bytes.sol";
import { TableId } from "../src/TableId.sol";
import { SliceLib } from "../src/Slice.sol";
Expand Down
1 change: 0 additions & 1 deletion packages/store/test/StoreCoreGas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { StoreCore, StoreCoreInternal } from "../src/StoreCore.sol";
import { Utils } from "../src/Utils.sol";
import { Bytes } from "../src/Bytes.sol";
import { SliceLib } from "../src/Slice.sol";
import { EncodeArray } from "../src/tightcoder/EncodeArray.sol";
Expand Down
35 changes: 35 additions & 0 deletions packages/store/test/Utils.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { leftMask } from "../src/Utils.sol";

contract UtilsTest is Test, GasReporter {
function testLeftMask() public {
bytes32 mask;
assertEq(leftMask(0), uint256(mask));
bytes32 highByte = hex"ff";
for (uint256 i = 1; i <= 32; i++) {
mask |= highByte;
highByte >>= 8;
assertEq(leftMask(i), uint256(mask));
}
}

function testFuzzLeftMaskOver32(uint256 byteLength) public {
// for values >32 the mask must always be type(uint256).max
vm.assume(byteLength > 32);
assertEq(leftMask(byteLength), type(uint256).max);
}

function testLeftMaskOver32() public {
// manually test for overflow issues
for (uint256 i; i < 100; i++) {
assertEq(leftMask(type(uint256).max - i), type(uint256).max);
}
for (uint256 i; i < 100; i++) {
assertEq(leftMask((type(uint256).max >> 8) + 50 - i), type(uint256).max);
}
}
}
1 change: 0 additions & 1 deletion packages/world/abi/src/Utils.sol/Utils.abi.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/world/abi/world/src/Utils.sol/Utils.abi.json

This file was deleted.