From fef754f7ebe64ee65b7f5adbb74cbb9c5ee10c04 Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 22 Feb 2024 11:07:58 +0800 Subject: [PATCH 1/6] support op blob decode --- contracts/DecentralizedKV.sol | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index daf9bb1..903e332 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -10,7 +10,8 @@ contract DecentralizedKV is OwnableUpgradeable { enum DecodeType { RawData, - PaddingPer31Bytes + PaddingPer31Bytes, + OptimismCompactBlob } uint256 public storageCost; // Upfront storage cost (pre-dcf) @@ -122,22 +123,27 @@ contract DecentralizedKV is OwnableUpgradeable { uint256 len ) public view virtual returns (bytes memory) { require(len > 0, "data len should be non zero"); - - bytes32 skey = keccak256(abi.encode(msg.sender, key)); - PhyAddr memory paddr = kvMap[skey]; - require(paddr.hash != 0, "data not exist"); - if (decodeType == DecodeType.PaddingPer31Bytes) { + if (decodeType == DecodeType.OptimismCompactBlob) { // kvSize is the actual data size that dApp contract stores - require((paddr.kvSize >= off + len) && (off + len <= maxKvSize - 4096), "beyond the range of kvSize"); - } else { + require(off + len <= (4 * 31 + 3) * 1024 - 4, "the size exceeds the storage capacity of the OP blob"); + } else if (decodeType == DecodeType.PaddingPer31Bytes) { + // kvSize is the actual data size that dApp contract stores + require(off + len <= maxKvSize - 4096, "the size exceeds the storage capacity of the blob"); + } else if (decodeType == DecodeType.RawData) { // maxKvSize is blob size require(maxKvSize >= off + len, "beyond the range of maxKvSize"); + } else { + require(false, "invalid decode mode"); } + + bytes32 skey = keccak256(abi.encode(msg.sender, key)); + PhyAddr memory paddr = kvMap[skey]; + require(paddr.hash != 0, "data not exist"); + bytes memory input = abi.encode(paddr.kvIdx, decodeType, off, len, paddr.hash); bytes memory output = new bytes(len); uint256 retSize = 0; - assembly { if iszero(staticcall(not(0), 0x33301, add(input, 0x20), 0xa0, add(output, 0x20), len)) { revert(0, 0) From 6b00eb2a0e24641d09162f1ee281a13e68630ac2 Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 16 May 2024 11:32:17 +0800 Subject: [PATCH 2/6] change check size --- contracts/DecentralizedKV.sol | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index 081ea49..d37b245 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -12,7 +12,7 @@ contract DecentralizedKV is OwnableUpgradeable, EthStorageConstants { enum DecodeType { RawData, PaddingPer31Bytes, - OptimismCompactBlob + OptimismCompact } uint40 public lastKvIdx; // number of entries in the store @@ -114,23 +114,23 @@ contract DecentralizedKV is OwnableUpgradeable, EthStorageConstants { uint256 len ) public view virtual returns (bytes memory) { require(len > 0, "data len should be non zero"); - if (decodeType == DecodeType.OptimismCompactBlob) { + + bytes32 skey = keccak256(abi.encode(msg.sender, key)); + PhyAddr memory paddr = kvMap[skey]; + require(paddr.hash != 0, "data not exist"); + if (decodeType == DecodeType.OptimismCompact) { // kvSize is the actual data size that dApp contract stores - require(off + len <= (4 * 31 + 3) * 1024 - 4, "the size exceeds the storage capacity of the OP blob"); + uint256 size = (4 * 31 + 3) * 1024 - 4 > paddr.kvSize ? paddr.kvSize : (4 * 31 + 3) * 1024 - 4; + require(off + len <= size, "beyond the range of kvSize"); } else if (decodeType == DecodeType.PaddingPer31Bytes) { // kvSize is the actual data size that dApp contract stores - require(off + len <= maxKvSize - 4096, "the size exceeds the storage capacity of the blob"); - } else if (decodeType == DecodeType.RawData) { + uint256 size = maxKvSize - 4096 > paddr.kvSize ? paddr.kvSize : maxKvSize - 4096; + require(off + len <= size, "beyond the range of kvSize"); + } else { // maxKvSize is blob size require(maxKvSize >= off + len, "beyond the range of maxKvSize"); - } else { - require(false, "invalid decode mode"); } - bytes32 skey = keccak256(abi.encode(msg.sender, key)); - PhyAddr memory paddr = kvMap[skey]; - require(paddr.hash != 0, "data not exist"); - bytes memory input = abi.encode(paddr.kvIdx, decodeType, off, len, paddr.hash); bytes memory output = new bytes(len); From 3ccb30e9902954e6a6b112bf573e8054afcc4630 Mon Sep 17 00:00:00 2001 From: iteye Date: Wed, 10 Jul 2024 17:28:21 +0800 Subject: [PATCH 3/6] fix merge --- contracts/DecentralizedKV.sol | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index c8f3f0e..c25d789 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -176,12 +176,16 @@ contract DecentralizedKV is OwnableUpgradeable { require(paddr.hash != 0, "DecentralizedKV: data not exist"); if (_decodeType == DecodeType.OptimismCompact) { // kvSize is the actual data size that dApp contract stores - uint256 size = (4 * 31 + 3) * 1024 - 4 > paddr.kvSize ? paddr.kvSize : (4 * 31 + 3) * 1024 - 4; - require(_off + _len <= size, "DecentralizedKV: beyond the range of kvSize"); + require( + (paddr.kvSize >= _off + _len) && (_off + _len <= (4 * 31 + 3) * 1024 - 4), + "DecentralizedKV: beyond the range of kvSize" + ); } else if (_decodeType == DecodeType.PaddingPer31Bytes) { // kvSize is the actual data size that dApp contract stores - uint256 size = maxKvSize - 4096 > paddr.kvSize ? paddr.kvSize : MAX_KV_SIZE - 4096; - require(_off + _len <= size, "DecentralizedKV: beyond the range of kvSize"); + require( + (paddr.kvSize >= _off + _len) && (_off + _len <= MAX_KV_SIZE - 4096), + "DecentralizedKV: beyond the range of kvSize" + ); } else { // maxKvSize is blob size require(MAX_KV_SIZE >= _off + _len, "DecentralizedKV: beyond the range of maxKvSize"); From 2dc8c76600a9be10bbb8f273dc3afcdf702a8431 Mon Sep 17 00:00:00 2001 From: iteye Date: Wed, 10 Jul 2024 17:43:39 +0800 Subject: [PATCH 4/6] fix commit --- contracts/DecentralizedKV.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index c25d789..cafb341 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -176,6 +176,8 @@ contract DecentralizedKV is OwnableUpgradeable { require(paddr.hash != 0, "DecentralizedKV: data not exist"); if (_decodeType == DecodeType.OptimismCompact) { // kvSize is the actual data size that dApp contract stores + // (4*31+3)*1024 - 4 is the maximum value of optimization blob storage content. It can store 3068 bytes more data than standard blob. + // https://github.com/ethereum-optimism/optimism/blob/develop/op-service/eth/blob.go#L16 require( (paddr.kvSize >= _off + _len) && (_off + _len <= (4 * 31 + 3) * 1024 - 4), "DecentralizedKV: beyond the range of kvSize" From 20f99f7a0e58b4478d4e195167cd5fbf41eff0aa Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 11 Jul 2024 11:08:58 +0800 Subject: [PATCH 5/6] fix commit --- contracts/DecentralizedKV.sol | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index cafb341..de17c7b 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -30,6 +30,10 @@ contract DecentralizedKV is OwnableUpgradeable { OptimismCompact } + /// @notice The maximum value of optimization blob storage content. It can store 3068 bytes more data than standard blob. + /// https://github.com/ethereum-optimism/optimism/blob/develop/op-service/eth/blob.go#L16 + uint256 internal constant MAX_OPTIMISM_BLOB_DATA_SIZE = (4 * 31 + 3) * 1024 - 4; + /// @notice Upfront storage cost (pre-dcf) uint256 internal immutable STORAGE_COST; @@ -179,7 +183,7 @@ contract DecentralizedKV is OwnableUpgradeable { // (4*31+3)*1024 - 4 is the maximum value of optimization blob storage content. It can store 3068 bytes more data than standard blob. // https://github.com/ethereum-optimism/optimism/blob/develop/op-service/eth/blob.go#L16 require( - (paddr.kvSize >= _off + _len) && (_off + _len <= (4 * 31 + 3) * 1024 - 4), + (paddr.kvSize >= _off + _len) && (_off + _len <= MAX_OPTIMISM_BLOB_DATA_SIZE), "DecentralizedKV: beyond the range of kvSize" ); } else if (_decodeType == DecodeType.PaddingPer31Bytes) { From 0dad4d1603ef231fc4a55db9c5cb9bb6c080549f Mon Sep 17 00:00:00 2001 From: iteye Date: Thu, 11 Jul 2024 11:29:20 +0800 Subject: [PATCH 6/6] remove text --- contracts/DecentralizedKV.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/DecentralizedKV.sol b/contracts/DecentralizedKV.sol index de17c7b..d96be0f 100644 --- a/contracts/DecentralizedKV.sol +++ b/contracts/DecentralizedKV.sol @@ -180,8 +180,6 @@ contract DecentralizedKV is OwnableUpgradeable { require(paddr.hash != 0, "DecentralizedKV: data not exist"); if (_decodeType == DecodeType.OptimismCompact) { // kvSize is the actual data size that dApp contract stores - // (4*31+3)*1024 - 4 is the maximum value of optimization blob storage content. It can store 3068 bytes more data than standard blob. - // https://github.com/ethereum-optimism/optimism/blob/develop/op-service/eth/blob.go#L16 require( (paddr.kvSize >= _off + _len) && (_off + _len <= MAX_OPTIMISM_BLOB_DATA_SIZE), "DecentralizedKV: beyond the range of kvSize"