Files
pezkuwi-subxt/substrate/client/rpc/src/chain/tests.rs
T
Niklas Adolfsson e16ef0861f 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
2024-01-23 08:55:13 +00:00

276 lines
7.9 KiB
Rust

// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::testing::{test_executor, timeout_secs};
use assert_matches::assert_matches;
use jsonrpsee::core::EmptyServerParams as EmptyParams;
use sc_block_builder::BlockBuilderBuilder;
use sp_consensus::BlockOrigin;
use sp_rpc::list::ListOrValue;
use substrate_test_runtime_client::{
prelude::*,
runtime::{Block, Header, H256},
};
#[tokio::test]
async fn should_return_header() {
let client = Arc::new(substrate_test_runtime_client::new());
let api = new_full(client.clone(), test_executor()).into_rpc();
let res: Header =
api.call("chain_getHeader", [H256::from(client.genesis_hash())]).await.unwrap();
assert_eq!(
res,
Header {
parent_hash: H256::from_low_u64_be(0),
number: 0,
state_root: res.state_root,
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
.parse()
.unwrap(),
digest: Default::default(),
}
);
let res: Header = api.call("chain_getHeader", EmptyParams::new()).await.unwrap();
assert_eq!(
res,
Header {
parent_hash: H256::from_low_u64_be(0),
number: 0,
state_root: res.state_root,
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
.parse()
.unwrap(),
digest: Default::default(),
}
);
assert_matches!(
api.call::<_, Option<Header>>("chain_getHeader", [H256::from_low_u64_be(5)])
.await
.unwrap(),
None
);
}
#[tokio::test]
async fn should_return_a_block() {
let mut client = Arc::new(substrate_test_runtime_client::new());
let api = new_full(client.clone(), test_executor()).into_rpc();
let block = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().best_hash)
.with_parent_block_number(client.chain_info().best_number)
.build()
.unwrap()
.build()
.unwrap()
.block;
let block_hash = block.hash();
client.import(BlockOrigin::Own, block).await.unwrap();
let res: SignedBlock<Block> =
api.call("chain_getBlock", [H256::from(client.genesis_hash())]).await.unwrap();
// Genesis block is not justified
assert!(res.justifications.is_none());
let res: SignedBlock<Block> =
api.call("chain_getBlock", [H256::from(block_hash)]).await.unwrap();
assert_eq!(
res.block,
Block {
header: Header {
parent_hash: client.genesis_hash(),
number: 1,
state_root: res.block.header.state_root,
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
.parse()
.unwrap(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
let res: SignedBlock<Block> = api.call("chain_getBlock", Vec::<H256>::new()).await.unwrap();
assert_eq!(
res.block,
Block {
header: Header {
parent_hash: client.genesis_hash(),
number: 1,
state_root: res.block.header.state_root,
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
.parse()
.unwrap(),
digest: Default::default(),
},
extrinsics: vec![],
}
);
assert_matches!(
api.call::<_, Option<Header>>("chain_getBlock", [H256::from_low_u64_be(5)])
.await
.unwrap(),
None
);
}
#[tokio::test]
async fn should_return_block_hash() {
let mut client = Arc::new(substrate_test_runtime_client::new());
let api = new_full(client.clone(), test_executor()).into_rpc();
let res: ListOrValue<Option<H256>> =
api.call("chain_getBlockHash", EmptyParams::new()).await.unwrap();
assert_matches!(
res,
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
);
let res: ListOrValue<Option<H256>> =
api.call("chain_getBlockHash", [ListOrValue::from(0_u64)]).await.unwrap();
assert_matches!(
res,
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
);
let res: Option<ListOrValue<Option<H256>>> =
api.call("chain_getBlockHash", [ListOrValue::from(1_u64)]).await.unwrap();
assert_matches!(res, None);
let block = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().best_hash)
.with_parent_block_number(client.chain_info().best_number)
.build()
.unwrap()
.build()
.unwrap()
.block;
client.import(BlockOrigin::Own, block.clone()).await.unwrap();
let res: ListOrValue<Option<H256>> =
api.call("chain_getBlockHash", [ListOrValue::from(0_u64)]).await.unwrap();
assert_matches!(
res,
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
);
let res: ListOrValue<Option<H256>> =
api.call("chain_getBlockHash", [ListOrValue::from(1_u64)]).await.unwrap();
assert_matches!(
res,
ListOrValue::Value(Some(ref x)) if x == &block.hash()
);
let res: ListOrValue<Option<H256>> = api
.call("chain_getBlockHash", [ListOrValue::Value(sp_core::U256::from(1_u64))])
.await
.unwrap();
assert_matches!(
res,
ListOrValue::Value(Some(ref x)) if x == &block.hash()
);
let res: ListOrValue<Option<H256>> = api
.call("chain_getBlockHash", [ListOrValue::List(vec![0_u64, 1_u64, 2_u64])])
.await
.unwrap();
assert_matches!(
res,
ListOrValue::List(list) if list == &[client.genesis_hash().into(), block.hash().into(), None]
);
}
#[tokio::test]
async fn should_return_finalized_hash() {
let mut client = Arc::new(substrate_test_runtime_client::new());
let api = new_full(client.clone(), test_executor()).into_rpc();
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
assert_eq!(res, client.genesis_hash());
// import new block
let block = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().best_hash)
.with_parent_block_number(client.chain_info().best_number)
.build()
.unwrap()
.build()
.unwrap()
.block;
let block_hash = block.hash();
client.import(BlockOrigin::Own, block).await.unwrap();
// no finalization yet
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
assert_eq!(res, client.genesis_hash());
// finalize
client.finalize_block(block_hash, None).unwrap();
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
assert_eq!(res, block_hash);
}
#[tokio::test]
async fn should_notify_about_latest_block() {
test_head_subscription("chain_subscribeAllHeads").await;
}
#[tokio::test]
async fn should_notify_about_best_block() {
test_head_subscription("chain_subscribeNewHeads").await;
}
#[tokio::test]
async fn should_notify_about_finalized_block() {
test_head_subscription("chain_subscribeFinalizedHeads").await;
}
async fn test_head_subscription(method: &str) {
let mut client = Arc::new(substrate_test_runtime_client::new());
let mut sub = {
let api = new_full(client.clone(), test_executor()).into_rpc();
let sub = api.subscribe_unbounded(method, EmptyParams::new()).await.unwrap();
let block = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().best_hash)
.with_parent_block_number(client.chain_info().best_number)
.build()
.unwrap()
.build()
.unwrap()
.block;
let block_hash = block.hash();
client.import(BlockOrigin::Own, block).await.unwrap();
client.finalize_block(block_hash, None).unwrap();
sub
};
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(Some(_)));
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(Some(_)));
sub.close();
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(None));
}