Logs not available on new block header event

I am running an application which continuously watches a smart contract for new logs. The way it is currently implemented is as follows:

  1. Subscribe to new block headers event
  2. When new block header is available:
    • Get all logs between last parsed block + 1 and new block.
    • Parse logs

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:

  1. New block is available
  2. Request logs in block
    • Returns no logs in block
  3. Later I realize there is something wrong
  4. I wipe the applications state, making it re-parse the blocks from 0
  5. 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!

Hi @Bobface thanks for the detailed question. It is possible that the system serving logs is not aware of the latest block at the same time the subscription is, I recommend trying a small lag between the two calls and see if that improves. We are rolling out a system that will remove these types of issues but for now the lag may solve your problem.

Thanks for your answer, I will try running it with a delay. Also, is there somewhere I can read more about that upcoming feature?

We haven’t published anything yet, it’s mainly a backend system change, shouldn’t require any changes on your side!

I see, thanks again for helping out!

1 Like