I am running an application which continuously watches a smart contract for new logs. The way it is currently implemented is as follows:
- Subscribe to new block headers event
- When new block header is available:
- Get all logs between
last parsed block + 1
andnew block
. - Parse logs
- Get all logs between
When the application is initialized, last parsed block + 1
and new block
will be equal - the application reads every block’s logs one by one as new blocks are available.
When the application is not initialized, last parsed block
is set to -1
and thus it will read all logs from block 0 to the current block. After that is done it will continue listening to new block header events and parse logs in new blocks.
Now to my question: It seems to me that sometimes the API will not return logs in a relatively new block.This is what is happening:
- New block is available
- Request logs in block
- Returns no logs in block
- Later I realize there is something wrong
- I wipe the applications state, making it re-parse the blocks from 0
- It now detects the events in the block
Additionally this does not always happen. Sometimes the logs in a new block are returned correctly, sometimes not. I do not believe there is something wrong with my application, because:
- The code handling logs is the same for new and old blocks.
- Tried with two completely independent implementations of the application: Node (web3js) and Go (ethclient) - both show the same behaviour
- When running on a local Ganache testnet everything works fine
Is it possible that if I request a block’s logs directly after receiving the notification that a new block header is available, your API has not fully parsed the block and will return no logs even though there were logs in that block, and will return the correct logs when queried again later?
Thanks in advance!