Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add dev_getBlockStats RPC #10939

Merged
merged 12 commits into from
Mar 22, 2022
Merged

Add dev_getBlockStats RPC #10939

merged 12 commits into from
Mar 22, 2022

Conversation

athei
Copy link
Member

@athei athei commented Feb 28, 2022

This adds a new RPC:

pub struct BlockStats {
    pub witness_len: u64,
    pub witness_compact_len: u64,
    pub block_len: u64,
    pub block_num_extrinsics: u64,
}

#[rpc(name = "dev_getBlockStats")]
fn block_stats(&self, hash: Hash) -> Result<Option<BlockStats>>;

It is meant as a way to gain insides about the bottle necks of a given chain. While some information can only be learned through this RPC (witness related information) some of them are redundant: You can query the whole block and then measure the size of the block and the number of extrinsics. However, I think it still makes sense to include them as this saves us from transmitting the whole block to the RPC client (possibly over the internet) in order to learn about these statistics.

I have written a CLI tool that queries this draft version of the RPC:

cargo install --git https://github.com/athei/povstats.git --locked

# This will just connect to a locally running node and calls the RPC on each new block
povstats

The output when running this against a substrate --dev node without submitting any extrinsics to it looks like this:

27: Some(BlockStats { witness_len: 8179, witness_compact_len: 6124, witness_compressed_len: 5685, block_len: 226, block_num_extrinsics: 1 })
28: Some(BlockStats { witness_len: 7960, witness_compact_len: 5869, witness_compressed_len: 5412, block_len: 129, block_num_extrinsics: 1 })
29: Some(BlockStats { witness_len: 7894, witness_compact_len: 5901, witness_compressed_len: 5473, block_len: 129, block_num_extrinsics: 1 })
30: Some(BlockStats { witness_len: 8564, witness_compact_len: 6379, witness_compressed_len: 5918, block_len: 226, block_num_extrinsics: 1 })
31: Some(BlockStats { witness_len: 8535, witness_compact_len: 6382, witness_compressed_len: 5913, block_len: 226, block_num_extrinsics: 1 })
32: Some(BlockStats { witness_len: 8698, witness_compact_len: 6513, witness_compressed_len: 6065, block_len: 226, block_num_extrinsics: 1 })
33: Some(BlockStats { witness_len: 7842, witness_compact_len: 5913, witness_compressed_len: 5471, block_len: 129, block_num_extrinsics: 1 })
34: Some(BlockStats { witness_len: 7886, witness_compact_len: 5925, witness_compressed_len: 5473, block_len: 129, block_num_extrinsics: 1 })
35: Some(BlockStats { witness_len: 8520, witness_compact_len: 6497, witness_compressed_len: 6064, block_len: 226, block_num_extrinsics: 1 })

@athei athei added the A3-in_progress Pull request is in progress. No review needed at this stage. label Feb 28, 2022
@kianenigma
Copy link
Contributor

It happens inside: sp_api::Core::execute_block which is called by the new RPC.

I am not sure with the abstraction level that you are using, and I don't see any code that specifies on top of which state you are executing the given block. What I can say for sure is that to execute any arbitrary block, you certainly need its parent state, which may not be available. This error that you said also rings a bell for me in try-runtime, and I recall the reason was not using the proper parent state.

@athei
Copy link
Member Author

athei commented Feb 28, 2022

I thought I did correctly specify the parent state by calling the runtime API at the block's parent's hash:

runtime_api.execute_block(&BlockId::Hash(*parent_hash), block)

@kianenigma
Copy link
Contributor

I thought I did correctly specify the parent state by calling the runtime API at the block's parent's hash:

runtime_api.execute_block(&BlockId::Hash(*parent_hash), block)

that sounds correct to me. Perhaps to double check: if you pass an old-block's hash to a non-archive node, do you get an error?

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In generally I'm against adding new rpc methods that are added for everybody by default. However, the functionality here could maybe added to the state_traceBlock rpc method. Or if you want to keep it separate, it should be put into its own crate.

client/rpc/src/chain/chain_full.rs Outdated Show resolved Hide resolved
@athei
Copy link
Member Author

athei commented Feb 28, 2022

In generally I'm against adding new rpc methods that are added for everybody by default. However, the functionality here could maybe added to the state_traceBlock rpc method.

Isn't modifying existing RPCs that are included for anyone even worse?

Or if you want to keep it separate, it should be put into its own crate.

This sounds sensible.

@bkchr
Copy link
Member

bkchr commented Feb 28, 2022

Isn't modifying existing RPCs that are included for anyone even worse?

I mean we would just add some data to the output that you can just ignore on the calling site.

@athei
Copy link
Member Author

athei commented Mar 10, 2022

Isn't modifying existing RPCs that are included for anyone even worse?

I mean we would just add some data to the output that you can just ignore on the calling site.

This is basically modifying the RPC in a backwards compatible way. So is adding a new RPC. Why is adding a new one worse?

@athei
Copy link
Member Author

athei commented Mar 11, 2022

@dvdplm We are kind of unsure what would be the best place to put this new RPC. Asking for your opinion because I don't know how this ties into the jsonrpsee transition.

I think we should not append it to the state_traceBlock RPC as this would conflate two totally different things. The new RPC would be flagged as unsafe in any case.

Options are:

  • Leave it as is: Additional RPC in the chain category
  • Create a new category within sc-rpc
  • Create a new crate within substrate to contain the new RPC
  • Create a new crate within cumulus to contain the new RPC

@dvdplm
Copy link
Contributor

dvdplm commented Mar 13, 2022

My two cents:

  • We don't have a good process to decide when/how/who to add RPCs (same for deprecation).
  • Over time I've seen people think that "unsafe" RPCs are a way to signal that an RPC is experimental/debug-only/unstable. This is not the case, or rather, it's a bit of a semantic acrobatics. Unsafe means that an RPC should not be exposed to untrusted input, either because it does dangerous things or because it puts a lot of strain on the node and thus exposing a DoS veector.
  • @tomaka 's approach when re-designing the RPCs is to be very explicit about which RPCs are stable and which aren't, by including "unstable" in the name. I think that's one way to go about it (preferably letting the proc macro do the heavy lifting).
  • Given that up until now, adding or changing an RPC has been pretty much "do whatever you want", I think it's unfair to block this PR and I don't think the functionality fits very well as an extension to traceBlock

In other words: no objections to merge this from me (but it'd be good to start making up our minds about how to add RPCs, the sooner the better).

@bkchr
Copy link
Member

bkchr commented Mar 13, 2022

In other words: no objections to merge this from me (but it'd be good to start making up our minds about how to add RPCs, the sooner the better).

This is relative easy. Don't add RPCs automatically to every node building on Substrate. Adding RPC functionality to Substrate isn't bad and we can name them unstable or whatever. However, people building on Substrate should decide on their own what kind of RPCs they want to expose.

primitives/runtime/src/lib.rs Outdated Show resolved Hide resolved
client/rpc/src/chain/mod.rs Outdated Show resolved Hide resolved
client/rpc/src/chain/chain_full.rs Outdated Show resolved Hide resolved
client/rpc/src/chain/chain_full.rs Outdated Show resolved Hide resolved
@athei
Copy link
Member Author

athei commented Mar 14, 2022

In other words: no objections to merge this from me (but it'd be good to start making up our minds about how to add RPCs, the sooner the better).

This is relative easy. Don't add RPCs automatically to every node building on Substrate. Adding RPC functionality to Substrate isn't bad and we can name them unstable or whatever. However, people building on Substrate should decide on their own what kind of RPCs they want to expose.

So a new category within sc-rpc would be fine? Putting it into a new crate just adds more boilerplate for no benefit.

@bkchr
Copy link
Member

bkchr commented Mar 14, 2022

Yeah, a new category in sc-rpc is okay :) I mean, I have no idea how the structure with jsonrpsee looks, that was the reason I proposed to speak with @dvdplm :)

@athei
Copy link
Member Author

athei commented Mar 14, 2022

@dvdplm said to me: The less boilerplate the better for our transition to jsonrpsee. So even adding a new category has a lot of boilerplate and makes the transition more work. That said, we need to balance this against how bad it is for RPCs to "just show up" when you bump substrate.

@dvdplm
Copy link
Contributor

dvdplm commented Mar 14, 2022

I mean, I have no idea how the structure with jsonrpsee looks, that was the reason I proposed to speak with @dvdplm :)

The amount of work for devs is quite similar, maybe slightly less but not by much.

@athei
Copy link
Member Author

athei commented Mar 14, 2022

Okay so I will add a new category then. I still have no idea how to call this category. I was thinking about calling it dev to clearly indicate that it will contain some seriously wonky RPCs that are not really stable. We could then add new stuff there without much concern because people shouldn't add this category to their production node anyways.

What do you think?

@athei athei added A0-please_review Pull request needs code review. B5-clientnoteworthy C1-low PR touches the given topic and has a low impact on builders. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Mar 15, 2022
@athei athei marked this pull request as ready for review March 15, 2022 12:49
@athei athei added the D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit. label Mar 15, 2022
@athei
Copy link
Member Author

athei commented Mar 16, 2022

Ready for review now:

  • Moved RPC to a new category.
  • Added docs.
  • Removed the witness_compressed_len. It is too assumptions. Client can compress itself if wanted.

@athei athei requested review from bkchr and dvdplm March 16, 2022 10:06
@athei athei changed the title Add chain_getBlockStats RPC Add dev_getBlockStats RPC Mar 21, 2022
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the stuff are just nitpicks, otherwise it looks good.

primitives/runtime/src/lib.rs Outdated Show resolved Hide resolved
client/rpc/src/dev/mod.rs Show resolved Hide resolved
primitives/runtime/src/lib.rs Outdated Show resolved Hide resolved
client/rpc/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/error.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/error.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM. Left some nitpicks. :)

client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc-api/src/dev/mod.rs Outdated Show resolved Hide resolved
client/rpc/src/dev/mod.rs Outdated Show resolved Hide resolved
athei and others added 2 commits March 22, 2022 08:45
Co-authored-by: David <dvdplm@gmail.com>
@athei
Copy link
Member Author

athei commented Mar 22, 2022

bot merge

@paritytech-processbot paritytech-processbot bot merged commit c558351 into master Mar 22, 2022
@paritytech-processbot paritytech-processbot bot deleted the at-stat-rpc branch March 22, 2022 08:23
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* Add chain_getBlockStats rpc

* Fix broken doc link

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fmt

* Fix compilation

* Move Blockstats

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: ascjones <ascjones@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* Add chain_getBlockStats rpc

* Fix broken doc link

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fmt

* Fix compilation

* Move Blockstats

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: ascjones <ascjones@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* Add chain_getBlockStats rpc

* Fix broken doc link

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fmt

* Fix compilation

* Move Blockstats

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: ascjones <ascjones@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
DaviRain-Su pushed a commit to octopus-network/substrate that referenced this pull request Aug 23, 2022
* Add chain_getBlockStats rpc

* Fix broken doc link

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fmt

* Fix compilation

* Move Blockstats

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: ascjones <ascjones@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* Add chain_getBlockStats rpc

* Fix broken doc link

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fmt

* Fix compilation

* Move Blockstats

* Apply suggestions from code review

Co-authored-by: David <dvdplm@gmail.com>

* fmt

Co-authored-by: ascjones <ascjones@gmail.com>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: David <dvdplm@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants