Skip to content

Commit

Permalink
reuse _prepareAppend
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhodl committed Jul 23, 2024
1 parent 3583685 commit 3aec460
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
12 changes: 3 additions & 9 deletions contracts/DecentralizedKV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,7 @@ contract DecentralizedKV is OwnableUpgradeable {
}

/// @notice Checks before appending the key-value.
function _prepareAppend() internal virtual {
require(msg.value >= upfrontPayment(), "DecentralizedKV: not enough payment");
}

/// @notice Checks before batch appending the key-value.
/// @param _batchSize The size of the batch.
function _prepareBatchAppend(uint256 _batchSize) internal virtual {
function _prepareAppend(uint256 _batchSize) internal virtual {
require(msg.value >= upfrontPayment() * _batchSize, "DecentralizedKV: not enough batch payment");
}

Expand All @@ -137,7 +131,7 @@ contract DecentralizedKV is OwnableUpgradeable {

if (paddr.hash == 0) {
// append (require payment from sender)
_prepareAppend();
_prepareAppend(1);
paddr.kvIdx = kvEntryCount;
idxMap[paddr.kvIdx] = skey;
kvEntryCount = kvEntryCount + 1;
Expand Down Expand Up @@ -175,7 +169,7 @@ contract DecentralizedKV is OwnableUpgradeable {
res[i] = paddr.kvIdx;
}

_prepareBatchAppend(batchPaymentSize);
_prepareAppend(batchPaymentSize);

return res;
}
Expand Down
25 changes: 25 additions & 0 deletions contracts/StorageContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ abstract contract StorageContract is DecentralizedKV {
/// @notice People can sent ETH to the contract.
function sendValue() public payable {}

/// @notice Checks the payment using the last mine time.
function _prepareAppendWithTimestamp(uint256 _timestamp, uint256 _batchSize) internal {
uint256 totalEntries = kvEntryCount + 1; // include the one to be put
uint256 shardId = kvEntryCount >> SHARD_ENTRY_BITS; // shard id of the new KV
if ((totalEntries % (1 << SHARD_ENTRY_BITS)) == 1) {
// Open a new shard if the KV is the first one of the shard
// and mark the shard is ready to mine.
// (TODO): Setup shard difficulty as current difficulty / factor?
if (shardId != 0) {
// shard0 is already opened in constructor
infos[shardId].lastMineTime = _timestamp;
}
}

require(
msg.value >= _upfrontPayment(infos[shardId].lastMineTime) * _batchSize,
"StorageContract: not enough batch payment"
);
}

/// @notice Upfront payment for the next insertion
function upfrontPayment() public view virtual override returns (uint256) {
uint256 totalEntries = kvEntryCount + 1; // include the one to be put
Expand All @@ -160,6 +180,11 @@ abstract contract StorageContract is DecentralizedKV {
}
}

/// @inheritdoc DecentralizedKV
function _prepareAppend(uint256 _batchSize) internal virtual override {
return _prepareAppendWithTimestamp(block.timestamp, _batchSize);
}

/// @notice Verify the samples of the BLOBs by the miner (storage provider) including
/// - decode the samples
/// - check the inclusive of the samples
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/EthStorageContractTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract EthStorageContractTest is Test {
uint256 insufficientCost = storageContract.upfrontPayment() - 1;

// Expect the specific revert reason from _prepareAppend due to insufficient msg.value
vm.expectRevert("DecentralizedKV: not enough payment");
vm.expectRevert("StorageContract: not enough batch payment");
storageContract.putBlob{value: insufficientCost}(key, blobIdx, length);

// Enough storage cost
Expand Down Expand Up @@ -71,7 +71,7 @@ contract EthStorageContractTest is Test {
uint256 insufficientCost = storageContract.upfrontPayment();

// Expect the specific revert reason from _prepareBatchAppend due to insufficient msg.value
vm.expectRevert("DecentralizedKV: not enough batch payment");
vm.expectRevert("StorageContract: not enough batch payment");
storageContract.putBlobs{value: insufficientCost}(keys, blobIdxs, lengths);

// Enough storage cost
Expand Down
8 changes: 4 additions & 4 deletions test/decentralized-kv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ describe("DecentralizedKV Test", function () {
await kv.initialize(ownerAddr);

expect(await kv.upfrontPayment()).to.equal("1000000000000000000");
await expect(kv.put(key1, "0x11223344")).to.be.revertedWith("DecentralizedKV: not enough payment");
await expect(kv.put(key1, "0x11223344")).to.be.revertedWith("DecentralizedKV: not enough batch payment");
await expect(
kv.put(key1, "0x11223344", {
value: "900000000000000000",
})
).to.be.revertedWith("DecentralizedKV: not enough payment");
).to.be.revertedWith("DecentralizedKV: not enough batch payment");
await kv.put(key1, "0x11223344", {
value: ethers.utils.parseEther("1.0"),
});
Expand Down Expand Up @@ -91,12 +91,12 @@ describe("DecentralizedKV Test", function () {
await kv.initialize(ownerAddr);

expect(await kv.upfrontPayment()).to.equal("1000000000000000000");
await expect(kv.put(key1, "0x11223344")).to.be.revertedWith("DecentralizedKV: not enough payment");
await expect(kv.put(key1, "0x11223344")).to.be.revertedWith("DecentralizedKV: not enough batch payment");
await expect(
kv.put(key1, "0x11223344", {
value: "900000000000000000",
})
).to.be.revertedWith("DecentralizedKV: not enough payment");
).to.be.revertedWith("DecentralizedKV: not enough batch payment");
await kv.put(key1, "0x11223344", {
value: ethers.utils.parseEther("1.0"),
});
Expand Down

0 comments on commit 3aec460

Please sign in to comment.