chainHead: Allow methods to be called from within a single connection context and limit connections (#3481)

This PR ensures that the chainHead RPC class can be called only from
within the same connection context.

The chainHead methods are now registered as raw methods. 
- https://github.com/paritytech/jsonrpsee/pull/1297
The concept of raw methods is introduced in jsonrpsee, which is an async
method that exposes the connection ID:
The raw method doesn't have the concept of a blocking method. Previously
blocking methods are now spawning a blocking task to handle their
blocking (ie DB) access. We spawn the same number of tasks as before,
however we do that explicitly.

Another approach would be implementing a RPC middleware that captures
and decodes the method parameters:
- https://github.com/paritytech/polkadot-sdk/pull/3343
However, that approach is prone to errors since the methods are
hardcoded by name. Performace is affected by the double deserialization
that needs to happen to extract the subscription ID we'd like to limit.
Once from the middleware, and once from the methods itself.

This PR paves the way to implement the chainHead connection limiter:
- https://github.com/paritytech/polkadot-sdk/issues/1505
Registering tokens (subscription ID / operation ID) on the
`RpcConnections` could be extended to return an error when the maximum
number of operations is reached.

While at it, have added an integration-test to ensure that chainHead
methods can be called from within the same connection context.

Before this is merged, a new JsonRPC release should be made to expose
the `raw-methods`:
- [x] Use jsonrpsee from crates io (blocked by:
https://github.com/paritytech/jsonrpsee/pull/1297)

Closes: https://github.com/paritytech/polkadot-sdk/issues/3207


cc @paritytech/subxt-team

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Alexandru VasileandNiklas Adolfsson authored and GitHub committed 2024-04-02 13:12:34 +00:00
1 parent 5eff3f94be
commit 7430f41350
11 files changed
+934 -154

No files matched your search

@@ -23,6 +23,9 @@ use jsonrpsee::types::error::ErrorObject;
/// ChainHead RPC errors.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Maximum number of chainHead_follow has been reached.
#[error("Maximum number of chainHead_follow has been reached")]
ReachedLimits,
/// The provided block hash is invalid.
#[error("Invalid block hash")]
InvalidBlock,
@@ -46,6 +49,8 @@ pub enum Error {
/// Errors for `chainHead` RPC module, as defined in
/// <https://github.com/paritytech/json-rpc-interface-spec>.
pub mod rpc_spec_v2 {
/// Maximum number of chainHead_follow has been reached.
pub const REACHED_LIMITS: i32 = -32800;
/// The provided block hash is invalid.
pub const INVALID_BLOCK_ERROR: i32 = -32801;
/// The follow subscription was started with `withRuntime` set to `false`.
@@ -70,6 +75,8 @@ impl From<Error> for ErrorObject<'static> {
let msg = e.to_string();
match e {
Error::ReachedLimits =>
ErrorObject::owned(rpc_spec_v2::REACHED_LIMITS, msg, None::<()>),
Error::InvalidBlock =>
ErrorObject::owned(rpc_spec_v2::INVALID_BLOCK_ERROR, msg, None::<()>),
Error::InvalidRuntimeCall(_) =>