Search code examples
blockchainsubstratepolkadot

Get relay chain blocknumber from parachain in substrate pallet


The title is quite self-explanatory, how can i get the blocknumber of the relay chain I'm connected to (Kusama) instead of my local blocknumber ?

All of this in a substrate/cumulus environment


Solution

  • I assume that you mean not Substrate, but Cumulus, to be specific, since you are talking about parachains.

    Cumulus parachains' runtimes receive the so called PersistedValidationData from the collators. It has several pieces of information passed by the relay-chain. It does include relay_parent_number. That is the relay chain block number from which the parachain block in question was created.

    In a cumulus parachain runtime, PersistedValidationData can be accessed through the validation_data getter.

    Be advised, this data is submitted by a collator in an inherent. That means, among other things, this data is not available in the on_initialize hooks.