From d9984737c5601d8e80de8571c026d5948f5d79be Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Thu, 15 Dec 2022 23:48:09 +0800 Subject: [PATCH 1/2] feat: refund user directly --- contracts/Git3.sol | 31 +++++++++++++++++++++++-------- package-lock.json | 25 +++++++------------------ package.json | 3 +-- test/git3-test.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/contracts/Git3.sol b/contracts/Git3.sol index cc2d5e7..60c825f 100644 --- a/contracts/Git3.sol +++ b/contracts/Git3.sol @@ -4,11 +4,9 @@ pragma solidity ^0.8.0; import "hardhat/console.sol"; import "./IFileOperator.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; -import "git3-evm-large-storage/contracts/LargeStorageManager.sol"; +import "git3-evm-large-storage/contracts/v2/LargeStorageManagerV2.sol"; -// import "evm-large-storage/contracts/W3RC3.sol"; - -contract Git3 is LargeStorageManager { +contract Git3 is LargeStorageManagerV2 { struct refInfo { bytes20 hash; uint96 index; @@ -31,7 +29,7 @@ contract Git3 is LargeStorageManager { res.name = repoNameToRefs[repoName][info.index]; } - constructor() LargeStorageManager(0) {} + constructor() LargeStorageManagerV2(0) {} modifier onlyOwner(bytes memory repoName) { require(repoNameToOwner[repoName] == msg.sender, "only owner"); @@ -56,6 +54,15 @@ contract Git3 is LargeStorageManager { return sTokens; } + function getChunkAddr( + bytes memory repoName, + bytes memory path, + uint256 chunkId + ) external view returns (address) { + bytes memory fullPath = bytes.concat(repoName, "/", path); + return _getChunkAddr(keccak256(fullPath), chunkId); + } + function download( bytes memory repoName, bytes memory path @@ -91,7 +98,7 @@ contract Git3 is LargeStorageManager { bytes memory repoName, bytes memory path, bytes calldata data - ) external payable onlyOwner(repoName) { + ) public payable onlyOwner(repoName) { _putChunkFromCalldata( keccak256(bytes.concat(repoName, "/", path)), 0, @@ -105,7 +112,7 @@ contract Git3 is LargeStorageManager { bytes memory path, uint256 chunkId, bytes calldata data - ) external payable onlyOwner(repoName) { + ) public payable onlyOwner(repoName) { _putChunkFromCalldata( keccak256(bytes.concat(repoName, "/", path)), chunkId, @@ -117,11 +124,19 @@ contract Git3 is LargeStorageManager { function remove( bytes memory repoName, bytes memory path - ) external onlyOwner(repoName) { + ) public onlyOwner(repoName) { // The actually process of remove will remove all the chunks _remove(keccak256(bytes.concat(repoName, "/", path)), 0); } + function removeChunk( + bytes memory repoName, + bytes memory path, + uint256 chunkId + ) public onlyOwner(repoName) { + _removeChunk(keccak256(bytes.concat(repoName, "/", path)), chunkId); + } + function size( bytes memory repoName, bytes memory name diff --git a/package-lock.json b/package-lock.json index cea132c..c445360 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,8 +14,7 @@ "@nomiclabs/hardhat-etherscan": "^3.1.3", "@openzeppelin/contracts": "^4.8.0", "chai": "^4.3.7", - "evm-large-storage": "^1.0.0", - "git3-evm-large-storage": "^1.0.2", + "git3-evm-large-storage": "^1.1.0", "typechain": "^8.1.1" }, "devDependencies": { @@ -3834,11 +3833,6 @@ "node": ">=6" } }, - "node_modules/evm-large-storage": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/evm-large-storage/-/evm-large-storage-1.0.0.tgz", - "integrity": "sha512-xvyWdIZFVOy+64UO801atjtF6XT7BnFKdkF/Pj8yRk2n63Qm8W5zl+XaHVN7nyG/SDRBY1KLErWYZpnEBGTOcA==" - }, "node_modules/evp_bytestokey": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", @@ -4171,9 +4165,9 @@ } }, "node_modules/git3-evm-large-storage": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.2.tgz", - "integrity": "sha512-jT9r3WyyEdWtX1DhP6/PKCtxSliY8R9niFrh02BvhZkakwF6wAgxDAiXaVQpgqJVcHMSk8Eek5x0fwAdufDK6A==" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.1.0.tgz", + "integrity": "sha512-nCRP5Wr2+ecHFdoMBtuJKbM99Y7F54dOJvB2HPuiajOP768gesVwr3yD5lJoZ0fEjT2VU0uES+0gD9U8KpwukQ==" }, "node_modules/glob": { "version": "7.2.0", @@ -11369,11 +11363,6 @@ "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" }, - "evm-large-storage": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/evm-large-storage/-/evm-large-storage-1.0.0.tgz", - "integrity": "sha512-xvyWdIZFVOy+64UO801atjtF6XT7BnFKdkF/Pj8yRk2n63Qm8W5zl+XaHVN7nyG/SDRBY1KLErWYZpnEBGTOcA==" - }, "evp_bytestokey": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", @@ -11622,9 +11611,9 @@ } }, "git3-evm-large-storage": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.2.tgz", - "integrity": "sha512-jT9r3WyyEdWtX1DhP6/PKCtxSliY8R9niFrh02BvhZkakwF6wAgxDAiXaVQpgqJVcHMSk8Eek5x0fwAdufDK6A==" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.1.0.tgz", + "integrity": "sha512-nCRP5Wr2+ecHFdoMBtuJKbM99Y7F54dOJvB2HPuiajOP768gesVwr3yD5lJoZ0fEjT2VU0uES+0gD9U8KpwukQ==" }, "glob": { "version": "7.2.0", diff --git a/package.json b/package.json index 5d6caa0..03a8547 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,7 @@ "@nomiclabs/hardhat-etherscan": "^3.1.3", "@openzeppelin/contracts": "^4.8.0", "chai": "^4.3.7", - "evm-large-storage": "^1.0.0", - "git3-evm-large-storage": "^1.0.2", + "git3-evm-large-storage": "^1.1.0", "typechain": "^8.1.1" } } diff --git a/test/git3-test.js b/test/git3-test.js index 7cdac86..4529c72 100644 --- a/test/git3-test.js +++ b/test/git3-test.js @@ -2,6 +2,7 @@ const { web3 } = require("hardhat"); const { expect } = require("chai"); const { ethers } = require("hardhat"); const { defaultAbiCoder } = require("ethers/lib/utils"); +const { isConstructorDeclaration } = require("typescript"); var ToBig = (x) => ethers.BigNumber.from(x); let ETH = ethers.BigNumber.from(10).pow(18); @@ -244,4 +245,37 @@ describe("Git3 Test", function () { expect(actualStakeNum1).to.equal(ToBig(0)); expect(actualStakeNum2).to.equal(ToBig(0)); }); + + it("Refund to user directly after removing chunk", async function () { + const Git3 = await ethers.getContractFactory("Git3"); + const git3 = await Git3.deploy(); + await git3.deployed(); + + let signer; + [signer] = await ethers.getSigners(); + + const repoName = Buffer.from("test"); + await git3.createRepo(repoName); + + stakeNum1 = ETH; + stakeNum2 = ToBig(2).mul(ETH); + + let data0 = Array.from({ length: 2 }, () => + Math.floor(Math.random() * 256) + ); + + await git3.connect(signer).uploadChunk(repoName, "0x616263", 0, data0, { + value: stakeNum1, + }); + + // check that the stake numer of chunk after removing chunks + let balBefore = await signer.getBalance(); + console.log("balBefore:", balBefore); + let tx1 = await git3.removeChunk(repoName, "0x616263", 0); // should succeed + let rec1 = await tx1.wait(); + let removeTxCost = rec1.gasUsed.mul(rec1.effectiveGasPrice); + let balAfter = await signer.getBalance(); + // check balance after refunding + expect(balBefore.add(stakeNum1).sub(removeTxCost)).to.eq(balAfter); + }); }); From ac0f50e45a2144a45aef524e34381c50a7915a3d Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Thu, 15 Dec 2022 23:51:36 +0800 Subject: [PATCH 2/2] fix: public => external --- contracts/Git3.sol | 14 +++++++------- test/git3-test.js | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/contracts/Git3.sol b/contracts/Git3.sol index 60c825f..3dbe9b6 100644 --- a/contracts/Git3.sol +++ b/contracts/Git3.sol @@ -98,7 +98,7 @@ contract Git3 is LargeStorageManagerV2 { bytes memory repoName, bytes memory path, bytes calldata data - ) public payable onlyOwner(repoName) { + ) external payable onlyOwner(repoName) { _putChunkFromCalldata( keccak256(bytes.concat(repoName, "/", path)), 0, @@ -112,7 +112,7 @@ contract Git3 is LargeStorageManagerV2 { bytes memory path, uint256 chunkId, bytes calldata data - ) public payable onlyOwner(repoName) { + ) external payable onlyOwner(repoName) { _putChunkFromCalldata( keccak256(bytes.concat(repoName, "/", path)), chunkId, @@ -124,7 +124,7 @@ contract Git3 is LargeStorageManagerV2 { function remove( bytes memory repoName, bytes memory path - ) public onlyOwner(repoName) { + ) external onlyOwner(repoName) { // The actually process of remove will remove all the chunks _remove(keccak256(bytes.concat(repoName, "/", path)), 0); } @@ -133,7 +133,7 @@ contract Git3 is LargeStorageManagerV2 { bytes memory repoName, bytes memory path, uint256 chunkId - ) public onlyOwner(repoName) { + ) external onlyOwner(repoName) { _removeChunk(keccak256(bytes.concat(repoName, "/", path)), chunkId); } @@ -153,7 +153,7 @@ contract Git3 is LargeStorageManagerV2 { function listRefs( bytes memory repoName - ) public view returns (refData[] memory list) { + ) external view returns (refData[] memory list) { list = new refData[](repoNameToRefs[repoName].length); for (uint index = 0; index < repoNameToRefs[repoName].length; index++) { list[index] = _convertRefInfo( @@ -167,7 +167,7 @@ contract Git3 is LargeStorageManagerV2 { bytes memory repoName, bytes memory name, bytes20 refHash - ) public onlyOwner(repoName) { + ) external onlyOwner(repoName) { bytes memory fullName = bytes.concat(repoName, "/", name); // only execute `sload` once to reduce gas consumption refInfo memory srs; @@ -194,7 +194,7 @@ contract Git3 is LargeStorageManagerV2 { function delRef( bytes memory repoName, bytes memory name - ) public onlyOwner(repoName) { + ) external onlyOwner(repoName) { bytes memory fullName = bytes.concat(repoName, "/", name); // only execute `sload` once to reduce gas consumption refInfo memory srs; diff --git a/test/git3-test.js b/test/git3-test.js index 4529c72..2931dae 100644 --- a/test/git3-test.js +++ b/test/git3-test.js @@ -270,7 +270,6 @@ describe("Git3 Test", function () { // check that the stake numer of chunk after removing chunks let balBefore = await signer.getBalance(); - console.log("balBefore:", balBefore); let tx1 = await git3.removeChunk(repoName, "0x616263", 0); // should succeed let rec1 = await tx1.wait(); let removeTxCost = rec1.gasUsed.mul(rec1.effectiveGasPrice);