Missing events in the latest block

Hi,

We noticed that sometimes querying contract events in the latest block returns empty list even though the block does contain the relevant events. And if I wait for sometime and query the same block again the events will be there. Is this due to the inconsistent state between the nodes behind the load balancer?

I wrote a simple script to demonstrate the problem

const { ethers } = require("ethers")

const infuraId = “…”

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

const events = {}

function addEvents(block, lookback, numEvents) {
events[block] = events[block] || {}
events[block][lookback] = numEvents
}

async function queryEvents(block, lookback, contract, filter) {
block = block - lookback
const events = await contract.queryFilter(filter, block, block)
addEvents(block, lookback, events.length)
}

async function main() {
const provider = new ethers.providers.InfuraProvider(1, infuraId)
provider.pollingInterval = 10 * 1000

const abi = [
    "event Transfer(address indexed from, address indexed to, uint256 value)"
]
// https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7
const address = "0xdAC17F958D2ee523a2206206994597C13D831ec7"
const contract = new ethers.Contract(address, abi, provider)
const filter = contract.filters.Transfer()
const lookbacks = [0, 3, 5]
const maxLookback = lookbacks[lookbacks.length-1]

provider.on('block', async (blockNumber) => {
    for (const lookback of lookbacks) {
        await queryEvents(blockNumber, lookback, contract, filter)
    }

    console.log(`${blockNumber}:`, Object.values(events[blockNumber - maxLookback] || {}))
})

while (true) {
    await sleep(100)
}

}

main()

The output of the script is like this

15159969: [ 0, 22, 22 ]
15159970: [ 0, 0, 0 ]
15159971: [ 11, 11, 11 ]
15159972: [ 17, 17, 17 ]
15159974: [ 14, 14, 14 ]

Each line means how many events are found when querying after 0, 3 and 5 blocks.
Most of the time they’re pretty consistent but you can see on block 15159969 it didn’t find any when queried immediately but there’re acutally 22 events in it.

1 Like

Hey, this might be actually caused by this https://github.com/ethers-io/ethers.js/issues/1814
Can you make sure that you are using the latest ethers version ? If you still see the issue try to see if using InfuraWebSocketProvider instead of InfuraProvider does any change.

1 Like

Hi, we are experiencing the same issue quite often. We use the rpc api directly (without ethers.js) by calling eth_getBlockByNumber and then eth_getLogs with the block number returned by the first request.
Events are missing from the response. Running the same eth_getLogs some time later returns the events missing from the first time.

1 Like

hi @mich can you please open a ticket about this, it would help to see your repro script. thanks !

1 Like

I’ve searched here and also in Google. Looks like this is an old issue that Infura wouldn’t fix and you also don’t mention it in your documentation. The damage that this caused to us is rather contained - but it’s infuriating.
I gave up on Infura and moved to other providers.
I wouldn’t recommend you to anyone (of course).

1 Like

I’m sorry for not having a good experience with us ! The issue that you mention should have been fixed a long time ago. I’ve tried to reproduce your use case without success so it would really help to see your script, of course if you’re still willing to spend some more time with us. :slight_smile: Thanks !

1 Like