Skip to content

Commit

Permalink
fixup: Return concrete types from find_incomplete_coinswaps
Browse files Browse the repository at this point in the history
  • Loading branch information
GeneFerneau committed Aug 21, 2021
1 parent 58c0f8b commit 2b7b188
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod wallet_sync;
use wallet_sync::Wallet;

mod contracts;
use contracts::read_locktime_from_contract;
use contracts::{read_locktime_from_contract, SwapCoin};

mod error;
mod maker_protocol;
Expand Down Expand Up @@ -171,15 +171,30 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option<bool>) {
let incomplete_coinswaps = wallet.find_incomplete_coinswaps(&rpc).unwrap();
if incomplete_coinswaps.len() > 0 {
println!("= incomplete coinswaps =");
for (hashvalue, utxo_swapcoins) in incomplete_coinswaps {
let balance: Amount = utxo_swapcoins
for (hashvalue, (utxo_incoming_swapcoins, utxo_outgoing_swapcoins)) in incomplete_coinswaps
{
let balance: Amount = utxo_incoming_swapcoins
.iter()
.map(|(l, i)| (l, (*i as &dyn SwapCoin)))
.chain(
utxo_outgoing_swapcoins
.iter()
.map(|(l, o)| (l, (*o as &dyn SwapCoin))),
)
.fold(Amount::ZERO, |acc, us| acc + us.0.amount);
println!(
"{:16} {:8} {:8} {:<15} {:<7} value",
"outpoint", "type", "preimage", "locktime/blocks", "conf",
);
for (utxo, swapcoin) in utxo_swapcoins {
for (utxo, swapcoin) in utxo_incoming_swapcoins
.iter()
.map(|(l, i)| (l, (*i as &dyn SwapCoin)))
.chain(
utxo_outgoing_swapcoins
.iter()
.map(|(l, o)| (l, (*o as &dyn SwapCoin))),
)
{
let txid = utxo.txid.to_hex();

#[rustfmt::skip]
Expand Down
75 changes: 52 additions & 23 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,16 @@ impl Wallet {
pub fn find_incomplete_coinswaps(
&self,
rpc: &Client,
) -> Result<HashMap<Hash160, Vec<(ListUnspentResultEntry, &dyn SwapCoin)>>, Error> {
) -> Result<
HashMap<
Hash160,
(
Vec<(ListUnspentResultEntry, &IncomingSwapCoin)>,
Vec<(ListUnspentResultEntry, &OutgoingSwapCoin)>,
),
>,
Error,
> {
rpc.call::<Value>("lockunspent", &[Value::Bool(true)])
.map_err(|e| Error::Rpc(e))?;

Expand All @@ -806,8 +815,24 @@ impl Wallet {
);
//TODO make this read_hashvalue_from_contract() a struct function of WalletCoinSwap

let mut incomplete_swapcoin_groups =
HashMap::<Hash160, Vec<(ListUnspentResultEntry, &dyn SwapCoin)>>::new();
let mut incomplete_swapcoin_groups = HashMap::<
Hash160,
(
Vec<(ListUnspentResultEntry, &IncomingSwapCoin)>,
Vec<(ListUnspentResultEntry, &OutgoingSwapCoin)>,
),
>::new();
let get_hashvalue = |s: &dyn SwapCoin| {
if s.is_known() {
return None;
}
let swapcoin_hashvalue = read_hashvalue_from_contract(&s.get_contract_redeemscript())
.expect("unable to read hashvalue from contract_redeemscript");
if completed_coinswap_hashvalues.contains(&swapcoin_hashvalue) {
return None;
}
Some(swapcoin_hashvalue)
};
for utxo in rpc.list_unspent(None, None, None, None, None)? {
if utxo.descriptor.is_none() {
continue;
Expand All @@ -817,27 +842,31 @@ impl Wallet {
} else {
continue;
};
let swapcoin: &dyn SwapCoin =
if let Some(s) = self.find_incoming_swapcoin(multisig_redeemscript) {
s
} else if let Some(s) = self.find_outgoing_swapcoin(multisig_redeemscript) {
s
} else {
continue;
};
if swapcoin.is_known() {
continue;
}
let swapcoin_hashvalue =
read_hashvalue_from_contract(&swapcoin.get_contract_redeemscript())
.expect("unable to read hashvalue from contract_redeemscript");
if completed_coinswap_hashvalues.contains(&swapcoin_hashvalue) {
if let Some(s) = self.find_incoming_swapcoin(multisig_redeemscript) {
if let Some(swapcoin_hashvalue) = get_hashvalue(s) {
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert((
Vec::<(ListUnspentResultEntry, &IncomingSwapCoin)>::new(),
Vec::<(ListUnspentResultEntry, &OutgoingSwapCoin)>::new(),
))
.0
.push((utxo, s));
}
} else if let Some(s) = self.find_outgoing_swapcoin(multisig_redeemscript) {
if let Some(swapcoin_hashvalue) = get_hashvalue(s) {
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert((
Vec::<(ListUnspentResultEntry, &IncomingSwapCoin)>::new(),
Vec::<(ListUnspentResultEntry, &OutgoingSwapCoin)>::new(),
))
.1
.push((utxo, s));
}
} else {
continue;
}
incomplete_swapcoin_groups
.entry(swapcoin_hashvalue)
.or_insert(Vec::<(ListUnspentResultEntry, &dyn SwapCoin)>::new())
.push((utxo, swapcoin));
};
}
Ok(incomplete_swapcoin_groups)
}
Expand Down

0 comments on commit 2b7b188

Please sign in to comment.