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: Implement archive_unstable_storageDiff #5997

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions substrate/client/rpc-spec-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ log = { workspace = true, default-features = true }
futures-util = { workspace = true }
rand = { workspace = true, default-features = true }
schnellru = { workspace = true }
itertools = { workspace = true }

[dev-dependencies]
jsonrpsee = { workspace = true, features = ["server", "ws-client"] }
Expand Down
18 changes: 17 additions & 1 deletion substrate/client/rpc-spec-v2/src/archive/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
//! API trait of the archive methods.

use crate::{
common::events::{ArchiveStorageResult, PaginatedStorageQuery},
common::events::{
ArchiveStorageDiffItem, ArchiveStorageDiffMethodResult, ArchiveStorageResult,
PaginatedStorageQuery,
},
MethodResult,
};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
Expand Down Expand Up @@ -104,4 +107,17 @@ pub trait ArchiveApi<Hash> {
items: Vec<PaginatedStorageQuery<String>>,
child_trie: Option<String>,
) -> RpcResult<ArchiveStorageResult>;

/// Returns the storage difference between two blocks.
///
/// # Unstable
///
/// This method is unstable and subject to change in the future.
#[method(name = "archive_unstable_storageDiff", blocking)]
fn archive_unstable_storage_diff(
&self,
hash: Hash,
previous_hash: Option<Hash>,
items: Vec<ArchiveStorageDiffItem<String>>,
) -> RpcResult<ArchiveStorageDiffMethodResult>;
}
51 changes: 47 additions & 4 deletions substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
//! API implementation for `archive`.

use crate::{
archive::{error::Error as ArchiveError, ArchiveApiServer},
common::events::{ArchiveStorageResult, PaginatedStorageQuery},
archive::{
archive_storage::{ArchiveStorage, ArchiveStorageDiff},
error::Error as ArchiveError,
ArchiveApiServer,
},
common::events::{
ArchiveStorageDiffItem, ArchiveStorageDiffMethodResult, ArchiveStorageResult,
PaginatedStorageQuery,
},
hex_string, MethodResult,
};

Expand All @@ -41,8 +48,6 @@ use sp_runtime::{
};
use std::{collections::HashSet, marker::PhantomData, sync::Arc};

use super::archive_storage::ArchiveStorage;

/// The configuration of [`Archive`].
pub struct ArchiveConfig {
/// The maximum number of items the `archive_storage` can return for a descendant query before
Expand Down Expand Up @@ -278,4 +283,42 @@ where

Ok(storage_client.handle_query(hash, items, child_trie))
}

fn archive_unstable_storage_diff(
&self,
hash: Block::Hash,
previous_hash: Option<Block::Hash>,
items: Vec<ArchiveStorageDiffItem<String>>,
) -> RpcResult<ArchiveStorageDiffMethodResult> {
let storage_client = ArchiveStorageDiff::new(self.client.clone());

// Deduplicate the items.
let trie_items = storage_client.deduplicate_items(items)?;

let previous_hash = if let Some(previous_hash) = previous_hash {
previous_hash
} else {
let Ok(Some(current_header)) = self.client.header(hash) else {
return Err(ArchiveError::InvalidParam(format!(
"Block header is not present: {}",
hash
))
.into());
};
*current_header.parent_hash()
};

if trie_items.is_empty() {
let result = storage_client.handle_trie_queries(hash, previous_hash, Vec::new())?;
return Ok(ArchiveStorageDiffMethodResult { result })
}

let mut storage_results = Vec::new();
for trie_queries in trie_items {
let result = storage_client.handle_trie_queries(hash, previous_hash, trie_queries)?;
storage_results.extend(result);
}

Ok(ArchiveStorageDiffMethodResult { result: storage_results })
}
}
Loading
Loading