Skip to content

Commit

Permalink
Aggressively backfill the block history controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpshr committed Aug 1, 2018
1 parent 1475b29 commit da8906a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/BlockHistoryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class BlockHistoryController extends BaseController<BlockHistoryState, Bl
private internalBlockTracker: any;

private backfill() {
this.backfilled = false;
this.internalBlockTracker &&
this.internalBlockTracker.once('block', async (block: Block) => {
const currentBlockNumber = Number.parseInt(block.number, 16);
Expand Down Expand Up @@ -180,7 +181,7 @@ export class BlockHistoryController extends BaseController<BlockHistoryState, Bl
this.internalBlockTracker && this.internalBlockTracker.removeAllListeners();
this.internalBlockTracker = blockTracker;
this.internalBlockTracker.on('block', this.onBlock.bind(this));
!this.backfilled && this.backfill();
this.backfill();
}

/**
Expand All @@ -190,6 +191,7 @@ export class BlockHistoryController extends BaseController<BlockHistoryState, Bl
*/
set provider(provider: any) {
this.ethQuery = new EthQuery(provider);
this.backfill();
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export class NetworkController extends BaseController<NetworkState, NetworkConfi
this.lookupNetwork();
}

private registerProvider() {
this.provider.on('block', this.verifyNetwork.bind(this));
this.provider.on('error', this.verifyNetwork.bind(this));
this.ethQuery = new EthQuery(this.provider);
}

private setupInfuraProvider(type: NetworkType) {
const infuraProvider = createInfuraProvider({ network: type });
const infuraSubprovider = new Subprovider(infuraProvider);
Expand Down Expand Up @@ -109,6 +115,7 @@ export class NetworkController extends BaseController<NetworkState, NetworkConfi
private updateProvider(provider: any) {
this.provider && this.provider.stop();
this.provider = provider;
this.registerProvider();
}

private verifyNetwork() {
Expand Down Expand Up @@ -144,9 +151,7 @@ export class NetworkController extends BaseController<NetworkState, NetworkConfi
this.internalProviderConfig = providerConfig;
const { type, rpcTarget } = this.state.provider;
this.initializeProvider(type, rpcTarget);
this.provider.on('block', this.verifyNetwork.bind(this));
this.provider.on('error', this.verifyNetwork.bind(this));
this.ethQuery = new EthQuery(this.provider);
this.registerProvider();
this.lookupNetwork();
}

Expand Down

0 comments on commit da8906a

Please sign in to comment.