From e02b28c4f0711984d6cac435fa801a8115280e1a Mon Sep 17 00:00:00 2001 From: iteye Date: Wed, 28 Aug 2024 14:04:47 +0800 Subject: [PATCH 1/9] Supports obtaining hashes in batches --- contracts/ERC5018.sol | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index 4b2b90e..7d69d17 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -161,4 +161,19 @@ contract ERC5018 is IERC5018, LargeStorageManager, BlobStorageManager { } return 0; } + + function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; + + hashes = new bytes32[](chunkIds.length); + for (uint8 i = 0; i < chunkIds.length; i++) { + if (mode == StorageMode.Blob) { + hashes[i] = _getChunkHashFromBlob(key, chunkIds[i]); + } else if (mode == StorageMode.OnChain) { + (bytes memory localData,) = _getChunk(key, chunkIds[i]); + hashes[i] = keccak256(localData); + } + } + } } From 5819e71f190f0955dff12006c86029c1d10feb85 Mon Sep 17 00:00:00 2001 From: iteye Date: Mon, 2 Sep 2024 16:04:19 +0800 Subject: [PATCH 2/9] add get upload info --- contracts/ERC5018.sol | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index e76699c..0efe964 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -169,7 +169,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return 0; } - function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { + function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public view returns (bytes32[] memory hashes) { bytes32 key = keccak256(name); StorageMode mode = storageModes[key]; @@ -183,4 +183,19 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } } } + + function getUploadInfo(bytes memory name) public view returns (StorageMode mode, uint256 count, uint256 payment) { + bytes32 key = keccak256(name); + mode = storageModes[key]; + + if (mode == StorageMode.Blob) { + count = _countChunksFromBlob(key); + } else if (mode == StorageMode.OnChain) { + count = _countChunks(key); + } else { + count = 0; + } + + payment = address(storageContract) != address(0) ? upfrontPayment() : 0; + } } From 46ebbb592fbd4a6783c4f1953ffcfe27ea8f4ae1 Mon Sep 17 00:00:00 2001 From: iteye Date: Mon, 2 Sep 2024 16:12:53 +0800 Subject: [PATCH 3/9] code optimization --- contracts/ERC5018.sol | 84 +++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index 0efe964..38f299a 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -31,10 +31,6 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return storageModes[keccak256(name)]; } - function _setStorageMode(bytes memory name, StorageMode mode) internal { - storageModes[keccak256(name)] = mode; - } - // Large storage methods function write(bytes memory name, bytes calldata data) public onlyOwner payable virtual override { // TODO: support multiple chunks @@ -42,41 +38,46 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function read(bytes memory name) public view virtual override returns (bytes memory, bool) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _getFromBlob(keccak256(name)); + return _getFromBlob(key); } else if (mode == StorageMode.OnChain) { - return _get(keccak256(name)); + return _get(key); } return (new bytes(0), false); } function size(bytes memory name) public view virtual override returns (uint256, uint256) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _sizeFromBlob(keccak256(name)); + return _sizeFromBlob(key); } else if (mode == StorageMode.OnChain) { - return _size(keccak256(name)); + return _size(key); } return (0, 0); } function remove(bytes memory name) public virtual override onlyOwner returns (uint256) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; + storageModes[key] = StorageMode.Uninitialized; if (mode == StorageMode.Blob) { - return _removeFromBlob(keccak256(name), 0); + return _removeFromBlob(key, 0); } else if (mode == StorageMode.OnChain) { - return _remove(keccak256(name), 0); + return _remove(key, 0); } return 0; } function countChunks(bytes memory name) public view virtual override returns (uint256) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _countChunksFromBlob(keccak256(name)); + return _countChunksFromBlob(key); } else if (mode == StorageMode.OnChain) { - return _countChunks(keccak256(name)); + return _countChunks(key); } return 0; } @@ -87,12 +88,13 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { uint256 chunkId, bytes calldata data ) public payable onlyOwner virtual override { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; require(mode == StorageMode.Uninitialized || mode == StorageMode.OnChain, "Invalid storage mode"); if (mode == StorageMode.Uninitialized) { - _setStorageMode(name, StorageMode.OnChain); + storageModes[key] = StorageMode.OnChain; } - _putChunkFromCalldata(keccak256(name), chunkId, data, msg.value); + _putChunkFromCalldata(key, chunkId, data, msg.value); } function writeChunks( @@ -102,50 +104,55 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { ) public onlyOwner override payable { require(isSupportBlob(), "The current network does not support blob upload"); - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; require(mode == StorageMode.Uninitialized || mode == StorageMode.Blob, "Invalid storage mode"); if (mode == StorageMode.Uninitialized) { - _setStorageMode(name, StorageMode.Blob); + storageModes[key] = StorageMode.Blob; } - _putChunks(keccak256(name), chunkIds, sizes); + _putChunks(key, chunkIds, sizes); } function readChunk(bytes memory name, uint256 chunkId) public view virtual override returns (bytes memory, bool) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _getChunkFromBlob(keccak256(name), chunkId); + return _getChunkFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { - return _getChunk(keccak256(name), chunkId); + return _getChunk(key, chunkId); } return (new bytes(0), false); } function chunkSize(bytes memory name, uint256 chunkId) public view virtual override returns (uint256, bool) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _chunkSizeFromBlob(keccak256(name), chunkId); + return _chunkSizeFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { - return _chunkSize(keccak256(name), chunkId); + return _chunkSize(key, chunkId); } return (0, false); } function removeChunk(bytes memory name, uint256 chunkId) public virtual onlyOwner override returns (bool) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _removeChunkFromBlob(keccak256(name), chunkId); + return _removeChunkFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { - return _removeChunk(keccak256(name), chunkId); + return _removeChunk(key, chunkId); } return false; } function truncate(bytes memory name, uint256 chunkId) public virtual onlyOwner override returns (uint256) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _removeFromBlob(keccak256(name), chunkId); + return _removeFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { - return _remove(keccak256(name), chunkId); + return _remove(key, chunkId); } return 0; } @@ -159,17 +166,18 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function getChunkHash(bytes memory name, uint256 chunkId) public override view returns (bytes32) { - StorageMode mode = getStorageMode(name); + bytes32 key = keccak256(name); + StorageMode mode = storageModes[key]; if (mode == StorageMode.Blob) { - return _getChunkHashFromBlob(keccak256(name), chunkId); + return _getChunkHashFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { - (bytes memory localData,) = readChunk(name, chunkId); + (bytes memory localData,) = _getChunk(key, chunkId); return keccak256(localData); } return 0; } - function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public view returns (bytes32[] memory hashes) { + function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { bytes32 key = keccak256(name); StorageMode mode = storageModes[key]; From b762979fc763a4c431842d4ab4e108933cee7586 Mon Sep 17 00:00:00 2001 From: iteye Date: Mon, 2 Sep 2024 16:39:50 +0800 Subject: [PATCH 4/9] code optimization --- contracts/BlobStorageManager.sol | 19 ++++--------------- contracts/ERC5018.sol | 7 +------ contracts/IERC5018.sol | 10 ++++++++++ contracts/LargeStorageManager.sol | 24 ++++++------------------ 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/contracts/BlobStorageManager.sol b/contracts/BlobStorageManager.sol index cd2db25..10168f9 100644 --- a/contracts/BlobStorageManager.sol +++ b/contracts/BlobStorageManager.sol @@ -52,11 +52,7 @@ contract BlobStorageManager is Ownable { function _countChunksFromBlob(bytes32 key) internal view returns (uint256) { uint256 chunkId = 0; - while (true) { - bytes32 chunkKey = keyToChunks[key][chunkId]; - if (chunkKey == bytes32(0)) { - break; - } + while (keyToChunks[key][chunkId] != bytes32(0)) { chunkId++; } return chunkId; @@ -112,8 +108,7 @@ contract BlobStorageManager is Ownable { } function _removeChunkFromBlob(bytes32 key, uint256 chunkId) internal returns (bool) { - bytes32 chunkKey = keyToChunks[key][chunkId]; - if (chunkKey == bytes32(0)) { + if (keyToChunks[key][chunkId] == bytes32(0)) { return false; } if (keyToChunks[key][chunkId + 1] != bytes32(0)) { @@ -128,12 +123,7 @@ contract BlobStorageManager is Ownable { } function _removeFromBlob(bytes32 key, uint256 chunkId) internal returns (uint256) { - while (true) { - bytes32 chunkKey = keyToChunks[key][chunkId]; - if (chunkKey == bytes32(0)) { - break; - } - + while (keyToChunks[key][chunkId] != bytes32(0)) { // TODO The current version does not support the delete // storageContract.remove(keyToChunks[key][chunkId]); keyToChunks[key][chunkId] = bytes32(0); @@ -143,8 +133,7 @@ contract BlobStorageManager is Ownable { } function _preparePutFromBlob(bytes32 key, uint256 chunkId) private { - bytes32 chunkKey = keyToChunks[key][chunkId]; - if (chunkKey == bytes32(0)) { + if (keyToChunks[key][chunkId] == bytes32(0)) { require(chunkId == 0 || keyToChunks[key][chunkId - 1] != bytes32(0), "must replace or append"); } else { // TODO The current version does not support the delete diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index 38f299a..395f23e 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -8,11 +8,6 @@ import "./ISemver.sol"; contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { - enum StorageMode { - Uninitialized, - OnChain, - Blob - } mapping(bytes32 => StorageMode) storageModes; constructor( @@ -192,7 +187,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } } - function getUploadInfo(bytes memory name) public view returns (StorageMode mode, uint256 count, uint256 payment) { + function getUploadInfo(bytes memory name) public override view returns (StorageMode mode, uint256 count, uint256 payment) { bytes32 key = keccak256(name); mode = storageModes[key]; diff --git a/contracts/IERC5018.sol b/contracts/IERC5018.sol index 839af35..c309251 100644 --- a/contracts/IERC5018.sol +++ b/contracts/IERC5018.sol @@ -2,6 +2,12 @@ pragma solidity ^0.8.0; interface IERC5018 { + enum StorageMode { + Uninitialized, + OnChain, + Blob + } + // Large storage methods function write(bytes memory name, bytes memory data) external payable; @@ -36,4 +42,8 @@ interface IERC5018 { function destruct() external; function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32); + + function getChunkHashes(bytes memory name, uint256[] memory chunkIds) external view returns (bytes32[] memory hashes); + + function getUploadInfo(bytes memory name) external view returns (StorageMode mode, uint256 count, uint256 payment); } diff --git a/contracts/LargeStorageManager.sol b/contracts/LargeStorageManager.sol index dc07578..e1207b8 100644 --- a/contracts/LargeStorageManager.sol +++ b/contracts/LargeStorageManager.sol @@ -75,11 +75,9 @@ contract LargeStorageManager { bytes32 metadata = keyToMetadata[key][chunkId]; if (metadata.isInSlot()) { - bytes memory res = SlotHelper.getRaw(keyToSlots[key][chunkId], metadata); - return (res, true); + return (SlotHelper.getRaw(keyToSlots[key][chunkId], metadata), true); } else { - address addr = metadata.bytes32ToAddr(); - return StorageHelper.getRaw(addr); + return StorageHelper.getRaw(metadata.bytes32ToAddr()); } } @@ -89,23 +87,16 @@ contract LargeStorageManager { if (metadata == bytes32(0)) { return (0, false); } else if (metadata.isInSlot()) { - uint256 len = metadata.decodeLen(); - return (len, true); + return (metadata.decodeLen(), true); } else { - address addr = metadata.bytes32ToAddr(); - return StorageHelper.sizeRaw(addr); + return StorageHelper.sizeRaw(metadata.bytes32ToAddr()); } } function _countChunks(bytes32 key) internal view returns (uint256) { uint256 chunkId = 0; - while (true) { - bytes32 metadata = keyToMetadata[key][chunkId]; - if (metadata == bytes32(0x0)) { - break; - } - + while (keyToMetadata[key][chunkId] != bytes32(0)) { chunkId++; } @@ -162,11 +153,8 @@ contract LargeStorageManager { // Returns # of chunks deleted function _remove(bytes32 key, uint256 chunkId) internal returns (uint256) { - while (true) { + while (keyToMetadata[key][chunkId] != bytes32(0)) { bytes32 metadata = keyToMetadata[key][chunkId]; - if (metadata == bytes32(0x0)) { - break; - } if (!metadata.isInSlot()) { address addr = metadata.bytes32ToAddr(); From 3b4680df6d23b29cc1a0ef8a09015ebfe3179f22 Mon Sep 17 00:00:00 2001 From: iteye Date: Tue, 3 Sep 2024 15:54:26 +0800 Subject: [PATCH 5/9] update get hashes --- contracts/ERC5018.sol | 17 +++++------------ contracts/IERC5018.sol | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index 395f23e..c97daa0 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -172,18 +172,11 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return 0; } - function getChunkHashes(bytes memory name, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; - - hashes = new bytes32[](chunkIds.length); - for (uint8 i = 0; i < chunkIds.length; i++) { - if (mode == StorageMode.Blob) { - hashes[i] = _getChunkHashFromBlob(key, chunkIds[i]); - } else if (mode == StorageMode.OnChain) { - (bytes memory localData,) = _getChunk(key, chunkIds[i]); - hashes[i] = keccak256(localData); - } + function getChunkHashes(bytes[] memory names, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { + require(names.length == chunkIds.length, "Invalid length"); + hashes = new bytes32[](names.length); + for (uint8 i = 0; i < names.length; i++) { + hashes[i] = getChunkHash(names[i], chunkIds[i]); } } diff --git a/contracts/IERC5018.sol b/contracts/IERC5018.sol index c309251..878d0d1 100644 --- a/contracts/IERC5018.sol +++ b/contracts/IERC5018.sol @@ -43,7 +43,7 @@ interface IERC5018 { function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32); - function getChunkHashes(bytes memory name, uint256[] memory chunkIds) external view returns (bytes32[] memory hashes); + function getChunkHashes(bytes[] memory names, uint256[] memory chunkIds) external view returns (bytes32[] memory hashes); function getUploadInfo(bytes memory name) external view returns (StorageMode mode, uint256 count, uint256 payment); } From 1120f21c00a8fef72a720696a281e452bb667b96 Mon Sep 17 00:00:00 2001 From: iteye Date: Tue, 3 Sep 2024 19:17:21 +0800 Subject: [PATCH 6/9] update get hashes --- contracts/ERC5018.sol | 20 +++++++++++++++----- contracts/IERC5018.sol | 7 ++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index c97daa0..b2002ed 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -172,12 +172,22 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return 0; } - function getChunkHashes(bytes[] memory names, uint256[] memory chunkIds) public override view returns (bytes32[] memory hashes) { - require(names.length == chunkIds.length, "Invalid length"); - hashes = new bytes32[](names.length); - for (uint8 i = 0; i < names.length; i++) { - hashes[i] = getChunkHash(names[i], chunkIds[i]); + function getBatchChunkHashes(FileChunk[] memory fileChunks) external view returns (bytes32[] memory) { + uint totalChunks = 0; + + for (uint i = 0; i < fileChunks.length; i++) { + totalChunks += fileChunks[i].chunkIds.length; + } + + bytes32[] memory hashes = new bytes32[](totalChunks); + uint index = 0; + for (uint i = 0; i < fileChunks.length; i++) { + for (uint j = 0; j < fileChunks[i].chunkIds.length; j++) { + hashes[index] = getChunkHash(fileChunks[i].name, fileChunks[i].chunkIds[j]); + index++; + } } + return hashes; } function getUploadInfo(bytes memory name) public override view returns (StorageMode mode, uint256 count, uint256 payment) { diff --git a/contracts/IERC5018.sol b/contracts/IERC5018.sol index 878d0d1..1c7e582 100644 --- a/contracts/IERC5018.sol +++ b/contracts/IERC5018.sol @@ -8,6 +8,11 @@ interface IERC5018 { Blob } + struct FileChunk { + bytes name; + uint256[] chunkIds; + } + // Large storage methods function write(bytes memory name, bytes memory data) external payable; @@ -43,7 +48,7 @@ interface IERC5018 { function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32); - function getChunkHashes(bytes[] memory names, uint256[] memory chunkIds) external view returns (bytes32[] memory hashes); + function getBatchChunkHashes(FileChunk[] memory fileChunks) external view returns (bytes32[] memory); function getUploadInfo(bytes memory name) external view returns (StorageMode mode, uint256 count, uint256 payment); } From a88f2aedfac3ff87a25216451ef7e7a198404834 Mon Sep 17 00:00:00 2001 From: iteye Date: Fri, 6 Sep 2024 16:21:52 +0800 Subject: [PATCH 7/9] fix comment --- contracts/ERC5018.sol | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index b2002ed..bb7e2c3 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -33,8 +33,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function read(bytes memory name) public view virtual override returns (bytes memory, bool) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _getFromBlob(key); } else if (mode == StorageMode.OnChain) { @@ -44,8 +43,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function size(bytes memory name) public view virtual override returns (uint256, uint256) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _sizeFromBlob(key); } else if (mode == StorageMode.OnChain) { @@ -55,8 +53,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function remove(bytes memory name) public virtual override onlyOwner returns (uint256) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); storageModes[key] = StorageMode.Uninitialized; if (mode == StorageMode.Blob) { return _removeFromBlob(key, 0); @@ -67,8 +64,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function countChunks(bytes memory name) public view virtual override returns (uint256) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _countChunksFromBlob(key); } else if (mode == StorageMode.OnChain) { @@ -83,8 +79,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { uint256 chunkId, bytes calldata data ) public payable onlyOwner virtual override { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); require(mode == StorageMode.Uninitialized || mode == StorageMode.OnChain, "Invalid storage mode"); if (mode == StorageMode.Uninitialized) { storageModes[key] = StorageMode.OnChain; @@ -99,8 +94,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { ) public onlyOwner override payable { require(isSupportBlob(), "The current network does not support blob upload"); - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); require(mode == StorageMode.Uninitialized || mode == StorageMode.Blob, "Invalid storage mode"); if (mode == StorageMode.Uninitialized) { storageModes[key] = StorageMode.Blob; @@ -109,8 +103,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function readChunk(bytes memory name, uint256 chunkId) public view virtual override returns (bytes memory, bool) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _getChunkFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { @@ -120,8 +113,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function chunkSize(bytes memory name, uint256 chunkId) public view virtual override returns (uint256, bool) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _chunkSizeFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { @@ -131,8 +123,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function removeChunk(bytes memory name, uint256 chunkId) public virtual onlyOwner override returns (bool) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _removeChunkFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { @@ -142,8 +133,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function truncate(bytes memory name, uint256 chunkId) public virtual onlyOwner override returns (uint256) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _removeFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { @@ -161,8 +151,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function getChunkHash(bytes memory name, uint256 chunkId) public override view returns (bytes32) { - bytes32 key = keccak256(name); - StorageMode mode = storageModes[key]; + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { return _getChunkHashFromBlob(key, chunkId); } else if (mode == StorageMode.OnChain) { @@ -191,9 +180,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } function getUploadInfo(bytes memory name) public override view returns (StorageMode mode, uint256 count, uint256 payment) { - bytes32 key = keccak256(name); - mode = storageModes[key]; - + (bytes32 key, StorageMode mode) = _getModeAndKey(name); if (mode == StorageMode.Blob) { count = _countChunksFromBlob(key); } else if (mode == StorageMode.OnChain) { @@ -203,5 +190,11 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { } payment = address(storageContract) != address(0) ? upfrontPayment() : 0; + return (mode, count, payment); + } + + function _getModeAndKey(bytes memory name) private view returns (bytes32 key, StorageMode mode) { + key = keccak256(name); + mode = storageModes[key]; } } From 80a70112d5ca28831b4d9c20841974123d266a1c Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 12 Sep 2024 16:25:59 +0800 Subject: [PATCH 8/9] add new interface --- contracts/ERC5018.sol | 25 +++++++++++++++++-------- contracts/IERC5018.sol | 6 ++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index bb7e2c3..7fea677 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -161,7 +161,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return 0; } - function getBatchChunkHashes(FileChunk[] memory fileChunks) external view returns (bytes32[] memory) { + function getChunkHashesBatch(FileChunk[] memory fileChunks) external view returns (bytes32[] memory) { uint totalChunks = 0; for (uint i = 0; i < fileChunks.length; i++) { @@ -179,18 +179,27 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return hashes; } - function getUploadInfo(bytes memory name) public override view returns (StorageMode mode, uint256 count, uint256 payment) { - (bytes32 key, StorageMode mode) = _getModeAndKey(name); + function getChunkCountsBatch(bytes[] memory names) external view returns (uint256[] memory) { + uint256[] memory counts = new uint256[](names.length); + for (uint i = 0; i < names.length; i++) { + counts[i] = countChunks(names[i]); + } + return counts; + } + + function getUploadDetails(bytes memory name) public override view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost) { + bytes32 key; + (key, mode) = _getModeAndKey(name); + if (mode == StorageMode.Blob) { - count = _countChunksFromBlob(key); + chunkCount = _countChunksFromBlob(key); } else if (mode == StorageMode.OnChain) { - count = _countChunks(key); + chunkCount = _countChunks(key); } else { - count = 0; + chunkCount = 0; } - payment = address(storageContract) != address(0) ? upfrontPayment() : 0; - return (mode, count, payment); + storageCost = address(storageContract) != address(0) ? upfrontPayment() : 0; } function _getModeAndKey(bytes memory name) private view returns (bytes32 key, StorageMode mode) { diff --git a/contracts/IERC5018.sol b/contracts/IERC5018.sol index 1c7e582..0308ccf 100644 --- a/contracts/IERC5018.sol +++ b/contracts/IERC5018.sol @@ -48,7 +48,9 @@ interface IERC5018 { function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32); - function getBatchChunkHashes(FileChunk[] memory fileChunks) external view returns (bytes32[] memory); + function getChunkHashesBatch(FileChunk[] memory fileChunks) external view returns (bytes32[] memory); - function getUploadInfo(bytes memory name) external view returns (StorageMode mode, uint256 count, uint256 payment); + function getChunkCountsBatch(bytes[] memory names) external view returns (uint256[] memory); + + function getUploadDetails(bytes memory name) external view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost); } From 6db82d301a5a3af25ac2d29f41e2282d80c1eeae Mon Sep 17 00:00:00 2001 From: iteye Date: Tue, 24 Sep 2024 18:23:48 +0800 Subject: [PATCH 9/9] change method name --- contracts/ERC5018.sol | 2 +- contracts/IERC5018.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/ERC5018.sol b/contracts/ERC5018.sol index 7fea677..f015107 100644 --- a/contracts/ERC5018.sol +++ b/contracts/ERC5018.sol @@ -187,7 +187,7 @@ contract ERC5018 is LargeStorageManager, BlobStorageManager, IERC5018, ISemver { return counts; } - function getUploadDetails(bytes memory name) public override view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost) { + function getUploadInfo(bytes memory name) public override view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost) { bytes32 key; (key, mode) = _getModeAndKey(name); diff --git a/contracts/IERC5018.sol b/contracts/IERC5018.sol index 0308ccf..c1d87c1 100644 --- a/contracts/IERC5018.sol +++ b/contracts/IERC5018.sol @@ -52,5 +52,5 @@ interface IERC5018 { function getChunkCountsBatch(bytes[] memory names) external view returns (uint256[] memory); - function getUploadDetails(bytes memory name) external view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost); + function getUploadInfo(bytes memory name) external view returns (StorageMode mode, uint256 chunkCount, uint256 storageCost); }