Skip to content

Commit

Permalink
code cleanup and update e2e testing with maci v1 api
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Nov 21, 2023
1 parent ba251f2 commit 1a46db0
Show file tree
Hide file tree
Showing 60 changed files with 3,051 additions and 1,940 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/test-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ jobs:
- name: Install zkutil
run: |
cargo install zkutil --version 0.3.2
- name: Checkout rapidsnark source code
uses: actions/checkout@v3
with:
repository: iden3/rapidsnark
path: rapidsnark
- name: Install rapidsnark
npm install
git submodule init
git submodule update
npx task createFieldSources
npx task buildProver
- name: Checkout source code
uses: actions/checkout@v3
with:
path: monorepo
- name: Download batch 64 params
run: |
$GITHUB_WORKSPACE/monorepo/.github/scripts/download-batch64-params.sh
$GITHUB_WORKSPACE/monorepo/.github/scripts/download-6-8-2-3.sh
- name: Build CLR
run: |
cd monorepo
Expand All @@ -48,14 +59,6 @@ jobs:
yarn start:node &
- name: Run script tests
run: |
export RAPIDSNARK_DIRECTORY=$GITHUB_WORKSPACE/rapidsnark
cd monorepo/contracts
export NODE_CONFIG=$(node -e "const snarkParamsPath=process.env.GITHUB_WORKSPACE + '/params'; console.log(JSON.stringify({ snarkParamsPath }));")
echo $NODE_CONFIG
yarn deploy:local
yarn deployTestRound:local
yarn contribute:local
yarn vote:local
yarn hardhat evm-increase-time 1200 --network localhost
yarn tally:local
yarn finalize:local
yarn claim:local
./sh/runScriptTests.sh
12 changes: 10 additions & 2 deletions contracts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WALLET_PRIVATE_KEY=
NATIVE_TOKEN_ADDRESS=

# Required to use in the tally and finalize scripts
FACTORY_ADDRESS=
CLRFUND=
ROUND_ADDRESS=
COORDINATOR_PK=
COORDINATOR_ETH_PK=
Expand All @@ -50,7 +50,15 @@ CIRCUIT_TYPE=prod
# The IPFS gateway url used by the prepare-results.ts script
IPFS_GATEWAY_URL=

# circuit params and directory used by e2e script
# Parameters used in the tally script
CIRCUIT_TYPE=
CIRCUIT_DIRECTORY=
RAPIDSNARK_DIRECTORY=
# Used in MACI queue merging operation before genProofs, default is 4
NUM_QUEUE_OPS=
# Used in e2e testing to store intermediate states
STATE_FILE=
# MACI creation transaction hash, used to find the start block of MACI logs
MACI_TRANSACTION_HASH=
# genProofs output directory
PROOF_OUTPUT_DIR=
2 changes: 1 addition & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ proofs.json
tally.json
.env
.DS_Store
tasks/addresses.txt
addresses.txt
proof_output
37 changes: 32 additions & 5 deletions contracts/contracts/ClrFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import './userRegistry/IUserRegistry.sol';
import './recipientRegistry/IRecipientRegistry.sol';
import {FundingRound} from './FundingRound.sol';
import './OwnableUpgradeable.sol';
import {FundingRoundFactory} from './FundingRoundFactory.sol';
import {TopupToken} from './TopupToken.sol';

contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
using EnumerableSet for EnumerableSet.AddressSet;
Expand All @@ -33,13 +35,19 @@ contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
EnumerableSet.AddressSet private fundingSources;
FundingRound[] private rounds;

FundingRoundFactory public roundFactory;

// Events
event FundingSourceAdded(address _source);
event FundingSourceRemoved(address _source);
event RoundStarted(address _round);
event RoundFinalized(address _round);
event TokenChanged(address _token);
event CoordinatorChanged(address _coordinator);
event Initialized();
event UserRegistrySet();
event RecipientRegistrySet();
event FundingRoundTemplateChanged();

// errors
error FundingSourceAlreadyAdded();
Expand All @@ -53,14 +61,27 @@ contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
error NoRecipientRegistry();
error NoUserRegistry();
error NotOwnerOfMaciFactory();
error InvalidFundingRoundFactory();
error InvalidMaciFactory();

/**
* @dev Initialize clrfund instance with MACI factory and new round templates
*/
function init(
MACIFactory _maciFactory
address _maciFactory,
address _roundFactory
)
external
{
__Ownable_init();
maciFactory = _maciFactory;

if (address(_maciFactory) == address(0)) revert InvalidMaciFactory();
if (_roundFactory == address(0)) revert InvalidFundingRoundFactory();

maciFactory = MACIFactory(_maciFactory);
roundFactory = FundingRoundFactory(_roundFactory);

emit Initialized();
}

/**
Expand All @@ -72,6 +93,8 @@ contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
onlyOwner
{
userRegistry = _userRegistry;

emit UserRegistrySet();
}

/**
Expand All @@ -85,6 +108,8 @@ contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
recipientRegistry = _recipientRegistry;
(, uint256 maxVoteOptions) = maciFactory.maxValues();
recipientRegistry.setMaxRecipients(maxVoteOptions);

emit RecipientRegistrySet();
}

/**
Expand Down Expand Up @@ -150,18 +175,20 @@ contract ClrFund is OwnableUpgradeable, IPubKey, SnarkCommon, Params {
(, uint256 maxVoteOptions) = maciFactory.maxValues();
recipientRegistry.setMaxRecipients(maxVoteOptions);
// Deploy funding round and MACI contracts
FundingRound newRound = new FundingRound(
FundingRound newRound = roundFactory.deploy(
nativeToken,
userRegistry,
recipientRegistry,
coordinator
coordinator,
address(this)
);
rounds.push(newRound);

TopupToken topupToken = newRound.topupToken();
MACI maci = maciFactory.deployMaci(
SignUpGatekeeper(newRound),
InitialVoiceCreditProxy(newRound),
address(nativeToken),
address(topupToken),
duration,
coordinator,
coordinatorPubKey
Expand Down
97 changes: 68 additions & 29 deletions contracts/contracts/ClrFundDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,85 @@

pragma solidity 0.8.10;

import './MACIFactory.sol';
import './ClrFund.sol';
import {MACIFactory} from './MACIFactory.sol';
import {ClrFund} from './ClrFund.sol';
import {CloneFactory} from './CloneFactory.sol';
import {SignUpGatekeeper} from "@clrfund/maci-contracts/contracts/gatekeepers/SignUpGatekeeper.sol";
import {InitialVoiceCreditProxy} from "@clrfund/maci-contracts/contracts/initialVoiceCreditProxy/InitialVoiceCreditProxy.sol";
import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';

contract ClrFundDeployer is CloneFactory {
contract ClrFundDeployer is CloneFactory, Ownable {
address public clrfundTemplate;
address public maciFactory;
address public roundFactory;
mapping (address => bool) public clrfunds;

address public template;
mapping (address => bool) public clrfunds;
event NewInstance(address indexed clrfund);
event Register(address indexed clrfund, string metadata);
event NewFundingRoundTemplate(address newTemplate);
event NewClrfundTemplate(address newTemplate);

constructor(address _template) {
template = _template;
}

event NewInstance(address indexed clrfund);
event Register(address indexed clrfund, string metadata);
// errors
error ClrFundAlreadyRegistered();
error InvalidMaciFactory();
error InvalidClrFundTemplate();
error InvalidFundingRoundFactory();

// errors
error ClrFundAlreadyRegistered();
constructor(
address _clrfundTemplate,
address _maciFactory,
address _roundFactory
)
{
if (_clrfundTemplate == address(0)) revert InvalidClrFundTemplate();
if (_maciFactory == address(0)) revert InvalidMaciFactory();
if (_roundFactory == address(0)) revert InvalidFundingRoundFactory();

function deployClrFund(MACIFactory _maciFactory) public returns (address) {
clrfundTemplate = _clrfundTemplate;
maciFactory = _maciFactory;
roundFactory = _roundFactory;
}

ClrFund clrfund = ClrFund(createClone(template));
clrfund.init(_maciFactory);
emit NewInstance(address(clrfund));
/**
* @dev Set a new clrfund template
* @param _clrfundTemplate New template
*/
function setClrFundTemplate(address _clrfundTemplate)
external
onlyOwner
{
if (_clrfundTemplate == address(0)) revert InvalidClrFundTemplate();

return address(clrfund);
}

function registerInstance(
address _clrFundAddress,
string memory _metadata
) public returns (bool) {
clrfundTemplate = _clrfundTemplate;
emit NewClrfundTemplate(_clrfundTemplate);
}

if (clrfunds[_clrFundAddress] == true) revert ClrFundAlreadyRegistered();
/**
* @dev Deploy a new instance of ClrFund
*/
function deployClrFund() public returns (address) {
ClrFund clrfund = ClrFund(createClone(clrfundTemplate));
clrfund.init(maciFactory, roundFactory);
emit NewInstance(address(clrfund));

clrfunds[_clrFundAddress] = true;
return address(clrfund);
}

emit Register(_clrFundAddress, _metadata);
return true;
}
/**
* @dev Register the clrfund instance of subgraph event processing
* @param _clrFundAddress ClrFund address
* @param _metadata Clrfund metadata
*/
function registerInstance(
address _clrFundAddress,
string memory _metadata
) public returns (bool) {

if (clrfunds[_clrFundAddress] == true) revert ClrFundAlreadyRegistered();

clrfunds[_clrFundAddress] = true;

emit Register(_clrFundAddress, _metadata);
return true;
}
}
3 changes: 3 additions & 0 deletions contracts/contracts/FundingRound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {DomainObjs} from '@clrfund/maci-contracts/contracts/DomainObjs.sol';
import {MACI} from '@clrfund/maci-contracts/contracts/MACI.sol';
import {Poll} from '@clrfund/maci-contracts/contracts/Poll.sol';
import {Tally} from '@clrfund/maci-contracts/contracts/Tally.sol';
import {TopupToken} from './TopupToken.sol';
import {SignUpGatekeeper} from "@clrfund/maci-contracts/contracts/gatekeepers/SignUpGatekeeper.sol";
import {InitialVoiceCreditProxy} from "@clrfund/maci-contracts/contracts/initialVoiceCreditProxy/InitialVoiceCreditProxy.sol";

Expand Down Expand Up @@ -93,6 +94,7 @@ contract FundingRound is Ownable, SignUpGatekeeper, InitialVoiceCreditProxy, Dom
address public coordinator;
MACI public maci;
ERC20 public nativeToken;
TopupToken public topupToken;
IUserRegistry public userRegistry;
IRecipientRegistry public recipientRegistry;
string public tallyHash;
Expand Down Expand Up @@ -143,6 +145,7 @@ contract FundingRound is Ownable, SignUpGatekeeper, InitialVoiceCreditProxy, Dom
userRegistry = _userRegistry;
recipientRegistry = _recipientRegistry;
coordinator = _coordinator;
topupToken = new TopupToken();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions contracts/contracts/FundingRoundFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.10;

import {FundingRound} from './FundingRound.sol';
import {ERC20} from '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import {IUserRegistry} from './userRegistry/IUserRegistry.sol';
import {IRecipientRegistry} from './recipientRegistry/IRecipientRegistry.sol';

contract FundingRoundFactory {
function deploy(
ERC20 _nativeToken,
IUserRegistry _userRegistry,
IRecipientRegistry _recipientRegistry,
address _coordinator,
address _owner
)
external
returns (FundingRound newRound)
{
newRound = new FundingRound(
_nativeToken,
_userRegistry,
_recipientRegistry,
_coordinator
);

newRound.transferOwnership(_owner);
}
}
Loading

0 comments on commit 1a46db0

Please sign in to comment.