Skip to content

Commit

Permalink
v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
flipchan committed Dec 13, 2023
1 parent 9642f29 commit f1b558d
Show file tree
Hide file tree
Showing 33 changed files with 309 additions and 177 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uptest"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
license = "MIT"
homepage = "https://github.com/uptest-sc/uptest/"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ https://github.com/users/uptest-sc/projects/1/views/1

### Examples

Run examples:
```shell
$ sh run_example.sh
```

#### Get metadata version:
```rust
use libuptest::jsonrpseeclient::JsonrpseeClient;
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uptest-cli"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
license = "MIT"
homepage = "https://github.com/uptest-sc/uptest/"
Expand Down
93 changes: 60 additions & 33 deletions cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub async fn auto_test(wshost: &str) -> Result<(), Error> {
}

/// display meta information about chain X
pub async fn chain_info(wshost: &str) -> bool {
let client = JsonrpseeClient::new(wshost).unwrap();
let chain_info: RuntimeVersion = get_runtime_version(client).await.unwrap();
pub async fn chain_info(wshost: &str) -> Result<bool, Error> {
let client = JsonrpseeClient::new(wshost)?;
let chain_info: RuntimeVersion = get_runtime_version(client).await?;
println!("----Chain-Info----");
println!(
"Chain Name: {:?}
Expand All @@ -50,18 +50,17 @@ State Version: {:?}",
chain_info.state_version
);
println!("--E-O-L--");
true
Ok(true)
}

/// return all storagevalues and storagemaps for all pallets
pub async fn get_all_pallets_storage(wshost: &str) -> Vec<storage_map_info> {
let client = JsonrpseeClient::new(wshost).unwrap();
pub async fn get_all_pallets_storage(wshost: &str) -> Result<Vec<storage_map_info>, Error> {
let client = JsonrpseeClient::new(wshost)?;
// get the chain's metadata
let metadatablob = get_raw_metadata(client).await.unwrap();
let metadatablob = get_raw_metadata(client).await?;

let pallet_list: Vec<storage_map_info> =
parse_pallet_storage_types(metadatablob).await.unwrap();
pallet_list
let pallet_list: Vec<storage_map_info> = parse_pallet_storage_types(metadatablob).await?;
Ok(pallet_list)
}

/// subscribe to a chain, wait for runtime upgrade to be triggered, display changes in the storage item types before and after
Expand All @@ -81,7 +80,7 @@ pub async fn storage_changes(
};
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();
parse_pallet_storage_types(old_metadatablob).await?;
println!("Gathered current storage types");
println!("Waiting for runtime upgrade");
let event_grab: H256 = event_watch(client.clone(), runtime_upgrade_event, block_limit)
Expand All @@ -95,8 +94,8 @@ pub async fn storage_changes(
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();
parse_pallet_storage_types(new_metadatablob).await?;
let new_version = get_runtime_version(client.clone()).await?;
println!(
"Runtime upgraded from version: {:?} to new version: {:?}",
old_version.spec_version, new_version.spec_version
Expand Down Expand Up @@ -167,18 +166,24 @@ pub async fn storage_changes(
}

/// return all storagevalues and storagemaps for one single pallets
pub async fn get_single_pallet_storage(wshost: &str, pallet_name: &str) -> Vec<storage_map_info> {
let pallet_list: Vec<storage_map_info> = get_all_pallets_storage(wshost).await;
pub async fn get_single_pallet_storage(
wshost: &str,
pallet_name: &str,
) -> Result<Vec<storage_map_info>, Error> {
let pallet_list: Vec<storage_map_info> = get_all_pallets_storage(wshost).await?;
let new_list: Vec<storage_map_info> = pallet_list
.into_iter()
.filter(|pallet_entry: &storage_map_info| pallet_entry.pallet_name == pallet_name)
.collect(); // filter list based on pallet name
new_list
Ok(new_list)
}

/// display what pallet and functions where triggers in the X amount of latest finalized blocks
pub async fn event_summary_for_latest_blocks(wshost: &str, block_amount: u32) -> bool {
let client = JsonrpseeClient::new(wshost).unwrap(); // change me
pub async fn event_summary_for_latest_blocks(
wshost: &str,
block_amount: u32,
) -> anyhow::Result<bool, Error> {
let client = JsonrpseeClient::new(wshost)?; // change me
let metadatablob = get_raw_metadata(client.clone()).await.unwrap();
println!("Subscribing to latest finalized blocks at {wshost:?}");
let mut subscrib: SubscriptionWrapper<Header> = client
Expand All @@ -188,20 +193,28 @@ pub async fn event_summary_for_latest_blocks(wshost: &str, block_amount: u32) ->
RpcParams::new(),
"chain_unsubscribeFinalizedHeads",
)
.unwrap();
.map_err(|_err| Error::ConnectionSubscriptionProblem)?;

for _ in 0..block_amount {
let tmp_client = JsonrpseeClient::new(wshost).unwrap();
let tmp_client = JsonrpseeClient::new(wshost)?;
let nextone = subscrib.next();
let blocknr = nextone.unwrap().unwrap().number;
let blocknr = match nextone {
Some(Ok(header)) => header.clone().number,
Some(Err(_err)) => {
return Err(Error::BlockparseError);
}
None => {
return Err(Error::BlockparseError);
}
};
println!("------------------------------------------------");
println!("Latest finalized block number: #{}", blocknr);
let blockhash: H256 = blocknumber_to_blockhash(tmp_client.clone(), blocknr.clone())
.await
.unwrap();
.map_err(|_err| Error::CouldNotGetBlock)?;
println!("Finalized block hash: {blockhash:?}");

let preblock = get_block_events(blockhash, tmp_client).await.unwrap();
let preblock = get_block_events(blockhash, tmp_client).await?;

let extrinsics = preblock.block.extrinsics;

Expand All @@ -223,11 +236,15 @@ pub async fn event_summary_for_latest_blocks(wshost: &str, block_amount: u32) ->
}

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

/// Subscribe and break on user defined event
pub async fn watch_for_event(wshost: &str, pallet_name: &str, pallet_method: &str) -> bool {
pub async fn watch_for_event(
wshost: &str,
pallet_name: &str,
pallet_method: &str,
) -> Result<bool, Error> {
println!("Subscribing to Chain X, Metadata Version Y");
println!("Connecting to chain..");
let client = JsonrpseeClient::new(wshost).unwrap();
Expand All @@ -242,18 +259,28 @@ pub async fn watch_for_event(wshost: &str, pallet_name: &str, pallet_method: &st
RpcParams::new(),
"chain_unsubscribeFinalizedHeads",
)
.unwrap();
let metadatablob = get_raw_metadata(client.clone()).await.unwrap();
.map_err(|_err| Error::ConnectionSubscriptionProblem)?;
let metadatablob = get_raw_metadata(client.clone())
.await
.map_err(|_err| Error::NoMetaData)?;

for _ in 0..100 {
let nextone = subscrib.next();
let blocknr = nextone.unwrap().unwrap().number;
let blockhash: H256 = blocknumber_to_blockhash(client.clone(), blocknr.clone())
.await
.unwrap(); // can we change this syntax so we are not looping clone's?

let blocknr = match nextone {
Some(Ok(header)) => header.clone().number,
Some(Err(_err)) => {
return Err(Error::BlockparseError);
}
None => {
// Handle the case when nextone is None (no value)
return Err(Error::BlockparseError);
}
};
let blockhash: H256 = blocknumber_to_blockhash(client.clone(), blocknr.clone()).await?; // can we change this syntax so we are not looping clone's?

println!("Checking block #{}", blocknr);
let preblock = get_block_events(blockhash, client.clone()).await.unwrap();
let preblock = get_block_events(blockhash, client.clone()).await?;
let extrinsics = preblock.block.extrinsics;
println!("Got block events... Decoding it..");
let decodedevent_list: Vec<event_summary> = extrinsics
Expand All @@ -276,7 +303,7 @@ pub async fn watch_for_event(wshost: &str, pallet_name: &str, pallet_method: &st
}

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

/*
Expand Down
22 changes: 8 additions & 14 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
// Uptest --chain X --wshost ws://host:port --pallet-test scheduler --sudo "seed goes here"

use clap::ArgMatches;
use libuptest::error::Error;
use libuptest::pallet_storage_parse::storage_map_info;
mod cli;
mod helper;

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Error> {
println!("Uptest command line tool");
let matches: ArgMatches = cli::gen_cli().get_matches();
// println!("Matches: {:?}", matches.subcommand_name());
Expand All @@ -28,9 +29,9 @@ async fn main() {
let pallet_name = sub_m.get_one::<String>("pallet_name").map(|s| s.as_str());
// println!("Pallet name: {pallet_name:?} Pallet method: {pallet_method:?}");
// println!("pallet method sub ok");
helper::watch_for_event(
let _ = helper::watch_for_event(
"ws://127.0.0.1:9944",
pallet_name.unwrap(),
pallet_name.unwrap(), // this is a clap unwrap problem...
pallet_method.unwrap(),
)
.await;
Expand All @@ -52,18 +53,10 @@ async fn main() {
.expect("Could not get ws host")
.to_owned();

// some input validation
match &ws_host[0..5] == "ws://" {
true => {}
false => {
panic!("ws host does not start with ws://, double check ws address");
}
}

// let wshost: &str = sub_m.get_one::<&str>("ws_host").map(|s| s).unwrap();
println!("Gathering information about all pallet's storage information");
let listan: Vec<storage_map_info> =
helper::get_all_pallets_storage(ws_host.as_str()).await;
helper::get_all_pallets_storage(ws_host.as_str()).await?;
for mypallet in listan.iter() {
println!("Pallet name: {:?}\r\n - Storage item name: {:?}\r\n - Storage type: {:?}\r\n - Storage type id key: {:?}\r\n - Pallet Raw type: {:?}",
&mypallet.pallet_name, &mypallet.storage_item_name, &mypallet.storage_type, &mypallet.type_id, &mypallet.raw_type
Expand All @@ -77,7 +70,7 @@ async fn main() {
.get_one::<String>("ws")
.expect("Could not get ws host")
.to_owned();
let _out = helper::chain_info(&ws_host).await;
let _out = helper::chain_info(&ws_host).await?;
}

Some("auto-test") => {
Expand All @@ -99,7 +92,7 @@ async fn main() {

println!("Gathering information about pallet: {pallet_name:?}");
let listan: Vec<storage_map_info> =
helper::get_single_pallet_storage(&ws_host, &pallet_name).await;
helper::get_single_pallet_storage(&ws_host, &pallet_name).await?;
match listan.len() {
0 => {
println!("Could not find any pallet by that name, check spelling");
Expand Down Expand Up @@ -169,4 +162,5 @@ async fn main() {
*/
Ok(())
}
8 changes: 4 additions & 4 deletions examples/examples/block_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
use libuptest::jsonrpseeclient::JsonrpseeClient;
use libuptest::types::{PreBlock, H256};
use libuptest::ws_mod::get_block_events;

use libuptest::error::Error;
use std::str::FromStr;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<(), Error> {
println!("function started");
let polkadot_dial = JsonrpseeClient::polkadot_default_url().unwrap();
let polkadot_dial = JsonrpseeClient::polkadot_default_url()?;
println!("Connection established");
let mablock: H256 =
H256::from_str("0x8784cba4254c3800f502b0732b0260d0dee3b85701e8cbbd45bdddb7d3d2d5bf")?;
println!("mablock ok");
let output: PreBlock = get_block_events(mablock, polkadot_dial).await.unwrap();
let output: PreBlock = get_block_events(mablock, polkadot_dial).await?;
println!("got output: {:?}", output);
println!("Block Nr: {:?}", output.block.header.number);
println!("function passed, output:");
Expand Down
7 changes: 4 additions & 3 deletions examples/examples/chain_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ State Version: 1
*/
use libuptest::types::RuntimeVersion;
use libuptest::ws_mod::get_runtime_version;
use libuptest::error::Error;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<(), Error> {
// chain at 127.0.0.1:9944
let client = JsonrpseeClient::with_default_url().unwrap();
let chain_info: RuntimeVersion = get_runtime_version(client).await.unwrap();
let client = JsonrpseeClient::with_default_url()?;
let chain_info: RuntimeVersion = get_runtime_version(client).await?;
println!("----Chain-Info----");
println!(
"Chain Name: {:?}
Expand Down
8 changes: 5 additions & 3 deletions examples/examples/decode_extrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ use libuptest::decode_extrinsic::decode_extrinsic_hex_string;
use libuptest::jsonrpseeclient::JsonrpseeClient;
use libuptest::types::event_summary;
use libuptest::ws_mod::get_raw_metadata;
use libuptest::error::Error;


#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<(), Error> {
let raw_extrinsic = "0x280403000ba0ada8438801"; // time stamp extrinsic taken from random polkadot block
println!("Raw extrinsic value: {raw_extrinsic:?}");
println!("Downloading metadata");
let metadata: Vec<u8> = get_raw_metadata(JsonrpseeClient::polkadot_default_url().unwrap())
let metadata: Vec<u8> = get_raw_metadata(JsonrpseeClient::polkadot_default_url()?)
.await
.unwrap(); // yolo
?;
println!("Metadata downloaded ok");
let decoded_output = decode_extrinsic_hex_string(raw_extrinsic, &metadata);
let single_event: event_summary = event_summary {
Expand Down
22 changes: 22 additions & 0 deletions examples/examples/decode_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// decode a transaction and display it in a nice human readable format
/*
struct supported_chains {
Edgeware: "",
Polkadot: "",
Hydradx: "",
Sora: ""
}
*/
async fn tx_decode(network: String, tx: String) -> String {
"asd".to_string()
}

#[tokio::main]
async fn main() {
let network = "hydradx";
let tx = "";
//let flufr = tx_decode(network, tx).await;
// println!("GOt output: {:?}", flufr);
}
Loading

0 comments on commit f1b558d

Please sign in to comment.