paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.network/
1.8k stars 652 forks source link

[backport] chainHead/fix: Report bestBlock events only for newBlock reports (#5527) #5582

Closed lexnv closed 3 weeks ago

lexnv commented 3 weeks ago

This backports original PR: https://github.com/paritytech/polkadot-sdk/pull/5527 to the release branch


The https://github.com/paritytech/polkadot-sdk/issues/5512 has surfaced that we reported a `BestBlock` event for a block not previously reported via `NewBlock`.

This is because of a race between:
- the stream of events that announces new blocks
- `self.client.info().best_block`

It is possible that `client.info()` contains newer information than the information polled from the block stream (that may be lagging).

To mitigate this, instead of relying on the client's info use the last finalized block to emit a new event.

There are two cases when a new best block event is emitted:
- The best block is in the pruned list and is reported immediately
- The best block is not a descendant of the last finalized block 

Closes: https://github.com/paritytech/polkadot-sdk/issues/5512 

Thanks @jsdw and @josepot for helping debug this 🙏 

cc @paritytech/subxt-team
github-actions[bot] commented 3 weeks ago

This pull request is amending an existing release. Please proceed with extreme caution, as to not impact downstream teams that rely on the stability of it. Some things to consider: