diff --git a/contracts/FlatDirectoryFactory.sol b/contracts/FlatDirectoryFactory.sol index c1a3121..cdb343d 100644 --- a/contracts/FlatDirectoryFactory.sol +++ b/contracts/FlatDirectoryFactory.sol @@ -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); } } diff --git a/contracts/core/ERC5018.sol b/contracts/core/ERC5018.sol index 6102f8e..6b95cee 100644 --- a/contracts/core/ERC5018.sol +++ b/contracts/core/ERC5018.sol @@ -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) { diff --git a/contracts/interfaces/IERC5018.sol b/contracts/interfaces/IERC5018.sol index 0ca67d5..98ffd92 100644 --- a/contracts/interfaces/IERC5018.sol +++ b/contracts/interfaces/IERC5018.sol @@ -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); diff --git a/contracts/libraries/Memory.sol b/contracts/libraries/Memory.sol index 7fee1bd..cd62406 100644 --- a/contracts/libraries/Memory.sol +++ b/contracts/libraries/Memory.sol @@ -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); } @@ -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, @@ -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 + ) } } @@ -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 + ) } } @@ -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); } diff --git a/deploy.sh b/deploy.sh index 8db7c93..cd473df 100644 --- a/deploy.sh +++ b/deploy.sh @@ -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/ diff --git a/scripts/Deploy.s.sol b/scripts/Deploy.s.sol index b25b9ca..f321c20 100644 --- a/scripts/Deploy.s.sol +++ b/scripts/Deploy.s.sol @@ -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();