From 031554d4842903fdf61c65e65f0cfe77384e1e59 Mon Sep 17 00:00:00 2001 From: Daniel Kronovet Date: Mon, 24 Jan 2022 19:50:12 +0100 Subject: [PATCH] Additional updates to reputation miner --- .gitignore | 5 ++--- packages/reputation-miner/ReputationMiner.js | 15 +++++++++++---- .../reputation-miner/ReputationMinerClient.js | 11 +++++++---- packages/reputation-miner/bin/index.js | 7 +++++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index cdc278b55d..2b42eebc70 100644 --- a/.gitignore +++ b/.gitignore @@ -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* \ No newline at end of file diff --git a/packages/reputation-miner/ReputationMiner.js b/packages/reputation-miner/ReputationMiner.js index 1acc3938d7..a9218c90b5 100644 --- a/packages/reputation-miner/ReputationMiner.js +++ b/packages/reputation-miner/ReputationMiner.js @@ -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}`); } } @@ -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; @@ -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)}`); @@ -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 @@ -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"); } @@ -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.`); } } diff --git a/packages/reputation-miner/ReputationMinerClient.js b/packages/reputation-miner/ReputationMinerClient.js index efe7a454d3..a0a57e5b8d 100644 --- a/packages/reputation-miner/ReputationMinerClient.js +++ b/packages/reputation-miner/ReputationMinerClient.js @@ -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); @@ -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); } @@ -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); @@ -688,7 +691,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); @@ -706,7 +709,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)) { diff --git a/packages/reputation-miner/bin/index.js b/packages/reputation-miner/bin/index.js index 17916dab71..6337e97d23 100644 --- a/packages/reputation-miner/bin/index.js +++ b/packages/reputation-miner/bin/index.js @@ -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; @@ -33,6 +34,7 @@ const { oraclePort, processingDelay, adapterLabel, + startingHash, } = argv; class RetryProvider extends ethers.providers.StaticJsonRpcProvider { @@ -53,7 +55,7 @@ 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) { @@ -118,4 +120,5 @@ const client = new ReputationMinerClient({ oraclePort, processingDelay }); -client.initialise(colonyNetworkAddress, syncFrom); + +client.initialise(colonyNetworkAddress, syncFrom, startingHash);