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

Test signer #10

Merged
merged 7 commits into from
Jun 15, 2023
Merged
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
186 changes: 100 additions & 86 deletions dhatu/src/registrar/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,89 +42,103 @@ impl TxBuilder {
}
}

// #[cfg(test)]
// mod tests {
// use std::str::FromStr;

// use sp_core::{crypto::Ss58Codec, Pair};
// use subxt::{
// rpc::types::DryRunError,
// utils::{AccountId32, MultiAddress},
// };

// use super::*;

// async fn mock_client() -> crate::types::NodeClient {
// OnlineClient::<PolkadotConfig>::new().await.unwrap()
// }

// fn mock_payload(
// client: &crate::types::NodeClient,
// ) -> subxt::tx::StaticTxPayload<runtime_types::api::balances::calls::Transfer> {
// let _metadata = client.metadata();

// let dest = mock_acc();

// runtime_types::api::tx().balances().transfer(dest, 0)
// }

// fn mock_acc() -> MultiAddress<AccountId32, ()> {
// let (dest, _) = sp_core::sr25519::Pair::generate();
// let dest = dest.public();
// let dest = AccountId32::from_str(&dest.to_ss58check()).unwrap();

// subxt::utils::MultiAddress::Id(dest)
// }

// fn mock_pair() -> sp_core::sr25519::Pair {
// let (pair, _) = sp_core::sr25519::Pair::generate();

// pair
// }

// #[actix::test]
// async fn should_create_unsigned_tx() {
// let node_client = mock_client().await;
// let payload = mock_payload(&node_client);

// let extrinsic = TxBuilder::unsigned(&node_client, &payload).unwrap();

// let dry_run_result = extrinsic.dry_run(None).await.unwrap();
// let actual_result = extrinsic.submit().await;

// // should error because the transaction is unsigned and can only be
// // submitted through OCW
// // but it should be possible to include the transaction in the block.
// // that's why instead of validity erorr it's dispatch error
// if let Err(dry_run_result) = dry_run_result {
// assert_eq!(dry_run_result, DryRunError::DispatchError);
// }

// if let Err(actual_result) = actual_result {
// println!("{}", actual_result)
// }
// }

// #[actix::test]
// async fn should_create_signed_tx() {
// let node_client = mock_client().await;
// let payload = mock_payload(&node_client);

// let pair = mock_pair();
// let extrinsic = TxBuilder::signed(&node_client, pair, &payload)
// .await
// .unwrap();

// let dry_run_result = extrinsic.dry_run(None).await.unwrap();
// let actual_result = extrinsic.submit().await;

// // shoould error because the caller does not have enough balance
// if let Err(dry_run_result) = dry_run_result {
// assert_eq!(dry_run_result, DryRunError::TransactionValidityError);
// }

// if let Err(actual_result) = actual_result {
// println!("{}", actual_result)
// }
// }
// }
#[cfg(test)]
mod tests {
use subxt::{PolkadotConfig, SubstrateConfig, error::DispatchError};
pub(crate) use subxt::OnlineClient;
use std::str::FromStr;

use sp_core::{crypto::Ss58Codec, Pair};
use subxt::{
utils::{AccountId32, MultiAddress},
rpc::types::DryRunResult
};

use crate::types::MandalaConfig;

use super::*;

async fn mock_client() -> crate::types::NodeClient {
OnlineClient::<MandalaConfig>::new().await.unwrap()
}

// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "./src/registrar/signer/polkadot_metadata_small.scale")]
pub mod polkadot {}

// Mock implementation of `WrappedExtrinsic` for testing
struct MockWrappedExtrinsic<T: EncodeAsFields>(subxt::tx::Payload<T>);

impl<T: EncodeAsFields> WrappedExtrinsic<T> for MockWrappedExtrinsic<T> {
fn into_inner(self) -> subxt::tx::Payload<T> {
self.0
}
}

fn mock_payload(
client: &crate::types::NodeClient,
) -> MockWrappedExtrinsic<polkadot::balances::calls::types::Transfer> {
let _metadata = client.metadata();

let dest = mock_acc();

MockWrappedExtrinsic(polkadot::tx().balances().transfer(dest, 0))
}

fn mock_acc() -> MultiAddress<AccountId32, ()> {
let dest = sp_keyring::Sr25519Keyring::Bob.pair();
let dest = dest.public();
let dest = AccountId32::from_str(&dest.to_ss58check()).unwrap();

subxt::utils::MultiAddress::Id(dest)
}

fn mock_pair() -> sp_core::sr25519::Pair {
sp_keyring::Sr25519Keyring::Alice.pair()
}

#[tokio::test]
async fn should_create_unsigned_tx() {
let node_client = mock_client().await;
let payload = mock_payload(&node_client);

let extrinsic_result = TxBuilder::unsigned(&node_client, payload);

assert!(extrinsic_result.is_ok());

let extrinsic = extrinsic_result.unwrap();

let dry_run_result: DryRunResult = extrinsic.dry_run(None).await.unwrap();
let actual_result = extrinsic.submit().await;

// should error because the transaction is unsigned and can only be
// submitted through OCW
// but it should be possible to include the transaction in the block.
// that's why instead of validity erorr it's dispatch error
if let DryRunResult::DispatchError(err) = dry_run_result {
assert_eq!(
format!("{:?}", err),
format!("{:?}", DispatchError::BadOrigin)
);
}

if let Err(actual_result) = actual_result {
println!("{}", actual_result)
}
}

#[tokio::test]
async fn should_create_signed_tx() {
let node_client = mock_client().await;
let payload = mock_payload(&node_client);

let pair = mock_pair();
let extrinsic = TxBuilder::signed(&node_client, pair, payload)
.await
.unwrap();

let dry_run_result = extrinsic.dry_run(None).await.unwrap();
let actual_result = extrinsic.submit().await;
assert!(actual_result.is_ok());
}
}
Binary file not shown.
4 changes: 2 additions & 2 deletions dhatu/src/tx/dhatu_assets/migration_transaction/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use subxt::{tx::SubmittableExtrinsic, SubstrateConfig};

use crate::{tx::extrinsics::{prelude::{
extrinsics::TransactionMessage,
}, transaction_constructor::transfer_nft_contract::constructor::NftTransferPayload}, types::{SenderChannel, ReceiverChannel}};
}, transaction_constructor::transfer_nft_contract::constructor::NftTransferPayload}, types::{SenderChannel, ReceiverChannel, MandalaConfig}};

pub type MigrationTask<T> = std::pin::Pin<Box<dyn futures::Future<Output = T>>>;
pub type MigrationTransactionPayload = NftTransferPayload;
pub type MigrationTransactionResultNotifier =
SenderChannel<TransactionMessage>;
pub type MigrationTransactionResultReceiver =
ReceiverChannel<TransactionMessage>;
pub type MigrationTransaction = SubmittableExtrinsic<SubstrateConfig, crate::types::NodeClient>;
pub type MigrationTransaction = SubmittableExtrinsic<MandalaConfig, crate::types::NodeClient>;
4 changes: 2 additions & 2 deletions dhatu/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use subxt::{
tx::{SubmittableExtrinsic, TxProgress},
OnlineClient, SubstrateConfig,
OnlineClient, SubstrateConfig, PolkadotConfig,
};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

use crate::error::MandalaClientErorr;

pub(crate) type MandalaConfig = SubstrateConfig;
pub(crate) type MandalaConfig = PolkadotConfig;
pub(crate) type NodeClient = OnlineClient<MandalaConfig>;
pub(crate) type Extrinsic = SubmittableExtrinsic<MandalaConfig, NodeClient>;
pub(crate) type TransactionProgress = TxProgress<MandalaConfig, NodeClient>;
Expand Down