@@ -16,7 +16,7 @@ const minStake = ethers.BigNumber.from(10).pow(18).mul(2000);
1616
1717const DAY_IN_SECONDS = 60 * 60 * 24 ;
1818
19- const BLOCK_PAGING_SIZE = 1000000 ;
19+ const BLOCK_PAGING_SIZE = 25000 ;
2020
2121class ReputationMiner {
2222 /**
@@ -1365,44 +1365,54 @@ class ReputationMiner {
13651365 const latestBlockNumber = await this . realProvider . getBlockNumber ( ) ;
13661366
13671367 const filter = this . colonyNetwork . filters . ReputationMiningCycleComplete ( null , null ) ;
1368- filter . toBlock = Math . max ( blockNumber - 1 , 0 ) ;
1369- while ( filter . toBlock !== latestBlockNumber ) {
1370- filter . fromBlock = filter . toBlock + 1 ;
1371- filter . toBlock = Math . min ( filter . fromBlock + BLOCK_PAGING_SIZE , latestBlockNumber ) ;
1372- const partialEvents = await this . realProvider . getLogs ( filter ) ;
1373- events = events . concat ( partialEvents ) ;
1374- }
1368+ let syncFromIndex = - 1 ;
1369+ let foundKnownState = false ;
1370+ filter . fromBlock = latestBlockNumber + 1 ;
1371+ filter . toBlock = filter . fromBlock ;
13751372 let localHash = await this . reputationTree . getRootHash ( ) ;
13761373 let applyLogs = false ;
13771374
1378- // Run through events backwards find the most recent one that we know...
1379- let syncFromIndex = 0 ;
1380- for ( let i = events . length - 1 ; i >= 0 ; i -= 1 ) {
1381- const event = events [ i ] ;
1382- const hash = event . data . slice ( 0 , 66 ) ;
1383- const nLeaves = ethers . BigNumber . from ( `0x${ event . data . slice ( 66 , 130 ) } ` ) ;
1384- // Do we have such a state?
1385- const res = await this . queries . getReputationStateCount . get ( hash , nLeaves . toString ( ) ) ;
1386- if ( res . n === 1 ) {
1387- // We know that state! We can just sync from the next one...
1388- syncFromIndex = i + 1 ;
1389- await this . loadState ( hash ) ;
1390- applyLogs = true ;
1391- break ;
1375+ while ( foundKnownState === false && filter . toBlock > blockNumber ) {
1376+ filter . toBlock = filter . fromBlock - 1 ;
1377+ filter . fromBlock = Math . max ( filter . toBlock - BLOCK_PAGING_SIZE + 1 , blockNumber ) ;
1378+ // console.log(filter);
1379+ const partialEvents = await this . realProvider . getLogs ( filter ) ;
1380+ events = events . concat ( partialEvents . reverse ( ) ) ;
1381+
1382+ // Run through events backwards find the most recent one that we know...
1383+ for ( let i = 0 ; i < events . length ; i += 1 ) {
1384+ const event = events [ i ] ;
1385+ const hash = event . data . slice ( 0 , 66 ) ;
1386+ const nLeaves = ethers . BigNumber . from ( `0x${ event . data . slice ( 66 , 130 ) } ` ) ;
1387+ // Do we have such a state?
1388+ const res = await this . queries . getReputationStateCount . get ( hash , nLeaves . toString ( ) ) ;
1389+ if ( res . n === 1 ) {
1390+ console . log ( "KNOWN" )
1391+ // We know that state! We can just sync from the next one...
1392+ syncFromIndex = i - 1 ;
1393+ await this . loadState ( hash ) ;
1394+ applyLogs = true ;
1395+ foundKnownState = true ;
1396+ break ;
1397+ }
13921398 }
13931399 }
13941400
1401+ if ( syncFromIndex === - 1 ) {
1402+ syncFromIndex = events . length - 1 ;
1403+ }
1404+
13951405 // We're not going to apply the logs unless we're syncing from scratch (which is this if statement)
13961406 // or we find a hash that we recognise as our current state, and we're going to sync from there (which
13971407 // is the if statement at the end of the loop below
13981408 if ( localHash === `0x${ new BN ( 0 ) . toString ( 16 , 64 ) } ` ) {
13991409 applyLogs = true ;
14001410 }
14011411
1402- for ( let i = syncFromIndex ; i < events . length ; i + = 1 ) {
1403- console . log ( `Syncing mining cycle ${ i + 1 } of ${ events . length } ...` )
1412+ for ( let i = syncFromIndex ; i >= 0 ; i - = 1 ) {
1413+ console . log ( `Syncing mining cycle ${ syncFromIndex - i + 1 } of ${ syncFromIndex + 1 } ...` )
14041414 const event = events [ i ] ;
1405- if ( i === 0 ) {
1415+ if ( i === events . length - 1 && ! foundKnownState ) {
14061416 // If we are syncing from the very start of the reputation history, the block
14071417 // before the very first 'ReputationMiningCycleComplete' does not have an
14081418 // active reputation cycle. So we skip it if 'fromBlock' has not been judiciously
@@ -1435,7 +1445,7 @@ class ReputationMiner {
14351445 }
14361446
14371447 // Some more cycles might have completed since we started syncing
1438- const lastEventBlock = events [ events . length - 1 ] . blockNumber
1448+ const lastEventBlock = events [ 0 ] . blockNumber
14391449 filter . fromBlock = lastEventBlock ;
14401450 filter . toBlock = "latest" ;
14411451 const sinceEvents = await this . realProvider . getLogs ( filter ) ;
0 commit comments