Skip to content

Commit

Permalink
storage item diff, hell yeah :D
Browse files Browse the repository at this point in the history
  • Loading branch information
flipchan committed Jul 4, 2023
1 parent d24582f commit 71f30c9
Show file tree
Hide file tree
Showing 13 changed files with 1,663 additions and 144 deletions.
1,665 changes: 1,539 additions & 126 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
edition = "2021"

[dependencies]
libuptest = { path = "../libuptest", version = "0.1.1", features = ["metadatadecode"] }
libuptest = { path = "../libuptest", version = "0.1.1", features = ["metadatadecode"] } # "subxthelper"
anyhow = "1.0.70"
tokio = { version = "1.27", features = ["full"] }
jsonrpsee = { version = "0.16.2", features = ["server", "ws-client", "macros", "client-ws-transport"]}
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/get_pallets_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libuptest::ws_mod::{get_latest_finalized_head, get_raw_metadata};
//use jsonrpsee::ws_client::{WsClientBuilder, WsClient};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<()> {
let tmpclient = JsonrpseeClient::with_default_url().unwrap();

let metadata: Vec<u8> = get_raw_metadata(tmpclient).await.unwrap();
Expand All @@ -17,7 +17,7 @@ async fn main() -> anyhow::Result<()> {
let metadata: Metadata = Metadata::from_bytes(metadata_polkadot_scale).expect("valid metadata");
let output: Extrinsic =
decoder::decode_extrinsic(&metadata, ext_bytes).expect("can decode extrinsic");
output.into_owned()
let _ = output.into_owned();

Ok(())
}
}
90 changes: 90 additions & 0 deletions examples/examples/upgrade_change_diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/// get a diff of storage items that have changed during a runtime upgrade
use libuptest::jsonrpseeclient::JsonrpseeClient;
use libuptest::types::{event_summary, H256};
use libuptest::ws_mod::{event_watch, get_raw_metadata, get_runtime_version};
use tokio::time::{sleep, Duration};

use libuptest::pallet_storage_parse::{parse_pallet_storage_types, storage_map_info};

#[tokio::main]
async fn main() -> anyhow::Result<(), libuptest::error::Error> {
let client = JsonrpseeClient::with_default_url().unwrap();
let old_version = get_runtime_version(client.clone()).await.unwrap();
println!(
"Connected to: {:?} Runtime version: {:?}",
old_version.spec_name, old_version.spec_version
);
let runtime_upgrade_event: event_summary = event_summary {
pallet_name: "Sudo".to_string(),
pallet_method: "sudo_unchecked_weight".to_string(),
};
let block_limit: u32 = 100u32;
println!("Waiting for custom event to be triggered");
let old_metadatablob = get_raw_metadata(client.clone()).await?;
let old_pallet_list: Vec<storage_map_info> =
parse_pallet_storage_types(old_metadatablob).await.unwrap();
let event_grab: Result<H256, libuptest::error::Error> =
event_watch(client.clone(), runtime_upgrade_event, block_limit).await;
println!("Event detected in block: {:?}", event_grab.unwrap());
println!("Having a coffee break before next block...");
let duration_to_wait = Duration::new(10, 0); // chill 10 seconds
let _ = sleep(duration_to_wait).await;
// diff the predata and the new data
println!("Scanning the new metadata for changes");
let new_metadatablob = get_raw_metadata(client.clone()).await?;
let new_pallet_list: Vec<storage_map_info> =
parse_pallet_storage_types(new_metadatablob).await.unwrap();
let new_version = get_runtime_version(client.clone()).await.unwrap();
println!(
"Runtime upgraded from version: {:?} to new version: {:?}",
old_version.spec_version, new_version.spec_version
);

// check which items only the type has been changed, storagemap could have changed type but not name
let changed_items: Vec<_> = new_pallet_list
.iter()
.filter(|new_item| {
old_pallet_list
.iter()
.find(|old_item| {
old_item.pallet_name == new_item.pallet_name
&& old_item.storage_item_name == new_item.storage_item_name
// && old_item.type_id == new_item.type_id
&& old_item.raw_type != new_item.raw_type
})
.is_some()
})
.collect();

// Print the changed items
for item in &changed_items {
println!(
"Changed {:?}: {:?} in Pallet: {:?} to the new type: {:?}",
item.storage_type, item.storage_item_name, item.pallet_name, item.raw_type
);
}
// lets check the new storage items that has been added to the runtime
let added_items: Vec<_> = new_pallet_list
.iter()
.filter(|new_item| {
old_pallet_list
.iter()
.find(|old_item| {
old_item.pallet_name == new_item.pallet_name
&& old_item.storage_item_name == new_item.storage_item_name
})
.is_none()
})
.collect();

for sitem in added_items.iter() {
println!(
"Pallet: {:?} has added a {:?} with the type: {:?}",
sitem.pallet_name, sitem.storage_type, sitem.raw_type
);
}


println!("All good");
Ok(())
}
8 changes: 7 additions & 1 deletion libuptest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ tokio = { version = "1.27", features = ["full"] }


[features]
all = ["metadatadecode"]
all = ["metadatadecode", "subxthelper"]
metadatadecode = ["desub-current", "frame-metadata"]
subxthelper = ["subxt"]

[dependencies.subxt]
version = "0.28.0"
optional = true
#git = "https://github.com/paritytech/subxt/"


[dependencies.desub-current]
Expand Down
5 changes: 4 additions & 1 deletion libuptest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
mod chains; // depricate me
pub mod codec;
mod connect;
mod error;
pub mod error;
pub mod jsonrpseeclient;
pub mod metadata;
pub mod types;
pub mod ws_mod;

#[cfg(feature = "subxthelper")]
pub mod subxt_helper;

#[cfg(feature = "metadatadecode")]
pub mod decode_extrinsic;
#[cfg(feature = "metadatadecode")]
Expand Down
2 changes: 1 addition & 1 deletion libuptest/src/pallet_storage_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use desub_current::{
type TypeDef = desub_current::scale_info::TypeDef<PortableForm>;
use frame_metadata::v14::StorageEntryType; // v14 only rn..

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct storage_map_info {
pub pallet_name: String,
pub storage_item_name: String, // name of storagemap
Expand Down
4 changes: 4 additions & 0 deletions libuptest/src/subxt_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// schedule a subxt transaction to happen before or after a user defined event is triggered
use subxt;

pub async fn send_during_event(beforetx: String, aftertx: String) -> bool {}
2 changes: 1 addition & 1 deletion libuptest/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct event_summary {
pub pallet_method: String,
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum storage_types {
/// Substrate StorageValue
StorageValue,
Expand Down
6 changes: 6 additions & 0 deletions libuptest/src/ws_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ pub async fn get_runtime_version(

/// return the H256 hash of the block the user given event is triggered on
/// client, block event to find, amount of blocks to check
/// let custom_event: event_summary = event_summary {
/// pallet_name: "Sudo".to_string(),
/// pallet_method: "sudo_unchecked_weight".to_string(),
///};
/// let block_limit: u32 = 100u32;
/// let search_n_find = event_watch(client, custom_event, block_limit).await
#[cfg(feature = "metadatadecode")]
#[maybe_async::maybe_async(?Send)]
pub async fn event_watch(
Expand Down
2 changes: 0 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ pub fn gen_cli() -> Command {
.arg(arg!(<ws> "ws endpoint of the chain to connect"))
.arg(arg!(<block_limit> "amount of blocks of latest blocks to subscribe to").required(true)),
)


// read local wasm file and submit runtime upgrade
.subcommand(
Command::new("submit-wasm")
Expand Down
4 changes: 2 additions & 2 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn event_summary_for_latest_blocks(wshost: &str, block_amount: u32) ->
.unwrap();

for _ in 0..block_amount {
let tmp_client = JsonrpseeClient::new(wshost).unwrap();
let tmp_client = JsonrpseeClient::new(wshost).unwrap();
let nextone = subscrib.next();
let blocknr = nextone.unwrap().unwrap().number;
println!("Latest finalized block: {:?}", blocknr);
Expand All @@ -72,7 +72,7 @@ pub async fn event_summary_for_latest_blocks(wshost: &str, block_amount: u32) ->
}

let _ = subscrib.unsubscribe();
true//Ok(true)
true //Ok(true)
}

/// Subscribe and break on user defined event
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,19 @@ async fn main() {
let wshost: String = sub_m.get_one::<String>("ws").unwrap().to_owned();
let mut dalimit: u32 = 0;
if let Some(c) = sub_m.get_one::<String>("block_limit") {
let k: u32 = c.parse::<u32>().unwrap();;
let k: u32 = c.parse::<u32>().unwrap();
dalimit = k;
println!("Value for blocklimit: {c}");
}
//let block_amount: &u32 = sub_m.get_one("blocklimit").unwrap();
// let block_amount: u32 = sub_m.get_one::<u32>("blocklimit").unwrap().to_owned();
// let block_amount: u32 = sub_m.get_one::<u32>("block_limit").unwrap().to_owned();

//let block_amount: &u32 = sub_m.get_one("blocklimit").unwrap();
// let block_amount: u32 = sub_m.get_one::<u32>("blocklimit").unwrap().to_owned();
// let block_amount: u32 = sub_m.get_one::<u32>("block_limit").unwrap().to_owned();

let _runner = helper::event_summary_for_latest_blocks(&wshost, dalimit).await;
println!("all good");
}
Some("submit-wasm") => {

/*
let sub_m = matches.subcommand_matches("submit-wasm").unwrap();
let file_path: &OsStr = sub_m.get_one::<&OsStr>("wasm_filepath").map(|s| s).unwrap();
Expand Down

0 comments on commit 71f30c9

Please sign in to comment.