Move current_epoch from lib to bitcoin.rs

This commit is contained in:
Daniel Karzel 2020-12-22 16:45:30 +11:00
parent 83ce6f2c85
commit 0cdb7ca8a8
4 changed files with 28 additions and 26 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};
use crate::bitcoin::{BlockHeight, TransactionBlockHeight, current_epoch};
pub use message::{Message, Message0, Message1, Message2};
#[derive(Debug)]
@ -697,7 +697,7 @@ impl State3 {
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
crate::current_epoch(
current_epoch(
bitcoin_wallet,
self.refund_timelock,
self.punish_timelock,

View File

@ -14,6 +14,7 @@ use std::str::FromStr;
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:
@ -243,3 +244,25 @@ where
tokio::time::delay_for(std::time::Duration::from_secs(1)).await;
}
}
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,
{
let current_block_height = bitcoin_wallet.block_height().await;
let t0 = bitcoin_wallet.transaction_block_height(lock_tx_id).await;
let t1 = t0 + refund_timelock;
let t2 = t1 + punish_timelock;
match (current_block_height < t1, current_block_height < t2) {
(true, _) => Ok(Epoch::T0),
(false, true) => Ok(Epoch::T1),
(false, false) => Ok(Epoch::T2),
}
}

View File

@ -39,6 +39,7 @@ use crate::{
};
use ::bitcoin::{Transaction, Txid};
pub use message::{Message, Message0, Message1, Message2, Message3};
use crate::bitcoin::current_epoch;
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
@ -650,7 +651,7 @@ impl State3 {
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
crate::current_epoch(
current_epoch(
bitcoin_wallet,
self.refund_timelock,
self.punish_timelock,
@ -795,7 +796,7 @@ impl State4 {
where
W: WatchForRawTransaction + TransactionBlockHeight + BlockHeight,
{
crate::current_epoch(
current_epoch(
bitcoin_wallet,
self.refund_timelock,
self.punish_timelock,

View File

@ -60,26 +60,4 @@ pub mod monero;
pub mod serde;
pub mod transport;
use crate::bitcoin::{BlockHeight, TransactionBlockHeight, WatchForRawTransaction};
pub use cross_curve_dleq;
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,
{
let current_block_height = bitcoin_wallet.block_height().await;
let t0 = bitcoin_wallet.transaction_block_height(lock_tx_id).await;
let t1 = t0 + refund_timelock;
let t2 = t1 + punish_timelock;
match (current_block_height < t1, current_block_height < t2) {
(true, _) => Ok(Epoch::T0),
(false, true) => Ok(Epoch::T1),
(false, false) => Ok(Epoch::T2),
}
}