-
Notifications
You must be signed in to change notification settings - Fork 289
Support v1 archive RPCs #1977
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
Merged
Merged
Support v1 archive RPCs #1977
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -292,6 +292,128 @@ impl<T: RpcConfig> ChainHeadRpcMethods<T> { | |
| .await | ||
| } | ||
|
|
||
| /// Fetch the block body (ie the extrinsics in the block) given its hash. | ||
| /// | ||
| /// Returns an array of the hexadecimal-encoded scale-encoded extrinsics found in the block, | ||
| /// or `None` if the block wasn't found. | ||
| pub async fn archive_v1_body(&self, block_hash: T::Hash) -> Result<Option<Vec<Bytes>>, Error> { | ||
| self.client | ||
| .request("archive_v1_body", rpc_params![block_hash]) | ||
| .await | ||
| } | ||
|
|
||
| /// Call the `archive_v1_call` method and return the response. | ||
| pub async fn archive_v1_call( | ||
| &self, | ||
| block_hash: T::Hash, | ||
| function: &str, | ||
| call_parameters: &[u8], | ||
| ) -> Result<ArchiveCallResult, Error> { | ||
| use serde::de::Error as _; | ||
|
|
||
| // We deserialize to this intermediate shape, since | ||
| // we can't have a boolean tag to denote variants. | ||
| #[derive(Deserialize)] | ||
| struct Response { | ||
| success: bool, | ||
| value: Option<Bytes>, | ||
| error: Option<String>, | ||
| // This was accidentally used instead of value in Substrate, | ||
| // so to support those impls we try it here if needed: | ||
| result: Option<Bytes>, | ||
| } | ||
|
|
||
| let res: Response = self | ||
| .client | ||
| .request( | ||
| "archive_v1_call", | ||
| rpc_params![block_hash, function, to_hex(call_parameters)], | ||
| ) | ||
| .await?; | ||
|
|
||
| let value = res.value.or(res.result); | ||
| match (res.success, value, res.error) { | ||
| (true, Some(value), _) => Ok(ArchiveCallResult::Success(value)), | ||
| (false, _, err) => Ok(ArchiveCallResult::Error(err.unwrap_or(String::new()))), | ||
| (true, None, _) => { | ||
| let m = "archive_v1_call: 'success: true' response should have `value: 0x1234` alongside it"; | ||
| Err(Error::Deserialization(serde_json::Error::custom(m))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Return the finalized block height of the chain. | ||
| pub async fn archive_v1_finalized_height(&self) -> Result<usize, Error> { | ||
| self.client | ||
| .request("archive_v1_finalizedHeight", rpc_params![]) | ||
| .await | ||
| } | ||
|
|
||
| /// Return the genesis hash. | ||
| pub async fn archive_v1_genesis_hash(&self) -> Result<T::Hash, Error> { | ||
| self.client | ||
| .request("archive_v1_genesisHash", rpc_params![]) | ||
| .await | ||
| } | ||
|
|
||
| /// Given a block height, return the hashes of the zero or more blocks at that height. | ||
| /// For blocks older than the latest finalized block, only one entry will be returned. For blocks | ||
| /// newer than the latest finalized block, it's possible to have 0, 1 or multiple blocks at | ||
| /// that height given that forks could occur. | ||
| pub async fn archive_v1_hash_by_height(&self, height: usize) -> Result<Vec<T::Hash>, Error> { | ||
| self.client | ||
| .request("archive_v1_hashByHeight", rpc_params![height]) | ||
| .await | ||
| } | ||
|
|
||
| /// Fetch the header for a block with the given hash, or `None` if no block with that hash exists. | ||
| pub async fn archive_v1_header(&self, block_hash: T::Hash) -> Result<Option<T::Header>, Error> { | ||
| let maybe_encoded_header: Option<Bytes> = self | ||
| .client | ||
| .request("archive_v1_header", rpc_params![block_hash]) | ||
| .await?; | ||
|
|
||
| let Some(encoded_header) = maybe_encoded_header else { | ||
| return Ok(None); | ||
| }; | ||
|
|
||
| let header = | ||
| <T::Header as codec::Decode>::decode(&mut &*encoded_header.0).map_err(Error::Decode)?; | ||
| Ok(Some(header)) | ||
| } | ||
|
|
||
| /// Query the node storage and return a subscription which streams corresponding storage events back. | ||
| pub async fn archive_v1_storage( | ||
| &self, | ||
| block_hash: T::Hash, | ||
| items: impl IntoIterator<Item = StorageQuery<&[u8]>>, | ||
| child_key: Option<&[u8]>, | ||
| ) -> Result<ArchiveStorageSubscription<T::Hash>, Error> { | ||
| let items: Vec<StorageQuery<String>> = items | ||
| .into_iter() | ||
| .map(|item| StorageQuery { | ||
| key: to_hex(item.key), | ||
| query_type: item.query_type, | ||
| }) | ||
| .collect(); | ||
|
|
||
| let sub = self | ||
| .client | ||
| .subscribe( | ||
| "archive_v1_storage", | ||
| rpc_params![block_hash, items, child_key.map(to_hex)], | ||
| "archive_v1_stopStorage", | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(ArchiveStorageSubscription { sub, done: false }) | ||
| } | ||
|
|
||
| // Dev note: we continue to support the latest "unstable" archive methods because | ||
| // they will be around for a while before the stable ones make it into a release. | ||
| // The below are just a copy-paste of the v1 methods, above, but calling the | ||
| // "unstable" RPCs instead. Eventually we'll remove them. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe open a tracking issue for that so we don't forget |
||
|
|
||
| /// Fetch the block body (ie the extrinsics in the block) given its hash. | ||
| /// | ||
| /// Returns an array of the hexadecimal-encoded scale-encoded extrinsics found in the block, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, we could perhaps have a shared implementation for this for both
archive_v1_callandarchive_unstable_callbut doesn't matter that much since we will remove the unstable stuff eventually :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I contemplated that, but then it'd make it messier to remove them, whereas at the mo we can just delete the unstable ones nice and cleanly :)