polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
240 stars 114 forks source link

Blocks are skipped when finalized_only=True #260

Closed KarimJedda closed 1 year ago

KarimJedda commented 1 year ago

So a bit of a curiosity this time. When subscribing to new blocks but only caring about finalized ones, some blocks get skipped.

from substrateinterface import SubstrateInterface

def block_subscription_handler(obj, update_nr, subscription_id):
    block_id = int(obj['header']['number'])
    print(f"Successfully parsed & pushed complete block {block_id}.")

substrate = SubstrateInterface(
    url="wss://kusama-rpc.polkadot.io",
)

result = substrate.subscribe_block_headers(
    block_subscription_handler, 
    include_author=True, 
    finalized_only=True
)

will give:

INFO:kusama-logger:Successfully parsed & pushed complete block 15050085.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050087.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050088.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050090.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050091.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050093.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050094.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050095.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050097.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050098.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050099.
INFO:kusama-logger:Successfully parsed & pushed complete block 15050101.

Am I doing something wrong? Thanks a lot!

arjanz commented 1 year ago

I don't think you are doing something wrong, I was actually wondering the same, but when I tried the same code with PolkadotJS I was experiencing the same (executed on JS playground https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama.api.onfinality.io%2Fpublic-ws#/js):

const unsub = await api.rpc.chain.subscribeFinalizedHeads((header) => {
  console.log(`#${header.number}:`, header);
});

So it is returned this way by the RPC, not sure what the exact reason is..

KarimJedda commented 1 year ago

Alright, thanks for confirming. I'll ask around to try to understand what is going on. It seems that this might be hinting at a potential reason.

arjanz commented 1 year ago

Alright, thanks for confirming. I'll ask around to try to understand what is going on. It seems that this might be hinting at a potential reason.

That makes a lot of sense, learned something too here.. Thanks for sharing