Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added batchSize configuration option for parallelizing requests in EsploraProvider #22

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ app:
providerType: <providerType>
esplora:
url: <host>
batchSize: <batchSize>
1 change: 1 addition & 0 deletions config/dev.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ app:
providerType: ESPLORA
esplora:
url: https://blockstream.info
batchSize: 5
47 changes: 27 additions & 20 deletions src/block-data-providers/esplora.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class EsploraProvider
protected readonly operationStateKey = 'esplora-operation-state';
private readonly baseUrl: string;
private isSyncing = false;
private readonly batchSize: number;

constructor(
private readonly configService: ConfigService,
Expand All @@ -73,6 +74,8 @@ export class EsploraProvider
) {
super(indexerService, operationStateService);

this.batchSize = this.configService.get<number>('esplora.batchSize');

let pathPrefix;
switch (this.configService.get<BitcoinNetwork>('app.network')) {
case BitcoinNetwork.TESTNET:
Expand Down Expand Up @@ -161,26 +164,30 @@ export class EsploraProvider
private async processBlock(height: number, hash: string) {
const txids = await this.getTxidsForBlock(hash);

// this is not very efficient
// at the time of implementation, I'm using a public esplora instance,
// so there is a rate limit on the number of requests
// in the future, we should add a flag to parallelize this
for (let i = 1; i < txids.length; i++) {
const txid = txids[i];
const tx = await this.getTx(txid);
const vin: TransactionInput[] = tx.vin.map((input) => ({
txid: input.txid,
vout: input.vout,
scriptSig: input.scriptsig,
prevOutScript: input.prevout.scriptpubkey,
witness: input.witness,
}));
const vout = tx.vout.map((output) => ({
scriptPubKey: output.scriptpubkey,
value: output.value,
}));

await this.indexTransaction(txid, vin, vout, height, hash);
for (let i = 1; i < txids.length; i += this.batchSize) {
const batch = txids.slice(
i,
Math.min(i + this.batchSize, txids.length),
);

await Promise.all(
batch.map(async (txid) => {
const tx = await this.getTx(txid);
const vin: TransactionInput[] = tx.vin.map((input) => ({
txid: input.txid,
vout: input.vout,
scriptSig: input.scriptsig,
prevOutScript: input.prevout.scriptpubkey,
witness: input.witness,
}));
const vout = tx.vout.map((output) => ({
scriptPubKey: output.scriptpubkey,
value: output.value,
}));

await this.indexTransaction(txid, vin, vout, height, hash);
}, this),
);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/configuration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class EsploraConfig {
require_host: true,
})
url: string;

@IsInt()
@Min(1)
@Max(100)
batchSize: number;
}

export class Config {
Expand Down