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

@ -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,15 +245,14 @@ where
}
}
pub async fn current_epoch<W>(
bitcoin_wallet: &W,
refund_timelock: u32,
punish_timelock: u32,
lock_tx_id: ::bitcoin::Txid,
) -> anyhow::Result<Epoch>
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
let current_block_height = bitcoin_wallet.block_height().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),
}
}
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>