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

refactor(store,world,world-modules): use BYTE_TO_BITS constant where applicable [N-09] #2015

Merged
merged 6 commits into from
Jan 12, 2024
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
6 changes: 4 additions & 2 deletions packages/store/src/PackedCounter.sol
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/store/src/ResourceId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 5 additions & 4 deletions packages/store/src/Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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))))
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/store/src/leftMask.sol
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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));
}
}
4 changes: 2 additions & 2 deletions packages/world/src/WorldResourceId.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading