WeChartWeb3 icon indicating copy to clipboard operation
WeChartWeb3 copied to clipboard

Limit the number of requests in a batch call (<500)

Open developer9 opened this issue 3 years ago • 4 comments

Hey mate, Hope you're well.

Is it easy to limit the number of requests in a batch call for instance, less than 500.

For example, analyseBlock-> getBlockSyncEvents->getReceiptsBatch(transactions) is returning error due to the batch size being greater than 500.

Transaction hashes are 1200 and the node provider is limiting to 500 in a batch call.

Can we update getReceiptsBatch(hashes) function in this file ./workers/lib/scrape.block.past.js to split the hashes array into multiple as per the set size like 500 in each array.

Any thoughts please?

developer9 avatar Aug 31 '22 15:08 developer9

Ehi, thanks for the report, going to check this days and once done i'll update you :+1: :smile:

Linch1 avatar Sep 01 '22 07:09 Linch1

Getblock limit it

tanhsnkt1997 avatar Sep 03 '22 08:09 tanhsnkt1997

image

tanhsnkt1997 avatar Sep 03 '22 08:09 tanhsnkt1997

Hey @Linch1

Just wondering if the below works, I am not sure if I am doing correct combining the receipts data from the node provider.

New function to slice hashes array into multiple arrays.

function sliceIntoChunks(arr, chunkSize) {
    const res = [];
    for (let i = 0; i < arr.length; i += chunkSize) {
        const chunk = arr.slice(i, i + chunkSize);
        res.push(chunk);
    }
    return res;
}
async function getReceiptsBatch( hashes ){

    if(!hashes.length) return [];

    let start = Date.now();
    let body = [];
    let batchSize = hashes.length/500;
    let recResult= [];
if (batchSize>1){
    const chunkSize = 500;
const chunkArr = sliceIntoChunks(hashes,500);

console.log("chunkArray Length " + chunkArr.length);
for (let j=0; j < chunkArr.length; j++){
    console.log("Chunk Array length: " + chunkArr[j].length)
    for(let i = 0; i < chunkArr[j].length; i++ ){
       
        let hash = chunkArr[j][i];
        
        body.push({"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":[hash], "id": i })
    };
    const receipts = await axios.post(
        scraperConfig[process.env.CHAIN_ID].http_provider, 
        body, 
        { headers: { 'Content-Type': 'application/json' } }
    );
    body = [];
    recResult.concat(receipts.data);
    }

}else {
    let start = Date.now();
    let body = [];
    for(let i = 0; i < hashes.length; i++ ){
        let hash = hashes[i];
        body.push({"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":[hash], "id": i })
    };
    const receipts = await axios.post(
        scraperConfig[process.env.CHAIN_ID].http_provider, 
        body, 
        { headers: { 'Content-Type': 'application/json' } }
    );
    return receipts.data;

}

    return recResult;
}

developer9 avatar Sep 06 '22 13:09 developer9