Skip to content
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
23 changes: 15 additions & 8 deletions contracts/FlatDirectoryFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import "./FlatDirectory.sol";
contract FlatDirectoryFactory {
event FlatDirectoryCreated(address);

function create(address _ethStorage) public returns (address) {
address public ethStorage;

constructor(address _ethStorage) {
ethStorage = _ethStorage;
}

function create() public returns (address) {
uint32 dataSize = (4 * 31 + 3) * 1024 - 4;
return _create(0, dataSize, _ethStorage);
return _create(0, dataSize);
}

function createWithSize(uint32 _size, address _ethStorage) public returns (address) {
return _create(0, _size, _ethStorage);
function createWithSize(uint32 _size) public returns (address) {
return _create(0, _size);
}

function createWithOptimized(uint32 _size, address _ethStorage) public returns (address) {
return _create(220, _size, _ethStorage);
function createWithOptimized(uint32 _size) public returns (address) {
return _create(220, _size);
}

function _create(uint8 _slotLimit, uint32 _size, address _ethStorage) private returns (address) {
FlatDirectory fd = new FlatDirectory(_slotLimit, _size, _ethStorage);
function _create(uint8 _slotLimit, uint32 _size) private returns (address) {
FlatDirectory fd = new FlatDirectory(_slotLimit, _size, ethStorage);
emit FlatDirectoryCreated(address(fd));
fd.transferOwnership(msg.sender);
return address(fd);
}
}
26 changes: 26 additions & 0 deletions contracts/core/ERC5018.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver {
return (new bytes(0), false);
}

function readChunksPaged(bytes memory name, uint256 startChunkId, uint256 limit)
external
view
returns (bytes[] memory chunks)
{
uint256 totalChunks = countChunks(name);
uint256 end = startChunkId + limit;
if (end > totalChunks) {
end = totalChunks;
}
uint256 count = end > startChunkId ? end - startChunkId : 0;

(bytes32 key, StorageMode mode) = _getModeAndKey(name);
chunks = new bytes[](count);
for (uint256 i = 0; i < count; i++) {
uint256 chunkId = startChunkId + i;
if (mode == StorageMode.Blob) {
(chunks[i],) = _getChunkFromBlob(key, chunkId);
} else if (mode == StorageMode.OnChain) {
(chunks[i],) = _getChunk(key, chunkId);
} else {
chunks[i] = new bytes(0);
}
}
}

function chunkSize(bytes memory name, uint256 chunkId) public view virtual override returns (uint256, bool) {
(bytes32 key, StorageMode mode) = _getModeAndKey(name);
if (mode == StorageMode.Blob) {
Expand Down
9 changes: 6 additions & 3 deletions contracts/interfaces/IERC5018.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ interface IERC5018 {
// Chunk-based large storage methods
function writeChunkByCalldata(bytes memory name, uint256 chunkId, bytes memory data) external;

function writeChunksByBlobs(bytes memory name, uint256[] memory chunkIds, uint256[] memory sizes)
external
payable;
function writeChunksByBlobs(bytes memory name, uint256[] memory chunkIds, uint256[] memory sizes) external payable;

function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory, bool);

function readChunksPaged(bytes memory name, uint256 startChunkId, uint256 limit)
external
view
returns (bytes[] memory chunks);

function chunkSize(bytes memory name, uint256 chunkId) external view returns (uint256, bool);

function removeChunk(bytes memory name, uint256 chunkId) external returns (bool);
Expand Down
53 changes: 24 additions & 29 deletions contracts/libraries/Memory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ library Memory {
require(bts.length >= len);
uint256 addr2;
assembly {
addr2 :=
add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
addr2 := add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
}
return equals(addr, addr2, len);
}
Expand All @@ -43,11 +42,10 @@ library Memory {
function allocate(uint256 numBytes) internal pure returns (uint256 addr) {
// Take the current value of the free memory pointer, and update.
assembly {
addr :=
mload(
/*FREE_MEM_PTR*/
0x40
)
addr := mload(
/*FREE_MEM_PTR*/
0x40
)
mstore(
/*FREE_MEM_PTR*/
0x40,
Expand Down Expand Up @@ -111,12 +109,11 @@ library Memory {
// Returns a memory pointer to the data portion of the provided bytes array.
function dataPtr(bytes memory bts) internal pure returns (uint256 addr) {
assembly {
addr :=
add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
addr := add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
}
}

Expand All @@ -125,12 +122,11 @@ library Memory {
function fromBytes(bytes memory bts) internal pure returns (uint256 addr, uint256 len) {
len = bts.length;
assembly {
addr :=
add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
addr := add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
}
}

Expand All @@ -141,12 +137,11 @@ library Memory {
bts = new bytes(len);
uint256 btsptr;
assembly {
btsptr :=
add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
btsptr := add(
bts,
/*BYTES_HEADER_SIZE*/
32
)
}
copy(addr, btsptr, len);
}
Expand Down
2 changes: 1 addition & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ forge build

forge script scripts/Deploy.s.sol:Deploy \
--broadcast \
--rpc-url https://rpc.beta.testnet.l2.quarkchain.io:8545
--rpc-url http://65.108.230.142:8545/
18 changes: 6 additions & 12 deletions scripts/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@
pragma solidity ^0.8.0;

import "forge-std/Script.sol";
import "contracts/FlatDirectory.sol";
import "contracts/FlatDirectoryFactory.sol";

contract Deploy is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");

vm.startBroadcast(deployerPrivateKey);

uint8 slotLimit = 0;
uint32 maxChunkSize = (4 * 31 + 3) * 1024 - 4;

address storageAddress;
if (block.chainid == 11155111) {// Sepolia
storageAddress = 0x804C520d3c084C805E37A35E90057Ac32831F96f;
} else if (block.chainid == 3335) {// quarkchain L2 network
if (block.chainid == 11155111) { // Sepolia
storageAddress = 0xAb3d380A268d088BA21Eb313c1C23F3BEC5cfe93;
} else if (block.chainid == 3335) { // quarkchain L2 network
storageAddress = 0x64003adbdf3014f7E38FC6BE752EB047b95da89A;
} else if (block.chainid == 1) { // etherum mainnet
storageAddress = 0xf0193d6E8fc186e77b6E63af4151db07524f6a7A;
} else {
storageAddress = address(0);
}

FlatDirectory dir = new FlatDirectory(slotLimit, maxChunkSize, storageAddress);
console.log("Deployed FlatDirectory at:", address(dir));

FlatDirectoryFactory factory = new FlatDirectoryFactory();
FlatDirectoryFactory factory = new FlatDirectoryFactory(storageAddress);
console.log("Deployed FlatDirectoryFactory at:", address(factory));

vm.stopBroadcast();
Expand Down