This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
make remote ext use batch ws-client #8916
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
76207f5
make remote ext use batch ws-client
kianenigma 874d0a6
Add debug log for key length
emostov 9463c47
better assertions
kianenigma 48dad42
new sanity_checl
kianenigma c129e00
try and make it work with batch
kianenigma 639eb8c
Upstream.into()
kianenigma 07cd7b4
update test
kianenigma 6ee383a
remove exctra uri
kianenigma a02425d
add missing at
kianenigma 305bcc4
remove unused rpc stuff
kianenigma d6f66ac
improve
kianenigma 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
|---|---|---|
|
|
@@ -114,17 +114,18 @@ use sp_core::{ | |
| }; | ||
| use codec::{Encode, Decode}; | ||
| use sp_runtime::traits::Block as BlockT; | ||
| use jsonrpsee_ws_client::{WsClientBuilder, WsClient}; | ||
| use jsonrpsee_ws_client::{ | ||
| WsClientBuilder, WsClient, v2::params::JsonRpcParams, traits::Client, | ||
| }; | ||
|
|
||
| type KeyPair = (StorageKey, StorageData); | ||
|
|
||
| const LOG_TARGET: &str = "remote-ext"; | ||
| const DEFAULT_TARGET: &str = "wss://rpc.polkadot.io"; | ||
| const BATCH_SIZE: usize = 512; | ||
|
|
||
| jsonrpsee_proc_macros::rpc_client_api! { | ||
| RpcApi<B: BlockT> { | ||
| #[rpc(method = "state_getStorage", positional_params)] | ||
| fn get_storage(prefix: StorageKey, hash: Option<B::Hash>) -> StorageData; | ||
| #[rpc(method = "state_getKeysPaged", positional_params)] | ||
| fn get_keys_paged( | ||
| prefix: Option<StorageKey>, | ||
|
|
@@ -279,7 +280,7 @@ impl<B: BlockT> Builder<B> { | |
| async fn get_keys_paged( | ||
| &self, | ||
| prefix: StorageKey, | ||
| hash: B::Hash, | ||
| at: B::Hash, | ||
| ) -> Result<Vec<StorageKey>, &'static str> { | ||
| const PAGE: u32 = 512; | ||
| let mut last_key: Option<StorageKey> = None; | ||
|
|
@@ -290,7 +291,7 @@ impl<B: BlockT> Builder<B> { | |
| Some(prefix.clone()), | ||
| PAGE, | ||
| last_key.clone(), | ||
| Some(hash), | ||
| Some(at), | ||
| ) | ||
| .await | ||
| .map_err(|e| { | ||
|
|
@@ -328,29 +329,53 @@ impl<B: BlockT> Builder<B> { | |
| prefix: StorageKey, | ||
| at: B::Hash, | ||
| ) -> Result<Vec<KeyPair>, &'static str> { | ||
| use serde_json::to_value; | ||
| let keys = self.get_keys_paged(prefix, at).await?; | ||
| let keys_count = keys.len(); | ||
| info!(target: LOG_TARGET, "Querying a total of {} keys", keys.len()); | ||
|
|
||
| let mut key_values: Vec<KeyPair> = vec![]; | ||
| for key in keys { | ||
| let value = | ||
| RpcApi::<B>::get_storage(self.as_online().rpc_client(), key.clone(), Some(at)) | ||
| .await | ||
| .map_err(|e| { | ||
| error!(target: LOG_TARGET, "Error = {:?}", e); | ||
| "rpc get_storage failed" | ||
| })?; | ||
| key_values.push((key, value)); | ||
| if key_values.len() % 1000 == 0 { | ||
| let ratio: f64 = key_values.len() as f64 / keys_count as f64; | ||
| debug!( | ||
| target: LOG_TARGET, | ||
| "progress = {:.2} [{} / {}]", | ||
| ratio, | ||
| key_values.len(), | ||
| keys_count, | ||
| ); | ||
| let client = self.as_online().rpc_client(); | ||
| for chunk_keys in keys.chunks(BATCH_SIZE) { | ||
| let batch = chunk_keys | ||
| .iter() | ||
| .cloned() | ||
| .map(|key| { | ||
| ( | ||
| "state_getStorage", | ||
| JsonRpcParams::Array( | ||
| vec![ | ||
| to_value(key).expect("json serialization will work; qed."), | ||
| to_value(at).expect("json serialization will work; qed."), | ||
| ] | ||
| ), | ||
| ) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| let values = client.batch_request::<Option<StorageData>>(batch) | ||
| .await | ||
| .map_err(|e| { | ||
| log::error!(target: LOG_TARGET, "failed to execute batch {:?} due to {:?}", chunk_keys, e); | ||
| "batch failed." | ||
| })?; | ||
| assert_eq!(chunk_keys.len(), values.len()); | ||
| for (idx, key) in chunk_keys.into_iter().enumerate() { | ||
| let maybe_value = values[idx].clone(); | ||
| let value = maybe_value.unwrap_or_else(|| { | ||
| log::warn!(target: LOG_TARGET, "key {:?} had none corresponding value.", &key); | ||
| StorageData(vec![]) | ||
| }); | ||
| key_values.push((key.clone(), value)); | ||
| if key_values.len() % (10 * BATCH_SIZE) == 0 { | ||
| let ratio: f64 = key_values.len() as f64 / keys_count as f64; | ||
| debug!( | ||
| target: LOG_TARGET, | ||
| "progress = {:.2} [{} / {}]", | ||
| ratio, | ||
| key_values.len(), | ||
| keys_count, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -529,7 +554,7 @@ mod remote_tests { | |
| init_logger(); | ||
| Builder::<Block>::new() | ||
| .mode(Mode::Online(OnlineConfig { | ||
| modules: vec!["Proxy".to_owned()], | ||
| modules: vec!["System".to_owned()], | ||
|
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. Should we add somewhere |
||
| ..Default::default() | ||
| })) | ||
| .build() | ||
|
|
@@ -538,6 +563,55 @@ mod remote_tests { | |
| .execute_with(|| {}); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn can_build_few_pallet() { | ||
| init_logger(); | ||
| Builder::<Block>::new() | ||
| .mode(Mode::Online(OnlineConfig { | ||
| modules: vec!["Proxy".to_owned(), "Multisig".to_owned(), "PhragmenElection".to_owned()], | ||
| ..Default::default() | ||
| })) | ||
| .build() | ||
| .await | ||
| .expect("Can't reach the remote node. Is it running?") | ||
| .execute_with(|| {}); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn sanity_check_decoding() { | ||
| use pallet_elections_phragmen::SeatHolder; | ||
| use sp_core::crypto::Ss58Codec; | ||
| type AccountId = sp_runtime::AccountId32; | ||
| type Balance = u128; | ||
| frame_support::generate_storage_alias!( | ||
| PhragmenElection, | ||
| Members => | ||
| Value<Vec<SeatHolder<AccountId, Balance>>> | ||
| ); | ||
|
|
||
| init_logger(); | ||
| Builder::<Block>::new() | ||
| .mode(Mode::Online(OnlineConfig { | ||
| modules: vec!["PhragmenElection".to_owned()], | ||
| ..Default::default() | ||
| })) | ||
| .build() | ||
| .await | ||
| .expect("Can't reach the remote node. Is it running?") | ||
| .execute_with(|| { | ||
| // Gav's polkadot account. 99% this will be in the council. | ||
| let gav_polkadot = | ||
| AccountId::from_ss58check("13RDY9nrJpyTDBSUdBw12dGwhk19sGwsrVZ2bxkzYHBSagP2") | ||
| .unwrap(); | ||
| let members = Members::get().unwrap(); | ||
| assert!(members | ||
| .iter() | ||
| .map(|s| s.who.clone()) | ||
| .find(|a| a == &gav_polkadot) | ||
| .is_some()); | ||
| }); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn can_create_state_snapshot() { | ||
| init_logger(); | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.