[Blockchain Watcher] (FIX) Fix quantity logs process per block (#1044)
* Add divided blocks method in getBlocks * Run prettier --------- Co-authored-by: julian merlo <julianmerlo@julians-MacBook-Pro.local>
This commit is contained in:
parent
63819a6e42
commit
13ec7846c0
|
@ -51,7 +51,7 @@
|
||||||
},
|
},
|
||||||
"arbitrum": {
|
"arbitrum": {
|
||||||
"network": "mainnet",
|
"network": "mainnet",
|
||||||
"rpcs": ["https://arb-mainnet-public.unifra.io"]
|
"rpcs": ["https://arb1.arbitrum.io/rpc"]
|
||||||
},
|
},
|
||||||
"optimism": {
|
"optimism": {
|
||||||
"network": "mainnet",
|
"network": "mainnet",
|
||||||
|
|
|
@ -44,33 +44,44 @@ export class EvmJsonRPCBlockRepository implements EvmBlockRepository {
|
||||||
async getBlocks(chain: string, blockNumbers: Set<bigint>): Promise<Record<string, EvmBlock>> {
|
async getBlocks(chain: string, blockNumbers: Set<bigint>): Promise<Record<string, EvmBlock>> {
|
||||||
if (!blockNumbers.size) return {};
|
if (!blockNumbers.size) return {};
|
||||||
|
|
||||||
const reqs: any[] = [];
|
let combinedResults: ResultBlocks[] = [];
|
||||||
for (let blockNumber of blockNumbers) {
|
|
||||||
const blockNumberStrParam = `${HEXADECIMAL_PREFIX}${blockNumber.toString(16)}`;
|
|
||||||
const blockNumberStrId = blockNumber.toString();
|
|
||||||
|
|
||||||
reqs.push({
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id: blockNumberStrId,
|
|
||||||
method: "eth_getBlockByNumber",
|
|
||||||
params: [blockNumberStrParam, false],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const chainCfg = this.getCurrentChain(chain);
|
const chainCfg = this.getCurrentChain(chain);
|
||||||
let results: (undefined | { id: string; result?: EvmBlock; error?: ErrorBlock })[];
|
const batches = this.divideIntoBatches(blockNumbers, 9);
|
||||||
try {
|
|
||||||
results = await this.httpClient.post<typeof results>(chainCfg.rpc.href, reqs, {
|
for (const batch of batches) {
|
||||||
timeout: chainCfg.timeout,
|
const reqs: any[] = [];
|
||||||
retries: chainCfg.retries,
|
for (let blockNumber of batch) {
|
||||||
});
|
const blockNumberStrParam = `${HEXADECIMAL_PREFIX}${blockNumber.toString(16)}`;
|
||||||
} catch (e: HttpClientError | any) {
|
const blockNumberStrId = blockNumber.toString();
|
||||||
this.handleError(chain, e, "getBlocks", "eth_getBlockByNumber");
|
|
||||||
throw e;
|
reqs.push({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id: blockNumberStrId,
|
||||||
|
method: "eth_getBlockByNumber",
|
||||||
|
params: [blockNumberStrParam, false],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let results: (undefined | ResultBlocks)[] = [];
|
||||||
|
try {
|
||||||
|
results = await this.httpClient.post<typeof results>(chainCfg.rpc.href, reqs, {
|
||||||
|
timeout: chainCfg.timeout,
|
||||||
|
retries: chainCfg.retries,
|
||||||
|
});
|
||||||
|
} catch (e: HttpClientError | any) {
|
||||||
|
this.handleError(chain, e, "getBlocks", "eth_getBlockByNumber");
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let result of results) {
|
||||||
|
if (result) {
|
||||||
|
combinedResults.push(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results && results.length) {
|
if (combinedResults && combinedResults.length) {
|
||||||
return results
|
return combinedResults
|
||||||
.map(
|
.map(
|
||||||
(
|
(
|
||||||
response: undefined | { id: string; result?: EvmBlock; error?: ErrorBlock },
|
response: undefined | { id: string; result?: EvmBlock; error?: ErrorBlock },
|
||||||
|
@ -103,15 +114,13 @@ export class EvmJsonRPCBlockRepository implements EvmBlockRepository {
|
||||||
|
|
||||||
const msg = `[${chain}][getBlocks] Got error ${
|
const msg = `[${chain}][getBlocks] Got error ${
|
||||||
response?.error?.message
|
response?.error?.message
|
||||||
} for eth_getBlockByNumber for ${response?.id ?? reqs[idx].id} on ${
|
} for eth_getBlockByNumber for ${response?.id ?? idx} on ${chainCfg.rpc.hostname}`;
|
||||||
chainCfg.rpc.hostname
|
|
||||||
}`;
|
|
||||||
|
|
||||||
this.logger.error(msg);
|
this.logger.error(msg);
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Unable to parse result of eth_getBlockByNumber[${chain}] for ${
|
`Unable to parse result of eth_getBlockByNumber[${chain}] for ${
|
||||||
response?.id ?? reqs[idx].id
|
response?.id ?? idx
|
||||||
}: ${msg}`
|
}: ${msg}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +133,7 @@ export class EvmJsonRPCBlockRepository implements EvmBlockRepository {
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Unable to parse ${
|
`Unable to parse ${
|
||||||
results?.length ?? 0
|
combinedResults?.length ?? 0
|
||||||
} blocks for eth_getBlockByNumber for numbers ${blockNumbers} on ${chainCfg.rpc.hostname}`
|
} blocks for eth_getBlockByNumber for numbers ${blockNumbers} on ${chainCfg.rpc.hostname}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -310,7 +319,7 @@ export class EvmJsonRPCBlockRepository implements EvmBlockRepository {
|
||||||
* This method divide in batches the object to send, because we have one restriction about how many object send to the endpoint
|
* This method divide in batches the object to send, because we have one restriction about how many object send to the endpoint
|
||||||
* the maximum is 10 object per request
|
* the maximum is 10 object per request
|
||||||
*/
|
*/
|
||||||
private divideIntoBatches(set: Set<string>, batchSize = 10) {
|
private divideIntoBatches(set: Set<string | bigint>, batchSize = 10) {
|
||||||
const batches = [];
|
const batches = [];
|
||||||
let batch: any[] = [];
|
let batch: any[] = [];
|
||||||
|
|
||||||
|
@ -379,3 +388,9 @@ type ResultTransactionReceipt = {
|
||||||
result: ReceiptTransaction;
|
result: ReceiptTransaction;
|
||||||
error?: ErrorBlock;
|
error?: ErrorBlock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type ResultBlocks = {
|
||||||
|
id: string;
|
||||||
|
result?: EvmBlock;
|
||||||
|
error?: ErrorBlock;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue