Skip to content

Commit

Permalink
First pass at updating reputation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Jan 2, 2024
1 parent a041583 commit 9031260
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 127 deletions.
6 changes: 3 additions & 3 deletions test/reputation-system/client-calculations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* globals ethers */

const path = require("path");
const BN = require("bn.js");
const chai = require("chai");
const bnChai = require("bn-chai");
const { ethers } = require("ethers");

const { TruffleLoader } = require("../../packages/package-utils");
const { UINT256_MAX, DEFAULT_STAKE, INITIAL_FUNDING } = require("../../helpers/constants");
Expand All @@ -29,7 +30,6 @@ let colonyNetwork;
let metaColony;
let clnyToken;
let goodClient;
const realProviderPort = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;

const setupNewNetworkInstance = async (MINER1, MINER2) => {
colonyNetwork = await setupColonyNetwork();
Expand All @@ -50,7 +50,7 @@ const setupNewNetworkInstance = async (MINER1, MINER2) => {
await colonyNetwork.initialiseReputationMining();
await colonyNetwork.startNextCycle();

goodClient = new ReputationMinerTestWrapper({ loader, realProviderPort, useJsTree, minerAddress: MINER1 });
goodClient = new ReputationMinerTestWrapper({ loader, provider: ethers.provider, useJsTree, minerAddress: MINER1 });
};

process.env.SOLIDITY_COVERAGE
Expand Down
8 changes: 3 additions & 5 deletions test/reputation-system/client-core-functionality.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals artifacts */
/* globals artifacts, ethers */

const path = require("path");
const request = require("async-request");
Expand Down Expand Up @@ -27,8 +27,6 @@ const loader = new TruffleLoader({
contractRoot: path.resolve(__dirname, "..", "..", "artifacts", "contracts"),
});

const realProviderPort = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;

process.env.SOLIDITY_COVERAGE
? contract.skip
: contract("Reputation mining - client core functionality", (accounts) => {
Expand All @@ -55,7 +53,7 @@ process.env.SOLIDITY_COVERAGE
const lock = await tokenLocking.getUserLock(clnyToken.address, MINER1);
expect(lock.balance).to.eq.BN(DEFAULT_STAKE);

reputationMiner = new ReputationMinerTestWrapper({ loader, minerAddress: MINER1, realProviderPort, useJsTree: true });
reputationMiner = new ReputationMinerTestWrapper({ loader, minerAddress: MINER1, provider: ethers.provider, useJsTree: true });
});

beforeEach(async () => {
Expand All @@ -75,7 +73,7 @@ process.env.SOLIDITY_COVERAGE

const adapter = new TestAdapter();

client = new ReputationMinerClient({ loader, realProviderPort, minerAddress: MINER1, useJsTree: true, auto: false, adapter });
client = new ReputationMinerClient({ loader, provider: ethers.provider, minerAddress: MINER1, useJsTree: true, auto: false, adapter });
await client.initialise(colonyNetwork.address, 1);
});

Expand Down
14 changes: 7 additions & 7 deletions test/reputation-system/client-sync-functionality.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals artifacts */
/* globals artifacts, ethers */

const path = require("path");
const chai = require("chai");
Expand Down Expand Up @@ -26,8 +26,8 @@ const loader = new TruffleLoader({
contractRoot: path.resolve(__dirname, "..", "..", "artifacts", "contracts"),
});

const realProviderPort = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;
const useJsTree = true;
const { provider } = ethers;

process.env.SOLIDITY_COVERAGE
? contract.skip
Expand All @@ -53,8 +53,8 @@ process.env.SOLIDITY_COVERAGE
const clnyAddress = await metaColony.getToken();
clnyToken = await Token.at(clnyAddress);

reputationMiner1 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER1, realProviderPort, useJsTree });
reputationMiner2 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER2, realProviderPort, useJsTree });
reputationMiner1 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER1, provider, useJsTree });
reputationMiner2 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER2, provider, useJsTree });
});

beforeEach(async () => {
Expand Down Expand Up @@ -178,7 +178,7 @@ process.env.SOLIDITY_COVERAGE
await reputationMiner2.sync(startingBlockNumber);

// And load a state on a client that's not using the JS Tree
const reputationMiner3 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER2, realProviderPort, useJsTree: !useJsTree });
const reputationMiner3 = new ReputationMinerTestWrapper({ loader, minerAddress: MINER2, provider, useJsTree: !useJsTree });
await reputationMiner3.initialise(colonyNetwork.address);
await reputationMiner3.loadState(savedHash);
await reputationMiner3.sync(startingBlockNumber);
Expand Down Expand Up @@ -232,7 +232,7 @@ process.env.SOLIDITY_COVERAGE

it("should be able to download a sqlite file containing the latest state", async () => {
const adapter = new TestAdapter();
const client = new ReputationMinerClient({ loader, realProviderPort, minerAddress: MINER1, useJsTree: true, auto: false, adapter });
const client = new ReputationMinerClient({ loader, provider, minerAddress: MINER1, useJsTree: true, auto: false, adapter });
await client.initialise(colonyNetwork.address, 1);

await fundColonyWithTokens(metaColony, clnyToken, INITIAL_FUNDING.muln(100));
Expand All @@ -258,7 +258,7 @@ process.env.SOLIDITY_COVERAGE
const reputationMiner3 = new ReputationMinerTestWrapper({
loader,
minerAddress: MINER1,
realProviderPort,
provider,
useJsTree: true,
dbPath: fileName,
});
Expand Down
60 changes: 27 additions & 33 deletions test/reputation-system/dispute-resolution-misbehaviour.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* globals artifacts, BigInt */
/* globals artifacts, BigInt, ethers */

const path = require("path");
const BN = require("bn.js");
const chai = require("chai");
const bnChai = require("bn-chai");
const { ethers } = require("ethers");

const { TruffleLoader } = require("../../packages/package-utils");
const {
Expand Down Expand Up @@ -56,15 +55,14 @@ const loader = new TruffleLoader({
});

const useJsTree = true;
const { provider } = ethers;

let colonyNetwork;
let metaColony;
let clnyToken;
let localSkillId;
let goodClient;

const realProviderPort = process.env.SOLIDITY_COVERAGE ? 8555 : 8545;

const setupNewNetworkInstance = async (MINER1, MINER2) => {
colonyNetwork = await setupColonyNetwork();
({ metaColony, clnyToken } = await setupMetaColonyWithLockedCLNYToken(colonyNetwork));
Expand All @@ -84,7 +82,7 @@ const setupNewNetworkInstance = async (MINER1, MINER2) => {
await colonyNetwork.initialiseReputationMining();
await colonyNetwork.startNextCycle();

goodClient = new ReputationMinerTestWrapper({ loader, realProviderPort, useJsTree, minerAddress: MINER1 });
goodClient = new ReputationMinerTestWrapper({ loader, provider, useJsTree, minerAddress: MINER1 });
};

contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
Expand Down Expand Up @@ -131,7 +129,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
// invalidateHash => confirmNewHash
describe("when dispute flow order is not kept", () => {
it("should prevent a user from jumping ahead during dispute resolution", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -202,7 +200,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
});

it("should prevent a user from attempting to defend a DisputedEntry beyond the size of a round.", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -280,9 +278,9 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
await goodClient.saveCurrentState();
const savedHash = await goodClient.reputationTree.getRootHash();

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);
const badClient2 = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER3 }, 1, 0xffffffffff);
const badClient2 = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER3 }, 1, 0xffffffffff);
await badClient2.initialise(colonyNetwork.address);

await badClient.loadState(savedHash);
Expand Down Expand Up @@ -339,7 +337,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
const clients = await Promise.all(
accountsForTest.map(async (addr, index) => {
const client = new MaliciousReputationMinerExtraRep(
{ loader, realProviderPort, useJsTree, minerAddress: addr },
{ loader, provider, useJsTree, minerAddress: addr },
accountsForTest.length - index,
index,
);
Expand Down Expand Up @@ -559,7 +557,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
await goodClient.saveCurrentState();
const savedHash = await goodClient.reputationTree.getRootHash();

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 5, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 5, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await badClient.loadState(savedHash);
Expand Down Expand Up @@ -634,7 +632,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
await giveUserCLNYTokensAndStake(colonyNetwork, MINER2, DEFAULT_STAKE);
await giveUserCLNYTokensAndStake(colonyNetwork, MINER3, DEFAULT_STAKE);

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -671,7 +669,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

it(`should not allow miners to respond immediately during a dispute,
but they should be able to some time before the end of the window`, async function gateTest() {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await giveUserCLNYTokensAndStake(colonyNetwork, MINER2, DEFAULT_STAKE);
Expand Down Expand Up @@ -784,7 +782,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

describe("when miner misbehaving during confirmJustificationRootHash stage", async () => {
it("should prevent a user from confirming a JRH they can't prove is correct", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -830,7 +828,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

describe("when miner misbehaving during respondToBinarySearchForChallenge stage", async () => {
it("should fail to respondToBinarySearchForChallenge if not consistent with JRH", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -862,7 +860,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
it("incorrectly confirming a binary search result should fail", async () => {
await advanceMiningCycleNoContest({ colonyNetwork, test: this });

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 3, 0xffffffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 3, 0xffffffffffff);
await badClient.initialise(colonyNetwork.address);

const repCycle = await getActiveRepCycle(colonyNetwork);
Expand Down Expand Up @@ -919,10 +917,10 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
const nLogEntries = await repCycle.getReputationUpdateLogLength();
expect(nLogEntries).to.eq.BN(5);

const badClient = new MaliciousReputationMinerExtraRep({ loader, minerAddress: MINER2, realProviderPort, useJsTree }, 14, 0xffffffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, minerAddress: MINER2, provider, useJsTree }, 14, 0xffffffffffff);
await badClient.initialise(colonyNetwork.address);

const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, realProviderPort, useJsTree }, 14, 123456);
const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, provider, useJsTree }, 14, 123456);
await badClient2.initialise(colonyNetwork.address);

await submitAndForwardTimeToDispute([goodClient, badClient], this);
Expand Down Expand Up @@ -976,10 +974,10 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
const nLogEntries = await repCycle.getReputationUpdateLogLength();
expect(nLogEntries).to.eq.BN(9);

const badClient = new MaliciousReputationMinerExtraRep({ loader, minerAddress: MINER2, realProviderPort, useJsTree }, 30, 0xffffffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, minerAddress: MINER2, provider, useJsTree }, 30, 0xffffffffffff);
await badClient.initialise(colonyNetwork.address);

const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, realProviderPort, useJsTree }, 17, 123456);
const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, provider, useJsTree }, 17, 123456);
await badClient2.initialise(colonyNetwork.address);

await submitAndForwardTimeToDispute([goodClient, badClient], this);
Expand Down Expand Up @@ -1014,13 +1012,13 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

await advanceMiningCycleNoContest({ colonyNetwork, test: this });

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 24, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 24, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, realProviderPort, useJsTree }, 4, 123456);
const badClient2 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, provider, useJsTree }, 4, 123456);
await badClient2.initialise(colonyNetwork.address);

const badClient3 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, realProviderPort, useJsTree }, 6, 123456);
const badClient3 = new MaliciousReputationMinerWrongResponse({ loader, minerAddress: MINER1, provider, useJsTree }, 6, 123456);
await badClient3.initialise(colonyNetwork.address);

await submitAndForwardTimeToDispute([goodClient, badClient], this);
Expand Down Expand Up @@ -1053,7 +1051,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
it("should fail to respondToChallenge if any part of the key or hashedKey is wrong", async () => {
await advanceMiningCycleNoContest({ colonyNetwork, test: this });

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 3, 0xffffffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 3, 0xffffffffffff);
await badClient.initialise(colonyNetwork.address);

const repCycle = await getActiveRepCycle(colonyNetwork);
Expand Down Expand Up @@ -1144,7 +1142,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {
});

it("should fail to respondToChallenge if binary search for challenge is not complete yet", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand Down Expand Up @@ -1212,15 +1210,11 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

await goodClient.addLogContentsToReputationTree();

const badClient = new MaliciousReputationMinerExtraRep(
{ loader, realProviderPort, useJsTree, minerAddress: MINER2 },
args.badClient1Argument,
10,
);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, args.badClient1Argument, 10);
await badClient.initialise(colonyNetwork.address);

const badClient2 = new MaliciousReputationMinerWrongProofLogEntry(
{ loader, realProviderPort, useJsTree, minerAddress: MINER3 },
{ loader, provider, useJsTree, minerAddress: MINER3 },
args.badClient2Argument,
);
await badClient2.initialise(colonyNetwork.address);
Expand Down Expand Up @@ -1257,7 +1251,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

describe("when miner misbehaving during confirmNewHash stage", async () => {
it("should only allow the last hash standing to be confirmed", async () => {
const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);

await advanceMiningCycleNoContest({ colonyNetwork, test: this });
Expand All @@ -1283,7 +1277,7 @@ contract("Reputation Mining - disputes resolution misbehaviour", (accounts) => {

const repCycle = await getActiveRepCycle(colonyNetwork);

const badClient = new MaliciousReputationMinerExtraRep({ loader, realProviderPort, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
const badClient = new MaliciousReputationMinerExtraRep({ loader, provider, useJsTree, minerAddress: MINER2 }, 1, 0xfffffffff);
await badClient.initialise(colonyNetwork.address);
await goodClient.addLogContentsToReputationTree();
await badClient.addLogContentsToReputationTree();
Expand Down
Loading

0 comments on commit 9031260

Please sign in to comment.