Merge pull request #3 from cyl19970726/dev-refund-user

feat: refund user directly
main
Call White 2 years ago committed by GitHub
commit 85a90ac505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
@ -122,6 +129,14 @@ contract Git3 is LargeStorageManager {
_remove(keccak256(bytes.concat(repoName, "/", path)), 0);
}
function removeChunk(
bytes memory repoName,
bytes memory path,
uint256 chunkId
) external onlyOwner(repoName) {
_removeChunk(keccak256(bytes.concat(repoName, "/", path)), chunkId);
}
function size(
bytes memory repoName,
bytes memory name
@ -138,7 +153,7 @@ contract Git3 is LargeStorageManager {
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(
@ -152,7 +167,7 @@ contract Git3 is LargeStorageManager {
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;
@ -179,7 +194,7 @@ contract Git3 is LargeStorageManager {
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;

25
package-lock.json generated

@ -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",

@ -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"
}
}

@ -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,36 @@ 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();
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);
});
});

Loading…
Cancel
Save