From 570fd8aeacc0cd0ab49af335150297ab612c45ab Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Wed, 25 Aug 2021 12:05:31 +0100 Subject: [PATCH] Remove contract_type() from SwapCoin trait I think this style is too OOP and not enough rustic. Now that we have separate structs Incoming/OutgoingSwapCoin we can more simply say that Outgoing = timelock and Incoming = hashlock --- src/contracts.rs | 13 ------------- src/main.rs | 10 +++++++--- src/wallet_sync.rs | 8 -------- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/contracts.rs b/src/contracts.rs index ddc1348e..2ebdef6d 100644 --- a/src/contracts.rs +++ b/src/contracts.rs @@ -51,7 +51,6 @@ pub trait SwapCoin { fn verify_contract_tx_receiver_sig(&self, sig: &Signature) -> bool; fn verify_contract_tx_sender_sig(&self, sig: &Signature) -> bool; fn apply_privkey(&mut self, privkey: SecretKey) -> Result<(), Error>; - fn contract_type(&self) -> &str; fn is_known(&self) -> bool; } @@ -506,10 +505,6 @@ impl SwapCoin for IncomingSwapCoin { Ok(()) } - fn contract_type(&self) -> &str { - self.type_string() - } - fn is_known(&self) -> bool { self.contract_privkey_is_known() } @@ -560,10 +555,6 @@ impl SwapCoin for OutgoingSwapCoin { } } - fn contract_type(&self) -> &str { - self.type_string() - } - fn is_known(&self) -> bool { self.contract_privkey_is_known() } @@ -656,10 +647,6 @@ impl SwapCoin for WatchOnlySwapCoin { } } - fn contract_type(&self) -> &str { - "watchonly" - } - fn is_known(&self) -> bool { false } diff --git a/src/main.rs b/src/main.rs index e01af8f1..5dd810a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate bitcoincore_rpc; use dirs::home_dir; use std::io; +use std::iter::repeat; use std::path::PathBuf; use std::sync::{Arc, RwLock}; @@ -186,13 +187,16 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option) { "{:16} {:8} {:8} {:<15} {:<7} value", "outpoint", "type", "preimage", "locktime/blocks", "conf", ); - for (utxo, swapcoin) in utxo_incoming_swapcoins + + for ((utxo, swapcoin), contract_type) in utxo_incoming_swapcoins .iter() .map(|(l, i)| (l, (*i as &dyn SwapCoin))) + .zip(repeat("hashlock")) .chain( utxo_outgoing_swapcoins .iter() - .map(|(l, o)| (l, (*o as &dyn SwapCoin))), + .map(|(l, o)| (l, (*o as &dyn SwapCoin))) + .zip(repeat("timelock")), ) { let txid = utxo.txid.to_hex(); @@ -203,7 +207,7 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option) { if long_form { "" } else { ".." }, if long_form { &"" } else { &txid[58..64] }, utxo.vout, - swapcoin.contract_type(), + contract_type, if swapcoin.is_known() { "known" } else { "unknown" }, read_locktime_from_contract(&swapcoin.get_contract_redeemscript()) .expect("unable to read locktime from contract"), diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index e0329fa0..5e878264 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -154,10 +154,6 @@ impl IncomingSwapCoin { } } - pub fn type_string(&self) -> &str { - "hashlock" - } - pub fn contract_privkey_is_known(&self) -> bool { self.other_privkey.is_some() || self.hash_preimage.is_some() } @@ -193,10 +189,6 @@ impl OutgoingSwapCoin { } } - pub fn type_string(&self) -> &str { - "timelock" - } - pub fn contract_privkey_is_known(&self) -> bool { self.hash_preimage.is_some() }