feat: update contract

main
cyl19970726 2 years ago
parent 9b0f0a5473
commit 94f8daa091

@ -0,0 +1,5 @@
node_modules
artifacts
cache
coverage*
gasReporterOutput.json

@ -0,0 +1,3 @@
{
"printWidth": 120
}

@ -4,11 +4,11 @@ pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "./IFileOperator.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "evm-large-storage/contracts/examples/FlatDirectory.sol";
import "git3-evm-large-storage/contracts/LargeStorageManager.sol";
// import "evm-large-storage/contracts/W3RC3.sol";
contract Git3 {
IFileOperator public immutable storageManager;
contract Git3 is LargeStorageManager {
struct refInfo {
bytes20 hash;
uint96 index;
@ -27,34 +27,32 @@ contract Git3 {
res.name = refs[info.index];
}
constructor() {
storageManager = IFileOperator(address(new FlatDirectory(0)));
constructor() LargeStorageManager(0) {
}
function download(bytes memory path) external view returns (bytes memory, bool) {
// call flat directory(FD)
return storageManager.read(path);
return _get(keccak256(path));
}
function upload(bytes memory path, bytes memory data) external payable {
storageManager.writeChunk(path, 0, data);
function upload(bytes memory path, bytes calldata data) external payable {
_putChunkFromCalldata(keccak256(path), 0, data,msg.value);
}
function uploadChunk(bytes memory path,uint256 chunkId, bytes memory data) external payable {
storageManager.writeChunk(path, chunkId, data);
function uploadChunk(bytes memory path,uint256 chunkId, bytes calldata data) external payable {
_putChunkFromCalldata(keccak256(path), chunkId, data,msg.value);
}
function remove(bytes memory path) external {
// The actually process of remove will remove all the chunks
storageManager.remove(path);
_remove(keccak256(path),0);
}
function size(bytes memory name) external view returns (uint256,uint256){
return storageManager.size(name);
return _size(keccak256(name));
}
function countChunks(bytes memory name)external view returns (uint256){
return storageManager.countChunks(name);
function countChunks(bytes memory path)external view returns (uint256){
return _countChunks(keccak256(path));
}
function listRefs() public view returns (refData[] memory list) {
@ -91,7 +89,7 @@ contract Git3 {
srs = nameToRefInfo[name];
uint256 refsLen = refs.length;
require(srs.hash != bytes20(0),"Reference of this name does not exist");
require(srs.hash != bytes20(0),"The name of reference does not exist");
require(srs.index < refsLen,"System Error: Invalid index");
if (srs.index < refsLen-1){

@ -1,5 +1,3 @@
require("dotenv").config();
import { HardhatUserConfig } from "hardhat/config";
@ -11,8 +9,8 @@ const config: HardhatUserConfig = {
w3qGalileo: {
url: "https://galileo.web3q.io:8545",
accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
}
}
},
},
};
export default config;

20
package-lock.json generated

@ -10,12 +10,14 @@
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^4.8.0",
"evm-large-storage": "^1.0.0"
"evm-large-storage": "^1.0.0",
"git3-evm-large-storage": "^1.0.0"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"dotenv": "^16.0.3",
"hardhat": "^2.12.4"
"hardhat": "^2.12.4",
"prettier": "2.8.1"
}
},
"node_modules/@cspotcode/source-map-support": {
@ -4577,6 +4579,11 @@
"testrpc-sc": "index.js"
}
},
"node_modules/git3-evm-large-storage": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.0.tgz",
"integrity": "sha512-ixye8NwoEiGFb56jTCHXPouqU/Y8Auy4ijnc++L1PPZGHtYF8KR/frdaMn5WtoMV5GqQG/q2pxLxvaHvkX4B7A=="
},
"node_modules/glob": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz",
@ -6513,7 +6520,6 @@
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.1.tgz",
"integrity": "sha512-lqGoSJBQNJidqCHE80vqZJHWHRFoNYsSpP9AjFhlhi9ODCJA541svILes/+/1GM3VaL/abZi7cpFzOpdR9UPKg==",
"dev": true,
"peer": true,
"bin": {
"prettier": "bin-prettier.js"
},
@ -12608,6 +12614,11 @@
"node-emoji": "^1.10.0"
}
},
"git3-evm-large-storage": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/git3-evm-large-storage/-/git3-evm-large-storage-1.0.0.tgz",
"integrity": "sha512-ixye8NwoEiGFb56jTCHXPouqU/Y8Auy4ijnc++L1PPZGHtYF8KR/frdaMn5WtoMV5GqQG/q2pxLxvaHvkX4B7A=="
},
"glob": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz",
@ -14069,8 +14080,7 @@
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.1.tgz",
"integrity": "sha512-lqGoSJBQNJidqCHE80vqZJHWHRFoNYsSpP9AjFhlhi9ODCJA541svILes/+/1GM3VaL/abZi7cpFzOpdR9UPKg==",
"dev": true,
"peer": true
"dev": true
},
"process-nextick-args": {
"version": "2.0.1",

@ -19,10 +19,12 @@
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"dotenv": "^16.0.3",
"hardhat": "^2.12.4"
"hardhat": "^2.12.4",
"prettier": "2.8.1"
},
"dependencies": {
"@openzeppelin/contracts": "^4.8.0",
"evm-large-storage": "^1.0.0"
"evm-large-storage": "^1.0.0",
"git3-evm-large-storage": "^1.0.0"
}
}

@ -11,25 +11,22 @@ describe("Git3 Test", function () {
const git3 = await Git3.deploy();
await git3.deployed();
let singer;
[singer] = await ethers.getSigners();
console.log("singer", singer.address);
await git3.upload("0x616263", "0x112233");
expect(await git3.download("0x616263")).to.eql(["0x112233", true]);
let data = Array.from({ length: 40 }, () =>
Math.floor(Math.random() * 256)
);
let data = Array.from({ length: 40 }, () => Math.floor(Math.random() * 256));
await git3.upload("0x616263", data);
expect(await git3.download("0x616263")).to.eql([
ethers.utils.hexlify(data),
true,
]);
expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data), true]);
expect(await git3.size("0x616263")).to.eql([ToBig(40), ToBig(1)]);
await git3.remove("0x616263");
expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]);
});
it("upload/download/remove chunks", async function () {
@ -39,75 +36,59 @@ describe("Git3 Test", function () {
expect(await git3.countChunks("0x616263")).to.eql(ToBig(0));
let data0 = Array.from({ length: 10 }, () =>
Math.floor(Math.random() * 256)
);
await git3.uploadChunk("0x616263",0, data0);
expect(await git3.download("0x616263")).to.eql([
ethers.utils.hexlify(data0),
true,
]);
let data1 = Array.from({ length: 20 }, () =>
Math.floor(Math.random() * 256)
);
let data0 = Array.from({ length: 10 }, () => Math.floor(Math.random() * 256));
await git3.uploadChunk("0x616263", 0, data0);
expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data0), true]);
let data1 = Array.from({ length: 20 }, () => Math.floor(Math.random() * 256));
await git3.uploadChunk("0x616263", 1, data1);
expect(await git3.download("0x616263")).to.eql([
ethers.utils.hexlify(data0.concat(data1)),
true,
]);
expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data0.concat(data1)), true]);
await git3.remove("0x616263"); // should succeed
expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]);
expect(await git3.download("0x616263")).to.eql([
"0x",
false,
]);
expect(await git3.download("0x616263")).to.eql(["0x", false]);
expect(await git3.countChunks("0x616263")).to.eql(ToBig(0));
});
it("set/update/list/remove Reference",async function() {
it("set/update/list/remove Reference", async function () {
const Git3 = await ethers.getContractFactory("Git3");
const git3 = await Git3.deploy();
await git3.deployed();
let key0 = "0x616263";
let data0 = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
await git3.setRef(key0,data0);
await git3.setRef(key0, data0);
let key1 = "0x717273";
let data1 = "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
await git3.setRef(key1,data1);
await git3.setRef(key1, data1);
let key2 = "0x818283";
let data2 = "0xcccccccccccccccccccccccccccccccccccccccc";
await git3.setRef(key2,data2);
await git3.setRef(key2, data2);
let refs = await git3.listRefs();
expect(refs[0]).to.eql([data0,key0]);
expect(refs[1]).to.eql([data1,key1]);
expect(refs[2]).to.eql([data2,key2]);
expect(refs[0]).to.eql([data0, key0]);
expect(refs[1]).to.eql([data1, key1]);
expect(refs[2]).to.eql([data2, key2]);
expect(refs.length).to.eql(3);
// check delRef
// check delRef
await git3.delRef(key0);
refs = await git3.listRefs();
expect(refs[0]).to.eql([data2,key2]);
expect(refs[1]).to.eql([data1,key1]);
expect(refs[0]).to.eql([data2, key2]);
expect(refs[1]).to.eql([data1, key1]);
expect(refs.length).to.eql(2);
await git3.delRef(key1);
refs = await git3.listRefs();
expect(refs[0]).to.eql([data2,key2]);
expect(refs[0]).to.eql([data2, key2]);
expect(refs.length).to.eql(1);
// check update
let data3 = "0xdddddddddddddddddddddddddddddddddddddddd";
await git3.setRef(key2,data3);
await git3.setRef(key2, data3);
refs = await git3.listRefs();
expect(refs[0]).to.eql([data3,key2]);
})
expect(refs[0]).to.eql([data3, key2]);
});
});

Loading…
Cancel
Save