mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-25 15:39:25 -05:00
Remove unnecessary state variables by constructing TXs on demand
This commit is contained in:
parent
e5c0158597
commit
84ea092a1b
@ -1,4 +1,4 @@
|
|||||||
use crate::bitcoin::{EncryptedSignature, TxCancel, TxRefund};
|
use crate::bitcoin::EncryptedSignature;
|
||||||
use crate::monero;
|
use crate::monero;
|
||||||
use crate::monero::monero_private_key;
|
use crate::monero::monero_private_key;
|
||||||
use crate::protocol::alice;
|
use crate::protocol::alice;
|
||||||
@ -177,37 +177,18 @@ impl From<Alice> for AliceState {
|
|||||||
Alice::BtcCancelled {
|
Alice::BtcCancelled {
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
state3,
|
state3,
|
||||||
} => {
|
} => AliceState::BtcCancelled {
|
||||||
let tx_cancel = TxCancel::new(
|
monero_wallet_restore_blockheight,
|
||||||
&state3.tx_lock,
|
state3: Box::new(state3),
|
||||||
state3.cancel_timelock,
|
},
|
||||||
state3.a.public(),
|
|
||||||
state3.B,
|
|
||||||
);
|
|
||||||
|
|
||||||
AliceState::BtcCancelled {
|
|
||||||
monero_wallet_restore_blockheight,
|
|
||||||
state3: Box::new(state3),
|
|
||||||
tx_cancel: Box::new(tx_cancel),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Alice::BtcPunishable {
|
Alice::BtcPunishable {
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
state3,
|
state3,
|
||||||
} => {
|
} => AliceState::BtcPunishable {
|
||||||
let tx_cancel = TxCancel::new(
|
monero_wallet_restore_blockheight,
|
||||||
&state3.tx_lock,
|
state3: Box::new(state3),
|
||||||
state3.cancel_timelock,
|
},
|
||||||
state3.a.public(),
|
|
||||||
state3.B,
|
|
||||||
);
|
|
||||||
let tx_refund = TxRefund::new(&tx_cancel, &state3.refund_address);
|
|
||||||
AliceState::BtcPunishable {
|
|
||||||
monero_wallet_restore_blockheight,
|
|
||||||
tx_refund: Box::new(tx_refund),
|
|
||||||
state3: Box::new(state3),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Alice::BtcRefunded {
|
Alice::BtcRefunded {
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
state3,
|
state3,
|
||||||
|
@ -36,7 +36,6 @@ pub enum AliceState {
|
|||||||
BtcRedeemed,
|
BtcRedeemed,
|
||||||
BtcCancelled {
|
BtcCancelled {
|
||||||
monero_wallet_restore_blockheight: BlockHeight,
|
monero_wallet_restore_blockheight: BlockHeight,
|
||||||
tx_cancel: Box<TxCancel>,
|
|
||||||
state3: Box<State3>,
|
state3: Box<State3>,
|
||||||
},
|
},
|
||||||
BtcRefunded {
|
BtcRefunded {
|
||||||
@ -46,7 +45,6 @@ pub enum AliceState {
|
|||||||
},
|
},
|
||||||
BtcPunishable {
|
BtcPunishable {
|
||||||
monero_wallet_restore_blockheight: BlockHeight,
|
monero_wallet_restore_blockheight: BlockHeight,
|
||||||
tx_refund: Box<TxRefund>,
|
|
||||||
state3: Box<State3>,
|
state3: Box<State3>,
|
||||||
},
|
},
|
||||||
XmrRefunded,
|
XmrRefunded,
|
||||||
@ -337,7 +335,7 @@ impl State3 {
|
|||||||
&self,
|
&self,
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
) -> Result<ExpiredTimelocks> {
|
) -> Result<ExpiredTimelocks> {
|
||||||
let tx_cancel = TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B);
|
let tx_cancel = self.tx_cancel();
|
||||||
|
|
||||||
let tx_lock_status = bitcoin_wallet
|
let tx_lock_status = bitcoin_wallet
|
||||||
.status_of_script(&self.tx_lock.script_pubkey(), &self.tx_lock.txid())
|
.status_of_script(&self.tx_lock.script_pubkey(), &self.tx_lock.txid())
|
||||||
@ -354,10 +352,19 @@ impl State3 {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tx_punish(&self) -> TxPunish {
|
pub fn tx_cancel(&self) -> TxCancel {
|
||||||
let tx_cancel =
|
TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B)
|
||||||
bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B);
|
}
|
||||||
|
|
||||||
bitcoin::TxPunish::new(&tx_cancel, &self.punish_address, self.punish_timelock)
|
pub fn tx_punish(&self) -> TxPunish {
|
||||||
|
bitcoin::TxPunish::new(
|
||||||
|
&self.tx_cancel(),
|
||||||
|
&self.punish_address,
|
||||||
|
self.punish_timelock,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tx_refund(&self) -> TxRefund {
|
||||||
|
bitcoin::TxRefund::new(&self.tx_cancel(), &self.refund_address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ pub async fn publish_cancel_transaction(
|
|||||||
cancel_timelock: CancelTimelock,
|
cancel_timelock: CancelTimelock,
|
||||||
tx_cancel_sig_bob: bitcoin::Signature,
|
tx_cancel_sig_bob: bitcoin::Signature,
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
) -> Result<bitcoin::TxCancel> {
|
) -> Result<()> {
|
||||||
bitcoin_wallet
|
bitcoin_wallet
|
||||||
.watch_until_status(tx_lock.txid(), tx_lock.script_pubkey(), |status| {
|
.watch_until_status(tx_lock.txid(), tx_lock.script_pubkey(), |status| {
|
||||||
status.is_confirmed_with(cancel_timelock)
|
status.is_confirmed_with(cancel_timelock)
|
||||||
@ -81,7 +81,6 @@ pub async fn publish_cancel_transaction(
|
|||||||
let sig_b = tx_cancel_sig_bob.clone();
|
let sig_b = tx_cancel_sig_bob.clone();
|
||||||
|
|
||||||
let tx_cancel = tx_cancel
|
let tx_cancel = tx_cancel
|
||||||
.clone()
|
|
||||||
.add_signatures((a.public(), sig_a), (B, sig_b))
|
.add_signatures((a.public(), sig_a), (B, sig_b))
|
||||||
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
.expect("sig_{a,b} to be valid signatures for tx_cancel");
|
||||||
|
|
||||||
@ -92,7 +91,7 @@ pub async fn publish_cancel_transaction(
|
|||||||
// block height
|
// block height
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(tx_cancel)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn wait_for_bitcoin_refund(
|
pub async fn wait_for_bitcoin_refund(
|
||||||
|
@ -285,7 +285,7 @@ async fn run_until_internal(
|
|||||||
state3,
|
state3,
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
} => {
|
} => {
|
||||||
let tx_cancel = publish_cancel_transaction(
|
publish_cancel_transaction(
|
||||||
state3.tx_lock.clone(),
|
state3.tx_lock.clone(),
|
||||||
state3.a.clone(),
|
state3.a.clone(),
|
||||||
state3.B,
|
state3.B,
|
||||||
@ -297,7 +297,6 @@ async fn run_until_internal(
|
|||||||
|
|
||||||
let state = AliceState::BtcCancelled {
|
let state = AliceState::BtcCancelled {
|
||||||
state3,
|
state3,
|
||||||
tx_cancel: Box::new(tx_cancel),
|
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
};
|
};
|
||||||
let db_state = (&state).into();
|
let db_state = (&state).into();
|
||||||
@ -317,11 +316,10 @@ async fn run_until_internal(
|
|||||||
}
|
}
|
||||||
AliceState::BtcCancelled {
|
AliceState::BtcCancelled {
|
||||||
state3,
|
state3,
|
||||||
tx_cancel,
|
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
} => {
|
} => {
|
||||||
let (tx_refund, published_refund_tx) = wait_for_bitcoin_refund(
|
let (tx_refund, published_refund_tx) = wait_for_bitcoin_refund(
|
||||||
&tx_cancel,
|
&state3.tx_cancel(),
|
||||||
state3.punish_timelock,
|
state3.punish_timelock,
|
||||||
&state3.refund_address,
|
&state3.refund_address,
|
||||||
&bitcoin_wallet,
|
&bitcoin_wallet,
|
||||||
@ -332,7 +330,6 @@ async fn run_until_internal(
|
|||||||
match published_refund_tx {
|
match published_refund_tx {
|
||||||
None => {
|
None => {
|
||||||
let state = AliceState::BtcPunishable {
|
let state = AliceState::BtcPunishable {
|
||||||
tx_refund: Box::new(tx_refund),
|
|
||||||
state3,
|
state3,
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
};
|
};
|
||||||
@ -401,7 +398,6 @@ async fn run_until_internal(
|
|||||||
Ok(state)
|
Ok(state)
|
||||||
}
|
}
|
||||||
AliceState::BtcPunishable {
|
AliceState::BtcPunishable {
|
||||||
tx_refund,
|
|
||||||
state3,
|
state3,
|
||||||
monero_wallet_restore_blockheight,
|
monero_wallet_restore_blockheight,
|
||||||
} => {
|
} => {
|
||||||
@ -424,7 +420,7 @@ async fn run_until_internal(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let refund_tx_seen = bitcoin_wallet.watch_until_status(
|
let refund_tx_seen = bitcoin_wallet.watch_until_status(
|
||||||
tx_refund.txid(),
|
state3.tx_refund().txid(),
|
||||||
state3.refund_address.script_pubkey(),
|
state3.refund_address.script_pubkey(),
|
||||||
|status| status.has_been_seen(),
|
|status| status.has_been_seen(),
|
||||||
);
|
);
|
||||||
@ -434,12 +430,13 @@ async fn run_until_internal(
|
|||||||
|
|
||||||
match select(refund_tx_seen, punish_tx_finalised).await {
|
match select(refund_tx_seen, punish_tx_finalised).await {
|
||||||
Either::Left((Ok(()), _)) => {
|
Either::Left((Ok(()), _)) => {
|
||||||
let published_refund_tx =
|
let published_refund_tx = bitcoin_wallet
|
||||||
bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?;
|
.get_raw_transaction(state3.tx_refund().txid())
|
||||||
|
.await?;
|
||||||
|
|
||||||
let spend_key = extract_monero_private_key(
|
let spend_key = extract_monero_private_key(
|
||||||
published_refund_tx,
|
published_refund_tx,
|
||||||
&tx_refund,
|
&state3.tx_refund(),
|
||||||
state3.s_a,
|
state3.s_a,
|
||||||
state3.a.clone(),
|
state3.a.clone(),
|
||||||
state3.S_b_bitcoin,
|
state3.S_b_bitcoin,
|
||||||
|
Loading…
Reference in New Issue
Block a user