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

420 remove subxtclient #450

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions clients/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ testing-utils = [
"rand",
"testchain",
"testchain-runtime",
"subxt-client",
"oracle"
]

[dependencies]
libc = "0.2.150"
async-trait = "0.1.40"
backoff = { version = "0.3.0", features = ["tokio"] }
cfg-if = "1.0"
Expand All @@ -45,8 +45,6 @@ sp-version = { git = "https://github.com/paritytech/substrate", branch = "polkad
# Subxt dependencies
subxt = "0.25.0"
jsonrpsee = { version = "0.16.0", features = ["macros", "jsonrpsee-types", "client", "jsonrpsee-ws-client", "jsonrpsee-client-transport"] }
# Used for performing the integration tests
subxt-client = { path = "./client", optional = true }

# Needed for the `BalanceWrapper` struct
module-oracle-rpc-runtime-api = { path = "../../pallets/oracle/rpc/runtime-api" }
Expand Down
92 changes: 47 additions & 45 deletions clients/runtime/src/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#![cfg(feature = "testing-utils")]

use std::{sync::Arc, time::Duration};
use std::{
os::unix::process::CommandExt,
process::{Child, Command, Stdio},
sync::Arc,
time::Duration,
};

use frame_support::assert_ok;
use futures::{future::Either, pin_mut, Future, FutureExt, SinkExt, StreamExt};
use oracle::dia::{DiaOracleKeyConvertor, NativeCurrencyKey, XCMCurrencyConversion};
use sp_keyring::AccountKeyring;
use sp_runtime::traits::Convert;
use subxt::{
events::StaticEvent as Event,
Expand All @@ -13,15 +19,9 @@ use subxt::{
use tempdir::TempDir;
use tokio::{sync::RwLock, time::timeout};

pub use subxt_client::SubxtClient;
use subxt_client::{
AccountKeyring, DatabaseSource, KeystoreConfig, Role, SubxtClientConfig, WasmExecutionMethod,
WasmtimeInstantiationStrategy,
};

use crate::{
rpc::{OraclePallet, VaultRegistryPallet},
CurrencyId, FixedU128, SpacewalkParachain, SpacewalkSigner,
CurrencyId, FixedU128, SpacewalkParachain, SpacewalkSigner, SudoPallet,
};
use primitives::oracle::Key as OracleKey;

Expand Down Expand Up @@ -54,55 +54,41 @@ impl XCMCurrencyConversion for MockValue {
}
}

fn get_pair_from_keyring(keyring: AccountKeyring) -> Result<Pair, String> {
let pair = Pair::from_string(keyring.to_seed().as_str(), None)
.map_err(|_| "Could not create pair for keyring")?;
Ok(pair)
}

/// Start a new instance of the parachain. The second item in the returned tuple must remain in
/// scope as long as the parachain is active, since dropping it will remove the temporary directory
/// that the parachain uses
pub async fn default_provider_client(key: AccountKeyring) -> (SubxtClient, TempDir) {
pub async fn default_root_provider(key: AccountKeyring) -> (SpacewalkParachain, TempDir) {
let tmp = TempDir::new("spacewalk-parachain-").expect("failed to create tempdir");
let config = SubxtClientConfig {
impl_name: "spacewalk-parachain-full-client",
impl_version: "0.0.1",
author: "SatoshiPay",
copyright_start_year: 2020,
db: DatabaseSource::ParityDb { path: tmp.path().join("db") },
keystore: KeystoreConfig::Path { path: tmp.path().join("keystore"), password: None },
chain_spec: testchain::chain_spec::development_config(),
role: Role::Authority(key),
telemetry: None,
wasm_method: WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::LegacyInstanceReuse,
},
tokio_handle: tokio::runtime::Handle::current(),
};

// enable off chain workers
let mut service_config = config.into_service_config();
service_config.offchain_worker.enabled = true;

let (task_manager, rpc_handlers) =
testchain::service::start_instant(service_config).await.unwrap();

let client = SubxtClient::new(task_manager, rpc_handlers);

(client, tmp)
}
let root_provider = setup_provider(key).await;

fn get_pair_from_keyring(keyring: AccountKeyring) -> Result<Pair, String> {
let pair = Pair::from_string(keyring.to_seed().as_str(), None)
.map_err(|_| "Could not create pair for keyring")?;
Ok(pair)
if let Err(e) = root_provider.set_parachain_confirmations(1).await {
log::warn!("default_root_provider(): ERROR: {e:?}");
}

(root_provider, tmp)
}

/// Create a new parachain_rpc with the given keyring
pub async fn setup_provider(client: SubxtClient, key: AccountKeyring) -> SpacewalkParachain {
pub async fn setup_provider(key: AccountKeyring) -> SpacewalkParachain {
let pair = get_pair_from_keyring(key).unwrap();
let signer = SpacewalkSigner::new(pair);
let signer = Arc::new(RwLock::new(signer));
let shutdown_tx = crate::ShutdownSender::new();

SpacewalkParachain::new(client.into(), signer, shutdown_tx)
.await
.expect("Error creating parachain_rpc")
let parachain_rpc = SpacewalkParachain::from_url_with_retry(
&"ws://127.0.0.1:9944".to_string(),
signer.clone(),
Duration::from_secs(20),
shutdown_tx,
)
.await
.unwrap();
parachain_rpc
}

pub async fn set_exchange_rate_and_wait(
Expand Down Expand Up @@ -185,3 +171,19 @@ pub async fn periodically_produce_blocks(parachain_rpc: SpacewalkParachain) {
parachain_rpc.manual_seal().await;
}
}

pub async fn start_chain() -> std::io::Result<Child> {
unsafe {
Command::new("sh")
.arg("./scripts/run_parachain_node.sh")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.pre_exec(|| {
// Create a new session and set the process group ID
// Used for deleting all the processes spawned when starting the chain
libc::setsid();
Ok(())
})
.spawn()
}
}
24 changes: 10 additions & 14 deletions clients/runtime/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn dummy_public_key() -> StellarPublicKeyRaw {
[0u8; 32]
}

async fn set_exchange_rate(client: SubxtClient) {
let oracle_provider = setup_provider(client, AccountKeyring::Bob).await;
async fn set_exchange_rate() {
let oracle_provider = setup_provider(AccountKeyring::Bob).await;
let key = primitives::oracle::Key::ExchangeRate(DEFAULT_TESTING_CURRENCY);
let converted_key = DiaOracleKeyConvertor::<MockValue>::convert(key.clone()).unwrap();
let exchange_rate = FixedU128::saturating_from_rational(1u128, 100u128);
Expand All @@ -39,8 +39,7 @@ async fn set_exchange_rate(client: SubxtClient) {

#[tokio::test(flavor = "multi_thread")]
async fn test_getters() {
let (client, _tmp_dir) = default_provider_client(AccountKeyring::Alice).await;
let parachain_rpc = setup_provider(client.clone(), AccountKeyring::Alice).await;
let (parachain_rpc, _tmp_dir) = default_root_provider(AccountKeyring::Alice).await;

tokio::join!(
async {
Expand All @@ -62,27 +61,25 @@ async fn test_getters() {
#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_invalid_tx_matching() {
let (client, _tmp_dir) = default_provider_client(AccountKeyring::Alice).await;
let parachain_rpc = setup_provider(client.clone(), AccountKeyring::Alice).await;
let (parachain_rpc, _tmp_dir) = default_root_provider(AccountKeyring::Alice).await;
let err = parachain_rpc.get_invalid_tx_error(AccountKeyring::Bob.into()).await;
assert!(err.is_invalid_transaction().is_some())
}

#[ignore]
#[tokio::test(flavor = "multi_thread")]
async fn test_too_low_priority_matching() {
let (client, _tmp_dir) = default_provider_client(AccountKeyring::Alice).await;
let parachain_rpc = setup_provider(client.clone(), AccountKeyring::Alice).await;
let (parachain_rpc, _tmp_dir) = default_root_provider(AccountKeyring::Alice).await;
let err = parachain_rpc.get_too_low_priority_error(AccountKeyring::Bob.into()).await;
assert!(err.is_pool_too_low_priority().is_some())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_subxt_processing_events_after_dispatch_error() {
let (client, _tmp_dir) = default_provider_client(AccountKeyring::Alice).await;
let (_, _tmp_dir) = default_root_provider(AccountKeyring::Alice).await;

let oracle_provider = setup_provider(client.clone(), AccountKeyring::Bob).await;
let invalid_oracle = setup_provider(client, AccountKeyring::Dave).await;
let oracle_provider = setup_provider( AccountKeyring::Bob).await;
let invalid_oracle = setup_provider(AccountKeyring::Dave).await;

let key = primitives::oracle::Key::ExchangeRate(DEFAULT_TESTING_CURRENCY);
let converted_key = DiaOracleKeyConvertor::<MockValue>::convert(key.clone()).unwrap();
Expand All @@ -100,9 +97,8 @@ async fn test_subxt_processing_events_after_dispatch_error() {

#[tokio::test(flavor = "multi_thread")]
async fn test_register_vault() {
let (client, _tmp_dir) = default_provider_client(AccountKeyring::Alice).await;
let parachain_rpc = setup_provider(client.clone(), AccountKeyring::Alice).await;
set_exchange_rate(client.clone()).await;
let (parachain_rpc, _tmp_dir) = default_root_provider(AccountKeyring::Alice).await;
set_exchange_rate().await;

let vault_id = VaultId::new(
AccountKeyring::Alice.into(),
Expand Down
1 change: 1 addition & 0 deletions clients/vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parachain-metadata-foucoco = ["runtime/parachain-metadata-foucoco"]
integration-test = ["runtime/standalone-metadata", "integration"]

[dependencies]
libc = "0.2.150"
async-trait = "0.1.40"
base64 = { version = '0.13.0', default-features = false, features = ['alloc'] }
bincode = "1.3.3"
Expand Down
4 changes: 4 additions & 0 deletions clients/vault/scripts/run_parachain_node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd ../..
cargo run --bin spacewalk-standalone --release -- --dev
7 changes: 2 additions & 5 deletions clients/vault/tests/helper/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use async_trait::async_trait;
use frame_support::assert_ok;
use primitives::{stellar::Asset as StellarAsset, CurrencyId, StellarStroops, H256};
use runtime::{
integration::{
assert_event, get_required_vault_collateral_for_issue, setup_provider, SubxtClient,
},
integration::{assert_event, get_required_vault_collateral_for_issue, setup_provider},
stellar::SecretKey,
ExecuteRedeemEvent, IssuePallet, SpacewalkParachain, VaultId, VaultRegistryPallet,
};
Expand Down Expand Up @@ -44,13 +42,12 @@ pub trait SpacewalkParachainExt: VaultRegistryPallet {
}

pub async fn create_vault(
client: SubxtClient,
account: AccountKeyring,
wrapped_currency: CurrencyId,
) -> (VaultId, SpacewalkParachain) {
let vault_id = VaultId::new(account.clone().into(), DEFAULT_TESTING_CURRENCY, wrapped_currency);

let vault_provider = setup_provider(client, account).await;
let vault_provider = setup_provider(account).await;

(vault_id, vault_provider)
}
Expand Down
Loading
Loading