Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc v2: backpressure chainHead_v1_storage #5741

Merged
merged 37 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7f32fdf
rpc v2: rely backpressure `Storage::query_iter`
niklasad1 Sep 9, 2024
c7d9a50
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
niklasad1 Sep 17, 2024
540daa6
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
niklasad1 Sep 17, 2024
423bc30
Update substrate/client/rpc-spec-v2/src/chain_head/tests.rs
niklasad1 Sep 17, 2024
ef62794
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 17, 2024
7351206
cleanup
niklasad1 Sep 18, 2024
b963c8b
revert archive static limits
niklasad1 Sep 18, 2024
1cb17f3
cargo fmt
niklasad1 Sep 18, 2024
0c94a6b
add trait bound RawIter: Send
niklasad1 Sep 19, 2024
1612813
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 19, 2024
583ea58
cargo fmt
niklasad1 Sep 19, 2024
486b246
remove unused import
niklasad1 Sep 19, 2024
67a37ce
Update substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
niklasad1 Sep 19, 2024
05386e8
Update substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
niklasad1 Sep 19, 2024
dc17cc4
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 24, 2024
ab88c05
address grumbles
niklasad1 Sep 24, 2024
accb056
add prdoc
niklasad1 Sep 24, 2024
7a7a75e
Update prdoc/pr_5741.prdoc
niklasad1 Sep 24, 2024
6fa42e4
prdoc fixes
niklasad1 Sep 24, 2024
08699f6
".git/.scripts/commands/fmt/fmt.sh"
Sep 24, 2024
9766eb3
bump tokio for permit api
niklasad1 Sep 25, 2024
af28077
simplify the code
niklasad1 Sep 26, 2024
8ccd391
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 26, 2024
737e858
fix tests
niklasad1 Sep 27, 2024
797f1da
cargo fmt
niklasad1 Sep 27, 2024
2ceadfc
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 27, 2024
381fcc0
".git/.scripts/commands/fmt/fmt.sh"
Sep 27, 2024
6263c83
Revert "".git/.scripts/commands/fmt/fmt.sh""
niklasad1 Sep 27, 2024
097a50e
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 27, 2024
34393db
Update substrate/client/rpc-spec-v2/Cargo.toml
niklasad1 Sep 27, 2024
25051cd
Update prdoc/pr_5741.prdoc
niklasad1 Oct 1, 2024
4a8c5c3
Merge branch 'master' into na-fix-rpc-storage-iter
niklasad1 Oct 1, 2024
9152f9e
remove needless trait bounds
niklasad1 Oct 2, 2024
ec25f00
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Oct 2, 2024
bb251b0
remove more needless trait bounds
niklasad1 Oct 2, 2024
49e013c
remove more needless trait bounds again
niklasad1 Oct 2, 2024
4a70b0e
Merge branch 'master' into na-fix-rpc-storage-iter
niklasad1 Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions substrate/client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ where
),
};

let rpc_middleware =
RpcServiceBuilder::new().option_layer(middleware_layer.clone());
let rpc_middleware = RpcServiceBuilder::new()
.rpc_logger(1024)
.option_layer(middleware_layer.clone());
let mut svc = service_builder
.set_rpc_middleware(rpc_middleware)
.build(methods, stop_handle);
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/rpc-spec-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sp-rpc = { workspace = true, default-features = true }
sp-blockchain = { workspace = true, default-features = true }
sp-version = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-utils = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
codec = { workspace = true, default-features = true }
thiserror = { workspace = true }
Expand Down Expand Up @@ -59,3 +58,4 @@ sc-service = { features = ["test-helpers"], workspace = true, default-features =
assert_matches = { workspace = true }
pretty_assertions = { workspace = true }
sc-transaction-pool = { workspace = true, default-features = true }
sc-utils = { workspace = true, default-features = true }
10 changes: 10 additions & 0 deletions substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sc_client_api::{
Backend, BlockBackend, BlockchainEvents, CallExecutor, ChildInfo, ExecutorProvider, StorageKey,
StorageProvider,
};
use sc_rpc::SubscriptionTaskExecutor;
use sp_api::{CallApiAt, CallContext};
use sp_blockchain::{
Backend as BlockChainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
Expand Down Expand Up @@ -88,6 +89,8 @@ pub struct Archive<BE: Backend<Block>, Block: BlockT, Client> {
storage_max_queried_items: usize,
/// Phantom member to pin the block type.
_phantom: PhantomData<Block>,
/// Subscription task executor.
executor: SubscriptionTaskExecutor,
}

impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
Expand All @@ -97,6 +100,7 @@ impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
backend: Arc<BE>,
genesis_hash: GenesisHash,
config: ArchiveConfig,
executor: SubscriptionTaskExecutor,
) -> Self {
let genesis_hash = hex_string(&genesis_hash.as_ref());
Self {
Expand All @@ -106,6 +110,7 @@ impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
storage_max_descendant_responses: config.max_descendant_responses,
storage_max_queried_items: config.max_queried_items,
_phantom: PhantomData,
executor,
}
}
}
Expand Down Expand Up @@ -136,6 +141,9 @@ where
+ CallApiAt<Block>
+ StorageProvider<Block, BE>
+ 'static,
<<BE as sc_client_api::Backend<Block>>::State as sc_client_api::StateBackend<
<<Block as BlockT>::Header as HeaderT>::Hashing,
>>::RawIter: Send,
{
fn archive_unstable_body(&self, hash: Block::Hash) -> RpcResult<Option<Vec<String>>> {
let Ok(Some(signed_block)) = self.client.block(hash) else { return Ok(None) };
Expand Down Expand Up @@ -274,7 +282,9 @@ where
self.client.clone(),
self.storage_max_descendant_responses,
self.storage_max_queried_items,
self.executor.clone(),
);

Ok(storage_client.handle_query(hash, items, child_trie))
}
}
70 changes: 39 additions & 31 deletions substrate/client/rpc-spec-v2/src/archive/archive_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use std::sync::Arc;

use sc_client_api::{Backend, ChildInfo, StorageKey, StorageProvider};
use sp_runtime::traits::Block as BlockT;
use sc_rpc::SubscriptionTaskExecutor;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};

use crate::common::{
events::{ArchiveStorageResult, PaginatedStorageQuery, StorageQueryType},
Expand All @@ -44,9 +45,10 @@ impl<Client, Block, BE> ArchiveStorage<Client, Block, BE> {
client: Arc<Client>,
storage_max_descendant_responses: usize,
storage_max_queried_items: usize,
executor: SubscriptionTaskExecutor,
) -> Self {
Self {
client: Storage::new(client),
client: Storage::new(client, executor),
storage_max_descendant_responses,
storage_max_queried_items,
}
Expand All @@ -55,9 +57,12 @@ impl<Client, Block, BE> ArchiveStorage<Client, Block, BE> {

impl<Client, Block, BE> ArchiveStorage<Client, Block, BE>
where
Block: BlockT + 'static,
BE: Backend<Block> + 'static,
Client: StorageProvider<Block, BE> + 'static,
Block: BlockT + Send + 'static,
BE: Backend<Block> + Send + 'static,
Client: StorageProvider<Block, BE> + Send + Sync + 'static,
<<BE as sc_client_api::Backend<Block>>::State as sc_client_api::StateBackend<
<<Block as BlockT>::Header as HeaderT>::Hashing,
>>::RawIter: Send,
{
/// Generate the response of the `archive_storage` method.
pub fn handle_query(
Expand All @@ -70,6 +75,8 @@ where
items.truncate(self.storage_max_queried_items);

let mut storage_results = Vec::with_capacity(items.len());
let mut query_iter = Vec::new();

for item in items {
match item.query_type {
StorageQueryType::Value => {
Expand All @@ -92,38 +99,39 @@ where
Err(error) => return ArchiveStorageResult::err(error),
},
StorageQueryType::DescendantsValues => {
match self.client.query_iter_pagination(
QueryIter {
query_key: item.key,
ty: IterQueryType::Value,
pagination_start_key: item.pagination_start_key,
},
hash,
child_key.as_ref(),
self.storage_max_descendant_responses,
) {
Ok((results, _)) => storage_results.extend(results),
Err(error) => return ArchiveStorageResult::err(error),
}
query_iter.push(QueryIter {
query_key: item.key,
ty: IterQueryType::Value,
pagination_start_key: item.pagination_start_key,
});
},
StorageQueryType::DescendantsHashes => {
match self.client.query_iter_pagination(
QueryIter {
query_key: item.key,
ty: IterQueryType::Hash,
pagination_start_key: item.pagination_start_key,
},
hash,
child_key.as_ref(),
self.storage_max_descendant_responses,
) {
Ok((results, _)) => storage_results.extend(results),
Err(error) => return ArchiveStorageResult::err(error),
}
query_iter.push(QueryIter {
query_key: item.key,
ty: IterQueryType::Hash,
pagination_start_key: item.pagination_start_key,
});
},
};
}

if !query_iter.is_empty() {
let mut rx = self.client.query_iter_pagination(
query_iter,
hash,
child_key,
Some(self.storage_max_descendant_responses),
);

while let Some(val) = rx.blocking_recv() {
match val {
Ok(Some(value)) => storage_results.push(value),
Ok(None) => continue,
Err(error) => return ArchiveStorageResult::err(error),
}
}
}

ArchiveStorageResult::ok(storage_results, discarded_items)
}
}
3 changes: 2 additions & 1 deletion substrate/client/rpc-spec-v2/src/archive/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sc_block_builder::BlockBuilderBuilder;
use sc_client_api::ChildInfo;
use sp_blockchain::HeaderBackend;
use sp_consensus::BlockOrigin;
use sp_core::{Blake2Hasher, Hasher};
use sp_core::{testing::TaskExecutor, Blake2Hasher, Hasher};
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT},
SaturatedConversion,
Expand Down Expand Up @@ -79,6 +79,7 @@ fn setup_api(
backend,
CHAIN_GENESIS,
ArchiveConfig { max_descendant_responses, max_queried_items },
Arc::new(TaskExecutor::new()),
)
.into_rpc();

Expand Down
Loading
Loading