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

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

20
package-lock.json generated

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

@ -19,10 +19,12 @@
"devDependencies": { "devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.0", "@nomicfoundation/hardhat-toolbox": "^2.0.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"hardhat": "^2.12.4" "hardhat": "^2.12.4",
"prettier": "2.8.1"
}, },
"dependencies": { "dependencies": {
"@openzeppelin/contracts": "^4.8.0", "@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(); const git3 = await Git3.deploy();
await git3.deployed(); await git3.deployed();
let singer;
[singer] = await ethers.getSigners();
console.log("singer", singer.address);
await git3.upload("0x616263", "0x112233"); await git3.upload("0x616263", "0x112233");
expect(await git3.download("0x616263")).to.eql(["0x112233", true]); expect(await git3.download("0x616263")).to.eql(["0x112233", true]);
let data = Array.from({ length: 40 }, () => let data = Array.from({ length: 40 }, () => Math.floor(Math.random() * 256));
Math.floor(Math.random() * 256)
);
await git3.upload("0x616263", data); await git3.upload("0x616263", data);
expect(await git3.download("0x616263")).to.eql([ expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data), true]);
ethers.utils.hexlify(data),
true,
]);
expect(await git3.size("0x616263")).to.eql([ToBig(40), ToBig(1)]); expect(await git3.size("0x616263")).to.eql([ToBig(40), ToBig(1)]);
await git3.remove("0x616263"); await git3.remove("0x616263");
expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]); expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]);
}); });
it("upload/download/remove chunks", async function () { it("upload/download/remove chunks", async function () {
@ -39,30 +36,17 @@ describe("Git3 Test", function () {
expect(await git3.countChunks("0x616263")).to.eql(ToBig(0)); expect(await git3.countChunks("0x616263")).to.eql(ToBig(0));
let data0 = Array.from({ length: 10 }, () => let data0 = Array.from({ length: 10 }, () => Math.floor(Math.random() * 256));
Math.floor(Math.random() * 256)
);
await git3.uploadChunk("0x616263", 0, data0); await git3.uploadChunk("0x616263", 0, data0);
expect(await git3.download("0x616263")).to.eql([ expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data0), true]);
ethers.utils.hexlify(data0),
true, let data1 = Array.from({ length: 20 }, () => Math.floor(Math.random() * 256));
]);
let data1 = Array.from({ length: 20 }, () =>
Math.floor(Math.random() * 256)
);
await git3.uploadChunk("0x616263", 1, data1); await git3.uploadChunk("0x616263", 1, data1);
expect(await git3.download("0x616263")).to.eql([ expect(await git3.download("0x616263")).to.eql([ethers.utils.hexlify(data0.concat(data1)), true]);
ethers.utils.hexlify(data0.concat(data1)),
true,
]);
await git3.remove("0x616263"); // should succeed await git3.remove("0x616263"); // should succeed
expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]); expect(await git3.size("0x616263")).to.eql([ToBig(0), ToBig(0)]);
expect(await git3.download("0x616263")).to.eql([ expect(await git3.download("0x616263")).to.eql(["0x", false]);
"0x",
false,
]);
expect(await git3.countChunks("0x616263")).to.eql(ToBig(0)); expect(await git3.countChunks("0x616263")).to.eql(ToBig(0));
}); });
@ -83,7 +67,6 @@ describe("Git3 Test", function () {
let data2 = "0xcccccccccccccccccccccccccccccccccccccccc"; let data2 = "0xcccccccccccccccccccccccccccccccccccccccc";
await git3.setRef(key2, data2); await git3.setRef(key2, data2);
let refs = await git3.listRefs(); let refs = await git3.listRefs();
expect(refs[0]).to.eql([data0, key0]); expect(refs[0]).to.eql([data0, key0]);
expect(refs[1]).to.eql([data1, key1]); expect(refs[1]).to.eql([data1, key1]);
@ -107,7 +90,5 @@ describe("Git3 Test", function () {
await git3.setRef(key2, data3); await git3.setRef(key2, data3);
refs = await git3.listRefs(); refs = await git3.listRefs();
expect(refs[0]).to.eql([data3, key2]); expect(refs[0]).to.eql([data3, key2]);
});
})
}); });

Loading…
Cancel
Save