Is there any API to calculate each block? How much size does it occupy?
-
You mean how much space an encoded block takes up? For what is that important? You could compress them afterwards.– Oliver Tale-YazdiCommented 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..– muddlebeeCommented Oct 13, 2022 at 17:30
-
1On 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.– Oliver Tale-YazdiCommented Oct 14, 2022 at 14:05
Add a comment
|
1 Answer
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.