rpc: backpressured RPC server (bump jsonrpsee 0.20) (#1313)

This is a rather big change in jsonrpsee, the major things in this bump
are:
- Server backpressure (the subscription impls are modified to deal with
that)
- Allow custom error types / return types (remove jsonrpsee::core::Error
and jsonrpee::core::CallError)
- Bug fixes (graceful shutdown in particular not used by substrate
anyway)
   - Less dependencies for the clients in particular
   - Return type requires Clone in method call responses
   - Moved to tokio channels
   - Async subscription API (not used in this PR)

Major changes in this PR:
- The subscriptions are now bounded and if subscription can't keep up
with the server it is dropped
- CLI: add parameter to configure the jsonrpc server bounded message
buffer (default is 64)
- Add our own subscription helper to deal with the unbounded streams in
substrate

The most important things in this PR to review is the added helpers
functions in `substrate/client/rpc/src/utils.rs` and the rest is pretty
much chore.

Regarding the "bounded buffer limit" it may cause the server to handle
the JSON-RPC calls
slower than before.

The message size limit is bounded by "--rpc-response-size" thus "by
default 10MB * 64 = 640MB"
but the subscription message size is not covered by this limit and could
be capped as well.

Hopefully the last release prior to 1.0, sorry in advance for a big PR

Previous attempt: https://github.com/paritytech/substrate/pull/13992

Resolves https://github.com/paritytech/polkadot-sdk/issues/748, resolves
https://github.com/paritytech/polkadot-sdk/issues/627
This commit is contained in:
Niklas Adolfsson
2024-01-23 09:55:13 +01:00
committed by GitHub
parent 76c37c930b
commit e16ef0861f
117 changed files with 1245 additions and 1090 deletions
+39 -36
View File
@@ -18,10 +18,7 @@
//! Authoring RPC module errors.
use jsonrpsee::{
core::Error as JsonRpseeError,
types::error::{CallError, ErrorObject},
};
use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
use sp_runtime::transaction_validity::InvalidTransaction;
/// Author RPC Result type.
@@ -86,98 +83,104 @@ const POOL_NO_TAGS: i32 = POOL_INVALID_TX + 9;
const POOL_INVALID_BLOCK_ID: i32 = POOL_INVALID_TX + 10;
/// The pool is not accepting future transactions.
const POOL_FUTURE_TX: i32 = POOL_INVALID_TX + 11;
/// Other error.
const OTHER_ERR: i32 = BASE_ERROR + 40;
impl From<Error> for JsonRpseeError {
fn from(e: Error) -> Self {
impl From<Error> for ErrorObjectOwned {
fn from(e: Error) -> ErrorObjectOwned {
use sc_transaction_pool_api::error::Error as PoolError;
match e {
Error::BadFormat(e) => CallError::Custom(ErrorObject::owned(
Error::BadFormat(e) => ErrorObject::owned(
BAD_FORMAT,
format!("Extrinsic has invalid format: {}", e),
None::<()>,
)),
Error::Verification(e) => CallError::Custom(ErrorObject::owned(
),
Error::Verification(e) => ErrorObject::owned(
VERIFICATION_ERROR,
format!("Verification Error: {}", e),
Some(format!("{:?}", e)),
)),
),
Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => {
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_INVALID_TX,
"Invalid Transaction",
Some(format!("Custom error: {}", e)),
))
)
},
Error::Pool(PoolError::InvalidTransaction(e)) => {
let msg: &str = e.into();
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_INVALID_TX,
"Invalid Transaction",
Some(msg),
))
)
},
Error::Pool(PoolError::UnknownTransaction(e)) => {
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_UNKNOWN_VALIDITY,
"Unknown Transaction Validity",
Some(format!("{:?}", e)),
))
)
},
Error::Pool(PoolError::TemporarilyBanned) =>
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_TEMPORARILY_BANNED,
"Transaction is temporarily banned",
None::<()>,
)),
),
Error::Pool(PoolError::AlreadyImported(hash)) =>
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_ALREADY_IMPORTED,
"Transaction Already Imported",
Some(format!("{:?}", hash)),
)),
Error::Pool(PoolError::TooLowPriority { old, new }) => CallError::Custom(ErrorObject::owned(
),
Error::Pool(PoolError::TooLowPriority { old, new }) => ErrorObject::owned(
POOL_TOO_LOW_PRIORITY,
format!("Priority is too low: ({} vs {})", old, new),
Some("The transaction has too low priority to replace another transaction already in the pool.")
)),
),
Error::Pool(PoolError::CycleDetected) =>
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_CYCLE_DETECTED,
"Cycle Detected",
None::<()>
)),
Error::Pool(PoolError::ImmediatelyDropped) => CallError::Custom(ErrorObject::owned(
),
Error::Pool(PoolError::ImmediatelyDropped) => ErrorObject::owned(
POOL_IMMEDIATELY_DROPPED,
"Immediately Dropped",
Some("The transaction couldn't enter the pool because of the limit"),
)),
Error::Pool(PoolError::Unactionable) => CallError::Custom(ErrorObject::owned(
),
Error::Pool(PoolError::Unactionable) => ErrorObject::owned(
POOL_UNACTIONABLE,
"Unactionable",
Some("The transaction is unactionable since it is not propagable and \
the local node does not author blocks")
)),
Error::Pool(PoolError::NoTagsProvided) => CallError::Custom(ErrorObject::owned(
),
Error::Pool(PoolError::NoTagsProvided) => ErrorObject::owned(
POOL_NO_TAGS,
"No tags provided",
Some("Transaction does not provide any tags, so the pool can't identify it")
)),
),
Error::Pool(PoolError::InvalidBlockId(_)) =>
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_INVALID_BLOCK_ID,
"The provided block ID is not valid",
None::<()>
)),
),
Error::Pool(PoolError::RejectedFutureTransaction) => {
CallError::Custom(ErrorObject::owned(
ErrorObject::owned(
POOL_FUTURE_TX,
"The pool is not accepting future transactions",
None::<()>,
))
)
},
Error::UnsafeRpcCalled(e) => e.into(),
e => CallError::Failed(e.into()),
}.into()
other => ErrorObject::owned(
OTHER_ERR,
other.to_string(),
None::<()>,
)
}
}
}
+12 -11
View File
@@ -18,27 +18,28 @@
//! Substrate block-author/full-node API.
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use sc_transaction_pool_api::TransactionStatus;
use sp_core::Bytes;
pub mod error;
pub mod hash;
use error::Error;
use jsonrpsee::proc_macros::rpc;
use sc_transaction_pool_api::TransactionStatus;
use sp_core::Bytes;
/// Substrate authoring RPC API
#[rpc(client, server)]
pub trait AuthorApi<Hash, BlockHash> {
/// Submit hex-encoded extrinsic for inclusion in block.
#[method(name = "author_submitExtrinsic")]
async fn submit_extrinsic(&self, extrinsic: Bytes) -> RpcResult<Hash>;
async fn submit_extrinsic(&self, extrinsic: Bytes) -> Result<Hash, Error>;
/// Insert a key into the keystore.
#[method(name = "author_insertKey")]
fn insert_key(&self, key_type: String, suri: String, public: Bytes) -> RpcResult<()>;
fn insert_key(&self, key_type: String, suri: String, public: Bytes) -> Result<(), Error>;
/// Generate new session keys and returns the corresponding public keys.
#[method(name = "author_rotateKeys")]
fn rotate_keys(&self) -> RpcResult<Bytes>;
fn rotate_keys(&self) -> Result<Bytes, Error>;
/// Checks if the keystore has private keys for the given session public keys.
///
@@ -46,24 +47,24 @@ pub trait AuthorApi<Hash, BlockHash> {
///
/// Returns `true` iff all private keys could be found.
#[method(name = "author_hasSessionKeys")]
fn has_session_keys(&self, session_keys: Bytes) -> RpcResult<bool>;
fn has_session_keys(&self, session_keys: Bytes) -> Result<bool, Error>;
/// Checks if the keystore has private keys for the given public key and key type.
///
/// Returns `true` if a private key could be found.
#[method(name = "author_hasKey")]
fn has_key(&self, public_key: Bytes, key_type: String) -> RpcResult<bool>;
fn has_key(&self, public_key: Bytes, key_type: String) -> Result<bool, Error>;
/// Returns all pending extrinsics, potentially grouped by sender.
#[method(name = "author_pendingExtrinsics")]
fn pending_extrinsics(&self) -> RpcResult<Vec<Bytes>>;
fn pending_extrinsics(&self) -> Result<Vec<Bytes>, Error>;
/// Remove given extrinsic from the pool and temporarily ban it to prevent reimporting.
#[method(name = "author_removeExtrinsic")]
fn remove_extrinsic(
&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<Hash>>,
) -> RpcResult<Vec<Hash>>;
) -> Result<Vec<Hash>, Error>;
/// Submit an extrinsic to watch.
///