3

when I make a call to api.rpc.chain.getBlock , I see that there is a call to chain_getHeader and state_getRuntimeVersion before the block is fetched.

enter image description here

But, runtimeVersion does not get stored in the SignedBlock object that is returned. So, why fetch it? and is it possible to prevent this fetch by manually passing the runtimeVersion to the function from my local cache?

1 Answer 1

4

So polkadot-js has a very complex and intricate system underneath the hood that caches and detects changes between runtime versions in order to decorate the registry we use to decode, and create types. It's important to understand that the api does ALOT of heavy lifting to ensure the registries created are correct from a historical standpoint and current standpoint.

In this case, I wrote a very simple script to demonstrate the following:

const main = async () => {
    const api = await ApiPromise.create({
        provider: new WsProvider('wss://rpc.polkadot.io'),
    });
    await api.rpc.chain.getBlock()
};

Now when we run this we will get the first following calls made below. These results are cached and stored in the api. You can see exactly where in the code this happens here.

2023-05-26 10:16:15          API-WS: calling chain_getBlockHash {"id":1,"jsonrpc":"2.0","method":"chain_getBlockHash","params":[0]}
2023-05-26 10:16:15          API-WS: calling state_getRuntimeVersion {"id":2,"jsonrpc":"2.0","method":"state_getRuntimeVersion","params":[]}
2023-05-26 10:16:15          API-WS: calling system_chain {"id":3,"jsonrpc":"2.0","method":"system_chain","params":[]}
2023-05-26 10:16:15          API-WS: calling system_properties {"id":4,"jsonrpc":"2.0","method":"system_properties","params":[]}
2023-05-26 10:16:15          API-WS: calling rpc_methods {"id":5,"jsonrpc":"2.0","method":"rpc_methods","params":[]}
2023-05-26 10:16:15          API-WS: calling state_getMetadata {"id":6,"jsonrpc":"2.0","method":"state_getMetadata","params":[]}

Then after the above we get the following calls:

2023-05-26 10:16:15          API-WS: calling chain_getHeader {"id":8,"jsonrpc":"2.0","method":"chain_getHeader","params":["0xfd9d5f2d26fa639a4154a8735da44337d7cf967e99fc365d2cfaf805a594b05e"]}
2023-05-26 10:16:15          API-WS: received {"jsonrpc":"2.0","result":"ovMti9lPXcTP1sQ0","id":7}
2023-05-26 10:16:15          API-WS: received {"jsonrpc":"2.0","method":"state_runtimeVersion","params":{"subscription":"ovMti9lPXcTP1sQ0","result":{"specName":"polkadot","implName":"parity-polkadot","authoringVersion":0,"specVersion":9370,"implVersion":0,"apis":[["0xdf6acb689907609b",4],["0x37e397fc7c91f5e4",1],["0x40fe3ad401f8959a",6],["0x17a6bc0d0062aeb3",1],["0xd2bc9897eed08f15",3],["0xf78b278be53f454c",2],["0xaf2c0297a23e6d3d",2],["0x49eaaf1b548a0cb0",1],["0x91d5df18b0d2cf58",1],["0xed99c5acb25eedf5",3],["0xcbca25e39f142387",2],["0x687ad44ad37f03c2",1],["0xab3c0572291feb8b",1],["0xbc9d89904f5b923f",1],["0x37c8bb1350a9a2a8",2],["0xf3ff14d5ab527059",2]],"transactionVersion":20,"stateVersion":0}}}
2023-05-26 10:16:15          API-WS: received {"jsonrpc":"2.0","result":{"parentHash":"0xda2fce23bc3a9d6c46812a7b7e00ef97068f06dccb952d7778e08c32beaceb35","number":"0xef70da","stateRoot":"0x432ca10d3c50b440a0b39edc604856c85ba843cdab796b6b7504e9268cea9992","extrinsicsRoot":"0xb4976bc6db95bff85c9c3e5eb6ac9559975fb320a0016e1cb773c221318965db","digest":{"logs":["0x0642414245b50103780000002e75bd1000000000627c85812a0d35e0f65079d575d7fe8fb635079615060e00f750ab9f25c57e3722aa3b6e5a3786228e8036ba6dc6df485c91923bad33620702e89f497c908806e36a514dd93eef9def1dc259372e305a77d9038cd463e246c511573eb4704b0c","0x0542414245010174fd1eea2459ff260fdf1af8d3d17adc1aba7af585dbe4de4257ff4ccf58052b485603ed538f97f48c108f50e2e8d24556e7af11ff01b466626d7badc0d91485"]}},"id":8}
2023-05-26 10:16:15          API-WS: calling state_getRuntimeVersion {"id":9,"jsonrpc":"2.0","method":"state_getRuntimeVersion","params":["0xda2fce23bc3a9d6c46812a7b7e00ef97068f06dccb952d7778e08c32beaceb35"]}

Now the following calls are being used to ensure we have the correct information to retrieve the correct VersionedRegistry. You can see the following code here where the following RPC calls are made: https://github.com/polkadot-js/api/blob/master/packages/api/src/base/Init.ts#L185-L222

There is no way to turn this off as far as I know and understand from reading the underlying code, but nor would you want too. It increases performance for the longevity of the api's open connection, and also ensures that any and all information you have, create or decode is correct in respect to the registry it is fetching.

Your Answer

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

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