-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
100 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.19; | ||
|
||
import "./TestEthStorageContract.sol"; | ||
import "forge-std/Test.sol"; | ||
import "forge-std/Vm.sol"; | ||
import "forge-std/console.sol"; | ||
import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; | ||
|
||
contract EthStorageContractTest is Test { | ||
uint256 constant STORAGE_COST = 1000; | ||
uint256 constant SHARD_SIZE_BITS = 19; | ||
uint256 constant MAX_KV_SIZE = 17; | ||
uint256 constant PREPAID_AMOUNT = 2 * STORAGE_COST; | ||
|
||
TestEthStorageContract storageContract; | ||
address owner = address(0x1); | ||
|
||
function setUp() public { | ||
TestEthStorageContract imp = new TestEthStorageContract( | ||
StorageContract.Config(MAX_KV_SIZE, SHARD_SIZE_BITS, 2, 0, 0, 0), 0, STORAGE_COST, 0 | ||
); | ||
bytes memory data = abi.encodeWithSelector( | ||
storageContract.initialize.selector, 0, PREPAID_AMOUNT, 0, address(0x1), address(0x1) | ||
); | ||
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(imp), owner, data); | ||
|
||
storageContract = TestEthStorageContract(address(proxy)); | ||
} | ||
|
||
function testPutBlobs() public { | ||
bytes32[] memory keys = new bytes32[](2); | ||
keys[0] = bytes32(uint256(0)); | ||
keys[1] = bytes32(uint256(1)); | ||
uint256[] memory blobIdxs = new uint256[](2); | ||
blobIdxs[0] = 0; | ||
blobIdxs[1] = 0; | ||
uint256[] memory lengths = new uint256[](2); | ||
lengths[0] = 10; | ||
lengths[1] = 20; | ||
|
||
uint256 insufficientCost = storageContract.upfrontPayment(); | ||
|
||
// Expect the specific revert reason from _prepareBatchAppend due to insufficient msg.value | ||
vm.expectRevert("DecentralizedKV: not enough batch payment"); | ||
storageContract.putBlobs{value: insufficientCost}(keys, blobIdxs, lengths); | ||
|
||
// Enough storage cost | ||
uint256 sufficientCost = 2 * storageContract.upfrontPayment(); | ||
storageContract.putBlobs{value: sufficientCost}(keys, blobIdxs, lengths); | ||
|
||
assertEq(storageContract.kvEntryCount(), 2); | ||
assertEq(storageContract.hash(keys[0]), bytes32(uint256(1 << 8 * 8))); | ||
assertEq(storageContract.hash(keys[1]), bytes32(uint256(2 << 8 * 8))); | ||
assertEq(storageContract.size(keys[0]), 10); | ||
assertEq(storageContract.size(keys[1]), 20); | ||
|
||
// Still pass two keys, but only appending a new key-value | ||
lengths[0] = 30; | ||
keys[1] = bytes32(uint256(2)); | ||
lengths[1] = 30; | ||
|
||
sufficientCost = storageContract.upfrontPayment(); | ||
storageContract.putBlobs{value: sufficientCost}(keys, blobIdxs, lengths); | ||
assertEq(storageContract.kvEntryCount(), 3); | ||
assertEq(storageContract.hash(bytes32(uint256(0))), bytes32(uint256(1 << 8 * 8))); | ||
assertEq(storageContract.hash(bytes32(uint256(1))), bytes32(uint256(2 << 8 * 8))); | ||
assertEq(storageContract.hash(bytes32(uint256(2))), bytes32(uint256(2 << 8 * 8))); | ||
assertEq(storageContract.size(bytes32(uint256(0))), 30); | ||
assertEq(storageContract.size(bytes32(uint256(1))), 20); | ||
assertEq(storageContract.size(bytes32(uint256(2))), 30); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters