From 3df7e2002b1f5bd5224bc0a6e5e6b98f48361dfc Mon Sep 17 00:00:00 2001 From: zian Date: Mon, 12 Jun 2023 14:02:52 +0800 Subject: [PATCH 1/4] done adjusting extrinsic interface --- .../tx/extrinsics/callback_executor/mod.rs | 2 + .../extrinsics_tracker/extrinsics.rs | 37 +++++++++++---- .../extrinsics/extrinsics_tracker/tracker.rs | 46 +++++++++---------- dhatu/src/tx/extrinsics/manager/facade.rs | 10 ++-- dhatu/src/tx/extrinsics/mod.rs | 8 ++-- 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/dhatu/src/tx/extrinsics/callback_executor/mod.rs b/dhatu/src/tx/extrinsics/callback_executor/mod.rs index fe1b0af..58ba9f8 100644 --- a/dhatu/src/tx/extrinsics/callback_executor/mod.rs +++ b/dhatu/src/tx/extrinsics/callback_executor/mod.rs @@ -5,6 +5,7 @@ use serde_json::Value; use crate::{error::Error}; +#[cfg(feature = "tokio")] pub struct Executor { http_connection_pool: reqwest::Client, } @@ -43,6 +44,7 @@ impl Executor { } } + #[cfg(feature = "tokio", feature = "serde")] pub fn execute(&self, body: Value, callback: &str) -> Result<(), Error> { let client = self.http_connection_pool.clone(); diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs index 593171d..8fa7b63 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs @@ -17,11 +17,29 @@ use crate::{ use super::enums::{ExtrinsicStatus, Hash}; +pub struct TransactionMessage { + pub(crate) status: ExtrinsicStatus, + pub(crate) callback: Option, +} + +impl TransactionMessage { + pub fn new(status: ExtrinsicStatus, callback: Option) -> Self { + Self { status, callback } + } + + pub fn inner_status(&self) -> ExtrinsicStatus { + self.status.clone() + } + + pub fn callback(&self) -> Option<&String> { + self.callback.as_ref() + } +} + #[cfg(feature = "tokio")] pub struct Transaction { id: H256, status: Arc>, - transaction_notifier: SenderChannel, } impl Transaction { @@ -39,16 +57,15 @@ impl Transaction { impl Transaction { pub fn new( tx: MandalaTransactionProgress, - external_notifier: SenderChannel, + external_notifier: Option>, callback: Option, ) -> Self { let hash = tx.0.extrinsic_hash(); - let task_channel = Self::process_transaction(tx, external_notifier.clone(), callback); + let task_channel = Self::process_transaction(tx, external_notifier, callback); let default_status = Self::watch_transaction_status(task_channel); Self { - transaction_notifier: external_notifier, id: hash, status: default_status, } @@ -56,13 +73,12 @@ impl Transaction { fn process_transaction( tx: MandalaTransactionProgress, - external_status_notifier: SenderChannel, + external_status_notifier: Option>, callback: Option, - ) -> Receiver { + ) -> Receiver { let (internal_status_notifier, receiver) = Self::create_channel(); let task = async move { - let status = Self::wait(tx).await; internal_status_notifier @@ -70,9 +86,10 @@ impl Transaction { .await .expect("there should be only 1 message sent"); - external_status_notifier - .send(status) - .unwrap(); + if let Some(external_status_notifier) = external_status_notifier { + let msg = TransactionMessage::new(status, callback); + external_status_notifier.send(msg).await; + } }; tokio::task::spawn(task); receiver diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs index 8781b92..c540cb3 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs @@ -3,49 +3,49 @@ use std::{collections::HashMap, sync::Arc}; use sp_core::H256; use tokio::sync::RwLock; -use crate::{tx::extrinsics::{ - prelude::{NotificationMessage, TransactionId}, - types::{BlockchainClient, ExtrinsicTracker}, -}, types::SenderChannel}; - -use super::{enums::{ExtrinsicStatus, Hash}, extrinsics::Transaction}; +use crate::{ + tx::extrinsics::{ + prelude::{NotificationMessage, TransactionId}, + types::{BlockchainClient, ExtrinsicTracker}, + }, + types::{MandalaClient, MandalaTransactionProgress, SenderChannel}, +}; + +use super::{ + enums::{ExtrinsicStatus, Hash}, + extrinsics::Transaction, +}; #[doc(hidden)] type Inner = Arc>>; pub struct ExtrinsicWatcher { inner: Inner, - client: BlockchainClient, - transaction_notifier:SenderChannel, } impl Clone for ExtrinsicWatcher { fn clone(&self) -> Self { Self { inner: self.inner.clone(), - client: self.client.clone(), - transaction_notifier: self.transaction_notifier.clone(), } } } impl ExtrinsicWatcher { - pub fn new( - client: BlockchainClient, - transaction_notifier:SenderChannel, - ) -> Self { + pub fn new(client: MandalaClient) -> Self { let inner = HashMap::new(); let inner = Arc::new(RwLock::new(inner)); - Self { - inner, - client, - transaction_notifier, - } + Self { inner } } - pub async fn watch(&self, tx: ExtrinsicTracker, callback: Option) -> TransactionId { - let tx = Transaction::new(tx.into(), self.transaction_notifier.clone(), callback); + pub async fn watch( + &self, + tx: MandalaTransactionProgress, + external_notifier: Option>, + callback: Option, + ) -> TransactionId { + let tx = Transaction::new(tx, external_notifier, callback); let tx_id = tx.id(); self.watch_tx(tx).await; @@ -53,7 +53,7 @@ impl ExtrinsicWatcher { tx_id } - pub async fn check(&self, tx_id: &TransactionId) -> Option { + pub async fn check(&self, tx_id: &Hash) -> Option { let inner = self.inner.read().await; let Some(tx) = inner.get(tx_id) else { @@ -63,7 +63,7 @@ impl ExtrinsicWatcher { Some(tx.status().await) } - pub async fn stop_watching(&self, tx_id: &TransactionId) { + pub async fn stop_watching(&self, tx_id: &Hash) { let mut inner = self.inner.write().await; inner.remove(tx_id); } diff --git a/dhatu/src/tx/extrinsics/manager/facade.rs b/dhatu/src/tx/extrinsics/manager/facade.rs index cca3a73..70dba98 100644 --- a/dhatu/src/tx/extrinsics/manager/facade.rs +++ b/dhatu/src/tx/extrinsics/manager/facade.rs @@ -6,7 +6,7 @@ use crate::tx::extrinsics::prelude::{NotificationMessage, TransactionId}; use super::super::{ callback_executor::Executor, - extrinsics_tracker::{tracker::ExtrinsicWatcher}, + extrinsics_tracker::tracker::ExtrinsicWatcher, prelude::{ExtrinsicSubmitter, GenericError}, types::{BlockchainClient, Extrinsic}, }; @@ -32,9 +32,7 @@ impl Body { pub struct ExtrinsicFacade { transaction_watcher: TransactionWatcherInstance, - callback_executor: CallbackExecutorInstance, transaction_sender_channel: tokio::sync::mpsc::UnboundedSender, - notif_bus_handle: Task, } impl ExtrinsicFacade { @@ -43,9 +41,9 @@ impl ExtrinsicFacade { let callback_executor = Executor::new(); - let tx_watcher = ExtrinsicWatcher::new(client, tx_sender_channel.clone()); + let tx_watcher = ExtrinsicWatcher::new(client); - let rcv_handle = Self::initialize_receive_task( + Self::initialize_receive_task( tx_watcher.clone(), callback_executor.clone(), tx_receiver_channel, @@ -53,9 +51,7 @@ impl ExtrinsicFacade { Self { transaction_watcher: tx_watcher, - callback_executor, transaction_sender_channel: tx_sender_channel, - notif_bus_handle: rcv_handle, } } diff --git a/dhatu/src/tx/extrinsics/mod.rs b/dhatu/src/tx/extrinsics/mod.rs index 8763b01..919946a 100644 --- a/dhatu/src/tx/extrinsics/mod.rs +++ b/dhatu/src/tx/extrinsics/mod.rs @@ -1,9 +1,9 @@ pub mod callback_executor; pub mod extrinsics_submitter; pub mod extrinsics_tracker; -pub mod funds_reserve; +// pub mod funds_reserve; pub mod manager; -pub mod transaction_constructor; +// pub mod transaction_constructor; pub mod types; pub mod prelude { @@ -12,8 +12,8 @@ pub mod prelude { pub use callback_executor::*; pub use extrinsics_submitter::*; pub use extrinsics_tracker::*; - pub use funds_reserve::*; + // pub use funds_reserve::*; pub use manager::*; - pub use transaction_constructor::*; + // pub use transaction_constructor::*; pub use types::*; } From 838a5ea8516da32f02c0cee09f580f17cb220baa Mon Sep 17 00:00:00 2001 From: zian Date: Mon, 12 Jun 2023 14:31:16 +0800 Subject: [PATCH 2/4] partially adjust facade --- .../migration_transaction/transaction.rs | 2 +- .../migration_transaction/types.rs | 10 ++-- .../tx/extrinsics/callback_executor/mod.rs | 8 +-- .../extrinsics_tracker/extrinsics.rs | 16 +++++- .../extrinsics/extrinsics_tracker/tracker.rs | 4 +- dhatu/src/tx/extrinsics/funds_reserve/mod.rs | 1 - .../tx/extrinsics/funds_reserve/reserve.rs | 2 +- .../src/tx/extrinsics/funds_reserve/traits.rs | 0 dhatu/src/tx/extrinsics/manager/facade.rs | 55 +++++++++++-------- dhatu/src/tx/extrinsics/mod.rs | 8 +-- 10 files changed, 61 insertions(+), 45 deletions(-) delete mode 100644 dhatu/src/tx/extrinsics/funds_reserve/traits.rs diff --git a/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs b/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs index 54a8ae4..221aea6 100644 --- a/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs +++ b/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs @@ -103,7 +103,7 @@ impl MigrationTransaction { .take() .expect("inner transaction should have been built"); - let (progress, _) = ExtrinsicSubmitter::submit(tx.into()).await.unwrap(); + let progress = ExtrinsicSubmitter::submit(tx.into()).await.unwrap(); let notifier_channel = self.notifier.clone(); extrinsics::Transaction::new(progress, notifier_channel, None) diff --git a/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs b/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs index 93ba189..1e25524 100644 --- a/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs +++ b/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs @@ -1,13 +1,13 @@ use subxt::{tx::SubmittableExtrinsic, OnlineClient, PolkadotConfig}; -use crate::tx::extrinsics::prelude::{ - transfer_nft_contract::types::ContractTransactionPayload, NotificationMessage, -}; +use crate::{tx::extrinsics::prelude::{ + transfer_nft_contract::types::ContractTransactionPayload, NotificationMessage, extrinsics::TransactionMessage, +}, types::{SenderChannel, ReceiverChannel}}; pub type MigrationTask = std::pin::Pin>>; pub type MigrationTransactionPayload = ContractTransactionPayload; pub type MigrationTransactionResultNotifier = - tokio::sync::mpsc::UnboundedSender; + SenderChannel; pub type MigrationTransactionResultReceiver = - tokio::sync::mpsc::UnboundedReceiver; + ReceiverChannel; pub type MigrationTransaction = SubmittableExtrinsic>; diff --git a/dhatu/src/tx/extrinsics/callback_executor/mod.rs b/dhatu/src/tx/extrinsics/callback_executor/mod.rs index 58ba9f8..9ee49bd 100644 --- a/dhatu/src/tx/extrinsics/callback_executor/mod.rs +++ b/dhatu/src/tx/extrinsics/callback_executor/mod.rs @@ -1,9 +1,8 @@ -use std::{str::FromStr}; +use std::str::FromStr; use serde_json::Value; - -use crate::{error::Error}; +use crate::error::Error; #[cfg(feature = "tokio")] pub struct Executor { @@ -44,7 +43,8 @@ impl Executor { } } - #[cfg(feature = "tokio", feature = "serde")] + #[cfg(feature = "tokio")] + #[cfg(feature = "serde")] pub fn execute(&self, body: Value, callback: &str) -> Result<(), Error> { let client = self.http_connection_pool.clone(); diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs index 8fa7b63..de66930 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs @@ -20,11 +20,16 @@ use super::enums::{ExtrinsicStatus, Hash}; pub struct TransactionMessage { pub(crate) status: ExtrinsicStatus, pub(crate) callback: Option, + pub(crate) id: Hash, } impl TransactionMessage { - pub fn new(status: ExtrinsicStatus, callback: Option) -> Self { - Self { status, callback } + pub fn new(status: ExtrinsicStatus, callback: Option, id: Hash) -> Self { + Self { + status, + callback, + id, + } } pub fn inner_status(&self) -> ExtrinsicStatus { @@ -34,6 +39,10 @@ impl TransactionMessage { pub fn callback(&self) -> Option<&String> { self.callback.as_ref() } + + pub fn id(&self) -> &Hash { + &self.id + } } #[cfg(feature = "tokio")] @@ -79,6 +88,7 @@ impl Transaction { let (internal_status_notifier, receiver) = Self::create_channel(); let task = async move { + let id = tx.0.extrinsic_hash().into(); let status = Self::wait(tx).await; internal_status_notifier @@ -87,7 +97,7 @@ impl Transaction { .expect("there should be only 1 message sent"); if let Some(external_status_notifier) = external_status_notifier { - let msg = TransactionMessage::new(status, callback); + let msg = TransactionMessage::new(status, callback, id); external_status_notifier.send(msg).await; } }; diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs index c540cb3..de13bdc 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs @@ -13,7 +13,7 @@ use crate::{ use super::{ enums::{ExtrinsicStatus, Hash}, - extrinsics::Transaction, + extrinsics::{Transaction, TransactionMessage}, }; #[doc(hidden)] @@ -44,7 +44,7 @@ impl ExtrinsicWatcher { tx: MandalaTransactionProgress, external_notifier: Option>, callback: Option, - ) -> TransactionId { + ) -> Hash { let tx = Transaction::new(tx, external_notifier, callback); let tx_id = tx.id(); diff --git a/dhatu/src/tx/extrinsics/funds_reserve/mod.rs b/dhatu/src/tx/extrinsics/funds_reserve/mod.rs index a12c028..a96136a 100644 --- a/dhatu/src/tx/extrinsics/funds_reserve/mod.rs +++ b/dhatu/src/tx/extrinsics/funds_reserve/mod.rs @@ -1,2 +1 @@ pub mod reserve; -pub mod traits; diff --git a/dhatu/src/tx/extrinsics/funds_reserve/reserve.rs b/dhatu/src/tx/extrinsics/funds_reserve/reserve.rs index 46b735e..43eeaae 100644 --- a/dhatu/src/tx/extrinsics/funds_reserve/reserve.rs +++ b/dhatu/src/tx/extrinsics/funds_reserve/reserve.rs @@ -119,7 +119,7 @@ impl FundsReserve { .await .map_err(FundsReserveError::RpcError)?; - let status = Transaction::wait(tx).await; + let status = Transaction::wait(tx.into()).await; Ok(status) } diff --git a/dhatu/src/tx/extrinsics/funds_reserve/traits.rs b/dhatu/src/tx/extrinsics/funds_reserve/traits.rs deleted file mode 100644 index e69de29..0000000 diff --git a/dhatu/src/tx/extrinsics/manager/facade.rs b/dhatu/src/tx/extrinsics/manager/facade.rs index 70dba98..9b8e072 100644 --- a/dhatu/src/tx/extrinsics/manager/facade.rs +++ b/dhatu/src/tx/extrinsics/manager/facade.rs @@ -2,7 +2,13 @@ use serde::Serialize; use sp_core::H256; -use crate::tx::extrinsics::prelude::{NotificationMessage, TransactionId}; +use crate::{ + tx::extrinsics::{ + extrinsics_tracker::extrinsics::TransactionMessage, + prelude::{NotificationMessage, TransactionId, enums::Hash}, + }, + types::{MandalaClient, MandalaExtrinsics, ReceiverChannel, SenderChannel}, +}; use super::super::{ callback_executor::Executor, @@ -11,9 +17,6 @@ use super::super::{ types::{BlockchainClient, Extrinsic}, }; -#[doc(hidden)] -type Task = tokio::task::JoinHandle<()>; - pub type TransactionWatcherInstance = ExtrinsicWatcher; pub type CallbackExecutorInstance = Executor; @@ -30,17 +33,18 @@ impl Body { } } +#[cfg(feature = "tokio")] +#[cfg(feature = "serde")] pub struct ExtrinsicFacade { - transaction_watcher: TransactionWatcherInstance, - transaction_sender_channel: tokio::sync::mpsc::UnboundedSender, + transaction_watcher: ExtrinsicWatcher, + transaction_sender_channel: SenderChannel, } impl ExtrinsicFacade { - pub fn new(client: BlockchainClient) -> Self { + pub fn new(client: MandalaClient) -> Self { let (tx_sender_channel, tx_receiver_channel) = Self::create_channel(); let callback_executor = Executor::new(); - let tx_watcher = ExtrinsicWatcher::new(client); Self::initialize_receive_task( @@ -56,42 +60,45 @@ impl ExtrinsicFacade { } fn initialize_receive_task( - tx_watcher: TransactionWatcherInstance, - callback_executor: CallbackExecutorInstance, - mut tx_receiver_channel: tokio::sync::mpsc::UnboundedReceiver, - ) -> Task { + tx_watcher: ExtrinsicWatcher, + callback_executor: Executor, + mut tx_receiver_channel: ReceiverChannel, + ) { let recv = async move { loop { - let (id, _status, callback) = tx_receiver_channel.recv().await.unwrap(); + let msg = tx_receiver_channel.recv().await.unwrap(); - tx_watcher.stop_watching(&id).await; + tx_watcher.stop_watching(msg.id()).await; - if let Some(callback) = callback { + if let Some(callback) = msg.callback() { // TODO : customize body callback_executor - .execute(serde_json::to_value(Body::new(id)).unwrap(), &callback); + .execute(serde_json::to_value(Body::new(id)).unwrap(), callback); } } }; - tokio::task::spawn(recv) + tokio::task::spawn(recv); } pub async fn submit( &self, - tx: Extrinsic, + tx: MandalaExtrinsics, callback: Option, - ) -> Result { - let (tx, _id) = ExtrinsicSubmitter::submit(tx.into()).await?; - let tx = self.transaction_watcher.watch(tx, callback).await; + ) -> Result { + let progress = ExtrinsicSubmitter::submit(tx).await?; + let tx = self + .transaction_watcher + .watch(progress, Some(self.transaction_sender_channel.clone()), callback) + .await; Ok(tx) } pub fn create_channel() -> ( - tokio::sync::mpsc::UnboundedSender, - tokio::sync::mpsc::UnboundedReceiver, + SenderChannel, + ReceiverChannel, ) { - tokio::sync::mpsc::unbounded_channel::() + tokio::sync::mpsc::unbounded_channel::() } } diff --git a/dhatu/src/tx/extrinsics/mod.rs b/dhatu/src/tx/extrinsics/mod.rs index 919946a..8763b01 100644 --- a/dhatu/src/tx/extrinsics/mod.rs +++ b/dhatu/src/tx/extrinsics/mod.rs @@ -1,9 +1,9 @@ pub mod callback_executor; pub mod extrinsics_submitter; pub mod extrinsics_tracker; -// pub mod funds_reserve; +pub mod funds_reserve; pub mod manager; -// pub mod transaction_constructor; +pub mod transaction_constructor; pub mod types; pub mod prelude { @@ -12,8 +12,8 @@ pub mod prelude { pub use callback_executor::*; pub use extrinsics_submitter::*; pub use extrinsics_tracker::*; - // pub use funds_reserve::*; + pub use funds_reserve::*; pub use manager::*; - // pub use transaction_constructor::*; + pub use transaction_constructor::*; pub use types::*; } From 21f99172338c879c1c0a7efd0ed1cdca9d05cba3 Mon Sep 17 00:00:00 2001 From: zian Date: Mon, 12 Jun 2023 15:48:18 +0800 Subject: [PATCH 3/4] done adjusting public api --- .../migration_transaction/transaction.rs | 2 +- .../tx/extrinsics/callback_executor/mod.rs | 41 ++++++++++++++++++- .../tx/extrinsics/extrinsics_tracker/enums.rs | 12 +++--- .../extrinsics_tracker/extrinsics.rs | 4 +- dhatu/src/tx/extrinsics/manager/facade.rs | 29 ++++--------- dhatu/src/types.rs | 6 +-- 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs b/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs index 221aea6..aab143a 100644 --- a/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs +++ b/dhatu/src/tx/dhatu_assets/migration_transaction/transaction.rs @@ -106,7 +106,7 @@ impl MigrationTransaction { let progress = ExtrinsicSubmitter::submit(tx.into()).await.unwrap(); let notifier_channel = self.notifier.clone(); - extrinsics::Transaction::new(progress, notifier_channel, None) + extrinsics::Transaction::new(progress, Some(notifier_channel), None) } } diff --git a/dhatu/src/tx/extrinsics/callback_executor/mod.rs b/dhatu/src/tx/extrinsics/callback_executor/mod.rs index 9ee49bd..f8a26a9 100644 --- a/dhatu/src/tx/extrinsics/callback_executor/mod.rs +++ b/dhatu/src/tx/extrinsics/callback_executor/mod.rs @@ -1,7 +1,9 @@ use std::str::FromStr; +use serde::Serialize; use serde_json::Value; +use super::prelude::enums::{ExtrinsicStatus, Hash}; use crate::error::Error; #[cfg(feature = "tokio")] @@ -45,14 +47,49 @@ impl Executor { #[cfg(feature = "tokio")] #[cfg(feature = "serde")] - pub fn execute(&self, body: Value, callback: &str) -> Result<(), Error> { + pub fn execute(&self, status: ExtrinsicStatus, callback_url: &str) -> Result<(), Error> { let client = self.http_connection_pool.clone(); - let callback = Url::from_str(callback)?; + let body = Self::infer_callback_body(status); + + let callback = Url::from_str(callback_url)?; let task = client.post(callback.0).json(&body).send(); tokio::task::spawn(task); Ok(()) } + + fn infer_callback_body(status: ExtrinsicStatus) -> CallBackBody { + match status { + ExtrinsicStatus::Pending => CallBackBody::new(false, String::from("pending"), None), + ExtrinsicStatus::Failed(reason) => CallBackBody::new( + false, + format!("failed with reason : {}", reason.inner()), + None, + ), + ExtrinsicStatus::Success(result) => { + CallBackBody::new(true, String::from("success"), Some(result.hash())) + } + } + } +} + +#[cfg(feature = "serde")] +#[derive(Serialize)] +pub struct CallBackBody { + status: bool, + message: String, + data: Option, +} + +#[cfg(feature = "serde")] +impl CallBackBody { + pub fn new(status: bool, message: String, data: Option) -> Self { + Self { + status, + message, + data, + } + } } diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/enums.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/enums.rs index 453711e..b65274a 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/enums.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/enums.rs @@ -1,11 +1,13 @@ use std::sync::Arc; +use serde::Serialize; use sp_core::H256; use subxt::blocks::ExtrinsicEvents; use crate::types::MandalaConfig; -#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg(feature = "serde")] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] pub struct Reason(String); impl Reason { @@ -55,11 +57,11 @@ impl ExtrinsicResult { impl From> for ExtrinsicResult { fn from(value: ExtrinsicEvents) -> Self { - Self(value) + Self(Arc::new(value)) } } - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg(feature = "serde")] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] pub struct Hash(String); impl From for Hash { @@ -88,7 +90,7 @@ impl Hash { // we disable this by default because substrate sp_core does not follow semver // and we need to have a stable public api! #[cfg(feature = "unstable_sp_core")] - pub fn hash(&self) -> H256 { + pub fn into_inner(&self) -> H256 { use std::str::FromStr; H256::from_str(self.inner_as_str()).expect("internal conversion shouldn't fail!") diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs index de66930..03b2f2f 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs @@ -84,7 +84,7 @@ impl Transaction { tx: MandalaTransactionProgress, external_status_notifier: Option>, callback: Option, - ) -> Receiver { + ) -> Receiver { let (internal_status_notifier, receiver) = Self::create_channel(); let task = async move { @@ -98,7 +98,7 @@ impl Transaction { if let Some(external_status_notifier) = external_status_notifier { let msg = TransactionMessage::new(status, callback, id); - external_status_notifier.send(msg).await; + external_status_notifier.send(msg); } }; tokio::task::spawn(task); diff --git a/dhatu/src/tx/extrinsics/manager/facade.rs b/dhatu/src/tx/extrinsics/manager/facade.rs index 9b8e072..a38373a 100644 --- a/dhatu/src/tx/extrinsics/manager/facade.rs +++ b/dhatu/src/tx/extrinsics/manager/facade.rs @@ -5,7 +5,7 @@ use sp_core::H256; use crate::{ tx::extrinsics::{ extrinsics_tracker::extrinsics::TransactionMessage, - prelude::{NotificationMessage, TransactionId, enums::Hash}, + prelude::{enums::Hash, NotificationMessage, TransactionId}, }, types::{MandalaClient, MandalaExtrinsics, ReceiverChannel, SenderChannel}, }; @@ -17,22 +17,6 @@ use super::super::{ types::{BlockchainClient, Extrinsic}, }; -pub type TransactionWatcherInstance = ExtrinsicWatcher; -pub type CallbackExecutorInstance = Executor; - -// temporary callback body -#[doc(hidden)] -#[derive(Serialize)] -pub struct Body { - hash: H256, -} - -impl Body { - pub fn new(hash: H256) -> Self { - Self { hash } - } -} - #[cfg(feature = "tokio")] #[cfg(feature = "serde")] pub struct ExtrinsicFacade { @@ -71,9 +55,8 @@ impl ExtrinsicFacade { tx_watcher.stop_watching(msg.id()).await; if let Some(callback) = msg.callback() { - // TODO : customize body - callback_executor - .execute(serde_json::to_value(Body::new(id)).unwrap(), callback); + // will fail silently if if there's an error when executing the callback + callback_executor.execute(msg.status.clone(), &callback); } } }; @@ -89,7 +72,11 @@ impl ExtrinsicFacade { let progress = ExtrinsicSubmitter::submit(tx).await?; let tx = self .transaction_watcher - .watch(progress, Some(self.transaction_sender_channel.clone()), callback) + .watch( + progress, + Some(self.transaction_sender_channel.clone()), + callback, + ) .await; Ok(tx) diff --git a/dhatu/src/types.rs b/dhatu/src/types.rs index f15288e..2ed3981 100644 --- a/dhatu/src/types.rs +++ b/dhatu/src/types.rs @@ -34,7 +34,7 @@ impl From> for InternalChannels { impl Default for InternalChannels { fn default() -> Self { - let (receiver, sender) = tokio::sync::mpsc::unbounded_channel::(); + let (sender, receiver) = tokio::sync::mpsc::unbounded_channel::(); Self { receiver: Some(receiver), sender, @@ -49,9 +49,7 @@ impl InternalChannels { /// must be called only once, will panic if called twice pub fn get_receiver(&mut self) -> ReceiverChannel { - self.receiver - .take() - .expect("should be called only once") + self.receiver.take().expect("should be called only once") } #[must_use] From 3bbf77a6512f21a6b28ddd477f8ea4e234a29a94 Mon Sep 17 00:00:00 2001 From: zian Date: Mon, 12 Jun 2023 15:49:09 +0800 Subject: [PATCH 4/4] lint --- .../tx/dhatu_assets/migration_transaction/types.rs | 2 +- dhatu/src/tx/extrinsics/callback_executor/mod.rs | 2 +- dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs | 6 +----- .../tx/extrinsics/extrinsics_tracker/extrinsics.rs | 7 +------ dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs | 8 ++------ dhatu/src/tx/extrinsics/manager/facade.rs | 11 +++++------ 6 files changed, 11 insertions(+), 25 deletions(-) diff --git a/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs b/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs index 1e25524..a57af8b 100644 --- a/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs +++ b/dhatu/src/tx/dhatu_assets/migration_transaction/types.rs @@ -1,7 +1,7 @@ use subxt::{tx::SubmittableExtrinsic, OnlineClient, PolkadotConfig}; use crate::{tx::extrinsics::prelude::{ - transfer_nft_contract::types::ContractTransactionPayload, NotificationMessage, extrinsics::TransactionMessage, + transfer_nft_contract::types::ContractTransactionPayload, extrinsics::TransactionMessage, }, types::{SenderChannel, ReceiverChannel}}; pub type MigrationTask = std::pin::Pin>>; diff --git a/dhatu/src/tx/extrinsics/callback_executor/mod.rs b/dhatu/src/tx/extrinsics/callback_executor/mod.rs index f8a26a9..9856ef6 100644 --- a/dhatu/src/tx/extrinsics/callback_executor/mod.rs +++ b/dhatu/src/tx/extrinsics/callback_executor/mod.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use serde::Serialize; -use serde_json::Value; + use super::prelude::enums::{ExtrinsicStatus, Hash}; use crate::error::Error; diff --git a/dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs b/dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs index c8abe7d..cfdf122 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs @@ -1,9 +1,5 @@ use crate::{ error::Error, - tx::extrinsics::{ - prelude::TransactionId, - types::{Extrinsic, ExtrinsicTracker, GenericError}, - }, types::{MandalaExtrinsics, MandalaTransactionProgress}, }; @@ -14,7 +10,7 @@ impl ExtrinsicSubmitter { let result = tx.into_inner() .submit_and_watch() .await - .map_err(|e| Error::TransactionSubmitError(e))? + .map_err(Error::TransactionSubmitError)? .into(); Ok(result) diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs index 03b2f2f..f1eab9b 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/extrinsics.rs @@ -3,15 +3,10 @@ use std::sync::Arc; use sp_core::H256; use tokio::sync::{ - mpsc::{Receiver, Sender}, - Mutex, RwLock, + mpsc::{Receiver, Sender}, RwLock, }; use crate::{ - tx::extrinsics::{ - prelude::{NotificationMessage, TransactionId}, - types::ExtrinsicTracker, - }, types::{MandalaTransactionProgress, SenderChannel}, }; diff --git a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs index de13bdc..63363d8 100644 --- a/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs +++ b/dhatu/src/tx/extrinsics/extrinsics_tracker/tracker.rs @@ -1,13 +1,9 @@ use std::{collections::HashMap, sync::Arc}; -use sp_core::H256; + use tokio::sync::RwLock; use crate::{ - tx::extrinsics::{ - prelude::{NotificationMessage, TransactionId}, - types::{BlockchainClient, ExtrinsicTracker}, - }, types::{MandalaClient, MandalaTransactionProgress, SenderChannel}, }; @@ -32,7 +28,7 @@ impl Clone for ExtrinsicWatcher { } impl ExtrinsicWatcher { - pub fn new(client: MandalaClient) -> Self { + pub fn new(_client: MandalaClient) -> Self { let inner = HashMap::new(); let inner = Arc::new(RwLock::new(inner)); diff --git a/dhatu/src/tx/extrinsics/manager/facade.rs b/dhatu/src/tx/extrinsics/manager/facade.rs index a38373a..af13f49 100644 --- a/dhatu/src/tx/extrinsics/manager/facade.rs +++ b/dhatu/src/tx/extrinsics/manager/facade.rs @@ -1,11 +1,11 @@ -use serde::Serialize; -use sp_core::H256; + + use crate::{ tx::extrinsics::{ extrinsics_tracker::extrinsics::TransactionMessage, - prelude::{enums::Hash, NotificationMessage, TransactionId}, + prelude::{enums::Hash}, }, types::{MandalaClient, MandalaExtrinsics, ReceiverChannel, SenderChannel}, }; @@ -14,7 +14,6 @@ use super::super::{ callback_executor::Executor, extrinsics_tracker::tracker::ExtrinsicWatcher, prelude::{ExtrinsicSubmitter, GenericError}, - types::{BlockchainClient, Extrinsic}, }; #[cfg(feature = "tokio")] @@ -33,7 +32,7 @@ impl ExtrinsicFacade { Self::initialize_receive_task( tx_watcher.clone(), - callback_executor.clone(), + callback_executor, tx_receiver_channel, ); @@ -56,7 +55,7 @@ impl ExtrinsicFacade { if let Some(callback) = msg.callback() { // will fail silently if if there's an error when executing the callback - callback_executor.execute(msg.status.clone(), &callback); + callback_executor.execute(msg.status.clone(), callback); } } };