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
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!
-
Thanks Shawn! Super informative answer. For any other data analysts who primarily use Python, you can access the getblockhash rpc endpoint Shawn mentioned via: polkascan.github.io/py-substrate-interface/… Feb 9, 2022 at 23:48