mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 17:57:56 +00:00
Move test crates into a "testing" folder and add a ui (trybuild) test and ui-test helpers (#567)
* move test crates into a testing folder and add a ui test and helpers * undo wee mixup with another PR * cargo fmt * clippy * tidy ui-tests a little * test different DispatchError types * refactor dispatch error stuff * name ui tests * duff => useless * align versions and cargo fmt
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
test_node_process,
|
||||
test_node_process_with,
|
||||
utils::node_runtime::system,
|
||||
};
|
||||
|
||||
use sp_core::storage::{
|
||||
well_known_keys,
|
||||
StorageKey,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[tokio::test]
|
||||
async fn insert_key() {
|
||||
let test_node_process = test_node_process_with(AccountKeyring::Bob).await;
|
||||
let client = test_node_process.client();
|
||||
let public = AccountKeyring::Alice.public().as_array_ref().to_vec();
|
||||
client
|
||||
.rpc()
|
||||
.insert_key(
|
||||
"aura".to_string(),
|
||||
"//Alice".to_string(),
|
||||
public.clone().into(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(client
|
||||
.rpc()
|
||||
.has_key(public.clone().into(), "aura".to_string())
|
||||
.await
|
||||
.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_block_hash() {
|
||||
let node_process = test_node_process().await;
|
||||
node_process.client().rpc().block_hash(None).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_block() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let block_hash = client.rpc().block_hash(None).await.unwrap();
|
||||
client.rpc().block(block_hash).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_read_proof() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let block_hash = client.rpc().block_hash(None).await.unwrap();
|
||||
client
|
||||
.rpc()
|
||||
.read_proof(
|
||||
vec![
|
||||
StorageKey(well_known_keys::HEAP_PAGES.to_vec()),
|
||||
StorageKey(well_known_keys::EXTRINSIC_INDEX.to_vec()),
|
||||
],
|
||||
block_hash,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chain_subscribe_blocks() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut blocks = client.rpc().subscribe_blocks().await.unwrap();
|
||||
blocks.next().await.unwrap().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chain_subscribe_finalized_blocks() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut blocks = client.rpc().subscribe_finalized_blocks().await.unwrap();
|
||||
blocks.next().await.unwrap().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_keys() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let keys = client
|
||||
.storage()
|
||||
.fetch_keys::<system::storage::Account>(4, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(keys.len(), 4)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_iter() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut iter = client
|
||||
.storage()
|
||||
.iter::<system::storage::Account>(None)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut i = 0;
|
||||
while iter.next().await.unwrap().is_some() {
|
||||
i += 1;
|
||||
}
|
||||
assert_eq!(i, 13);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_system_info() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
assert_eq!(client.rpc().system_chain().await.unwrap(), "Development");
|
||||
assert_eq!(client.rpc().system_name().await.unwrap(), "Substrate Node");
|
||||
assert!(!client.rpc().system_version().await.unwrap().is_empty());
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/// Checks that code generated by `subxt-cli codegen` compiles. Allows inspection of compiler errors
|
||||
/// directly, more accurately than via the macro and `cargo expand`.
|
||||
///
|
||||
/// Generate by:
|
||||
///
|
||||
/// - run `polkadot --dev --tmp` node locally
|
||||
/// - `cargo run --release -p subxt-cli -- codegen | rustfmt > integration-tests/src/codegen/polkadot.rs`
|
||||
#[rustfmt::skip]
|
||||
#[allow(clippy::all)]
|
||||
mod polkadot;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,230 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
balances,
|
||||
system,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
// Check that we can subscribe to non-finalized block events.
|
||||
#[tokio::test]
|
||||
async fn non_finalized_block_subscription() -> Result<(), subxt::BasicError> {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let ctx = test_context().await;
|
||||
|
||||
let mut event_sub = ctx.api.events().subscribe().await?;
|
||||
|
||||
// Wait for the next set of events, and check that the
|
||||
// associated block hash is not finalized yet.
|
||||
let events = event_sub.next().await.unwrap()?;
|
||||
let event_block_hash = events.block_hash();
|
||||
let current_block_hash = ctx.api.client.rpc().block_hash(None).await?.unwrap();
|
||||
|
||||
assert_eq!(event_block_hash, current_block_hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Check that we can subscribe to finalized block events.
|
||||
#[tokio::test]
|
||||
async fn finalized_block_subscription() -> Result<(), subxt::BasicError> {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let ctx = test_context().await;
|
||||
|
||||
let mut event_sub = ctx.api.events().subscribe_finalized().await?;
|
||||
|
||||
// Wait for the next set of events, and check that the
|
||||
// associated block hash is the one we just finalized.
|
||||
// (this can be a bit slow as we have to wait for finalization)
|
||||
let events = event_sub.next().await.unwrap()?;
|
||||
let event_block_hash = events.block_hash();
|
||||
let finalized_hash = ctx.api.client.rpc().finalized_head().await?;
|
||||
|
||||
assert_eq!(event_block_hash, finalized_hash);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Check that our subscription actually keeps producing events for
|
||||
// a few blocks.
|
||||
#[tokio::test]
|
||||
async fn subscription_produces_events_each_block() -> Result<(), subxt::BasicError> {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let ctx = test_context().await;
|
||||
|
||||
let mut event_sub = ctx.api.events().subscribe().await?;
|
||||
|
||||
for i in 0..3 {
|
||||
let events = event_sub
|
||||
.next()
|
||||
.await
|
||||
.expect("events expected each block")?;
|
||||
let success_event = events
|
||||
.find_first::<system::events::ExtrinsicSuccess>()
|
||||
.expect("decode error");
|
||||
// Every now and then I get no bytes back for the first block events;
|
||||
// I assume that this might be the case for the genesis block, so don't
|
||||
// worry if no event found (but we should have no decode errors etc either way).
|
||||
if i > 0 && success_event.is_none() {
|
||||
let n = events.len();
|
||||
panic!("Expected an extrinsic success event on iteration {i} (saw {n} other events)")
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Check that our subscription receives events, and we can filter them based on
|
||||
// it's Stream impl, and ultimately see the event we expect.
|
||||
#[tokio::test]
|
||||
async fn balance_transfer_subscription() -> Result<(), subxt::BasicError> {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let ctx = test_context().await;
|
||||
|
||||
// Subscribe to balance transfer events, ignoring all else.
|
||||
let event_sub = ctx
|
||||
.api
|
||||
.events()
|
||||
.subscribe()
|
||||
.await?
|
||||
.filter_events::<(balances::events::Transfer,)>();
|
||||
|
||||
// Calling `.next()` on the above borrows it, and the `filter_map`
|
||||
// means it's no longer `Unpin`, so we pin it on the stack:
|
||||
futures::pin_mut!(event_sub);
|
||||
|
||||
// Make a transfer:
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id();
|
||||
ctx.api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(bob.clone().into(), 10_000)?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?;
|
||||
|
||||
// Wait for the next balance transfer event in our subscription stream
|
||||
// and check that it lines up:
|
||||
let event = event_sub.next().await.unwrap().unwrap().event;
|
||||
assert_eq!(
|
||||
event,
|
||||
balances::events::Transfer {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.clone(),
|
||||
amount: 10_000
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_block_headers_will_be_filled_in() -> Result<(), subxt::BasicError> {
|
||||
// This function is not publically available to use, but contains
|
||||
// the key logic for filling in missing blocks, so we want to test it.
|
||||
// This is used in `subscribe_finalized` to ensure no block headers are
|
||||
// missed.
|
||||
use subxt::events::subscribe_to_block_headers_filling_in_gaps;
|
||||
|
||||
let ctx = test_context().await;
|
||||
|
||||
// Manually subscribe to the next 6 finalized block headers, but deliberately
|
||||
// filter out some in the middle so we get back b _ _ b _ b. This guarantees
|
||||
// that there will be some gaps, even if there aren't any from the subscription.
|
||||
let some_finalized_blocks = ctx
|
||||
.api
|
||||
.client
|
||||
.rpc()
|
||||
.subscribe_finalized_blocks()
|
||||
.await?
|
||||
.enumerate()
|
||||
.take(6)
|
||||
.filter(|(n, _)| {
|
||||
let n = *n;
|
||||
async move { n == 0 || n == 3 || n == 5 }
|
||||
})
|
||||
.map(|(_, h)| h);
|
||||
|
||||
// This should spot any gaps in the middle and fill them back in.
|
||||
let all_finalized_blocks = subscribe_to_block_headers_filling_in_gaps(
|
||||
&ctx.api.client,
|
||||
None,
|
||||
some_finalized_blocks,
|
||||
);
|
||||
futures::pin_mut!(all_finalized_blocks);
|
||||
|
||||
// Iterate the block headers, making sure we get them all in order.
|
||||
let mut last_block_number = None;
|
||||
while let Some(header) = all_finalized_blocks.next().await {
|
||||
let header = header?;
|
||||
|
||||
use sp_runtime::traits::Header;
|
||||
let block_number: u128 = (*header.number()).into();
|
||||
|
||||
if let Some(last) = last_block_number {
|
||||
assert_eq!(last + 1, block_number);
|
||||
}
|
||||
last_block_number = Some(block_number);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// This is just a compile-time check that we can subscribe to events in
|
||||
// a context that requires the event subscription/filtering to be Send-able.
|
||||
// We test a typical use of EventSubscription and FilterEvents. We don't need
|
||||
// to run this code; just check that it compiles.
|
||||
#[allow(unused)]
|
||||
async fn check_events_are_sendable() {
|
||||
// check that EventSubscription can be used across await points.
|
||||
tokio::task::spawn(async {
|
||||
let ctx = test_context().await;
|
||||
|
||||
let mut event_sub = ctx.api.events().subscribe().await?;
|
||||
|
||||
while let Some(ev) = event_sub.next().await {
|
||||
// if `event_sub` doesn't implement Send, we can't hold
|
||||
// it across an await point inside of a tokio::spawn, which
|
||||
// requires Send. This will lead to a compile error.
|
||||
}
|
||||
|
||||
Ok::<_, subxt::BasicError>(())
|
||||
});
|
||||
|
||||
// Check that FilterEvents can be used across await points.
|
||||
tokio::task::spawn(async {
|
||||
let ctx = test_context().await;
|
||||
|
||||
let mut event_sub = ctx
|
||||
.api
|
||||
.events()
|
||||
.subscribe()
|
||||
.await?
|
||||
.filter_events::<(balances::events::Transfer,)>();
|
||||
|
||||
while let Some(ev) = event_sub.next().await {
|
||||
// if `event_sub` doesn't implement Send, we can't hold
|
||||
// it across an await point inside of a tokio::spawn, which
|
||||
// requires Send; This will lead to a compile error.
|
||||
}
|
||||
|
||||
Ok::<_, subxt::BasicError>(())
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
balances,
|
||||
runtime_types,
|
||||
system,
|
||||
DispatchError,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use codec::Decode;
|
||||
use sp_core::{
|
||||
sr25519::Pair,
|
||||
Pair as _,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
use sp_runtime::{
|
||||
AccountId32,
|
||||
MultiAddress,
|
||||
};
|
||||
use subxt::Error;
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_basic_transfer() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = pair_signer(AccountKeyring::Bob.pair());
|
||||
let bob_address = bob.account_id().clone().into();
|
||||
let cxt = test_context().await;
|
||||
let api = &cxt.api;
|
||||
|
||||
let alice_pre = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(alice.account_id(), None)
|
||||
.await?;
|
||||
let bob_pre = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(bob.account_id(), None)
|
||||
.await?;
|
||||
|
||||
let events = api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(bob_address, 10_000)?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
let event = events
|
||||
.find_first::<balances::events::Transfer>()
|
||||
.expect("Failed to decode balances::events::Transfer")
|
||||
.expect("Failed to find balances::events::Transfer");
|
||||
let _extrinsic_success = events
|
||||
.find_first::<system::events::ExtrinsicSuccess>()
|
||||
.expect("Failed to decode ExtrinisicSuccess")
|
||||
.expect("Failed to find ExtrinisicSuccess");
|
||||
|
||||
let expected_event = balances::events::Transfer {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.account_id().clone(),
|
||||
amount: 10_000,
|
||||
};
|
||||
assert_eq!(event, expected_event);
|
||||
|
||||
let alice_post = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(alice.account_id(), None)
|
||||
.await?;
|
||||
let bob_post = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(bob.account_id(), None)
|
||||
.await?;
|
||||
|
||||
assert!(alice_pre.data.free - 10_000 >= alice_post.data.free);
|
||||
assert_eq!(bob_pre.data.free + 10_000, bob_post.data.free);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn multiple_transfers_work_nonce_incremented(
|
||||
) -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = pair_signer(AccountKeyring::Bob.pair());
|
||||
let bob_address: MultiAddress<AccountId32, u32> = bob.account_id().clone().into();
|
||||
let cxt = test_context().await;
|
||||
let api = &cxt.api;
|
||||
|
||||
let bob_pre = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(bob.account_id(), None)
|
||||
.await?;
|
||||
|
||||
for _ in 0..3 {
|
||||
api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(bob_address.clone(), 10_000)?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_in_block() // Don't need to wait for finalization; this is quicker.
|
||||
.await?
|
||||
.wait_for_success()
|
||||
.await?;
|
||||
}
|
||||
|
||||
let bob_post = api
|
||||
.storage()
|
||||
.system()
|
||||
.account(bob.account_id(), None)
|
||||
.await?;
|
||||
|
||||
assert_eq!(bob_pre.data.free + 30_000, bob_post.data.free);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_total_issuance() {
|
||||
let cxt = test_context().await;
|
||||
let total_issuance = cxt
|
||||
.api
|
||||
.storage()
|
||||
.balances()
|
||||
.total_issuance(None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_ne!(total_issuance, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_balance_lock() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let bob = pair_signer(AccountKeyring::Bob.pair());
|
||||
let charlie = AccountKeyring::Charlie.to_account_id();
|
||||
let cxt = test_context().await;
|
||||
|
||||
cxt.api
|
||||
.tx()
|
||||
.staking()
|
||||
.bond(
|
||||
charlie.into(),
|
||||
100_000_000_000_000,
|
||||
runtime_types::pallet_staking::RewardDestination::Stash,
|
||||
)?
|
||||
.sign_and_submit_then_watch_default(&bob)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?
|
||||
.find_first::<system::events::ExtrinsicSuccess>()?
|
||||
.expect("No ExtrinsicSuccess Event found");
|
||||
|
||||
let locked_account = AccountKeyring::Bob.to_account_id();
|
||||
let locks = cxt
|
||||
.api
|
||||
.storage()
|
||||
.balances()
|
||||
.locks(&locked_account, None)
|
||||
.await?;
|
||||
|
||||
assert_eq!(
|
||||
locks.0,
|
||||
vec![runtime_types::pallet_balances::BalanceLock {
|
||||
id: *b"staking ",
|
||||
amount: 100_000_000_000_000,
|
||||
reasons: runtime_types::pallet_balances::Reasons::All,
|
||||
}]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn transfer_error() {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let alice_addr = alice.account_id().clone().into();
|
||||
let hans = pair_signer(Pair::generate().0);
|
||||
let hans_address = hans.account_id().clone().into();
|
||||
let ctx = test_context().await;
|
||||
|
||||
ctx.api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(hans_address, 100_000_000_000_000_000)
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await
|
||||
.unwrap()
|
||||
.wait_for_finalized_success()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let res = ctx
|
||||
.api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(alice_addr, 100_000_000_000_000_000)
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&hans)
|
||||
.await
|
||||
.unwrap()
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
|
||||
if let Err(Error::Module(err)) = res {
|
||||
assert_eq!(err.pallet, "Balances");
|
||||
assert_eq!(err.error, "InsufficientBalance");
|
||||
} else {
|
||||
panic!("expected a runtime module error");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn transfer_implicit_subscription() {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id();
|
||||
let bob_addr = bob.clone().into();
|
||||
let cxt = test_context().await;
|
||||
|
||||
let event = cxt
|
||||
.api
|
||||
.tx()
|
||||
.balances()
|
||||
.transfer(bob_addr, 10_000)
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await
|
||||
.unwrap()
|
||||
.wait_for_finalized_success()
|
||||
.await
|
||||
.unwrap()
|
||||
.find_first::<balances::events::Transfer>()
|
||||
.expect("Can decode events")
|
||||
.expect("Can find balance transfer event");
|
||||
|
||||
assert_eq!(
|
||||
event,
|
||||
balances::events::Transfer {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.clone(),
|
||||
amount: 10_000
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn constant_existential_deposit() {
|
||||
let cxt = test_context().await;
|
||||
let locked_metadata = cxt.client().metadata();
|
||||
let metadata = locked_metadata.read();
|
||||
let balances_metadata = metadata.pallet("Balances").unwrap();
|
||||
let constant_metadata = balances_metadata.constant("ExistentialDeposit").unwrap();
|
||||
let existential_deposit = u128::decode(&mut &constant_metadata.value[..]).unwrap();
|
||||
assert_eq!(existential_deposit, 100_000_000_000_000);
|
||||
assert_eq!(
|
||||
existential_deposit,
|
||||
cxt.api
|
||||
.constants()
|
||||
.balances()
|
||||
.existential_deposit()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
self,
|
||||
contracts::{
|
||||
calls::TransactionApi,
|
||||
events,
|
||||
storage,
|
||||
},
|
||||
system,
|
||||
DispatchError,
|
||||
},
|
||||
test_context,
|
||||
NodeRuntimeParams,
|
||||
TestContext,
|
||||
};
|
||||
use sp_core::sr25519::Pair;
|
||||
use sp_runtime::MultiAddress;
|
||||
use subxt::{
|
||||
Client,
|
||||
Config,
|
||||
DefaultConfig,
|
||||
Error,
|
||||
PairSigner,
|
||||
TransactionProgress,
|
||||
};
|
||||
|
||||
struct ContractsTestContext {
|
||||
cxt: TestContext,
|
||||
signer: PairSigner<DefaultConfig, Pair>,
|
||||
}
|
||||
|
||||
type Hash = <DefaultConfig as Config>::Hash;
|
||||
type AccountId = <DefaultConfig as Config>::AccountId;
|
||||
|
||||
impl ContractsTestContext {
|
||||
async fn init() -> Self {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let cxt = test_context().await;
|
||||
let signer = PairSigner::new(AccountKeyring::Alice.pair());
|
||||
|
||||
Self { cxt, signer }
|
||||
}
|
||||
|
||||
fn client(&self) -> &Client<DefaultConfig> {
|
||||
self.cxt.client()
|
||||
}
|
||||
|
||||
fn contracts_tx(&self) -> TransactionApi<DefaultConfig, NodeRuntimeParams> {
|
||||
self.cxt.api.tx().contracts()
|
||||
}
|
||||
|
||||
async fn instantiate_with_code(
|
||||
&self,
|
||||
) -> Result<(Hash, AccountId), Error<DispatchError>> {
|
||||
tracing::info!("instantiate_with_code:");
|
||||
const CONTRACT: &str = r#"
|
||||
(module
|
||||
(func (export "call"))
|
||||
(func (export "deploy"))
|
||||
)
|
||||
"#;
|
||||
let code = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
|
||||
|
||||
let events = self
|
||||
.cxt
|
||||
.api
|
||||
.tx()
|
||||
.contracts()
|
||||
.instantiate_with_code(
|
||||
100_000_000_000_000_000, // endowment
|
||||
500_000_000_000, // gas_limit
|
||||
None, // storage_deposit_limit
|
||||
code,
|
||||
vec![], // data
|
||||
vec![], // salt
|
||||
)?
|
||||
.sign_and_submit_then_watch_default(&self.signer)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
|
||||
let code_stored = events
|
||||
.find_first::<events::CodeStored>()?
|
||||
.ok_or_else(|| Error::Other("Failed to find a CodeStored event".into()))?;
|
||||
let instantiated = events
|
||||
.find_first::<events::Instantiated>()?
|
||||
.ok_or_else(|| Error::Other("Failed to find a Instantiated event".into()))?;
|
||||
let _extrinsic_success = events
|
||||
.find_first::<system::events::ExtrinsicSuccess>()?
|
||||
.ok_or_else(|| {
|
||||
Error::Other("Failed to find a ExtrinsicSuccess event".into())
|
||||
})?;
|
||||
|
||||
tracing::info!(" Block hash: {:?}", events.block_hash());
|
||||
tracing::info!(" Code hash: {:?}", code_stored.code_hash);
|
||||
tracing::info!(" Contract address: {:?}", instantiated.contract);
|
||||
Ok((code_stored.code_hash, instantiated.contract))
|
||||
}
|
||||
|
||||
async fn instantiate(
|
||||
&self,
|
||||
code_hash: Hash,
|
||||
data: Vec<u8>,
|
||||
salt: Vec<u8>,
|
||||
) -> Result<AccountId, Error<DispatchError>> {
|
||||
// call instantiate extrinsic
|
||||
let result = self
|
||||
.contracts_tx()
|
||||
.instantiate(
|
||||
100_000_000_000_000_000, // endowment
|
||||
500_000_000_000, // gas_limit
|
||||
None, // storage_deposit_limit
|
||||
code_hash,
|
||||
data,
|
||||
salt,
|
||||
)?
|
||||
.sign_and_submit_then_watch_default(&self.signer)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
|
||||
tracing::info!("Instantiate result: {:?}", result);
|
||||
let instantiated = result
|
||||
.find_first::<events::Instantiated>()?
|
||||
.ok_or_else(|| Error::Other("Failed to find a Instantiated event".into()))?;
|
||||
|
||||
Ok(instantiated.contract)
|
||||
}
|
||||
|
||||
async fn call(
|
||||
&self,
|
||||
contract: AccountId,
|
||||
input_data: Vec<u8>,
|
||||
) -> Result<
|
||||
TransactionProgress<'_, DefaultConfig, DispatchError, node_runtime::Event>,
|
||||
Error<DispatchError>,
|
||||
> {
|
||||
tracing::info!("call: {:?}", contract);
|
||||
let result = self
|
||||
.contracts_tx()
|
||||
.call(
|
||||
MultiAddress::Id(contract),
|
||||
0, // value
|
||||
500_000_000, // gas_limit
|
||||
None, // storage_deposit_limit
|
||||
input_data,
|
||||
)?
|
||||
.sign_and_submit_then_watch_default(&self.signer)
|
||||
.await?;
|
||||
|
||||
tracing::info!("Call result: {:?}", result);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_instantiate_with_code() {
|
||||
let ctx = ContractsTestContext::init().await;
|
||||
let result = ctx.instantiate_with_code().await;
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Error calling instantiate_with_code and receiving CodeStored and Instantiated Events: {:?}",
|
||||
result
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_instantiate() {
|
||||
let ctx = ContractsTestContext::init().await;
|
||||
let (code_hash, _) = ctx.instantiate_with_code().await.unwrap();
|
||||
|
||||
let instantiated = ctx.instantiate(code_hash, vec![], vec![1u8]).await;
|
||||
|
||||
assert!(
|
||||
instantiated.is_ok(),
|
||||
"Error instantiating contract: {:?}",
|
||||
instantiated
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_call() {
|
||||
let cxt = ContractsTestContext::init().await;
|
||||
let (_, contract) = cxt.instantiate_with_code().await.unwrap();
|
||||
|
||||
let contract_info = cxt
|
||||
.cxt
|
||||
.api
|
||||
.storage()
|
||||
.contracts()
|
||||
.contract_info_of(&contract, None)
|
||||
.await;
|
||||
assert!(contract_info.is_ok());
|
||||
|
||||
let keys = cxt
|
||||
.client()
|
||||
.storage()
|
||||
.fetch_keys::<storage::ContractInfoOf>(5, None, None)
|
||||
.await
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|key| hex::encode(&key.0))
|
||||
.collect::<Vec<_>>();
|
||||
println!("keys post: {:?}", keys);
|
||||
|
||||
let executed = cxt.call(contract, vec![]).await;
|
||||
|
||||
assert!(executed.is_ok(), "Error calling contract: {:?}", executed);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test interactions with some built-in FRAME pallets.
|
||||
|
||||
mod balances;
|
||||
mod contracts;
|
||||
mod staking;
|
||||
mod sudo;
|
||||
mod system;
|
||||
mod timestamp;
|
||||
@@ -0,0 +1,262 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
runtime_types::pallet_staking::{
|
||||
RewardDestination,
|
||||
ValidatorPrefs,
|
||||
},
|
||||
staking,
|
||||
DispatchError,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use assert_matches::assert_matches;
|
||||
use sp_core::{
|
||||
sr25519,
|
||||
Pair,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
use subxt::Error;
|
||||
|
||||
/// Helper function to generate a crypto pair from seed
|
||||
fn get_from_seed(seed: &str) -> sr25519::Pair {
|
||||
sr25519::Pair::from_string(&format!("//{}", seed), None)
|
||||
.expect("static values are valid; qed")
|
||||
}
|
||||
|
||||
fn default_validator_prefs() -> ValidatorPrefs {
|
||||
ValidatorPrefs {
|
||||
commission: sp_runtime::Perbill::default(),
|
||||
blocked: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn validate_with_controller_account() {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let ctx = test_context().await;
|
||||
ctx.api
|
||||
.tx()
|
||||
.staking()
|
||||
.validate(default_validator_prefs())
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await
|
||||
.unwrap()
|
||||
.wait_for_finalized_success()
|
||||
.await
|
||||
.expect("should be successful");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn validate_not_possible_for_stash_account() -> Result<(), Error<DispatchError>> {
|
||||
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
||||
let ctx = test_context().await;
|
||||
let announce_validator = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.validate(default_validator_prefs())?
|
||||
.sign_and_submit_then_watch_default(&alice_stash)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
assert_matches!(announce_validator, Err(Error::Module(err)) => {
|
||||
assert_eq!(err.pallet, "Staking");
|
||||
assert_eq!(err.error, "NotController");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nominate_with_controller_account() {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = pair_signer(AccountKeyring::Bob.pair());
|
||||
let ctx = test_context().await;
|
||||
|
||||
ctx.api
|
||||
.tx()
|
||||
.staking()
|
||||
.nominate(vec![bob.account_id().clone().into()])
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await
|
||||
.unwrap()
|
||||
.wait_for_finalized_success()
|
||||
.await
|
||||
.expect("should be successful");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn nominate_not_possible_for_stash_account() -> Result<(), Error<DispatchError>> {
|
||||
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
||||
let bob = pair_signer(AccountKeyring::Bob.pair());
|
||||
let ctx = test_context().await;
|
||||
|
||||
let nomination = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.nominate(vec![bob.account_id().clone().into()])?
|
||||
.sign_and_submit_then_watch_default(&alice_stash)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
|
||||
assert_matches!(nomination, Err(Error::Module(err)) => {
|
||||
assert_eq!(err.pallet, "Staking");
|
||||
assert_eq!(err.error, "NotController");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chill_works_for_controller_only() -> Result<(), Error<DispatchError>> {
|
||||
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
||||
let bob_stash = pair_signer(get_from_seed("Bob//stash"));
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let ctx = test_context().await;
|
||||
|
||||
// this will fail the second time, which is why this is one test, not two
|
||||
ctx.api
|
||||
.tx()
|
||||
.staking()
|
||||
.nominate(vec![bob_stash.account_id().clone().into()])?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
|
||||
let ledger = ctx
|
||||
.api
|
||||
.storage()
|
||||
.staking()
|
||||
.ledger(alice.account_id(), None)
|
||||
.await?
|
||||
.unwrap();
|
||||
assert_eq!(alice_stash.account_id(), &ledger.stash);
|
||||
|
||||
let chill = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.chill()?
|
||||
.sign_and_submit_then_watch_default(&alice_stash)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
|
||||
assert_matches!(chill, Err(Error::Module(err)) => {
|
||||
assert_eq!(err.pallet, "Staking");
|
||||
assert_eq!(err.error, "NotController");
|
||||
});
|
||||
|
||||
let is_chilled = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.chill()?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?
|
||||
.has::<staking::events::Chilled>()?;
|
||||
assert!(is_chilled);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_bond() -> Result<(), Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let ctx = test_context().await;
|
||||
|
||||
let bond = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.bond(
|
||||
AccountKeyring::Bob.to_account_id().into(),
|
||||
100_000_000_000_000,
|
||||
RewardDestination::Stash,
|
||||
)
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
|
||||
assert!(bond.is_ok());
|
||||
|
||||
let bond_again = ctx
|
||||
.api
|
||||
.tx()
|
||||
.staking()
|
||||
.bond(
|
||||
AccountKeyring::Bob.to_account_id().into(),
|
||||
100_000_000_000_000,
|
||||
RewardDestination::Stash,
|
||||
)
|
||||
.unwrap()
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await;
|
||||
|
||||
assert_matches!(bond_again, Err(Error::Module(err)) => {
|
||||
assert_eq!(err.pallet, "Staking");
|
||||
assert_eq!(err.error, "AlreadyBonded");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_history_depth() -> Result<(), Error<DispatchError>> {
|
||||
let ctx = test_context().await;
|
||||
let history_depth = ctx.api.storage().staking().history_depth(None).await?;
|
||||
assert_eq!(history_depth, 84);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_current_era() -> Result<(), Error<DispatchError>> {
|
||||
let ctx = test_context().await;
|
||||
let _current_era = ctx
|
||||
.api
|
||||
.storage()
|
||||
.staking()
|
||||
.current_era(None)
|
||||
.await?
|
||||
.expect("current era always exists");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_era_reward_points() -> Result<(), Error<DispatchError>> {
|
||||
let cxt = test_context().await;
|
||||
let current_era_result = cxt
|
||||
.api
|
||||
.storage()
|
||||
.staking()
|
||||
.eras_reward_points(&0, None)
|
||||
.await;
|
||||
assert!(current_era_result.is_ok());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
runtime_types,
|
||||
sudo,
|
||||
DispatchError,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
type Call = runtime_types::node_runtime::Call;
|
||||
type BalancesCall = runtime_types::pallet_balances::pallet::Call;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_sudo() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id().into();
|
||||
let cxt = test_context().await;
|
||||
|
||||
let call = Call::Balances(BalancesCall::transfer {
|
||||
dest: bob,
|
||||
value: 10_000,
|
||||
});
|
||||
|
||||
let found_event = cxt
|
||||
.api
|
||||
.tx()
|
||||
.sudo()
|
||||
.sudo(call)?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?
|
||||
.has::<sudo::events::Sudid>()?;
|
||||
|
||||
assert!(found_event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_sudo_unchecked_weight() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id().into();
|
||||
let cxt = test_context().await;
|
||||
|
||||
let call = Call::Balances(BalancesCall::transfer {
|
||||
dest: bob,
|
||||
value: 10_000,
|
||||
});
|
||||
|
||||
let found_event = cxt
|
||||
.api
|
||||
.tx()
|
||||
.sudo()
|
||||
.sudo_unchecked_weight(call, 0)?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?
|
||||
.has::<sudo::events::Sudid>()?;
|
||||
|
||||
assert!(found_event);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
system,
|
||||
DispatchError,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use assert_matches::assert_matches;
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_account() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
|
||||
let cxt = test_context().await;
|
||||
let account_info = cxt
|
||||
.api
|
||||
.storage()
|
||||
.system()
|
||||
.account(alice.account_id(), None)
|
||||
.await;
|
||||
|
||||
assert_matches!(account_info, Ok(_));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tx_remark_with_event() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let alice = pair_signer(AccountKeyring::Alice.pair());
|
||||
let cxt = test_context().await;
|
||||
|
||||
let found_event = cxt
|
||||
.api
|
||||
.tx()
|
||||
.system()
|
||||
.remark_with_event(b"remarkable".to_vec())?
|
||||
.sign_and_submit_then_watch_default(&alice)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?
|
||||
.has::<system::events::Remarked>()?;
|
||||
|
||||
assert!(found_event);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::test_context;
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_get_current_timestamp() {
|
||||
let cxt = test_context().await;
|
||||
|
||||
let timestamp = cxt.api.storage().timestamp().now(None).await;
|
||||
|
||||
assert!(timestamp.is_ok())
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#![deny(unused_crate_dependencies)]
|
||||
|
||||
#[cfg(test)]
|
||||
mod codegen;
|
||||
#[cfg(test)]
|
||||
mod utils;
|
||||
|
||||
#[cfg(test)]
|
||||
mod client;
|
||||
#[cfg(test)]
|
||||
mod events;
|
||||
#[cfg(test)]
|
||||
mod frame;
|
||||
#[cfg(test)]
|
||||
mod metadata;
|
||||
#[cfg(test)]
|
||||
mod storage;
|
||||
|
||||
#[cfg(test)]
|
||||
use test_runtime::node_runtime;
|
||||
#[cfg(test)]
|
||||
use utils::*;
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
mod validation;
|
||||
@@ -0,0 +1,340 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
test_context,
|
||||
TestContext,
|
||||
};
|
||||
use frame_metadata::{
|
||||
ExtrinsicMetadata,
|
||||
PalletCallMetadata,
|
||||
PalletMetadata,
|
||||
PalletStorageMetadata,
|
||||
RuntimeMetadataPrefixed,
|
||||
RuntimeMetadataV14,
|
||||
StorageEntryMetadata,
|
||||
StorageEntryModifier,
|
||||
StorageEntryType,
|
||||
};
|
||||
use scale_info::{
|
||||
build::{
|
||||
Fields,
|
||||
Variants,
|
||||
},
|
||||
meta_type,
|
||||
Path,
|
||||
Type,
|
||||
TypeInfo,
|
||||
};
|
||||
use subxt::{
|
||||
ClientBuilder,
|
||||
DefaultConfig,
|
||||
Metadata,
|
||||
SubstrateExtrinsicParams,
|
||||
};
|
||||
|
||||
use crate::utils::node_runtime;
|
||||
|
||||
type RuntimeApi =
|
||||
node_runtime::RuntimeApi<DefaultConfig, SubstrateExtrinsicParams<DefaultConfig>>;
|
||||
|
||||
async fn metadata_to_api(metadata: RuntimeMetadataV14, cxt: &TestContext) -> RuntimeApi {
|
||||
let prefixed = RuntimeMetadataPrefixed::from(metadata);
|
||||
let metadata = Metadata::try_from(prefixed).unwrap();
|
||||
|
||||
ClientBuilder::new()
|
||||
.set_url(cxt.node_proc.ws_url().to_string())
|
||||
.set_metadata(metadata)
|
||||
.build()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_runtime_api::<node_runtime::RuntimeApi<
|
||||
DefaultConfig,
|
||||
SubstrateExtrinsicParams<DefaultConfig>,
|
||||
>>()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn full_metadata_check() {
|
||||
let cxt = test_context().await;
|
||||
let api = &cxt.api;
|
||||
|
||||
// Runtime metadata is identical to the metadata used during API generation.
|
||||
assert!(api.validate_metadata().is_ok());
|
||||
|
||||
// Modify the metadata.
|
||||
let mut metadata: RuntimeMetadataV14 = {
|
||||
let locked_client_metadata = api.client.metadata();
|
||||
let client_metadata = locked_client_metadata.read();
|
||||
client_metadata.runtime_metadata().clone()
|
||||
};
|
||||
metadata.pallets[0].name = "NewPallet".to_string();
|
||||
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
assert_eq!(
|
||||
new_api
|
||||
.validate_metadata()
|
||||
.err()
|
||||
.expect("Validation should fail for incompatible metadata"),
|
||||
::subxt::MetadataError::IncompatibleMetadata
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn constants_check() {
|
||||
let cxt = test_context().await;
|
||||
let api = &cxt.api;
|
||||
|
||||
// Ensure that `ExistentialDeposit` is compatible before altering the metadata.
|
||||
assert!(cxt.api.constants().balances().existential_deposit().is_ok());
|
||||
|
||||
// Modify the metadata.
|
||||
let mut metadata: RuntimeMetadataV14 = {
|
||||
let locked_client_metadata = api.client.metadata();
|
||||
let client_metadata = locked_client_metadata.read();
|
||||
client_metadata.runtime_metadata().clone()
|
||||
};
|
||||
|
||||
let mut existential = metadata
|
||||
.pallets
|
||||
.iter_mut()
|
||||
.find(|pallet| pallet.name == "Balances")
|
||||
.expect("Metadata must contain Balances pallet")
|
||||
.constants
|
||||
.iter_mut()
|
||||
.find(|constant| constant.name == "ExistentialDeposit")
|
||||
.expect("ExistentialDeposit constant must be present");
|
||||
existential.value = vec![0u8; 32];
|
||||
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
|
||||
assert!(new_api.validate_metadata().is_err());
|
||||
assert!(new_api
|
||||
.constants()
|
||||
.balances()
|
||||
.existential_deposit()
|
||||
.is_err());
|
||||
|
||||
// Other constant validation should not be impacted.
|
||||
assert!(new_api.constants().balances().max_locks().is_ok());
|
||||
}
|
||||
|
||||
fn default_pallet() -> PalletMetadata {
|
||||
PalletMetadata {
|
||||
name: "Test",
|
||||
storage: None,
|
||||
calls: None,
|
||||
event: None,
|
||||
constants: vec![],
|
||||
error: None,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn pallets_to_metadata(pallets: Vec<PalletMetadata>) -> RuntimeMetadataV14 {
|
||||
RuntimeMetadataV14::new(
|
||||
pallets,
|
||||
ExtrinsicMetadata {
|
||||
ty: meta_type::<()>(),
|
||||
version: 0,
|
||||
signed_extensions: vec![],
|
||||
},
|
||||
meta_type::<()>(),
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn calls_check() {
|
||||
let cxt = test_context().await;
|
||||
|
||||
// Ensure that `Unbond` and `WinthdrawUnbonded` calls are compatible before altering the metadata.
|
||||
assert!(cxt.api.tx().staking().unbond(123_456_789_012_345).is_ok());
|
||||
assert!(cxt.api.tx().staking().withdraw_unbonded(10).is_ok());
|
||||
|
||||
// Reconstruct the `Staking` call as is.
|
||||
struct CallRec;
|
||||
impl TypeInfo for CallRec {
|
||||
type Identity = Self;
|
||||
fn type_info() -> Type {
|
||||
Type::builder()
|
||||
.path(Path::new("Call", "pallet_staking::pallet::pallet"))
|
||||
.variant(
|
||||
Variants::new()
|
||||
.variant("unbond", |v| {
|
||||
v.index(0).fields(Fields::named().field(|f| {
|
||||
f.compact::<u128>()
|
||||
.name("value")
|
||||
.type_name("BalanceOf<T>")
|
||||
}))
|
||||
})
|
||||
.variant("withdraw_unbonded", |v| {
|
||||
v.index(1).fields(Fields::named().field(|f| {
|
||||
f.ty::<u32>().name("num_slashing_spans").type_name("u32")
|
||||
}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
let pallet = PalletMetadata {
|
||||
name: "Staking",
|
||||
calls: Some(PalletCallMetadata {
|
||||
ty: meta_type::<CallRec>(),
|
||||
}),
|
||||
..default_pallet()
|
||||
};
|
||||
let metadata = pallets_to_metadata(vec![pallet]);
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
assert!(new_api.tx().staking().unbond(123_456_789_012_345).is_ok());
|
||||
assert!(new_api.tx().staking().withdraw_unbonded(10).is_ok());
|
||||
|
||||
// Change `Unbond` call but leave the rest as is.
|
||||
struct CallRecSecond;
|
||||
impl TypeInfo for CallRecSecond {
|
||||
type Identity = Self;
|
||||
fn type_info() -> Type {
|
||||
Type::builder()
|
||||
.path(Path::new("Call", "pallet_staking::pallet::pallet"))
|
||||
.variant(
|
||||
Variants::new()
|
||||
.variant("unbond", |v| {
|
||||
v.index(0).fields(Fields::named().field(|f| {
|
||||
// Is of type u32 instead of u128.
|
||||
f.compact::<u32>().name("value").type_name("BalanceOf<T>")
|
||||
}))
|
||||
})
|
||||
.variant("withdraw_unbonded", |v| {
|
||||
v.index(1).fields(Fields::named().field(|f| {
|
||||
f.ty::<u32>().name("num_slashing_spans").type_name("u32")
|
||||
}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
let pallet = PalletMetadata {
|
||||
name: "Staking",
|
||||
calls: Some(PalletCallMetadata {
|
||||
ty: meta_type::<CallRecSecond>(),
|
||||
}),
|
||||
..default_pallet()
|
||||
};
|
||||
let metadata = pallets_to_metadata(vec![pallet]);
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
// Unbond call should fail, while withdraw_unbonded remains compatible.
|
||||
assert!(new_api.tx().staking().unbond(123_456_789_012_345).is_err());
|
||||
assert!(new_api.tx().staking().withdraw_unbonded(10).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_check() {
|
||||
let cxt = test_context().await;
|
||||
|
||||
// Ensure that `ExtrinsicCount` and `EventCount` storages are compatible before altering the metadata.
|
||||
assert!(cxt
|
||||
.api
|
||||
.storage()
|
||||
.system()
|
||||
.extrinsic_count(None)
|
||||
.await
|
||||
.is_ok());
|
||||
assert!(cxt
|
||||
.api
|
||||
.storage()
|
||||
.system()
|
||||
.all_extrinsics_len(None)
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
// Reconstruct the storage.
|
||||
let storage = PalletStorageMetadata {
|
||||
prefix: "System",
|
||||
entries: vec![
|
||||
StorageEntryMetadata {
|
||||
name: "ExtrinsicCount",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Plain(meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "AllExtrinsicsLen",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Plain(meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
],
|
||||
};
|
||||
let pallet = PalletMetadata {
|
||||
name: "System",
|
||||
storage: Some(storage),
|
||||
..default_pallet()
|
||||
};
|
||||
let metadata = pallets_to_metadata(vec![pallet]);
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
assert!(new_api
|
||||
.storage()
|
||||
.system()
|
||||
.extrinsic_count(None)
|
||||
.await
|
||||
.is_ok());
|
||||
assert!(new_api
|
||||
.storage()
|
||||
.system()
|
||||
.all_extrinsics_len(None)
|
||||
.await
|
||||
.is_ok());
|
||||
|
||||
// Reconstruct the storage while modifying ExtrinsicCount.
|
||||
let storage = PalletStorageMetadata {
|
||||
prefix: "System",
|
||||
entries: vec![
|
||||
StorageEntryMetadata {
|
||||
name: "ExtrinsicCount",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
// Previously was u32.
|
||||
ty: StorageEntryType::Plain(meta_type::<u8>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "AllExtrinsicsLen",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Plain(meta_type::<u32>()),
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
],
|
||||
};
|
||||
let pallet = PalletMetadata {
|
||||
name: "System",
|
||||
storage: Some(storage),
|
||||
..default_pallet()
|
||||
};
|
||||
let metadata = pallets_to_metadata(vec![pallet]);
|
||||
let new_api = metadata_to_api(metadata, &cxt).await;
|
||||
assert!(new_api
|
||||
.storage()
|
||||
.system()
|
||||
.extrinsic_count(None)
|
||||
.await
|
||||
.is_err());
|
||||
assert!(new_api
|
||||
.storage()
|
||||
.system()
|
||||
.all_extrinsics_len(None)
|
||||
.await
|
||||
.is_ok());
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
node_runtime::{
|
||||
self,
|
||||
DispatchError,
|
||||
},
|
||||
pair_signer,
|
||||
test_context,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_plain_lookup() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let ctx = test_context().await;
|
||||
|
||||
// Look up a plain value. Wait long enough that we don't get the genesis block data,
|
||||
// because it may have no storage associated with it.
|
||||
tokio::time::sleep(std::time::Duration::from_secs(6)).await;
|
||||
let entry = ctx.api.storage().timestamp().now(None).await?;
|
||||
assert!(entry > 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_map_lookup() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let ctx = test_context().await;
|
||||
|
||||
let signer = pair_signer(AccountKeyring::Alice.pair());
|
||||
let alice = AccountKeyring::Alice.to_account_id();
|
||||
|
||||
// Do some transaction to bump the Alice nonce to 1:
|
||||
ctx.api
|
||||
.tx()
|
||||
.system()
|
||||
.remark(vec![1, 2, 3, 4, 5])?
|
||||
.sign_and_submit_then_watch_default(&signer)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
|
||||
// Look up the nonce for the user (we expect it to be 1).
|
||||
let entry = ctx.api.storage().system().account(&alice, None).await?;
|
||||
assert_eq!(entry.nonce, 1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// This fails until the fix in https://github.com/paritytech/subxt/pull/458 is introduced.
|
||||
// Here we create a key that looks a bit like a StorageNMap key, but should in fact be
|
||||
// treated as a StorageKey (ie we should hash both values together with one hasher, rather
|
||||
// than hash both values separately, or ignore the second value).
|
||||
#[tokio::test]
|
||||
async fn storage_n_mapish_key_is_properly_created(
|
||||
) -> Result<(), subxt::Error<DispatchError>> {
|
||||
use codec::Encode;
|
||||
use node_runtime::{
|
||||
runtime_types::sp_core::crypto::KeyTypeId,
|
||||
session::storage::KeyOwner,
|
||||
};
|
||||
use subxt::{
|
||||
storage::StorageKeyPrefix,
|
||||
StorageEntry,
|
||||
};
|
||||
|
||||
// This is what the generated code hashes a `session().key_owner(..)` key into:
|
||||
let actual_key_bytes = KeyOwner(&KeyTypeId([1, 2, 3, 4]), &[5u8, 6, 7, 8])
|
||||
.key()
|
||||
.final_key(StorageKeyPrefix::new::<KeyOwner>())
|
||||
.0;
|
||||
|
||||
// Let's manually hash to what we assume it should be and compare:
|
||||
let expected_key_bytes = {
|
||||
// Hash the prefix to the storage entry:
|
||||
let mut bytes = sp_core::twox_128("Session".as_bytes()).to_vec();
|
||||
bytes.extend(&sp_core::twox_128("KeyOwner".as_bytes())[..]);
|
||||
// twox64_concat a *tuple* of the args expected:
|
||||
let suffix = (KeyTypeId([1, 2, 3, 4]), vec![5u8, 6, 7, 8]).encode();
|
||||
bytes.extend(&sp_core::twox_64(&suffix));
|
||||
bytes.extend(&suffix);
|
||||
bytes
|
||||
};
|
||||
|
||||
assert_eq!(actual_key_bytes, expected_key_bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn storage_n_map_storage_lookup() -> Result<(), subxt::Error<DispatchError>> {
|
||||
let ctx = test_context().await;
|
||||
|
||||
// Boilerplate; we create a new asset class with ID 99, and then
|
||||
// we "approveTransfer" of some of this asset class. This gives us an
|
||||
// entry in the `Approvals` StorageNMap that we can try to look up.
|
||||
let signer = pair_signer(AccountKeyring::Alice.pair());
|
||||
let alice = AccountKeyring::Alice.to_account_id();
|
||||
let bob = AccountKeyring::Bob.to_account_id();
|
||||
ctx.api
|
||||
.tx()
|
||||
.assets()
|
||||
.create(99, alice.clone().into(), 1)?
|
||||
.sign_and_submit_then_watch_default(&signer)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
ctx.api
|
||||
.tx()
|
||||
.assets()
|
||||
.approve_transfer(99, bob.clone().into(), 123)?
|
||||
.sign_and_submit_then_watch_default(&signer)
|
||||
.await?
|
||||
.wait_for_finalized_success()
|
||||
.await?;
|
||||
|
||||
// The actual test; look up this approval in storage:
|
||||
let entry = ctx
|
||||
.api
|
||||
.storage()
|
||||
.assets()
|
||||
.approvals(&99, &alice, &bob, None)
|
||||
.await?;
|
||||
assert_eq!(entry.map(|a| a.amount), Some(123));
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pub(crate) use crate::{
|
||||
node_runtime,
|
||||
TestNodeProcess,
|
||||
};
|
||||
|
||||
use sp_core::sr25519::Pair;
|
||||
use sp_keyring::AccountKeyring;
|
||||
use subxt::{
|
||||
Client,
|
||||
DefaultConfig,
|
||||
PairSigner,
|
||||
SubstrateExtrinsicParams,
|
||||
};
|
||||
|
||||
/// substrate node should be installed on the $PATH
|
||||
const SUBSTRATE_NODE_PATH: &str = "substrate";
|
||||
|
||||
pub type NodeRuntimeParams = SubstrateExtrinsicParams<DefaultConfig>;
|
||||
|
||||
pub async fn test_node_process_with(
|
||||
key: AccountKeyring,
|
||||
) -> TestNodeProcess<DefaultConfig> {
|
||||
let path = std::env::var("SUBSTRATE_NODE_PATH").unwrap_or_else(|_| {
|
||||
if which::which(SUBSTRATE_NODE_PATH).is_err() {
|
||||
panic!("A substrate binary should be installed on your path for integration tests. \
|
||||
See https://github.com/paritytech/subxt/tree/master#integration-testing")
|
||||
}
|
||||
SUBSTRATE_NODE_PATH.to_string()
|
||||
});
|
||||
|
||||
let proc = TestNodeProcess::<DefaultConfig>::build(path.as_str())
|
||||
.with_authority(key)
|
||||
.spawn::<DefaultConfig>()
|
||||
.await;
|
||||
proc.unwrap()
|
||||
}
|
||||
|
||||
pub async fn test_node_process() -> TestNodeProcess<DefaultConfig> {
|
||||
test_node_process_with(AccountKeyring::Alice).await
|
||||
}
|
||||
|
||||
pub struct TestContext {
|
||||
pub node_proc: TestNodeProcess<DefaultConfig>,
|
||||
pub api: node_runtime::RuntimeApi<DefaultConfig, NodeRuntimeParams>,
|
||||
}
|
||||
|
||||
impl TestContext {
|
||||
pub fn client(&self) -> &Client<DefaultConfig> {
|
||||
&self.api.client
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn test_context() -> TestContext {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let node_proc = test_node_process_with(AccountKeyring::Alice).await;
|
||||
let api = node_proc.client().clone().to_runtime_api();
|
||||
TestContext { node_proc, api }
|
||||
}
|
||||
|
||||
pub fn pair_signer(pair: Pair) -> PairSigner<DefaultConfig, Pair> {
|
||||
PairSigner::new(pair)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
mod context;
|
||||
mod node_proc;
|
||||
|
||||
pub use context::*;
|
||||
pub use node_proc::TestNodeProcess;
|
||||
@@ -0,0 +1,193 @@
|
||||
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use sp_keyring::AccountKeyring;
|
||||
use std::{
|
||||
ffi::{
|
||||
OsStr,
|
||||
OsString,
|
||||
},
|
||||
io::{
|
||||
BufRead,
|
||||
BufReader,
|
||||
Read,
|
||||
},
|
||||
process,
|
||||
};
|
||||
use subxt::{
|
||||
Client,
|
||||
ClientBuilder,
|
||||
Config,
|
||||
};
|
||||
|
||||
/// Spawn a local substrate node for testing subxt.
|
||||
pub struct TestNodeProcess<R: Config> {
|
||||
proc: process::Child,
|
||||
client: Client<R>,
|
||||
ws_url: String,
|
||||
}
|
||||
|
||||
impl<R> Drop for TestNodeProcess<R>
|
||||
where
|
||||
R: Config,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
let _ = self.kill();
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> TestNodeProcess<R>
|
||||
where
|
||||
R: Config,
|
||||
{
|
||||
/// Construct a builder for spawning a test node process.
|
||||
pub fn build<S>(program: S) -> TestNodeProcessBuilder
|
||||
where
|
||||
S: AsRef<OsStr> + Clone,
|
||||
{
|
||||
TestNodeProcessBuilder::new(program)
|
||||
}
|
||||
|
||||
/// Attempt to kill the running substrate process.
|
||||
pub fn kill(&mut self) -> Result<(), String> {
|
||||
tracing::info!("Killing node process {}", self.proc.id());
|
||||
if let Err(err) = self.proc.kill() {
|
||||
let err = format!("Error killing node process {}: {}", self.proc.id(), err);
|
||||
tracing::error!("{}", err);
|
||||
return Err(err)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the subxt client connected to the running node.
|
||||
pub fn client(&self) -> &Client<R> {
|
||||
&self.client
|
||||
}
|
||||
|
||||
/// Returns the address to which the client is connected.
|
||||
pub fn ws_url(&self) -> &str {
|
||||
&self.ws_url
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a test node process.
|
||||
pub struct TestNodeProcessBuilder {
|
||||
node_path: OsString,
|
||||
authority: Option<AccountKeyring>,
|
||||
}
|
||||
|
||||
impl TestNodeProcessBuilder {
|
||||
pub fn new<P>(node_path: P) -> TestNodeProcessBuilder
|
||||
where
|
||||
P: AsRef<OsStr>,
|
||||
{
|
||||
Self {
|
||||
node_path: node_path.as_ref().into(),
|
||||
authority: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the authority dev account for a node in validator mode e.g. --alice.
|
||||
pub fn with_authority(&mut self, account: AccountKeyring) -> &mut Self {
|
||||
self.authority = Some(account);
|
||||
self
|
||||
}
|
||||
|
||||
/// Spawn the substrate node at the given path, and wait for rpc to be initialized.
|
||||
pub async fn spawn<R>(&self) -> Result<TestNodeProcess<R>, String>
|
||||
where
|
||||
R: Config,
|
||||
{
|
||||
let mut cmd = process::Command::new(&self.node_path);
|
||||
cmd.env("RUST_LOG", "info")
|
||||
.arg("--dev")
|
||||
.arg("--tmp")
|
||||
.stdout(process::Stdio::piped())
|
||||
.stderr(process::Stdio::piped())
|
||||
.arg("--port=0")
|
||||
.arg("--rpc-port=0")
|
||||
.arg("--ws-port=0");
|
||||
|
||||
if let Some(authority) = self.authority {
|
||||
let authority = format!("{:?}", authority);
|
||||
let arg = format!("--{}", authority.as_str().to_lowercase());
|
||||
cmd.arg(arg);
|
||||
}
|
||||
|
||||
let mut proc = cmd.spawn().map_err(|e| {
|
||||
format!(
|
||||
"Error spawning substrate node '{}': {}",
|
||||
self.node_path.to_string_lossy(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
|
||||
// Wait for RPC port to be logged (it's logged to stderr):
|
||||
let stderr = proc.stderr.take().unwrap();
|
||||
let ws_port = find_substrate_port_from_output(stderr);
|
||||
let ws_url = format!("ws://127.0.0.1:{}", ws_port);
|
||||
|
||||
// Connect to the node with a subxt client:
|
||||
let client = ClientBuilder::new().set_url(ws_url.clone()).build().await;
|
||||
match client {
|
||||
Ok(client) => {
|
||||
Ok(TestNodeProcess {
|
||||
proc,
|
||||
client,
|
||||
ws_url,
|
||||
})
|
||||
}
|
||||
Err(err) => {
|
||||
let err = format!("Failed to connect to node rpc at {}: {}", ws_url, err);
|
||||
tracing::error!("{}", err);
|
||||
proc.kill().map_err(|e| {
|
||||
format!("Error killing substrate process '{}': {}", proc.id(), e)
|
||||
})?;
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Consume a stderr reader from a spawned substrate command and
|
||||
// locate the port number that is logged out to it.
|
||||
fn find_substrate_port_from_output(r: impl Read + Send + 'static) -> u16 {
|
||||
BufReader::new(r)
|
||||
.lines()
|
||||
.find_map(|line| {
|
||||
let line =
|
||||
line.expect("failed to obtain next line from stdout for port discovery");
|
||||
|
||||
// does the line contain our port (we expect this specific output from substrate).
|
||||
let line_end = line
|
||||
.rsplit_once("Listening for new connections on 127.0.0.1:")
|
||||
.or_else(|| {
|
||||
line.rsplit_once("Running JSON-RPC WS server: addr=127.0.0.1:")
|
||||
})
|
||||
.map(|(_, port_str)| port_str)?;
|
||||
|
||||
// trim non-numeric chars from the end of the port part of the line.
|
||||
let port_str = line_end.trim_end_matches(|b| !('0'..='9').contains(&b));
|
||||
|
||||
// expect to have a number here (the chars after '127.0.0.1:') and parse them into a u16.
|
||||
let port_num = port_str.parse().unwrap_or_else(|_| {
|
||||
panic!("valid port expected for log line, got '{port_str}'")
|
||||
});
|
||||
|
||||
Some(port_num)
|
||||
})
|
||||
.expect("We should find a port before the reader ends")
|
||||
}
|
||||
Reference in New Issue
Block a user