Skip to content

Commit

Permalink
Remove unnecessary searches of wrong swapcoin list
Browse files Browse the repository at this point in the history
Previously the maker_protocol.rs functions handle_sign_receivers_contract_tx()
and handle_private_key_handover() would search both the incoming_swapcoins list
and the outgoing_swapcoins list, which was unnecessary and wrong. It was leftover
from old code when both kinds of swapcoins were in the same list.

This change leaves some functions in the IncomingSwapCoin or OutgoingSwapCoin
structs unused. Delete them.
  • Loading branch information
chris-belcher committed Dec 31, 2021
1 parent 8d93f7f commit c938596
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 47 deletions.
38 changes: 20 additions & 18 deletions src/contracts.rs
Expand Up @@ -572,24 +572,9 @@ impl SwapCoin for OutgoingSwapCoin {
}
}

macro_rules! sign_and_verify_contract {
macro_rules! verify_contract {
($coin:ident) => {
impl $coin {
//"_with_my_privkey" as opposed to with other_privkey
pub fn sign_contract_tx_with_my_privkey(
&self,
contract_tx: &Transaction,
) -> Result<Signature, Error> {
let multisig_redeemscript = self.get_multisig_redeemscript();
Ok(sign_contract_tx(
contract_tx,
&multisig_redeemscript,
self.funding_amount,
&self.my_privkey,
)
.map_err(|_| Error::Protocol("error with signing contract tx"))?)
}

pub fn verify_contract_tx_sig(&self, sig: &Signature) -> bool {
verify_contract_tx_sig(
&self.contract_tx,
Expand All @@ -603,8 +588,25 @@ macro_rules! sign_and_verify_contract {
};
}

sign_and_verify_contract!(IncomingSwapCoin);
sign_and_verify_contract!(OutgoingSwapCoin);
verify_contract!(IncomingSwapCoin);
verify_contract!(OutgoingSwapCoin);

impl OutgoingSwapCoin {
//"_with_my_privkey" as opposed to with other_privkey
pub fn sign_contract_tx_with_my_privkey(
&self,
contract_tx: &Transaction,
) -> Result<Signature, Error> {
let multisig_redeemscript = self.get_multisig_redeemscript();
Ok(sign_contract_tx(
contract_tx,
&multisig_redeemscript,
self.funding_amount,
&self.my_privkey,
)
.map_err(|_| Error::Protocol("error with signing contract tx"))?)
}
}

impl SwapCoin for WatchOnlySwapCoin {
fn get_multisig_redeemscript(&self) -> Script {
Expand Down
33 changes: 11 additions & 22 deletions src/maker_protocol.rs
Expand Up @@ -724,20 +724,15 @@ fn handle_sign_receivers_contract_tx(
let mut sigs = Vec::<Signature>::new();
for receivers_contract_tx_info in message.txes {
sigs.push(
if let Some(c) = wallet
//the fact that the peer knows the correct multisig_redeemscript is what ensures
//security here, a random peer out there who isnt involved in a coinswap wont know
//what the multisig_redeemscript is
wallet
.read()
.unwrap()
.find_incoming_swapcoin(&receivers_contract_tx_info.multisig_redeemscript)
{
c.sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)?
} else {
wallet
.read()
.unwrap()
.find_outgoing_swapcoin(&receivers_contract_tx_info.multisig_redeemscript)
.ok_or(Error::Protocol("multisig_redeemscript not found"))?
.sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)?
},
.find_outgoing_swapcoin(&receivers_contract_tx_info.multisig_redeemscript)
.ok_or(Error::Protocol("multisig_redeemscript not found"))?
.sign_contract_tx_with_my_privkey(&receivers_contract_tx_info.contract_tx)?,
);
}
Ok(Some(MakerToTakerMessage::ReceiversContractSig(
Expand Down Expand Up @@ -799,16 +794,10 @@ fn handle_private_key_handover(
) -> Result<Option<MakerToTakerMessage>, Error> {
let mut wallet_ref = wallet.write().unwrap();
for swapcoin_private_key in message.swapcoin_private_keys {
if let Some(c) =
wallet_ref.find_incoming_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript)
{
c.apply_privkey(swapcoin_private_key.key)?
} else {
wallet_ref
.find_outgoing_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript)
.ok_or(Error::Protocol("multisig_redeemscript not found"))?
.apply_privkey(swapcoin_private_key.key)?
}
wallet_ref
.find_incoming_swapcoin_mut(&swapcoin_private_key.multisig_redeemscript)
.ok_or(Error::Protocol("multisig_redeemscript not found"))?
.apply_privkey(swapcoin_private_key.key)?
}
wallet_ref.update_swap_coins_list()?;
log::info!("Successfully Completed Coinswap");
Expand Down
7 changes: 0 additions & 7 deletions src/wallet_sync.rs
Expand Up @@ -438,13 +438,6 @@ impl Wallet {
self.incoming_swap_coins.get_mut(multisig_redeemscript)
}

pub fn find_outgoing_swapcoin_mut(
&mut self,
multisig_redeemscript: &Script,
) -> Option<&mut OutgoingSwapCoin> {
self.outgoing_swap_coins.get_mut(multisig_redeemscript)
}

pub fn add_incoming_swapcoin(&mut self, coin: IncomingSwapCoin) {
self.incoming_swap_coins
.insert(coin.get_multisig_redeemscript(), coin);
Expand Down

0 comments on commit c938596

Please sign in to comment.