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?;
// todo: watch until t1, not indefinitely
let msg2 = event_loop_handle.recv_message2().await?;
let state4 = state3
.watch_for_lock_xmr(monero_wallet.as_ref(), msg2)
.await?;
let msg2_watcher = event_loop_handle.recv_message2();
let t1_timeout = state3.wait_for_t1(bitcoin_wallet.as_ref());
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 {
let state4 = state3.t1_expired();
BobState::T1Expired(state4)

View File

@ -28,7 +28,7 @@ use std::{
use tokio::{sync::Mutex, time::timeout};
use tracing::{error, info};
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};
#[derive(Debug)]
@ -684,13 +684,7 @@ impl State3 {
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
let tx_id = self.tx_lock.txid();
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(())
wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
}
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 std::str::FromStr;
use crate::Epoch;
pub use bitcoin::{util::psbt::PartiallySignedTransaction, *};
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund};
use crate::Epoch;
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
// Current reasoning:
@ -245,7 +245,6 @@ where
}
}
pub async fn current_epoch<W>(
bitcoin_wallet: &W,
refund_timelock: u32,
@ -266,3 +265,19 @@ pub async fn current_epoch<W>(
(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;
use crate::{
bitcoin::{BlockHeight, GetRawTransaction, Network, TransactionBlockHeight},
bitcoin::{
current_epoch, wait_for_t1, BlockHeight, GetRawTransaction, Network, TransactionBlockHeight,
},
monero::{CreateWalletForOutput, WatchForTransfer},
};
use ::bitcoin::{Transaction, Txid};
pub use message::{Message, Message0, Message1, Message2, Message3};
use crate::bitcoin::current_epoch;
#[allow(clippy::large_enum_variant)]
#[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 {
State4 {
A: self.A,
@ -783,13 +791,7 @@ impl State4 {
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
let tx_id = self.tx_lock.txid();
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(())
wait_for_t1(bitcoin_wallet, self.refund_timelock, self.tx_lock.txid()).await
}
pub async fn current_epoch<W>(&self, bitcoin_wallet: &W) -> Result<Epoch>