2

I can see how to do so from the subscan api but I am interested in understanding how to query this data from the chain itself. Ie via a storage function or by another means

1 Answer 1

3

Substrate exposes an RPC endpoint which provide the blockhash for any block: https://polkadot.js.org/docs/substrate/rpc#getblockhashblocknumber-blocknumber-blockhash

chain_getBlockHash

For example:

> curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "chain_getBlockHash"}' http://localhost:9933/

{"jsonrpc":"2.0","result":"0xec69c048cbfca1c9e50bec88c5f47fd00ea2bcfc1f651fdd995eeb283ea0baf5","id":1}

This information can also be found inside of the System Pallet in runtime storage:

/// Map of block numbers to block hashes.
#[pallet::storage]
#[pallet::getter(fn block_hash)]
pub type BlockHash<T: Config> =
    StorageMap<_, Twox64Concat, T::BlockNumber, T::Hash, ValueQuery>;

But note, this data is pruned, so only BlockHashCount number of items will exist at any time. To get all of the data in this way, you will need an archive node with the state of all the previous blocks.

So the RPC endpoint is probably your best bet!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.