Merge branch 'main' of github.com:cyhhao/git3-contract

main
cyhhao 2 years ago
commit 494c06a8fa

@ -4,11 +4,9 @@ pragma solidity ^0.8.0;
import "hardhat/console.sol"; import "hardhat/console.sol";
import "./IFileOperator.sol"; import "./IFileOperator.sol";
import "@openzeppelin/contracts/access/Ownable.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 LargeStorageManagerV2 {
contract Git3 is LargeStorageManager {
struct refInfo { struct refInfo {
bytes20 hash; bytes20 hash;
uint96 index; uint96 index;
@ -31,7 +29,7 @@ contract Git3 is LargeStorageManager {
res.name = repoNameToRefs[repoName][info.index]; res.name = repoNameToRefs[repoName][info.index];
} }
constructor() LargeStorageManager(0) {} constructor() LargeStorageManagerV2(0) {}
modifier onlyOwner(bytes memory repoName) { modifier onlyOwner(bytes memory repoName) {
require(repoNameToOwner[repoName] == msg.sender, "only owner"); require(repoNameToOwner[repoName] == msg.sender, "only owner");
@ -87,6 +85,15 @@ contract Git3 is LargeStorageManager {
return sTokens; 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( function download(
bytes memory repoName, bytes memory repoName,
bytes memory path bytes memory path
@ -130,6 +137,14 @@ contract Git3 is LargeStorageManager {
_remove(keccak256(bytes.concat(repoName, "/", path)), 0); _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( function size(
bytes memory repoName, bytes memory repoName,
bytes memory name bytes memory name
@ -146,7 +161,7 @@ contract Git3 is LargeStorageManager {
function listRefs( function listRefs(
bytes memory repoName bytes memory repoName
) public view returns (refData[] memory list) { ) external view returns (refData[] memory list) {
list = new refData[](repoNameToRefs[repoName].length); list = new refData[](repoNameToRefs[repoName].length);
for (uint index = 0; index < repoNameToRefs[repoName].length; index++) { for (uint index = 0; index < repoNameToRefs[repoName].length; index++) {
list[index] = _convertRefInfo( list[index] = _convertRefInfo(
@ -160,7 +175,7 @@ contract Git3 is LargeStorageManager {
bytes memory repoName, bytes memory repoName,
bytes memory name, bytes memory name,
bytes20 refHash bytes20 refHash
) public onlyOwner(repoName) { ) external onlyOwner(repoName) {
bytes memory fullName = bytes.concat(repoName, "/", name); bytes memory fullName = bytes.concat(repoName, "/", name);
// only execute `sload` once to reduce gas consumption // only execute `sload` once to reduce gas consumption
refInfo memory srs; refInfo memory srs;
@ -187,7 +202,7 @@ contract Git3 is LargeStorageManager {
function delRef( function delRef(
bytes memory repoName, bytes memory repoName,
bytes memory name bytes memory name
) public onlyOwner(repoName) { ) external onlyOwner(repoName) {
bytes memory fullName = bytes.concat(repoName, "/", name); bytes memory fullName = bytes.concat(repoName, "/", name);
// only execute `sload` once to reduce gas consumption // only execute `sload` once to reduce gas consumption
refInfo memory srs; refInfo memory srs;

25
package-lock.json generated

@ -14,8 +14,7 @@
"@nomiclabs/hardhat-etherscan": "^3.1.3", "@nomiclabs/hardhat-etherscan": "^3.1.3",
"@openzeppelin/contracts": "^4.8.0", "@openzeppelin/contracts": "^4.8.0",
"chai": "^4.3.7", "chai": "^4.3.7",
"evm-large-storage": "^1.0.0", "git3-evm-large-storage": "^1.1.0",
"git3-evm-large-storage": "^1.0.2",
"typechain": "^8.1.1" "typechain": "^8.1.1"
}, },
"devDependencies": { "devDependencies": {
@ -3834,11 +3833,6 @@
"node": ">=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": { "node_modules/evp_bytestokey": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz",
@ -4171,9 +4165,9 @@
} }
}, },
"node_modules/git3-evm-large-storage": { "node_modules/git3-evm-large-storage": {
"version": "1.0.2", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.2.tgz", "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.1.0.tgz",
"integrity": "sha512-jT9r3WyyEdWtX1DhP6/PKCtxSliY8R9niFrh02BvhZkakwF6wAgxDAiXaVQpgqJVcHMSk8Eek5x0fwAdufDK6A==" "integrity": "sha512-nCRP5Wr2+ecHFdoMBtuJKbM99Y7F54dOJvB2HPuiajOP768gesVwr3yD5lJoZ0fEjT2VU0uES+0gD9U8KpwukQ=="
}, },
"node_modules/glob": { "node_modules/glob": {
"version": "7.2.0", "version": "7.2.0",
@ -11369,11 +11363,6 @@
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" "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": { "evp_bytestokey": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz",
@ -11622,9 +11611,9 @@
} }
}, },
"git3-evm-large-storage": { "git3-evm-large-storage": {
"version": "1.0.2", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.2.tgz", "resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.1.0.tgz",
"integrity": "sha512-jT9r3WyyEdWtX1DhP6/PKCtxSliY8R9niFrh02BvhZkakwF6wAgxDAiXaVQpgqJVcHMSk8Eek5x0fwAdufDK6A==" "integrity": "sha512-nCRP5Wr2+ecHFdoMBtuJKbM99Y7F54dOJvB2HPuiajOP768gesVwr3yD5lJoZ0fEjT2VU0uES+0gD9U8KpwukQ=="
}, },
"glob": { "glob": {
"version": "7.2.0", "version": "7.2.0",

@ -36,8 +36,7 @@
"@nomiclabs/hardhat-etherscan": "^3.1.3", "@nomiclabs/hardhat-etherscan": "^3.1.3",
"@openzeppelin/contracts": "^4.8.0", "@openzeppelin/contracts": "^4.8.0",
"chai": "^4.3.7", "chai": "^4.3.7",
"evm-large-storage": "^1.0.0", "git3-evm-large-storage": "^1.1.0",
"git3-evm-large-storage": "^1.0.2",
"typechain": "^8.1.1" "typechain": "^8.1.1"
} }
} }

@ -2,6 +2,7 @@ const { web3 } = require("hardhat");
const { expect } = require("chai"); const { expect } = require("chai");
const { ethers } = require("hardhat"); const { ethers } = require("hardhat");
const { defaultAbiCoder } = require("ethers/lib/utils"); const { defaultAbiCoder } = require("ethers/lib/utils");
const { isConstructorDeclaration } = require("typescript");
var ToBig = (x) => ethers.BigNumber.from(x); var ToBig = (x) => ethers.BigNumber.from(x);
let ETH = ethers.BigNumber.from(10).pow(18); let ETH = ethers.BigNumber.from(10).pow(18);
@ -244,4 +245,36 @@ describe("Git3 Test", function () {
expect(actualStakeNum1).to.equal(ToBig(0)); expect(actualStakeNum1).to.equal(ToBig(0));
expect(actualStakeNum2).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