From 23c06f456fdf1eaff07be4671e4566a734016134 Mon Sep 17 00:00:00 2001 From: cyl19970726 <15258378443@163.com> Date: Mon, 27 Feb 2023 11:19:17 +0800 Subject: [PATCH] update hubv3 --- contracts/v3/Hubv3.sol | 29 +++++++++++++++++++++++++---- contracts/v3/database/database.sol | 7 +++++++ contracts/v3/database/filecoin.sol | 9 +++++++++ test/v3-hub-test.js | 24 +++++++++++++++++++----- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/contracts/v3/Hubv3.sol b/contracts/v3/Hubv3.sol index 904adfa..0b063f5 100644 --- a/contracts/v3/Hubv3.sol +++ b/contracts/v3/Hubv3.sol @@ -1,15 +1,14 @@ //SPDX-License-Identifier: GLP-3.0 pragma solidity ^0.8.0; -import "@openzeppelin/contracts/access/AccessControl.sol"; +import "@openzeppelin/contracts/access/AccessControlEnumerable.sol"; import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; -import "./EnumerableSet.sol"; import "./Repolib.sol"; import "./database/database.sol"; import "./database/ethstorage.sol"; import "./database/filecoin.sol"; -contract Hubv3 is AccessControl, Initializable { +contract Hubv3 is AccessControlEnumerable, Initializable { using EnumerableSet for EnumerableSet.AddressSet; using Repolib for Repolib.BranchInfo; @@ -55,7 +54,7 @@ contract Hubv3 is AccessControl, Initializable { } // ===== hub operator functions====== - function openPermissonlessJoin(bool open) public { + function setPermissonlessJoin(bool open) public { require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender())); permissionless = open; } @@ -89,6 +88,14 @@ contract Hubv3 is AccessControl, Initializable { return false; } + function roleToMembers(bytes32 role) public view returns(address[] memory members){ + uint256 count = getRoleMemberCount(role); + members = new address[](count); + for (uint i = 0; i < count; i++){ + members[i] = getRoleMember(role, i); + } + } + function addManager(address member) public { grantRole(MANAGER, member); } @@ -112,6 +119,11 @@ contract Hubv3 is AccessControl, Initializable { } // ===== repository operator functions====== + + function repoList() public view returns(bytes[] memory rn){ + return repoNames; + } + //createRepository can be invoked by anyone within Hub function createRepo(bytes memory repoName) public { require(membership(_msgSender())); @@ -260,6 +272,15 @@ contract Hubv3 is AccessControl, Initializable { return db.upload(repoName, path, data); } + function uploadChunk( + bytes memory repoName, + bytes memory path, + uint256 chunkId, + bytes calldata data + ) external payable { + return db.uploadChunk(repoName, path,chunkId, data); + } + function batchUpload( bytes memory repoName, bytes[] memory path, diff --git a/contracts/v3/database/database.sol b/contracts/v3/database/database.sol index 22116a3..26c2d1a 100644 --- a/contracts/v3/database/database.sol +++ b/contracts/v3/database/database.sol @@ -12,4 +12,11 @@ interface database { bytes memory path, bytes calldata data ) external payable; + + function uploadChunk( + bytes memory repoName, + bytes memory path, + uint256 chunkId, + bytes calldata data + ) external payable; } diff --git a/contracts/v3/database/filecoin.sol b/contracts/v3/database/filecoin.sol index 7777d33..991f707 100644 --- a/contracts/v3/database/filecoin.sol +++ b/contracts/v3/database/filecoin.sol @@ -23,4 +23,13 @@ contract filecoin is database { bytes32 fullName = keccak256(bytes.concat(repoName, "/", path)); pathToHash[fullName] = data; } + + function uploadChunk( + bytes memory repoName, + bytes memory path, + uint256 chunkId, + bytes calldata data + ) external payable override { + revert("unsupport uploadChunk"); + } } diff --git a/test/v3-hub-test.js b/test/v3-hub-test.js index ceb0711..4c6e844 100644 --- a/test/v3-hub-test.js +++ b/test/v3-hub-test.js @@ -12,7 +12,7 @@ describe("Hub V3 Test", function () { it("Hub Access Control", async function () { const hubfacFac = await ethers.getContractFactory("HubFactory"); - const hubFac = await hubfacFac.deploy(); + const hubFac = await hubfacFac.deploy(false); await hubFac.deployed(); await hubFac.newHubImp(); @@ -29,6 +29,10 @@ describe("Hub V3 Test", function () { expect(IsManager).to.equal(true); expect(IsContributor).to.equal(false); + let ADMIN_ROLE = await git3.DEFAULT_ADMIN_ROLE(); + let addrs = await git3.roleToMembers(ADMIN_ROLE); + expect(addrs[0]).to.equal(singer.address); + await git3.addManager(manager1.address) await git3.addManager(manager2.address) let roles = await git3.memberRole(manager1.address) @@ -36,6 +40,13 @@ describe("Hub V3 Test", function () { expect(roles[1]).to.equal(true); expect(roles[2]).to.equal(false); + let MANAGER_ROLE = await git3.MANAGER(); + let m_addrs = await git3.roleToMembers(MANAGER_ROLE); + expect(m_addrs[0]).to.equal(singer.address); + expect(m_addrs[1]).to.equal(manager1.address); + expect(m_addrs[2]).to.equal(manager2.address); + + roles = await git3.memberRole(manager2.address) expect(roles[0]).to.equal(false); expect(roles[1]).to.equal(true); @@ -46,6 +57,10 @@ describe("Hub V3 Test", function () { expect(roles[0]).to.equal(false); expect(roles[1]).to.equal(false); expect(roles[2]).to.equal(false); + m_addrs = await git3.roleToMembers(MANAGER_ROLE); + expect(m_addrs.length).to.equal(2); + expect(m_addrs[0]).to.equal(singer.address); + expect(m_addrs[1]).to.equal(manager1.address); // power no enough await expect( @@ -118,7 +133,7 @@ describe("Hub V3 Test", function () { it("upload/download/remove", async function () { const hubfacFac = await ethers.getContractFactory("HubFactory"); - const hubFac = await hubfacFac.deploy(); + const hubFac = await hubfacFac.deploy(true); await hubFac.deployed(); await hubFac.newHubImp(); @@ -158,7 +173,7 @@ describe("Hub V3 Test", function () { it("set/update/list/remove Branch", async function () { const hubfacFac = await ethers.getContractFactory("HubFactory"); - const hubFac = await hubfacFac.deploy(); + const hubFac = await hubfacFac.deploy(true); await hubFac.deployed(); await hubFac.newHubImp(); @@ -214,9 +229,8 @@ describe("Hub V3 Test", function () { it("HubFactory", async function () { - const hubfacFac = await ethers.getContractFactory("HubFactory"); - const hubFac = await hubfacFac.deploy(); + const hubFac = await hubfacFac.deploy(true); await hubFac.deployed(); await hubFac.newHubImp();