Skip to content

Commit

Permalink
Additional updates to reputation miner
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Jan 24, 2022
1 parent 5df4220 commit 2f9fa0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ ganache-accounts.json
.vscode/
yarn-error.log
reputations.json
reputationStates.sqlite
reputationStates.sqlite-shm
reputationStates.sqlite-wal
truffle-security-output.json
.coverage_contracts
etherrouter-address.json
ganache-chain-db
.DS_Store
*.sqlite*
15 changes: 11 additions & 4 deletions packages/reputation-miner/ReputationMiner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ReputationMiner {
this.minerAddress = this.realWallet.address;
// TODO: Check that this wallet can stake?
this.minerAddress = this.realWallet.address;
console.log("Transactions will be signed from ", this.realWallet.address);
console.log(`Transactions will be signed from ${this.realWallet.address}`);
}
}

Expand Down Expand Up @@ -1254,6 +1254,8 @@ class ReputationMiner {
if (!blockNumber) {
throw new Error("Block number not supplied to sync");
}
console.log(`Beginning sync from block ${blockNumber}`)

// Get the events
const filter = this.colonyNetwork.filters.ReputationMiningCycleComplete(null, null);
filter.fromBlock = blockNumber;
Expand All @@ -1263,7 +1265,7 @@ class ReputationMiner {

// Run through events backwards find the most recent one that we know...
let syncFromIndex = 0;
for (let i = events.length - 1 ; i >= 0 ; i -= 1){
for (let i = events.length - 1 ; i >= 0 ; i -= 1) {
const event = events[i];
const hash = event.data.slice(0, 66);
const nLeaves = ethers.BigNumber.from(`0x${event.data.slice(66, 130)}`);
Expand All @@ -1286,8 +1288,10 @@ class ReputationMiner {
}

for (let i = syncFromIndex; i < events.length; i += 1) {
console.log(`${new Date().toLocaleTimeString()}: Syncing mining cycle ${i + 1} of ${events.length}...`)
const event = events[i];
console.log(
`${new Date().toLocaleTimeString()}: Syncing mining cycle ${i + 1} of ${events.length}, from block ${event.blockNumber} and localHash ${localHash}`
);
if (i === 0) {
// If we are syncing from the very start of the reputation history, the block
// before the very first 'ReputationMiningCycleComplete' does not have an
Expand Down Expand Up @@ -1327,7 +1331,8 @@ class ReputationMiner {
localHash = await this.reputationTree.getRootHash();
const localNLeaves = await this.nReputations;
if (localHash !== currentHash || !currentNLeaves.eq(localNLeaves)) {
console.log("ERROR: Sync failed and did not recover");
console.log(`Error: Sync failed and did not recover, final hash does not match ${currentHash}.`);
console.log("If the miner has been syncing for a while, try restarting, as the mining cycle may have advanced.");
} else {
console.log("Sync successful, even if there were warnings above");
}
Expand Down Expand Up @@ -1401,6 +1406,8 @@ class ReputationMiner {
const currentStateHash = await this.reputationTree.getRootHash();
if (currentStateHash !== reputationRootHash) {
console.log("WARNING: The supplied state failed to be recreated successfully. Are you sure it was saved?");
} else {
console.log(`Reputation state ${reputationRootHash} was loaded successfully.`);
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/reputation-miner/ReputationMinerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class ReputationMinerClient {
* @param {string} colonyNetworkAddress The address of the current `ColonyNetwork` contract
* @return {Promise}
*/
async initialise(colonyNetworkAddress, startingBlock) {
async initialise(colonyNetworkAddress, startingBlock, startingHash) {
this.resolveBlockChecksFinished = undefined;
await this._miner.initialise(colonyNetworkAddress);

Expand All @@ -234,6 +234,9 @@ class ReputationMinerClient {
await this._miner.loadState(latestReputationHash);
if (this._miner.nReputations.eq(0)) {
this._adapter.log("Latest state not found - need to sync");
if (startingHash !== undefined) {
await this._miner.loadState(startingHash);
}
await this._miner.sync(startingBlock, true);
}

Expand Down Expand Up @@ -422,7 +425,7 @@ class ReputationMinerClient {
const {entryIndex} = this.best12Submissions[this.submissionIndex];
const canSubmit = await this._miner.submissionPossible(entryIndex);
if (canSubmit) {
this._adapter.log("⏰ Looks like it's time to submit an entry to the current cycle");
this._adapter.log(`⏰ ${new Date().toLocaleTimeString()}: Looks like it's time to submit an entry to the current cycle`);
this.submissionIndex += 1;
await this.updateGasEstimate('average');
await this.submitEntry(entryIndex);
Expand Down Expand Up @@ -675,7 +678,7 @@ class ReputationMinerClient {

async submitEntry(entryIndex) {
const rootHash = await this._miner.getRootHash();
this._adapter.log(`#️⃣ Miner ${this._miner.minerAddress} submitting new reputation hash ${rootHash} at entry index ${entryIndex.toNumber()}`);
this._adapter.log(`#️⃣ Submitting new reputation hash ${rootHash} at entry index ${entryIndex.toNumber()}`);

// Submit hash
let submitRootHashTx = await this._miner.submitRootHash(entryIndex);
Expand All @@ -693,7 +696,7 @@ class ReputationMinerClient {
const addr = await this._miner.colonyNetwork.getReputationMiningCycle(true);
const repCycle = new ethers.Contract(addr, this._miner.repCycleContractDef.abi, this._miner.realWallet);

this._adapter.log("⏰ Looks like it's time to confirm the new hash");
this._adapter.log(`⏰ ${new Date().toLocaleTimeString()}: Looks like it's time to confirm the new hash`);
// Confirm hash
const [round] = await this._miner.getMySubmissionRoundAndIndex();
if (round && round.gte(0)) {
Expand Down
9 changes: 6 additions & 3 deletions packages/reputation-miner/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { argv } = require("yargs")
.option('privateKey', {string:true})
.option('colonyNetworkAddress', {string:true})
.option('minerAddress', {string:true})
.option('startingHash', {string:true})
.option('providerAddress', {type: "array", default: []});
const ethers = require("ethers");
const backoff = require("exponential-backoff").backOff;
Expand All @@ -33,6 +34,7 @@ const {
oraclePort,
processingDelay,
adapterLabel,
startingHash,
} = argv;

class RetryProvider extends ethers.providers.StaticJsonRpcProvider {
Expand All @@ -52,11 +54,11 @@ class RetryProvider extends ethers.providers.StaticJsonRpcProvider {
return backoff(() => super.getNetwork(), {retry: RetryProvider.attemptCheck});
}

// This should return a Promise (and may throw erros)
// This should return a Promise (and may throw errors)
// method is the method name (e.g. getBalance) and params is an
// object with normalized values passed in, depending on the method
perform(method, params) {
return backoff(() => super.perform(method, params), {retry: RetryProvider.attemptCheck});
return backoff(() => super.perform(method, params), {retry: RetryProvider.attemptCheck, startingDelay: 1000});
}
}

Expand Down Expand Up @@ -117,4 +119,5 @@ const client = new ReputationMinerClient({
oraclePort,
processingDelay
});
client.initialise(colonyNetworkAddress, syncFrom);

client.initialise(colonyNetworkAddress, syncFrom, startingHash);

0 comments on commit 2f9fa0e

Please sign in to comment.