diff --git a/packages/store/src/PackedCounter.sol b/packages/store/src/PackedCounter.sol index bff5867e9f..eb0104cd6b 100644 --- a/packages/store/src/PackedCounter.sol +++ b/packages/store/src/PackedCounter.sol @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.21; +import { BYTE_TO_BITS } from "./constants.sol"; + /** * @title PackedCounter Type Definition * @dev Describes how the packed counter is structured. @@ -18,9 +20,9 @@ using PackedCounterInstance for PackedCounter global; // Constants for packed counter handling: // Number of bits for the 7-byte accumulator -uint256 constant ACC_BITS = 7 * 8; +uint256 constant ACC_BITS = 7 * BYTE_TO_BITS; // Number of bits for the 5-byte sections -uint256 constant VAL_BITS = 5 * 8; +uint256 constant VAL_BITS = 5 * BYTE_TO_BITS; // Maximum value of a 5-byte section uint256 constant MAX_VAL = type(uint40).max; diff --git a/packages/store/src/ResourceId.sol b/packages/store/src/ResourceId.sol index 345c0cb823..03d3189004 100644 --- a/packages/store/src/ResourceId.sol +++ b/packages/store/src/ResourceId.sol @@ -9,9 +9,9 @@ pragma solidity >=0.8.21; type ResourceId is bytes32; /// @dev Number of bits reserved for the type in the ResourceId. -uint256 constant TYPE_BITS = 2 * 8; +uint256 constant TYPE_BITS = 2 * 8; // 2 bytes * 8 bits per byte /// @dev Number of bits reserved for the name in the ResourceId. -uint256 constant NAME_BITS = 32 * 8 - TYPE_BITS; +uint256 constant NAME_BITS = 32 * 8 - TYPE_BITS; // 32 bytes * 8 bits per byte - type bits /// @dev Bitmask to extract the type from the ResourceId. bytes32 constant TYPE_MASK = bytes32(hex"ffff"); diff --git a/packages/store/src/Storage.sol b/packages/store/src/Storage.sol index c0f4fa2b10..a1cf0cde72 100644 --- a/packages/store/src/Storage.sol +++ b/packages/store/src/Storage.sol @@ -3,6 +3,7 @@ pragma solidity >=0.8.21; import { leftMask } from "./leftMask.sol"; import { Memory } from "./Memory.sol"; +import { BYTE_TO_BITS } from "./constants.sol"; /** * @title Storage Library @@ -60,7 +61,7 @@ library Storage { /// @solidity memory-safe-assembly assembly { // Load data from memory and offset it to match storage - let bitOffset := mul(offset, 8) + let bitOffset := mul(offset, BYTE_TO_BITS) mask := shr(bitOffset, mask) let offsetData := shr(bitOffset, mload(memoryPointer)) @@ -208,7 +209,7 @@ library Storage { /// @solidity memory-safe-assembly assembly { // Load data from storage and offset it to match memory - let offsetData := shl(mul(offset, 8), sload(storagePointer)) + let offsetData := shl(mul(offset, BYTE_TO_BITS), sload(storagePointer)) mstore( memoryPointer, @@ -284,7 +285,7 @@ library Storage { // Extra data past length is not truncated // This assumes that the caller will handle the overflow bits appropriately assembly { - result := shl(mul(offset, 8), sload(storagePointer)) + result := shl(mul(offset, BYTE_TO_BITS), sload(storagePointer)) } uint256 wordRemainder; @@ -296,7 +297,7 @@ library Storage { // Read from the next slot if field spans 2 slots if (length > wordRemainder) { assembly { - result := or(result, shr(mul(wordRemainder, 8), sload(add(storagePointer, 1)))) + result := or(result, shr(mul(wordRemainder, BYTE_TO_BITS), sload(add(storagePointer, 1)))) } } } diff --git a/packages/store/src/leftMask.sol b/packages/store/src/leftMask.sol index 604ffd446d..9343cfc5a8 100644 --- a/packages/store/src/leftMask.sol +++ b/packages/store/src/leftMask.sol @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.21; +import { BYTE_TO_BITS } from "./constants.sol"; + /** * @title Byte Mask Utility * @notice Utility functions to manage bytes in memory. @@ -24,6 +26,6 @@ pragma solidity >=0.8.21; */ function leftMask(uint256 byteLength) pure returns (uint256 mask) { unchecked { - return ~(type(uint256).max >> (byteLength * 8)); + return ~(type(uint256).max >> (byteLength * BYTE_TO_BITS)); } } diff --git a/packages/world/src/WorldResourceId.sol b/packages/world/src/WorldResourceId.sol index c609621f60..d085428251 100644 --- a/packages/world/src/WorldResourceId.sol +++ b/packages/world/src/WorldResourceId.sol @@ -7,8 +7,8 @@ import { ResourceId, ResourceIdInstance, TYPE_BITS } from "@latticexyz/store/src import { ROOT_NAMESPACE, ROOT_NAME } from "./constants.sol"; import { RESOURCE_NAMESPACE, MASK_RESOURCE_NAMESPACE } from "./worldResourceTypes.sol"; -uint256 constant NAMESPACE_BITS = 14 * 8; -uint256 constant NAME_BITS = 16 * 8; +uint256 constant NAMESPACE_BITS = 14 * 8; // 14 bytes * 8 bits per byte +uint256 constant NAME_BITS = 16 * 8; // 16 bytes * 8 bits per byte bytes16 constant ROOT_NAMESPACE_STRING = bytes16("ROOT_NAMESPACE"); bytes16 constant ROOT_NAME_STRING = bytes16("ROOT_NAME");