Skip to content

Commit 54da1d1

Browse files
committed
Add BytePackLib for efficient content storage
- Introduced BytePackLib to pack small byte arrays (0-31 bytes) into a bytes32 slot. - Updated Ethscriptions contract to use packed content or SSTORE2 pointers based on content size. - Modified content storage mapping to accommodate both packed data and SSTORE2 pointers. - Enhanced getEthscriptionContent function to handle unpacking of packed data.
1 parent 51cc758 commit 54da1d1

2 files changed

Lines changed: 91 additions & 7 deletions

File tree

‎contracts/src/Ethscriptions.sol‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity 0.8.24;
44
import "./ERC721EthscriptionsSequentialEnumerableUpgradeable.sol";
55
import {LibString} from "solady/utils/LibString.sol";
66
import "./libraries/SSTORE2Unlimited.sol";
7+
import "./libraries/BytePackLib.sol";
78
import "./libraries/EthscriptionsRendererLib.sol";
89
import "./EthscriptionsProver.sol";
910
import "./libraries/Predeploys.sol";
@@ -75,8 +76,8 @@ contract Ethscriptions is ERC721EthscriptionsSequentialEnumerableUpgradeable {
7576
/// @dev Ethscription ID (L1 tx hash) => Ethscription data
7677
mapping(bytes32 => Ethscription) internal ethscriptions;
7778

78-
/// @dev Content SHA => SSTORE2 pointer (single address, no array)
79-
mapping(bytes32 => address) public contentPointerBySha;
79+
/// @dev Content SHA => packed content (for <32 bytes) or SSTORE2 pointer (for >=32 bytes)
80+
mapping(bytes32 => bytes32) public contentStorageBySha;
8081

8182
/// @dev Content URI hash => first ethscription tx hash that used it (for protocol uniqueness check)
8283
/// @dev bytes32(0) means unused, non-zero means the content URI has been used
@@ -373,7 +374,16 @@ contract Ethscriptions is ERC721EthscriptionsSequentialEnumerableUpgradeable {
373374
/// @notice Get content for an ethscription
374375
function getEthscriptionContent(bytes32 ethscriptionId) public view returns (bytes memory) {
375376
Ethscription storage ethscription = _getEthscriptionOrRevert(ethscriptionId);
376-
address pointer = contentPointerBySha[ethscription.contentSha];
377+
bytes32 ref = contentStorageBySha[ethscription.contentSha];
378+
379+
// Check if it's inline content using BytePackLib
380+
if (BytePackLib.isPacked(ref)) {
381+
return BytePackLib.unpack(ref);
382+
}
383+
384+
// It's a pointer to SSTORE2 contract
385+
address pointer = address(uint160(uint256(ref)));
386+
377387
return SSTORE2Unlimited.read(pointer);
378388
}
379389

@@ -496,14 +506,21 @@ contract Ethscriptions is ERC721EthscriptionsSequentialEnumerableUpgradeable {
496506
contentSha = sha256(content);
497507

498508
// Check if content already exists
499-
address existingPointer = contentPointerBySha[contentSha];
500-
if (existingPointer != address(0)) {
509+
bytes32 existing = contentStorageBySha[contentSha];
510+
if (existing != bytes32(0)) {
501511
// Content already stored, just return the SHA
502512
return contentSha;
503513
}
504514

505-
// Content doesn't exist, store it using SSTORE2Unlimited (handles any size)
506-
contentPointerBySha[contentSha] = SSTORE2Unlimited.write(content);
515+
// Store based on size
516+
if (content.length <= 31) {
517+
// Pack small content directly into bytes32 (0-31 bytes)
518+
contentStorageBySha[contentSha] = BytePackLib.packCalldata(content);
519+
} else {
520+
// Deploy via SSTORE2 for larger content (32+ bytes)
521+
address pointer = SSTORE2Unlimited.write(content);
522+
contentStorageBySha[contentSha] = bytes32(uint256(uint160(pointer)));
523+
}
507524

508525
return contentSha;
509526
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.4;
3+
4+
/// @title BytePackLib
5+
/// @notice Library for packing small byte arrays (0-31 bytes) into a single bytes32 slot
6+
/// @dev Uses a tag byte (length + 1) to distinguish packed data from regular addresses/data
7+
library BytePackLib {
8+
error ContentTooLarge(uint256 size);
9+
error NotPackedData();
10+
11+
/// @notice Pack bytes calldata up to 31 bytes into a bytes32
12+
/// @dev Calldata version for gas optimization when called with external data
13+
/// @param data The data to pack (must be <= 31 bytes)
14+
/// @return packed The packed bytes32 value
15+
function packCalldata(bytes calldata data) internal pure returns (bytes32 packed) {
16+
uint256 len = data.length;
17+
if (len >= 32) revert ContentTooLarge(len);
18+
19+
assembly {
20+
// Pack: tag byte (len+1) | first 31 bytes of data
21+
packed := or(
22+
shl(248, add(len, 1)), // Tag in first byte
23+
shr(8, calldataload(data.offset)) // Data in remaining 31 bytes
24+
)
25+
}
26+
}
27+
28+
/// @notice Unpack a bytes32 value into bytes
29+
/// @dev Extracts the data based on the tag byte (length + 1)
30+
/// @param packed The packed bytes32 value
31+
/// @return data The unpacked bytes data
32+
function unpack(bytes32 packed) internal pure returns (bytes memory data) {
33+
uint256 tag = uint8(uint256(packed >> 248));
34+
if (tag == 0) revert NotPackedData();
35+
36+
uint256 len = tag - 1;
37+
data = new bytes(len);
38+
39+
if (len > 0) {
40+
assembly {
41+
// Store the data (shift left by 8 to remove tag byte)
42+
mstore(add(data, 0x20), shl(8, packed))
43+
// Zero the memory after the data for hygiene
44+
mstore(add(add(data, 0x20), len), 0)
45+
}
46+
}
47+
}
48+
49+
/// @notice Check if a bytes32 value is packed data
50+
/// @dev Returns true if the first byte indicates packed data (non-zero tag)
51+
/// @param value The bytes32 value to check
52+
/// @return True if the value is packed data, false otherwise
53+
function isPacked(bytes32 value) internal pure returns (bool) {
54+
// Packed data has a non-zero tag byte (1-32) in the first byte
55+
return uint8(uint256(value >> 248)) != 0;
56+
}
57+
58+
/// @notice Get the length of packed data without unpacking
59+
/// @dev Returns the length stored in the tag byte
60+
/// @param packed The packed bytes32 value
61+
/// @return The length of the packed data (0-31)
62+
function packedLength(bytes32 packed) internal pure returns (uint256) {
63+
uint256 tag = uint8(uint256(packed >> 248));
64+
if (tag == 0) revert NotPackedData();
65+
return tag - 1;
66+
}
67+
}

0 commit comments

Comments
 (0)