0

Is there any API to calculate each block? How much size does it occupy?

3
  • You mean how much space an encoded block takes up? For what is that important? You could compress them afterwards. Commented Oct 13, 2022 at 16:07
  • @OliverTale-Yazdi yes somewhat along those lines. Any docs you can point me to for reading about the same? I am curios to know about the entire process like block encoding which you mentioned..
    – muddlebee
    Commented Oct 13, 2022 at 17:30
  • 1
    On StackExchange questions have to be asked with a very narrow scope, otherwise they cannot be answered with one simple post. You can ask in the Matrix chat for more general questions. Commented Oct 14, 2022 at 14:05

1 Answer 1

2

You can load the block and then print the encoded size with PolkadotJS like such:

import { ApiPromise, WsProvider } from '@polkadot/api';

const wsProvider = new WsProvider('wss://kusama-rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });

// Load the last block
const lastBlock = await api.rpc.chain.getBlock();
console.log(`Block ${lastBlock.block.header.number} with ${lastBlock.block.extrinsics.length} \
extrinsics has ${lastBlock.encodedLength} bytes`);

Prints something like Block 14878286 with 2 extrinsics has 32567 bytes. Should be easy to extend for subscribing to new blocks.

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.