Skip to content

Commit

Permalink
Vaguely port chainid-based-behaviour tests (needs more work)
Browse files Browse the repository at this point in the history
  • Loading branch information
area committed Aug 28, 2024
1 parent dc7dbeb commit 8d5526b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 181 deletions.
20 changes: 1 addition & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,31 +309,13 @@ jobs:
- <<: *step_pull_solc_docker
- <<: *step_setup_global_packages
- run:
name: "Running chainid tests with coverage for mainnet"
command: CHAIN_ID=1 pnpm run test:contracts:chainid:coverage
environment:
NODE_OPTIONS: --max-old-space-size=6144
- run:
name: "Running chainid tests with coverage for goerli"
command: CHAIN_ID=5 pnpm run test:contracts:chainid:coverage
environment:
NODE_OPTIONS: --max-old-space-size=6144
- run:
name: "Running chainid tests with coverage for xDai"
command: CHAIN_ID=100 pnpm run test:contracts:chainid:coverage
environment:
NODE_OPTIONS: --max-old-space-size=6144
- run:
name: "Running chainid tests with coverage for an unsupported network"
name: "Running chainid tests with for network where mining takes place"
command: CHAIN_ID=777 pnpm run test:contracts:chainid:coverage
environment:
NODE_OPTIONS: --max-old-space-size=6144
- persist_to_workspace:
root: ./
paths:
- coverage-chainid-1
- coverage-chainid-5
- coverage-chainid-100
- coverage-chainid-777
coverage-test-bridging:
<<: *job_common
Expand Down
24 changes: 12 additions & 12 deletions contracts/colonyNetwork/ColonyNetworkMining.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
_;
}

function setMiningDelegate(address _delegate, bool _allowed) public onlyMiningChain stoppable {
function setMiningDelegate(address _delegate, bool _allowed) public stoppable miningInitialised {
if (miningDelegators[_delegate] != address(0x00)) {
require(
miningDelegators[_delegate] == msgSender(),
Expand Down Expand Up @@ -69,7 +69,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
address _colony,
uint128 _nUpdates,
uint128 _nPreviousUpdates
) public onlyMiningChain recovery auth {
) public recovery auth miningInitialised {
replacementReputationUpdateLogsExist[_reputationMiningCycle] = true;

replacementReputationUpdateLog[_reputationMiningCycle][_id] = ReputationLogEntry(
Expand Down Expand Up @@ -102,7 +102,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
bytes32 newHash,
uint256 newNLeaves,
address[] memory stakers
) public onlyMiningChain stoppable onlyReputationMiningCycle {
) public stoppable miningInitialised onlyReputationMiningCycle {
reputationRootHash = newHash;
reputationRootHashNLeaves = newNLeaves;
// Reward stakers
Expand Down Expand Up @@ -163,7 +163,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
}

// slither-disable-next-line reentrancy-no-eth
function startNextCycle() public onlyMiningChain stoppable {
function startNextCycle() public stoppable miningInitialised {
address clnyToken = IMetaColony(metaColony).getToken();
require(clnyToken != address(0x0), "colony-reputation-mining-clny-token-invalid-address");
require(activeReputationMiningCycle == address(0x0), "colony-reputation-mining-still-active");
Expand Down Expand Up @@ -263,7 +263,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
function punishStakers(
address[] memory _stakers,
uint256 _amount
) public onlyMiningChain stoppable onlyReputationMiningCycle {
) public stoppable miningInitialised onlyReputationMiningCycle {
address clnyToken = IMetaColony(metaColony).getToken();
uint256 lostStake;
// Passing an array so that we don't incur the EtherRouter overhead for each staker if we looped over
Expand All @@ -285,19 +285,19 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
function reward(
address _recipient,
uint256 _amount
) public onlyMiningChain stoppable onlyReputationMiningCycle {
) public stoppable miningInitialised onlyReputationMiningCycle {
// TODO: Gain rep?
pendingMiningRewards[_recipient] += _amount;
}

function claimMiningReward(address _recipient) public onlyMiningChain stoppable {
function claimMiningReward(address _recipient) public miningInitialised stoppable {
address clnyToken = IMetaColony(metaColony).getToken();
uint256 amount = pendingMiningRewards[_recipient];
pendingMiningRewards[_recipient] = 0;
ITokenLocking(tokenLocking).transfer(clnyToken, amount, _recipient, true);
}

function stakeForMining(uint256 _amount) public onlyMiningChainOrDuringSetup stoppable {
function stakeForMining(uint256 _amount) public stoppable miningInitialisedOrDuringSetup {
address clnyToken = IMetaColony(metaColony).getToken();

ITokenLocking(tokenLocking).approveStake(msgSender(), _amount, clnyToken);
Expand All @@ -312,7 +312,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {
miningStakes[msgSender()].amount += _amount;
}

function unstakeForMining(uint256 _amount) public onlyMiningChain stoppable {
function unstakeForMining(uint256 _amount) public stoppable miningInitialised {
address clnyToken = IMetaColony(metaColony).getToken();
// Prevent those involved in a mining cycle withdrawing stake during the mining process.
require(
Expand All @@ -329,7 +329,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {

function burnUnneededRewards(
uint256 _amount
) public onlyMiningChain stoppable onlyReputationMiningCycle {
) public stoppable miningInitialised onlyReputationMiningCycle {
// If there are no rewards to burn, no need to do anything
if (_amount == 0) {
return;
Expand All @@ -346,7 +346,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {

function setReputationMiningCycleReward(
uint256 _amount
) public onlyMiningChain stoppable calledByMetaColony {
) public stoppable miningInitialised calledByMetaColony {
totalMinerRewardPerCycle = _amount;

emit ReputationMiningRewardSet(_amount);
Expand Down Expand Up @@ -380,7 +380,7 @@ contract ColonyNetworkMining is ColonyNetworkStorage {

function setMiningResolver(
address _miningResolver
) public stoppable onlyMiningChainOrDuringSetup auth {
) public stoppable auth miningInitialisedOrDuringSetup {
require(_miningResolver != address(0x0), "colony-mining-resolver-cannot-be-zero");

miningCycleResolver = _miningResolver;
Expand Down
25 changes: 7 additions & 18 deletions contracts/colonyNetwork/ColonyNetworkStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,22 @@ contract ColonyNetworkStorage is
_;
}

modifier onlyMiningChain() {
if (getMiningChainId() == block.chainid) {
require(
inactiveReputationMiningCycle != address(0x0),
"colony-reputation-mining-not-initialised"
);
}
require(isMiningChain(), "colony-only-valid-on-mining-chain");
modifier miningInitialised() {
require(
inactiveReputationMiningCycle != address(0x0),
"colony-reputation-mining-not-initialised"
);
_;
}

modifier onlyMiningChainOrDuringSetup() {
modifier miningInitialisedOrDuringSetup() {
require(
isMiningChain() || getMiningChainId() == 0,
inactiveReputationMiningCycle != address(0x0) || getMiningChainId() == 0,
"colony-only-valid-on-mining-chain-or-during-setup"
);
_;
}

modifier onlyNotMiningChain() {
require(!isMiningChain(), "colony-only-valid-not-on-mining-chain");
_;
}

// Internal functions

function toRootSkillId(uint256 _chainId) internal pure returns (uint256) {
Expand All @@ -211,9 +203,6 @@ contract ColonyNetworkStorage is
}

function getMiningChainId() public view returns (uint256) {
if (reputationMiningChainId == 0 && isXdai()) {
return block.chainid;
}
return reputationMiningChainId;
}

Expand Down
16 changes: 13 additions & 3 deletions helpers/test-data-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
const BN = require("bn.js");
const { signTypedData_v4: signTypedData } = require("eth-sig-util");

const { UINT256_MAX, MANAGER_PAYOUT, EVALUATOR_PAYOUT, WORKER_PAYOUT, INITIAL_FUNDING, SLOT0, SLOT1, SLOT2, ADDRESS_ZERO } = require("./constants");
const {
UINT256_MAX,
MANAGER_PAYOUT,
EVALUATOR_PAYOUT,
WORKER_PAYOUT,
INITIAL_FUNDING,
SLOT0,
SLOT1,
SLOT2,
ADDRESS_ZERO,
NETWORK_ADDRESS,
} = require("./constants");

const { getTokenArgs, web3GetAccounts, getChildSkillIndex, getChainId } = require("./test-helper");

Expand Down Expand Up @@ -226,8 +237,7 @@ exports.unlockCLNYToken = async function unlockCLNYToken(metaColony) {
};

exports.setupColonyNetwork = async function setupColonyNetwork() {
const cnAddress = (await EtherRouter.deployed()).address;
const deployedColonyNetwork = await IColonyNetwork.at(cnAddress);
const deployedColonyNetwork = await IColonyNetwork.at(NETWORK_ADDRESS);

// Make a new ColonyNetwork
const etherRouter = await EtherRouter.new();
Expand Down
104 changes: 50 additions & 54 deletions test/cross-chain/cross-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ const { expect } = chai;
chai.use(bnChai(web3.utils.BN));

const IColonyNetwork = artifacts.require("IColonyNetwork");
const EtherRouterCreate3 = artifacts.require("EtherRouterCreate3");
const EtherRouter = artifacts.require("EtherRouter");
const IMetaColony = artifacts.require("IMetaColony");
const Resolver = artifacts.require("Resolver");
const Token = artifacts.require("Token");
const IColony = artifacts.require("IColony");
const ICreateX = artifacts.require("ICreateX");
const ProxyColonyNetwork = artifacts.require("ProxyColonyNetwork");
const ProxyColony = artifacts.require("ProxyColony");
const MetaTxToken = artifacts.require("MetaTxToken");
Expand All @@ -48,7 +45,6 @@ const {
const { forwardTime, checkErrorRevertEthers, revert, snapshot } = require("../../helpers/test-helper");
const ReputationMinerTestWrapper = require("../../packages/reputation-miner/test/ReputationMinerTestWrapper");
const { TruffleLoader } = require("../../packages/package-utils");
const { setupProxyColonyNetwork, setupEtherRouter } = require("../../helpers/upgradable-contracts");

const UINT256_MAX_ETHERS = ethers.BigNumber.from(2).pow(256).sub(1);

Expand Down Expand Up @@ -139,56 +135,56 @@ contract("Cross-chain", (accounts) => {
foreignChainId = await ethersForeignSigner.provider.send("eth_chainId", []);

// Deploy shell colonyNetwork to whichever chain truffle hasn't already deployed to.
try {
if (process.env.HARDHAT_FOREIGN === "true") {
await exec(`CHAIN_ID=${parseInt(foreignChainId, 16)} npx hardhat ensureCreateXDeployed --network development`);
} else {
await exec(`CHAIN_ID=${parseInt(foreignChainId, 16)} npx hardhat ensureCreateXDeployed --network development2`);
}

const createX = await new ethers.Contract(CREATEX_ADDRESS, ICreateX.abi, ethersForeignSigner);

// This is a fake instance of an etherRouter, just so we can call encodeABs
const fakeEtherRouter = await EtherRouterCreate3.at(CREATEX_ADDRESS);
const setOwnerData = fakeEtherRouter.contract.methods.setOwner(accounts[0]).encodeABI();

const tx = await createX["deployCreate3AndInit(bytes32,bytes,bytes,(uint256,uint256))"](
`0xb77d57f4959eafa0339424b83fcfaf9c15407461005e95d52076387600e2c1e9`,
EtherRouterCreate3.bytecode,
setOwnerData,
[0, 0],
{ from: accounts[0] },
);

const receipt = await tx.wait();

const etherRouter = await new ethers.Contract(
receipt.events.filter((log) => log.event === "ContractCreation")[0].args.newContract,
EtherRouter.abi,
ethersForeignSigner,
);
let resolver = await new ethers.ContractFactory(Resolver.abi, Resolver.bytecode, ethersForeignSigner).deploy();
const proxyColonyNetworkImplementation = await new ethers.ContractFactory(
ProxyColonyNetwork.abi,
ProxyColonyNetwork.bytecode,
ethersForeignSigner,
).deploy();

await setupProxyColonyNetwork(etherRouter, proxyColonyNetworkImplementation, resolver);
console.log("**** shell colony network set up");

// Set up the resolver for shell colonies
resolver = await new ethers.ContractFactory(Resolver.abi, Resolver.bytecode, ethersForeignSigner).deploy();
const proxyColonyImplementation = await new ethers.ContractFactory(ProxyColony.abi, ProxyColony.bytecode, ethersForeignSigner).deploy();

await setupEtherRouter("bridging", "ProxyColony", { ProxyColony: proxyColonyImplementation.address }, resolver);
const proxyColonyNetwork = new ethers.Contract(etherRouter.address, ProxyColonyNetwork.abi, ethersForeignSigner);

await proxyColonyNetwork.setProxyColonyResolverAddress(resolver.address);
} catch (err) {
console.log(err);
process.exit(1);
}
// try {
// if (process.env.HARDHAT_FOREIGN === "true") {
// await exec(`CHAIN_ID=${parseInt(foreignChainId, 16)} npx hardhat ensureCreateXDeployed --network development`);
// } else {
// await exec(`CHAIN_ID=${parseInt(foreignChainId, 16)} npx hardhat ensureCreateXDeployed --network development2`);
// }

// const createX = await new ethers.Contract(CREATEX_ADDRESS, ICreateX.abi, ethersForeignSigner);

// // This is a fake instance of an etherRouter, just so we can call encodeABs
// const fakeEtherRouter = await EtherRouterCreate3.at(CREATEX_ADDRESS);
// const setOwnerData = fakeEtherRouter.contract.methods.setOwner(accounts[0]).encodeABI();

// const tx = await createX["deployCreate3AndInit(bytes32,bytes,bytes,(uint256,uint256))"](
// `0xb77d57f4959eafa0339424b83fcfaf9c15407461005e95d52076387600e2c1e9`,
// EtherRouterCreate3.bytecode,
// setOwnerData,
// [0, 0],
// { from: accounts[0] },
// );

// const receipt = await tx.wait();

// const etherRouter = await new ethers.Contract(
// receipt.events.filter((log) => log.event === "ContractCreation")[0].args.newContract,
// EtherRouter.abi,
// ethersForeignSigner,
// );
// let resolver = await new ethers.ContractFactory(Resolver.abi, Resolver.bytecode, ethersForeignSigner).deploy();
// const proxyColonyNetworkImplementation = await new ethers.ContractFactory(
// ProxyColonyNetwork.abi,
// ProxyColonyNetwork.bytecode,
// ethersForeignSigner,
// ).deploy();

// await setupProxyColonyNetwork(etherRouter, proxyColonyNetworkImplementation, resolver);
// console.log("**** shell colony network set up");

// // Set up the resolver for shell colonies
// resolver = await new ethers.ContractFactory(Resolver.abi, Resolver.bytecode, ethersForeignSigner).deploy();
// const proxyColonyImplementation = await new ethers.ContractFactory(ProxyColony.abi, ProxyColony.bytecode, ethersForeignSigner).deploy();

// await setupEtherRouter("bridging", "ProxyColony", { ProxyColony: proxyColonyImplementation.address }, resolver);
// const proxyColonyNetwork = new ethers.Contract(etherRouter.address, ProxyColonyNetwork.abi, ethersForeignSigner);

// await proxyColonyNetwork.setProxyColonyResolverAddress(resolver.address);
// } catch (err) {
// console.log(err);
// process.exit(1);
// }

// 0x539 is the chain id used by truffle by default (regardless of networkid), and if
// we see it in our tests that's the coverage chain, which builds the contract artifacts
Expand Down
2 changes: 2 additions & 0 deletions test/deploy-proxy-network-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = async () => {

let resolver = await Resolver.new();

await Resolver.setAsDeployed(resolver);

await setupProxyColonyNetwork(etherRouter, proxyColonyNetworkImplementation, resolver);

// Set up the resolver for shell colonies and register it with the network
Expand Down
Loading

0 comments on commit 8d5526b

Please sign in to comment.