@@ -62,9 +62,11 @@ class ReputationMiner {
62
62
if ( minerAddress ) {
63
63
this . realWallet = this . realProvider . getSigner ( minerAddress ) ;
64
64
} else {
65
+ // TODO: Check that this wallet can stake?
65
66
this . realWallet = new ethers . Wallet ( privateKey , this . realProvider ) ;
66
- console . log ( "Transactions will be signed from " , this . realWallet . address ) ;
67
+ this . minerAddress = this . realWallet . address ;
67
68
}
69
+ console . log ( `Transactions will be signed from ${ this . realWallet . address } ` ) ;
68
70
}
69
71
70
72
/**
@@ -1347,20 +1349,28 @@ class ReputationMiner {
1347
1349
* @param { Bool } saveHistoricalStates Whether to save historical (valid) states while syncing
1348
1350
* @return {Promise } A promise that resolves once the state is up-to-date
1349
1351
*/
1350
- async sync ( blockNumber , saveHistoricalStates = false ) {
1351
- if ( ! blockNumber ) {
1352
- throw new Error ( "Block number not supplied to sync" ) ;
1353
- }
1352
+ async sync ( blockNumber = 1 , saveHistoricalStates = false ) {
1353
+
1354
1354
// Get the events
1355
1355
const filter = this . colonyNetwork . filters . ReputationMiningCycleComplete ( null , null ) ;
1356
1356
filter . fromBlock = blockNumber ;
1357
1357
const events = await this . realProvider . getLogs ( filter ) ;
1358
1358
let localHash = await this . reputationTree . getRootHash ( ) ;
1359
+
1360
+ console . log ( `Beginning sync from block ${ blockNumber } and hash ${ localHash } with ${ events . length } cycles to go` )
1361
+
1359
1362
let applyLogs = false ;
1363
+ // We're going to apply logs if:
1364
+ // - We're syncing from a user-provided hash (which is this conditional)
1365
+ // - We find a match for an on-chain state in our db (which is the loop below)
1366
+ // - We are syncing from scratch (e.g. no user-given or on-chain state is found, localHash is 0)
1367
+ if ( localHash !== `0x${ new BN ( 0 ) . toString ( 16 , 64 ) } ` ) {
1368
+ applyLogs = true ;
1369
+ }
1360
1370
1361
1371
// Run through events backwards find the most recent one that we know...
1362
1372
let syncFromIndex = 0 ;
1363
- for ( let i = events . length - 1 ; i >= 0 ; i -= 1 ) {
1373
+ for ( let i = events . length - 1 ; i >= 0 ; i -= 1 ) {
1364
1374
const event = events [ i ] ;
1365
1375
const hash = event . data . slice ( 0 , 66 ) ;
1366
1376
const nLeaves = ethers . BigNumber . from ( `0x${ event . data . slice ( 66 , 130 ) } ` ) ;
@@ -1369,22 +1379,24 @@ class ReputationMiner {
1369
1379
if ( res . n === 1 ) {
1370
1380
// We know that state! We can just sync from the next one...
1371
1381
syncFromIndex = i + 1 ;
1382
+ localHash = hash ;
1372
1383
await this . loadState ( hash ) ;
1373
1384
applyLogs = true ;
1374
1385
break ;
1375
1386
}
1376
1387
}
1377
1388
1378
- // We're not going to apply the logs unless we're syncing from scratch (which is this if statement)
1379
- // or we find a hash that we recognise as our current state, and we're going to sync from there (which
1380
- // is the if statement at the end of the loop below
1389
+ console . log ( `Syncing forward from index ${ syncFromIndex } with local hash ${ localHash } ` )
1390
+
1381
1391
if ( localHash === `0x${ new BN ( 0 ) . toString ( 16 , 64 ) } ` ) {
1382
1392
applyLogs = true ;
1383
1393
}
1384
1394
1385
1395
for ( let i = syncFromIndex ; i < events . length ; i += 1 ) {
1386
- console . log ( `${ new Date ( ) . toLocaleTimeString ( ) } : Syncing mining cycle ${ i + 1 } of ${ events . length } ...` )
1387
1396
const event = events [ i ] ;
1397
+ const time = new Date ( ) . toLocaleTimeString ( ) ;
1398
+ console . log ( `${ time } : Syncing mining cycle ${ i + 1 } of ${ events . length } , from block ${ event . blockNumber } and localHash ${ localHash } ` ) ;
1399
+
1388
1400
if ( i === 0 ) {
1389
1401
// If we are syncing from the very start of the reputation history, the block
1390
1402
// before the very first 'ReputationMiningCycleComplete' does not have an
@@ -1433,7 +1445,8 @@ class ReputationMiner {
1433
1445
localHash = await this . reputationTree . getRootHash ( ) ;
1434
1446
const localNLeaves = await this . nReputations ;
1435
1447
if ( localHash !== currentHash || ! currentNLeaves . eq ( localNLeaves ) ) {
1436
- console . log ( "ERROR: Sync failed and did not recover" ) ;
1448
+ console . log ( `Error: Sync failed and did not recover, final hash does not match ${ currentHash } .` ) ;
1449
+ console . log ( "If the miner has been syncing for a while, try restarting, as the mining cycle may have advanced." ) ;
1437
1450
} else {
1438
1451
console . log ( "Sync successful, even if there were warnings above" ) ;
1439
1452
}
@@ -1504,7 +1517,9 @@ class ReputationMiner {
1504
1517
}
1505
1518
const currentStateHash = await this . reputationTree . getRootHash ( ) ;
1506
1519
if ( currentStateHash !== reputationRootHash ) {
1507
- console . log ( "WARNING: The supplied state failed to be recreated successfully. Are you sure it was saved?" ) ;
1520
+ console . log ( `WARNING: The supplied state ${ reputationRootHash } failed to be recreated successfully. Are you sure it was saved?` ) ;
1521
+ } else {
1522
+ console . log ( `Reputation state ${ reputationRootHash } was loaded successfully.` ) ;
1508
1523
}
1509
1524
}
1510
1525
0 commit comments