Skip to content

Commit

Permalink
test parallel extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib committed Nov 22, 2023
1 parent 84e6382 commit cfb2ac6
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions testing/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use substrate_api_client::{
AssetRuntimeConfig, Config, ExtrinsicSigner as GenericExtrinsicSigner, SignExtrinsic,
},
extrinsic::BalancesExtrinsics,
rpc::{HandleSubscription, JsonrpseeClient},
rpc::{HandleSubscription, TungsteniteRpcClient},
Api, SubmitAndWatch, SubmitExtrinsic, TransactionStatus, XtStatus,
};

Expand All @@ -36,7 +36,7 @@ type Hash = <AssetRuntimeConfig as Config>::Hash;
#[tokio::main]
async fn main() {
// Setup
let client = JsonrpseeClient::with_default_url().unwrap();
let client = TungsteniteRpcClient::with_default_url(5);
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();

Expand Down Expand Up @@ -65,7 +65,7 @@ async fn main() {
});

// Test different _watch_untils with events
thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt2 = api.balance_transfer_allow_death(bob.clone(), 1000);
let extrinsic_hash: H256 = sp_core::blake2_256(&xt2.encode()).into();
let report = api.submit_and_watch_extrinsic_until(xt2, XtStatus::Ready).unwrap();
Expand All @@ -75,29 +75,29 @@ async fn main() {
assert!(report.events.is_none());
println!("Success: submit_and_watch_extrinsic_until Ready");

thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt3 = api.balance_transfer_allow_death(bob.clone(), 1000);
let report = api.submit_and_watch_extrinsic_until(xt3, XtStatus::Broadcast).unwrap();
let report = api.submit_and_watch_extrinsic_until(xt3, XtStatus::Finalized).unwrap();
// The xt is not broadcast - we only have one node running. Therefore, InBlock is returned.
assert!(report.block_hash.is_some());
assert!(matches!(report.status, TransactionStatus::InBlock(_)));
assert!(matches!(report.status, TransactionStatus::Finalized(_)));
// But we still don't fetch events, since we originally only waited for Broadcast.
assert!(report.events.is_none());
//assert!(report.events.is_none());
println!("Success: submit_and_watch_extrinsic_until Broadcast");

let api2 = api.clone();
thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt4 = api2.balance_transfer_allow_death(bob.clone(), 1000);
let until_in_block_handle = thread::spawn(move || {
let report = api2.submit_and_watch_extrinsic_until(xt4, XtStatus::InBlock).unwrap();
let report = api2.submit_and_watch_extrinsic_until(xt4, XtStatus::Finalized).unwrap();
assert!(report.block_hash.is_some());
assert!(matches!(report.status, TransactionStatus::InBlock(_)));
assert!(matches!(report.status, TransactionStatus::Finalized(_)));
assert_associated_events_match_expected(report.events.unwrap());
println!("Success: submit_and_watch_extrinsic_until InBlock");
});

let api3 = api.clone();
thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt5 = api.balance_transfer_allow_death(bob.clone(), 1000);
let until_finalized_handle = thread::spawn(move || {
let report = api3.submit_and_watch_extrinsic_until(xt5, XtStatus::Finalized).unwrap();
Expand All @@ -109,24 +109,19 @@ async fn main() {

// Test some _watch_untils_without_events. One is enough, because it is tested implicitly by `submit_and_watch_extrinsic_until`
// as internal call.
thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt6 = api.balance_transfer_allow_death(bob.clone(), 1000);
let report = api
.submit_and_watch_extrinsic_until_without_events(xt6, XtStatus::Ready)
.submit_and_watch_extrinsic_until_without_events(xt6, XtStatus::Finalized)
.unwrap();
assert!(report.block_hash.is_none());
assert!(report.events.is_none());
println!("Success: submit_and_watch_extrinsic_until_without_events Ready!");

thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
thread::sleep(Duration::from_secs(2)); // Wait a little to avoid transaction too low priority error.
let xt7 = api.balance_transfer_allow_death(bob, 1000);
let report = api
.submit_and_watch_extrinsic_until_without_events(xt7, XtStatus::InBlock)
.submit_and_watch_extrinsic_until_without_events(xt7, XtStatus::Finalized)
.unwrap();
println!("Extrinsic got successfully included in Block!");
assert!(report.block_hash.is_some());
assert!(report.events.is_none());

watch_handle.join().unwrap();
until_in_block_handle.join().unwrap();
until_finalized_handle.join().unwrap();
Expand Down

0 comments on commit cfb2ac6

Please sign in to comment.