0

I am trying to measure the latency between the block production (eg block.timestamp) and the data reception (eg Date.now());

I tried with different nodes (Infura, Amazon Managed Blockchain and my very own node) and I am surprised to see a latency of close to 15 seconds, eg 1 block time.

Could someone please explained if my methodology is wrong or if I am misunderstanding something ?

My code below

const { ethers } = require("ethers");
const providerWs = new ethers.providers.WebSocketProvider('ws://localhost:3001');

function getTimestamp() {
    return Math.floor(Date.now() / 1000);
}

var cnt = 0;
var s = 0;

providerWs.on('block', (blockNumber) => {
    providerWs.getBlock(blockNumber).then(
        res => {
            let ts = getTimestamp();
            let diff = ts-res.timestamp;
            cnt++;
            s+=diff;
            console.log('[' + diff + '][' + ts + '][' + res.timestamp + '][' + res.number + '][' + res.hash + '][' + res.transactions.length + ']');
            if (0 == cnt%10) {
                console.log('[' + cnt + '][' + s/cnt  + ']');
            }
        },
        err => {
            console.log('ERROR [' + err + ']');
        }
    );
});

And a (partial) output

enter image description here

Thank you very much in advance for your help !

2
  • Have you checked the time it takes to get a block content alone (without subscribing to the last, just a random one) ? I guess the asyns should already takes a couple of sec, no? Nov 8 '21 at 13:07
  • That's a good point, I actually changed my code to set ts = getTimestamp() before getting the block's content (providerWs.getBlock(blockNumber)) but it doesn't change the latency I am seeing (probably because I am running my own node so getBlock is actually almost instantaneous)
    – Charles
    Nov 8 '21 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.