From 3868021ef59b8cff0bf1dc46258c8501ed534926 Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Wed, 14 Dec 2022 22:50:15 +0800 Subject: [PATCH] feat: support to obtain the token pledge amount corresponding to the file --- contracts/Git3.sol | 20 +++++++- package-lock.json | 11 ++++ package.json | 1 + test/git3-test.js | 124 ++++++++++++++++++++++++++++++++++++++------- 4 files changed, 137 insertions(+), 19 deletions(-) diff --git a/contracts/Git3.sol b/contracts/Git3.sol index 498709f..cc2d5e7 100644 --- a/contracts/Git3.sol +++ b/contracts/Git3.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import "hardhat/console.sol"; import "./IFileOperator.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; -import "evm-large-storage/contracts/LargeStorageManager.sol"; +import "git3-evm-large-storage/contracts/LargeStorageManager.sol"; // import "evm-large-storage/contracts/W3RC3.sol"; @@ -38,6 +38,24 @@ contract Git3 is LargeStorageManager { _; } + function stakeTokens( + bytes memory repoName, + bytes memory path + ) external view returns (uint256) { + bytes memory fullPath = bytes.concat(repoName, "/", path); + return _stakeTokens(keccak256(fullPath), 0); + } + + function chunkStakeTokens( + bytes memory repoName, + bytes memory path, + uint256 chunkId + ) external view returns (uint256) { + bytes memory fullPath = bytes.concat(repoName, "/", path); + (uint256 sTokens, ) = _chunkStakeTokens(keccak256(fullPath), chunkId); + return sTokens; + } + function download( bytes memory repoName, bytes memory path diff --git a/package-lock.json b/package-lock.json index 4e0936b..cea132c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@openzeppelin/contracts": "^4.8.0", "chai": "^4.3.7", "evm-large-storage": "^1.0.0", + "git3-evm-large-storage": "^1.0.2", "typechain": "^8.1.1" }, "devDependencies": { @@ -4169,6 +4170,11 @@ "testrpc-sc": "index.js" } }, + "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==" + }, "node_modules/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", @@ -11615,6 +11621,11 @@ "node-emoji": "^1.10.0" } }, + "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==" + }, "glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", diff --git a/package.json b/package.json index 848eb38..5d6caa0 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@openzeppelin/contracts": "^4.8.0", "chai": "^4.3.7", "evm-large-storage": "^1.0.0", + "git3-evm-large-storage": "^1.0.2", "typechain": "^8.1.1" } } diff --git a/test/git3-test.js b/test/git3-test.js index c28fb77..7cdac86 100644 --- a/test/git3-test.js +++ b/test/git3-test.js @@ -4,6 +4,7 @@ const { ethers } = require("hardhat"); const { defaultAbiCoder } = require("ethers/lib/utils"); var ToBig = (x) => ethers.BigNumber.from(x); +let ETH = ethers.BigNumber.from(10).pow(18); describe("Git3 Test", function () { it("upload/download/remove", async function () { @@ -18,7 +19,10 @@ describe("Git3 Test", function () { await git3.createRepo(repoName); await git3.upload(repoName, "0x616263", "0x112233"); - expect(await git3.download(repoName, "0x616263")).to.eql(["0x112233", true]); + expect(await git3.download(repoName, "0x616263")).to.eql([ + "0x112233", + true, + ]); let data = Array.from({ length: 40 }, () => Math.floor(Math.random() * 256) @@ -126,34 +130,118 @@ describe("Git3 Test", function () { let singer; let user1; - [singer, user1,] = await ethers.getSigners(); - const repoName = Buffer.from("test") + [singer, user1] = await ethers.getSigners(); + const repoName = Buffer.from("test"); await git3.connect(singer).createRepo(repoName); - await expect(git3.connect(user1).upload(repoName, "0x616263", "0x112233")).to.be.revertedWith("only owner"); - await expect(git3.connect(user1).uploadChunk(repoName, "0x616263", 0, "0x112233")).to.be.revertedWith("only owner"); - await expect(git3.connect(user1).setRef(repoName, "0x616263", "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")).to.be.revertedWith("only owner"); - - await git3.connect(singer).upload(repoName, "0x616263", "0x112233") - expect(await git3.download(repoName, "0x616263")).to.eql(["0x112233", true]); - await git3.connect(singer).setRef(repoName, "0x616263", "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") + await expect( + git3.connect(user1).upload(repoName, "0x616263", "0x112233") + ).to.be.revertedWith("only owner"); + await expect( + git3.connect(user1).uploadChunk(repoName, "0x616263", 0, "0x112233") + ).to.be.revertedWith("only owner"); + await expect( + git3 + .connect(user1) + .setRef( + repoName, + "0x616263", + "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ) + ).to.be.revertedWith("only owner"); + + await git3.connect(singer).upload(repoName, "0x616263", "0x112233"); + expect(await git3.download(repoName, "0x616263")).to.eql([ + "0x112233", + true, + ]); + await git3 + .connect(singer) + .setRef( + repoName, + "0x616263", + "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + ); + + await expect( + git3.connect(user1).remove(repoName, "0x616263") + ).to.be.revertedWith("only owner"); + await expect( + git3.connect(user1).delRef(repoName, "0x616263") + ).to.be.revertedWith("only owner"); + }); - await expect(git3.connect(user1).remove(repoName, "0x616263")).to.be.revertedWith("only owner"); - await expect(git3.connect(user1).delRef(repoName, "0x616263")).to.be.revertedWith("only owner"); + it("RepoName Check", async function () { + const Git3 = await ethers.getContractFactory("Git3"); + const git3 = await Git3.deploy(); + await git3.deployed(); + let repoName = Buffer.from( + "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.-_" + ); + await git3.createRepo(repoName); + await expect(git3.createRepo(repoName)).to.be.revertedWith( + "RepoName already exist" + ); + await expect(git3.createRepo(Buffer.from("a/b"))).to.be.revertedWith( + "RepoName must be alphanumeric or -._" + ); + await expect( + git3.createRepo(Buffer.from("a".repeat(101))) + ).to.be.revertedWith("RepoName length must be 1-100"); }); - it("RepoName Check", async function () { + it("Get the stake number of chunks", async function () { const Git3 = await ethers.getContractFactory("Git3"); const git3 = await Git3.deploy(); await git3.deployed(); - let repoName = Buffer.from("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"); + const repoName = Buffer.from("test"); await git3.createRepo(repoName); - await expect(git3.createRepo(repoName)).to.be.revertedWith("RepoName already exist"); - await expect(git3.createRepo(Buffer.from("a/b"))).to.be.revertedWith("RepoName must be alphanumeric or -._"); - await expect(git3.createRepo(Buffer.from("a".repeat(101)))).to.be.revertedWith("RepoName length must be 1-100"); - }) + stakeNum1 = ETH; + stakeNum2 = ToBig(2).mul(ETH); + + let data0 = Array.from({ length: 1024 }, () => + Math.floor(Math.random() * 256) + ); + + await git3.uploadChunk(repoName, "0x616263", 0, data0, { + value: stakeNum1, + }); + expect(await git3.download(repoName, "0x616263")).to.eql([ + ethers.utils.hexlify(data0), + true, + ]); + + let data1 = Array.from({ length: 1024 }, () => + Math.floor(Math.random() * 256) + ); + await git3.uploadChunk(repoName, "0x616263", 1, data1, { + value: stakeNum2, + }); + expect(await git3.download(repoName, "0x616263")).to.eql([ + ethers.utils.hexlify(data0.concat(data1)), + true, + ]); + + let stakeNum = await git3.stakeTokens(repoName, "0x616263"); + expect(stakeNum).to.equal(stakeNum1.add(stakeNum2)); + + let actualStakeNum1 = await git3.chunkStakeTokens(repoName, "0x616263", 0); + let actualStakeNum2 = await git3.chunkStakeTokens(repoName, "0x616263", 1); + expect(actualStakeNum1).to.equal(stakeNum1); + expect(actualStakeNum2).to.equal(stakeNum2); + + // check that the stake numer of chunk after removing chunks + await git3.remove(repoName, "0x616263"); // should succeed + + stakeNum = await git3.stakeTokens(repoName, "0x616263"); + actualStakeNum1 = await git3.chunkStakeTokens(repoName, "0x616263", 0); + actualStakeNum2 = await git3.chunkStakeTokens(repoName, "0x616263", 1); + expect(stakeNum).to.equal(ToBig(0)); + expect(actualStakeNum1).to.equal(ToBig(0)); + expect(actualStakeNum2).to.equal(ToBig(0)); + }); });