Only wait for xmr-lock until t1 expired

This commit is contained in:
Daniel Karzel 2020-12-22 17:08:17 +11:00
parent 0cdb7ca8a8
commit c9d492d155
4 changed files with 57 additions and 27 deletions

View File

@ -227,13 +227,32 @@ where
{ {
event_loop_handle.dial().await?; event_loop_handle.dial().await?;
// todo: watch until t1, not indefinitely let msg2_watcher = event_loop_handle.recv_message2();
let msg2 = event_loop_handle.recv_message2().await?; let t1_timeout = state3.wait_for_t1(bitcoin_wallet.as_ref());
let state4 = state3
.watch_for_lock_xmr(monero_wallet.as_ref(), msg2)
.await?;
BobState::XmrLocked(state4) select! {
msg2 = msg2_watcher => {
let xmr_lock_watcher = state3.clone()
.watch_for_lock_xmr(monero_wallet.as_ref(), msg2?);
let t1_timeout = state3.wait_for_t1(bitcoin_wallet.as_ref());
select! {
state4 = xmr_lock_watcher => {
BobState::XmrLocked(state4?)
},
_ = t1_timeout => {
let state4 = state3.t1_expired();
BobState::T1Expired(state4)
}
}
},
_ = t1_timeout => {
let state4 = state3.t1_expired();
BobState::T1Expired(state4)
}
}
} else { } else {
let state4 = state3.t1_expired(); let state4 = state3.t1_expired();
BobState::T1Expired(state4) BobState::T1Expired(state4)

View File

@ -28,7 +28,7 @@ use std::{
use tokio::{sync::Mutex, time::timeout}; use tokio::{sync::Mutex, time::timeout};
use tracing::{error, info}; use tracing::{error, info};
pub mod message; pub mod message;
use crate::bitcoin::{BlockHeight, TransactionBlockHeight, current_epoch}; use crate::bitcoin::{current_epoch, wait_for_t1, BlockHeight, TransactionBlockHeight};
pub use message::{Message, Message0, Message1, Message2}; pub use message::{Message, Message0, Message1, Message2};
#[derive(Debug)] #[derive(Debug)]
@ -684,13 +684,7 @@ impl State3 {
where where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{ {
let tx_id = self.tx_lock.txid(); wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
let tx_lock_height = bitcoin_wallet.transaction_block_height(tx_id).await;
let t1_timeout =
poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + self.refund_timelock);
t1_timeout.await;
Ok(())
} }
pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch> pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch>

View File

@ -11,10 +11,10 @@ use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::str::FromStr; use std::str::FromStr;
use crate::Epoch;
pub use bitcoin::{util::psbt::PartiallySignedTransaction, *}; pub use bitcoin::{util::psbt::PartiallySignedTransaction, *};
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature}; pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund}; pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund};
use crate::Epoch;
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping) // TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
// Current reasoning: // Current reasoning:
@ -245,15 +245,14 @@ where
} }
} }
pub async fn current_epoch<W>( pub async fn current_epoch<W>(
bitcoin_wallet: &W, bitcoin_wallet: &W,
refund_timelock: u32, refund_timelock: u32,
punish_timelock: u32, punish_timelock: u32,
lock_tx_id: ::bitcoin::Txid, lock_tx_id: ::bitcoin::Txid,
) -> anyhow::Result<Epoch> ) -> anyhow::Result<Epoch>
where where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{ {
let current_block_height = bitcoin_wallet.block_height().await; let current_block_height = bitcoin_wallet.block_height().await;
let t0 = bitcoin_wallet.transaction_block_height(lock_tx_id).await; let t0 = bitcoin_wallet.transaction_block_height(lock_tx_id).await;
@ -266,3 +265,19 @@ pub async fn current_epoch<W>(
(false, false) => Ok(Epoch::T2), (false, false) => Ok(Epoch::T2),
} }
} }
pub async fn wait_for_t1<W>(
bitcoin_wallet: &W,
refund_timelock: u32,
lock_tx_id: ::bitcoin::Txid,
) -> Result<()>
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
let tx_lock_height = bitcoin_wallet.transaction_block_height(lock_tx_id).await;
let t1_timeout =
poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + refund_timelock);
t1_timeout.await;
Ok(())
}

View File

@ -34,12 +34,13 @@ use tracing::error;
pub mod message; pub mod message;
use crate::{ use crate::{
bitcoin::{BlockHeight, GetRawTransaction, Network, TransactionBlockHeight}, bitcoin::{
current_epoch, wait_for_t1, BlockHeight, GetRawTransaction, Network, TransactionBlockHeight,
},
monero::{CreateWalletForOutput, WatchForTransfer}, monero::{CreateWalletForOutput, WatchForTransfer},
}; };
use ::bitcoin::{Transaction, Txid}; use ::bitcoin::{Transaction, Txid};
pub use message::{Message, Message0, Message1, Message2, Message3}; pub use message::{Message, Message0, Message1, Message2, Message3};
use crate::bitcoin::current_epoch;
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
#[derive(Debug)] #[derive(Debug)]
@ -622,6 +623,13 @@ impl State3 {
}) })
} }
pub async fn wait_for_t1<W>(&self, bitcoin_wallet: &W) -> Result<()>
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
}
pub fn t1_expired(&self) -> State4 { pub fn t1_expired(&self) -> State4 {
State4 { State4 {
A: self.A, A: self.A,
@ -783,13 +791,7 @@ impl State4 {
where where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight, W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{ {
let tx_id = self.tx_lock.txid(); wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
let tx_lock_height = bitcoin_wallet.transaction_block_height(tx_id).await;
let t1_timeout =
poll_until_block_height_is_gte(bitcoin_wallet, tx_lock_height + self.refund_timelock);
t1_timeout.await;
Ok(())
} }
pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch> pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch>