Remove unnecessary state variables by constructing TXs on demand

This commit is contained in:
Thomas Eizinger 2021-03-16 18:31:13 +11:00
parent e5c0158597
commit 84ea092a1b
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
4 changed files with 32 additions and 48 deletions

View File

@ -1,4 +1,4 @@
use crate::bitcoin::{EncryptedSignature, TxCancel, TxRefund};
use crate::bitcoin::EncryptedSignature;
use crate::monero;
use crate::monero::monero_private_key;
use crate::protocol::alice;
@ -177,37 +177,18 @@ impl From<Alice> for AliceState {
Alice::BtcCancelled {
monero_wallet_restore_blockheight,
state3,
} => {
let tx_cancel = TxCancel::new(
&state3.tx_lock,
state3.cancel_timelock,
state3.a.public(),
state3.B,
);
} => AliceState::BtcCancelled {
monero_wallet_restore_blockheight,
state3: Box::new(state3),
},
AliceState::BtcCancelled {
monero_wallet_restore_blockheight,
state3: Box::new(state3),
tx_cancel: Box::new(tx_cancel),
}
}
Alice::BtcPunishable {
monero_wallet_restore_blockheight,
state3,
} => {
let tx_cancel = TxCancel::new(
&state3.tx_lock,
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),
}
}
} => AliceState::BtcPunishable {
monero_wallet_restore_blockheight,
state3: Box::new(state3),
},
Alice::BtcRefunded {
monero_wallet_restore_blockheight,
state3,

View File

@ -36,7 +36,6 @@ pub enum AliceState {
BtcRedeemed,
BtcCancelled {
monero_wallet_restore_blockheight: BlockHeight,
tx_cancel: Box<TxCancel>,
state3: Box<State3>,
},
BtcRefunded {
@ -46,7 +45,6 @@ pub enum AliceState {
},
BtcPunishable {
monero_wallet_restore_blockheight: BlockHeight,
tx_refund: Box<TxRefund>,
state3: Box<State3>,
},
XmrRefunded,
@ -337,7 +335,7 @@ impl State3 {
&self,
bitcoin_wallet: &bitcoin::Wallet,
) -> 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
.status_of_script(&self.tx_lock.script_pubkey(), &self.tx_lock.txid())
@ -354,10 +352,19 @@ impl State3 {
))
}
pub fn tx_punish(&self) -> TxPunish {
let tx_cancel =
bitcoin::TxCancel::new(&self.tx_lock, self.cancel_timelock, self.a.public(), self.B);
pub fn tx_cancel(&self) -> TxCancel {
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)
}
}

View File

@ -59,7 +59,7 @@ pub async fn publish_cancel_transaction(
cancel_timelock: CancelTimelock,
tx_cancel_sig_bob: bitcoin::Signature,
bitcoin_wallet: &bitcoin::Wallet,
) -> Result<bitcoin::TxCancel> {
) -> Result<()> {
bitcoin_wallet
.watch_until_status(tx_lock.txid(), tx_lock.script_pubkey(), |status| {
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 tx_cancel = tx_cancel
.clone()
.add_signatures((a.public(), sig_a), (B, sig_b))
.expect("sig_{a,b} to be valid signatures for tx_cancel");
@ -92,7 +91,7 @@ pub async fn publish_cancel_transaction(
// block height
}
Ok(tx_cancel)
Ok(())
}
pub async fn wait_for_bitcoin_refund(

View File

@ -285,7 +285,7 @@ async fn run_until_internal(
state3,
monero_wallet_restore_blockheight,
} => {
let tx_cancel = publish_cancel_transaction(
publish_cancel_transaction(
state3.tx_lock.clone(),
state3.a.clone(),
state3.B,
@ -297,7 +297,6 @@ async fn run_until_internal(
let state = AliceState::BtcCancelled {
state3,
tx_cancel: Box::new(tx_cancel),
monero_wallet_restore_blockheight,
};
let db_state = (&state).into();
@ -317,11 +316,10 @@ async fn run_until_internal(
}
AliceState::BtcCancelled {
state3,
tx_cancel,
monero_wallet_restore_blockheight,
} => {
let (tx_refund, published_refund_tx) = wait_for_bitcoin_refund(
&tx_cancel,
&state3.tx_cancel(),
state3.punish_timelock,
&state3.refund_address,
&bitcoin_wallet,
@ -332,7 +330,6 @@ async fn run_until_internal(
match published_refund_tx {
None => {
let state = AliceState::BtcPunishable {
tx_refund: Box::new(tx_refund),
state3,
monero_wallet_restore_blockheight,
};
@ -401,7 +398,6 @@ async fn run_until_internal(
Ok(state)
}
AliceState::BtcPunishable {
tx_refund,
state3,
monero_wallet_restore_blockheight,
} => {
@ -424,7 +420,7 @@ async fn run_until_internal(
};
let refund_tx_seen = bitcoin_wallet.watch_until_status(
tx_refund.txid(),
state3.tx_refund().txid(),
state3.refund_address.script_pubkey(),
|status| status.has_been_seen(),
);
@ -434,12 +430,13 @@ async fn run_until_internal(
match select(refund_tx_seen, punish_tx_finalised).await {
Either::Left((Ok(()), _)) => {
let published_refund_tx =
bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?;
let published_refund_tx = bitcoin_wallet
.get_raw_transaction(state3.tx_refund().txid())
.await?;
let spend_key = extract_monero_private_key(
published_refund_tx,
&tx_refund,
&state3.tx_refund(),
state3.s_a,
state3.a.clone(),
state3.S_b_bitcoin,