Skip to content
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

add test-long-xt file #637

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ jobs:
compose_extrinsic_offline,
custom_nonce,
check_extrinsic_events,
test_long_xt,
get_account_identity,
get_blocks_async,
get_storage,
Expand Down
Binary file not shown.
51 changes: 51 additions & 0 deletions examples/examples/test_long_xt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2019 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! This example shows how to use the compose_extrinsic macro to create an extrinsic for any (custom)
//! module, whereas the desired module and call are supplied as a string.

use kitchensink_runtime::AccountId;
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_compose_macros::{compose_call, compose_extrinsic},
ac_primitives::{
AssetRuntimeConfig, ExtrinsicSigner as GenericExtrinsicSigner, SignExtrinsic,
UncheckedExtrinsicV4,
},
rpc::WsRpcClient,
Api, SubmitAndWatch, XtStatus,
};

#[tokio::main]
async fn main() {
env_logger::init();

// Initialize api and set the signer (sender) that is used to sign the extrinsics.
let sudoer = AccountKeyring::Alice.pair();
let client = WsRpcClient::with_default_url();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(GenericExtrinsicSigner::<_>::new(sudoer));

// Compose a call that should only be executable via Sudo.
let code: Vec<u8> = include_bytes!("integritee_node_runtime-v.compact.wasm").to_vec();
let call = compose_call!(api.metadata(), "System", "set_code", code);

let xt: UncheckedExtrinsicV4<_, _, _, _> = compose_extrinsic!(&api, "Sudo", "sudo", call);

// Send and watch extrinsic until in block.
let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).unwrap();
println!("[+] Extrinsic got successfully executed {:?}", report.extrinsic_hash);
println!("[+] Extrinsic got included. Block Hash: {:?}", report.block_hash);
}
57 changes: 23 additions & 34 deletions testing/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//! Tests for the author rpc interface functions.

use kitchensink_runtime::AccountId;
use kitchensink_runtime::{AccountId, BalancesCall, RuntimeCall};
use sp_core::{Encode, H256};
use sp_keyring::AccountKeyring;
use std::{thread, time::Duration};
Expand All @@ -25,8 +25,8 @@ use substrate_api_client::{
AssetRuntimeConfig, Config, ExtrinsicSigner as GenericExtrinsicSigner, SignExtrinsic,
},
extrinsic::BalancesExtrinsics,
rpc::{HandleSubscription, JsonrpseeClient},
Api, SubmitAndWatch, SubmitExtrinsic, TransactionStatus, XtStatus,
rpc::{HandleSubscription, TungsteniteRpcClient},
Api, SubmitAndWatch, TransactionStatus, XtStatus,
};

type ExtrinsicSigner = GenericExtrinsicSigner<AssetRuntimeConfig>;
Expand All @@ -36,22 +36,22 @@ 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();

api.set_signer(ExtrinsicSigner::new(alice_pair));

let bob: ExtrinsicAddressOf<ExtrinsicSigner> = AccountKeyring::Bob.to_account_id().into();

// Submit extrinsic.
let xt0 = api.balance_transfer_allow_death(bob.clone(), 1000);
let _tx_hash = api.submit_extrinsic(xt0).unwrap();
let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death {
dest: bob.clone(),
value: 1000,
});
let nonce = api.get_nonce().unwrap();

// Submit and watch.
thread::sleep(Duration::from_secs(6)); // Wait a little to avoid transaction too low priority error.
let api1 = api.clone();
let xt1 = api.balance_transfer_allow_death(bob.clone(), 1000);
let xt1 = api.compose_extrinsic_offline(call.clone(), nonce);
let watch_handle = thread::spawn(move || {
let mut tx_subscription = api1.submit_and_watch_extrinsic(xt1).unwrap();
let tx_status = tx_subscription.next().unwrap().unwrap();
Expand All @@ -65,8 +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.
let xt2 = api.balance_transfer_allow_death(bob.clone(), 1000);
let xt2 = api.compose_extrinsic_offline(call.clone(), nonce + 1);
let extrinsic_hash: H256 = sp_core::blake2_256(&xt2.encode()).into();
let report = api.submit_and_watch_extrinsic_until(xt2, XtStatus::Ready).unwrap();
assert_eq!(extrinsic_hash, report.extrinsic_hash);
Expand All @@ -75,30 +74,27 @@ 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.
let xt3 = api.balance_transfer_allow_death(bob.clone(), 1000);
let report = api.submit_and_watch_extrinsic_until(xt3, XtStatus::Broadcast).unwrap();
let xt3 = api.compose_extrinsic_offline(call.clone(), nonce + 2);
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.
let xt4 = api2.balance_transfer_allow_death(bob.clone(), 1000);
let xt4 = api.compose_extrinsic_offline(call.clone(), nonce + 3);
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.
let xt5 = api.balance_transfer_allow_death(bob.clone(), 1000);
let xt5 = api.compose_extrinsic_offline(call.clone(), nonce + 4);
let until_finalized_handle = thread::spawn(move || {
let report = api3.submit_and_watch_extrinsic_until(xt5, XtStatus::Finalized).unwrap();
assert!(report.block_hash.is_some());
Expand All @@ -109,24 +105,17 @@ 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.
let xt6 = api.balance_transfer_allow_death(bob.clone(), 1000);
let xt6 = api.compose_extrinsic_offline(call.clone(), nonce + 5);
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.
let xt7 = api.balance_transfer_allow_death(bob, 1000);
let xt7 = api.compose_extrinsic_offline(call.clone(), nonce + 6);
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