From 455d161e42609a072878b9a99de5890753b828cb Mon Sep 17 00:00:00 2001 From: Andrei Gubarev <1062334+agubarev@users.noreply.github.com> Date: Thu, 7 Jul 2022 14:44:30 +0300 Subject: [PATCH] fix: removed code duplication in TariUtxo conversion (#4283) Description --- Removed redundant code. Motivation and Context --- Unnecessary code duplication. How Has This Been Tested? --- unit tests --- Cargo.lock | 70 ++++++++++++++++---------------- base_layer/wallet_ffi/src/lib.rs | 70 ++++++++++---------------------- 2 files changed, 56 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ce5432687..69f8247a35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6280,7 +6280,7 @@ dependencies = [ [[package]] name = "tari_app_grpc" -version = "0.32.8" +version = "0.32.9" dependencies = [ "chrono", "prost", @@ -6297,7 +6297,7 @@ dependencies = [ [[package]] name = "tari_app_utilities" -version = "0.32.8" +version = "0.32.9" dependencies = [ "clap 3.2.5", "config", @@ -6320,7 +6320,7 @@ dependencies = [ [[package]] name = "tari_base_node" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "async-trait", @@ -6427,7 +6427,7 @@ dependencies = [ [[package]] name = "tari_common" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "config", @@ -6444,7 +6444,7 @@ dependencies = [ "serde_json", "sha2 0.9.9", "structopt", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tempfile", "thiserror", "toml", @@ -6452,7 +6452,7 @@ dependencies = [ [[package]] name = "tari_common_sqlite" -version = "0.32.8" +version = "0.32.9" dependencies = [ "diesel", "log", @@ -6461,7 +6461,7 @@ dependencies = [ [[package]] name = "tari_common_types" -version = "0.32.8" +version = "0.32.9" dependencies = [ "digest 0.9.0", "lazy_static", @@ -6475,7 +6475,7 @@ dependencies = [ [[package]] name = "tari_comms" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "async-trait", @@ -6511,7 +6511,7 @@ dependencies = [ "tari_metrics", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tempfile", "thiserror", @@ -6525,7 +6525,7 @@ dependencies = [ [[package]] name = "tari_comms_dht" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -6559,7 +6559,7 @@ dependencies = [ "tari_crypto", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tempfile", "thiserror", @@ -6571,7 +6571,7 @@ dependencies = [ [[package]] name = "tari_comms_rpc_macros" -version = "0.32.8" +version = "0.32.9" dependencies = [ "futures 0.3.21", "proc-macro2", @@ -6579,14 +6579,14 @@ dependencies = [ "quote", "syn", "tari_comms", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tokio 1.19.2", "tower-service", ] [[package]] name = "tari_console_wallet" -version = "0.32.8" +version = "0.32.9" dependencies = [ "bitflags 1.3.2", "chrono", @@ -6633,7 +6633,7 @@ dependencies = [ [[package]] name = "tari_core" -version = "0.32.8" +version = "0.32.9" dependencies = [ "async-trait", "bincode", @@ -6680,7 +6680,7 @@ dependencies = [ "tari_service_framework", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tempfile", "thiserror", @@ -6783,7 +6783,7 @@ dependencies = [ [[package]] name = "tari_key_manager" -version = "0.32.8" +version = "0.32.9" dependencies = [ "argon2", "arrayvec 0.7.2", @@ -6814,7 +6814,7 @@ dependencies = [ [[package]] name = "tari_launchpad" -version = "0.32.8" +version = "0.32.9" dependencies = [ "bollard", "config", @@ -6857,7 +6857,7 @@ dependencies = [ [[package]] name = "tari_merge_mining_proxy" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "bincode", @@ -6909,7 +6909,7 @@ dependencies = [ [[package]] name = "tari_miner" -version = "0.32.8" +version = "0.32.9" dependencies = [ "base64 0.13.0", "bufstream", @@ -6960,7 +6960,7 @@ dependencies = [ [[package]] name = "tari_mmr" -version = "0.32.8" +version = "0.32.9" dependencies = [ "bincode", "blake2", @@ -6978,7 +6978,7 @@ dependencies = [ [[package]] name = "tari_p2p" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "bytes 0.5.6", @@ -7006,7 +7006,7 @@ dependencies = [ "tari_service_framework", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tempfile", "thiserror", @@ -7035,7 +7035,7 @@ dependencies = [ [[package]] name = "tari_service_framework" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "async-trait", @@ -7043,7 +7043,7 @@ dependencies = [ "futures-test", "log", "tari_shutdown", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "thiserror", "tokio 1.19.2", "tower", @@ -7052,7 +7052,7 @@ dependencies = [ [[package]] name = "tari_shutdown" -version = "0.32.8" +version = "0.32.9" dependencies = [ "futures 0.3.21", "tokio 1.19.2", @@ -7060,7 +7060,7 @@ dependencies = [ [[package]] name = "tari_storage" -version = "0.32.8" +version = "0.32.9" dependencies = [ "bincode", "lmdb-zero", @@ -7088,7 +7088,7 @@ dependencies = [ [[package]] name = "tari_test_utils" -version = "0.32.8" +version = "0.32.9" dependencies = [ "futures 0.3.21", "futures-test", @@ -7114,7 +7114,7 @@ dependencies = [ [[package]] name = "tari_validator_node" -version = "0.32.8" +version = "0.32.9" dependencies = [ "anyhow", "async-trait", @@ -7148,7 +7148,7 @@ dependencies = [ "tari_service_framework", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "thiserror", "tokio 1.19.2", "tokio-stream", @@ -7157,7 +7157,7 @@ dependencies = [ [[package]] name = "tari_wallet" -version = "0.32.8" +version = "0.32.9" dependencies = [ "aes-gcm 0.8.0", "argon2", @@ -7199,7 +7199,7 @@ dependencies = [ "tari_service_framework", "tari_shutdown", "tari_storage", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tempfile", "thiserror", @@ -7209,7 +7209,7 @@ dependencies = [ [[package]] name = "tari_wallet_ffi" -version = "0.32.8" +version = "0.32.9" dependencies = [ "cbindgen 0.24.3", "chrono", @@ -7235,7 +7235,7 @@ dependencies = [ "tari_script", "tari_service_framework", "tari_shutdown", - "tari_test_utils 0.32.8", + "tari_test_utils 0.32.9", "tari_utilities", "tari_wallet", "tempfile", @@ -7449,7 +7449,7 @@ dependencies = [ [[package]] name = "test_faucet" -version = "0.32.8" +version = "0.32.9" dependencies = [ "rand 0.8.5", "serde", diff --git a/base_layer/wallet_ffi/src/lib.rs b/base_layer/wallet_ffi/src/lib.rs index ad86e0312b..e54bf11954 100644 --- a/base_layer/wallet_ffi/src/lib.rs +++ b/base_layer/wallet_ffi/src/lib.rs @@ -246,6 +246,26 @@ pub enum TariUtxoSort { MinedHeightDesc = 3, } +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[repr(C)] +pub enum TariTypeTag { + Text = 0, + Utxo = 1, + Commitment = 2, + MicroTari = 3, +} + +impl Display for TariTypeTag { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + TariTypeTag::Text => write!(f, "Text"), + TariTypeTag::Utxo => write!(f, "Utxo"), + TariTypeTag::Commitment => write!(f, "Commitment"), + TariTypeTag::MicroTari => write!(f, "MicroTari"), + } + } +} + #[derive(Debug, Clone)] #[repr(C)] pub struct TariUtxo { @@ -285,26 +305,6 @@ impl TryFrom for TariUtxo { } } -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -#[repr(C)] -pub enum TariTypeTag { - Text = 0, - Utxo = 1, - Commitment = 2, - MicroTari = 3, -} - -impl Display for TariTypeTag { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - TariTypeTag::Text => write!(f, "Text"), - TariTypeTag::Utxo => write!(f, "Utxo"), - TariTypeTag::Commitment => write!(f, "Commitment"), - TariTypeTag::MicroTari => write!(f, "MicroTari"), - } - } -} - #[derive(Debug, Clone)] #[repr(C)] pub struct TariOutputs { @@ -314,7 +314,6 @@ pub struct TariOutputs { } // WARNING: must be destroyed properly after use -// TODO: dedup impl TryFrom> for TariOutputs { type Error = InterfaceError; @@ -322,34 +321,7 @@ impl TryFrom> for TariOutputs { let mut outputs = ManuallyDrop::new( outputs .into_iter() - .map(|x| { - Ok(TariUtxo { - commitment: CString::new(x.commitment.to_hex()) - .map_err(|e| { - InterfaceError::InvalidArgument(format!( - "failed to obtain hex from a commitment: {:?}", - e - )) - })? - .into_raw(), - value: x.unblinded_output.value.as_u64(), - mined_height: x.mined_height.unwrap_or(0), - status: match x.status { - OutputStatus::Unspent => 0, - OutputStatus::Spent => 1, - OutputStatus::EncumberedToBeReceived => 2, - OutputStatus::EncumberedToBeSpent => 3, - OutputStatus::Invalid => 4, - OutputStatus::CancelledInbound => 5, - OutputStatus::UnspentMinedUnconfirmed => 6, - OutputStatus::ShortTermEncumberedToBeReceived => 7, - OutputStatus::ShortTermEncumberedToBeSpent => 8, - OutputStatus::SpentMinedUnconfirmed => 9, - OutputStatus::AbandonedCoinbase => 10, - OutputStatus::NotStored => 11, - }, - }) - }) + .map(TariUtxo::try_from) .try_collect::, InterfaceError>()?, );