Skip to content

Commit

Permalink
Merge pull request #13
Browse files Browse the repository at this point in the history
fixed Clone and Debug implementations on BaseTransactionNetworkExecutor
  • Loading branch information
gfusee authored Nov 7, 2023
2 parents 7af75d1 + 3bea493 commit d989b06
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
40 changes: 39 additions & 1 deletion executor/src/network/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::mem;
use async_trait::async_trait;
Expand All @@ -17,7 +18,6 @@ pub type NetworkExecutor = BaseTransactionNetworkExecutor<Interactor>;
///
/// This executor is designed to interact with a blockchain network via a specified gateway URL and a wallet
/// for signing transactions. It is parameterized by a type `Interactor` that encapsulates the blockchain interaction logic.
#[derive(Clone, Debug)]
pub struct BaseTransactionNetworkExecutor<Interactor: BlockchainInteractor> {
/// The URL of the blockchain network gateway through which transactions will be sent.
pub gateway_url: String,
Expand All @@ -28,6 +28,44 @@ pub struct BaseTransactionNetworkExecutor<Interactor: BlockchainInteractor> {
_phantom_data: PhantomData<Interactor>,
}

/// Custom implementation of `Clone` for `BaseTransactionNetworkExecutor`.
///
/// This implementation is necessary because the `Interactor` generic parameter might not
/// implement `Clone`. However, since `Interactor` is used only as phantom data (it does not
/// affect the state of `BaseTransactionNetworkExecutor`), we can safely implement `Clone`
/// without the `Interactor` needing to be `Clone`.
impl<Interactor> Clone for BaseTransactionNetworkExecutor<Interactor>
where
Interactor: BlockchainInteractor
{
fn clone(&self) -> Self {
Self {
gateway_url: self.gateway_url.clone(),
wallet: self.wallet,
_phantom_data: Default::default(),
}
}
}

/// Custom implementation of `Debug` for `BaseTransactionNetworkExecutor`.
///
/// This implementation is necessary because the `Interactor` generic parameter might not
/// implement `Debug`. As with `Clone`, since `Interactor` is only used as phantom data,
/// it does not impact the debug representation of `BaseTransactionNetworkExecutor`. This
/// implementation ensures that instances of `BaseTransactionNetworkExecutor` can be
/// formatted using the `Debug` trait regardless of whether `Interactor` implements `Debug`.
impl<Interactor> Debug for BaseTransactionNetworkExecutor<Interactor>
where
Interactor: BlockchainInteractor
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BaseTransactionNetworkExecutor")
.field("gateway_url", &self.gateway_url)
.field("wallet", &self.wallet)
.finish()
}
}

impl<Interactor: BlockchainInteractor> BaseTransactionNetworkExecutor<Interactor> {
/// Creates a new instance of `BaseTransactionNetworkExecutor`.
///
Expand Down
24 changes: 23 additions & 1 deletion tester/core/tests/network_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use novax::{Address, Wallet};
use novax::errors::NovaXError;
use num_bigint::BigUint;
use novax::tester::tester::{CustomEnum, CustomEnumWithFields, CustomEnumWithValues, CustomStruct, CustomStructWithStructAndVec, TesterContract};
use novax::executor::{BaseTransactionNetworkExecutor, BlockchainInteractor, SendableTransactionConvertible};
use novax::executor::{BaseTransactionNetworkExecutor, BlockchainInteractor, NetworkExecutor, SendableTransactionConvertible};
use novax_mocking::{ScCallStep, ScDeployStep, TxResponse};
use crate::utils::decode_scr_data::decode_scr_data_or_panic;

Expand Down Expand Up @@ -128,6 +128,28 @@ fn get_executor() -> Arc<Mutex<BaseTransactionNetworkExecutor<MockInteractor>>>
Arc::new(Mutex::new(executor))
}

// The below test is a success if it compiles
#[tokio::test]
async fn test_clone_network_executor() -> Result<(), NovaXError> {
let wallet = Wallet::from_private_key(CALLER_PRIVATE_KEY).unwrap();
let executor = NetworkExecutor::new("", &wallet);
#[allow(clippy::redundant_clone)]
let _executor2 = executor.clone();

Ok(())
}

// The below test is a success if it compiles
#[tokio::test]
async fn test_debug_network_executor() -> Result<(), NovaXError> {
let wallet = Wallet::from_private_key(CALLER_PRIVATE_KEY).unwrap();
let executor = NetworkExecutor::new("", &wallet);

println!("{executor:?}");

Ok(())
}

#[tokio::test]
async fn test_call_return_caller() -> Result<(), NovaXError> {
let executor = get_executor();
Expand Down

0 comments on commit d989b06

Please sign in to comment.