Type timelock and block height

To ensure no mistake is made (and none were!)
pull/114/head
Franck Royer 3 years ago
parent 81cbc24c46
commit bcbc54b569
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

@ -9,7 +9,6 @@ use futures::{
pin_mut,
};
use libp2p::request_response::ResponseChannel;
use rand::rngs::OsRng;
use sha2::Sha256;
use std::{sync::Arc, time::Duration};
@ -19,9 +18,9 @@ use xmr_btc::{
alice,
alice::State3,
bitcoin::{
poll_until_block_height_is_gte, BroadcastSignedTransaction, EncryptedSignature,
GetBlockHeight, GetRawTransaction, TransactionBlockHeight, TxCancel, TxLock, TxRefund,
WaitForTransactionFinality, WatchForRawTransaction,
poll_until_block_height_is_gte, BlockHeight, BroadcastSignedTransaction,
EncryptedSignature, GetBlockHeight, GetRawTransaction, Timelock, TransactionBlockHeight,
TxCancel, TxLock, TxRefund, WaitForTransactionFinality, WatchForRawTransaction,
},
config::Config,
cross_curve_dleq,
@ -207,7 +206,7 @@ pub async fn publish_cancel_transaction<W>(
tx_lock: TxLock,
a: bitcoin::SecretKey,
B: bitcoin::PublicKey,
cancel_timelock: u32,
cancel_timelock: Timelock,
tx_cancel_sig_bob: bitcoin::Signature,
bitcoin_wallet: Arc<W>,
) -> Result<bitcoin::TxCancel>
@ -253,8 +252,8 @@ where
pub async fn wait_for_bitcoin_refund<W>(
tx_cancel: &TxCancel,
cancel_tx_height: u32,
punish_timelock: u32,
cancel_tx_height: BlockHeight,
punish_timelock: Timelock,
refund_address: &bitcoin::Address,
bitcoin_wallet: Arc<W>,
) -> Result<(bitcoin::TxRefund, Option<bitcoin::Transaction>)>
@ -306,9 +305,9 @@ pub fn extract_monero_private_key(
pub fn build_bitcoin_punish_transaction(
tx_lock: &TxLock,
cancel_timelock: u32,
cancel_timelock: Timelock,
punish_address: &bitcoin::Address,
punish_timelock: u32,
punish_timelock: Timelock,
tx_punish_sig_bob: bitcoin::Signature,
a: bitcoin::SecretKey,
B: bitcoin::PublicKey,

@ -133,24 +133,26 @@ impl GetRawTransaction for Wallet {
#[async_trait]
impl GetBlockHeight for Wallet {
async fn get_block_height(&self) -> u32 {
(|| async { Ok(self.inner.client.getblockcount().await?) })
async fn get_block_height(&self) -> BlockHeight {
let height = (|| async { Ok(self.inner.client.getblockcount().await?) })
.retry(ConstantBackoff::new(Duration::from_secs(1)))
.await
.expect("transient errors to be retried")
.expect("transient errors to be retried");
BlockHeight::new(height)
}
}
#[async_trait]
impl TransactionBlockHeight for Wallet {
async fn transaction_block_height(&self, txid: Txid) -> u32 {
async fn transaction_block_height(&self, txid: Txid) -> BlockHeight {
#[derive(Debug)]
enum Error {
Io,
NotYetMined,
}
(|| async {
let height = (|| async {
let block_height = self
.inner
.transaction_block_height(txid)
@ -164,7 +166,9 @@ impl TransactionBlockHeight for Wallet {
})
.retry(ConstantBackoff::new(Duration::from_secs(1)))
.await
.expect("transient errors to be retried")
.expect("transient errors to be retried");
BlockHeight::new(height)
}
}

@ -29,7 +29,8 @@ use tokio::{sync::Mutex, time::timeout};
use tracing::{error, info};
pub mod message;
use crate::bitcoin::{
current_epoch, wait_for_cancel_timelock_to_expire, GetBlockHeight, TransactionBlockHeight,
current_epoch, wait_for_cancel_timelock_to_expire, GetBlockHeight, Timelock,
TransactionBlockHeight,
};
pub use message::{Message, Message0, Message1, Message2};
@ -412,8 +413,8 @@ impl State {
rng: &mut R,
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
) -> Self {
@ -443,8 +444,8 @@ pub struct State0 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
pub xmr: monero::Amount,
pub cancel_timelock: u32,
pub punish_timelock: u32,
pub cancel_timelock: Timelock,
pub punish_timelock: Timelock,
pub redeem_address: bitcoin::Address,
pub punish_address: bitcoin::Address,
}
@ -457,8 +458,8 @@ impl State0 {
v_a: monero::PrivateViewKey,
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
) -> Self {
@ -532,8 +533,8 @@ pub struct State1 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -571,8 +572,8 @@ pub struct State2 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -640,8 +641,8 @@ pub struct State3 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount,
pub xmr: monero::Amount,
pub cancel_timelock: u32,
pub punish_timelock: u32,
pub cancel_timelock: Timelock,
pub punish_timelock: Timelock,
pub refund_address: bitcoin::Address,
pub redeem_address: bitcoin::Address,
pub punish_address: bitcoin::Address,
@ -719,8 +720,8 @@ pub struct State4 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -816,8 +817,8 @@ pub struct State5 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -905,8 +906,8 @@ pub struct State6 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,

@ -1,6 +1,7 @@
mod timelocks;
pub mod transactions;
use crate::config::Config;
use crate::{config::Config, ExpiredTimelocks};
use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
use bitcoin::hashes::{hex::ToHex, Hash};
@ -11,9 +12,9 @@ use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::str::FromStr;
use crate::ExpiredTimelocks;
pub use bitcoin::{util::psbt::PartiallySignedTransaction, *};
pub use ecdsa_fun::{adaptor::EncryptedSignature, fun::Scalar, Signature};
pub use timelocks::*;
pub use transactions::{TxCancel, TxLock, TxPunish, TxRedeem, TxRefund};
// TODO: Configurable tx-fee (note: parties have to agree prior to swapping)
@ -202,17 +203,17 @@ pub trait WaitForTransactionFinality {
#[async_trait]
pub trait GetBlockHeight {
async fn get_block_height(&self) -> u32;
async fn get_block_height(&self) -> BlockHeight;
}
#[async_trait]
pub trait TransactionBlockHeight {
async fn transaction_block_height(&self, txid: Txid) -> u32;
async fn transaction_block_height(&self, txid: Txid) -> BlockHeight;
}
#[async_trait]
pub trait WaitForBlockHeight {
async fn wait_for_block_height(&self, height: u32);
async fn wait_for_block_height(&self, height: BlockHeight);
}
#[async_trait]
@ -236,7 +237,7 @@ pub fn recover(S: PublicKey, sig: Signature, encsig: EncryptedSignature) -> Resu
Ok(s)
}
pub async fn poll_until_block_height_is_gte<B>(client: &B, target: u32)
pub async fn poll_until_block_height_is_gte<B>(client: &B, target: BlockHeight)
where
B: GetBlockHeight,
{
@ -247,8 +248,8 @@ where
pub async fn current_epoch<W>(
bitcoin_wallet: &W,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
lock_tx_id: ::bitcoin::Txid,
) -> anyhow::Result<ExpiredTimelocks>
where
@ -271,7 +272,7 @@ where
pub async fn wait_for_cancel_timelock_to_expire<W>(
bitcoin_wallet: &W,
cancel_timelock: u32,
cancel_timelock: Timelock,
lock_tx_id: ::bitcoin::Txid,
) -> Result<()>
where

@ -0,0 +1,49 @@
use serde::{Deserialize, Serialize};
use std::ops::Add;
/// Represent a timelock, expressed in relative block height as defined in
/// [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki).
/// E.g. The timelock expires 10 blocks after the reference transaction is
/// mined.
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(transparent)]
pub struct Timelock(u32);
impl Timelock {
pub const fn new(number_of_blocks: u32) -> Self {
Self(number_of_blocks)
}
}
impl From<Timelock> for u32 {
fn from(timelock: Timelock) -> Self {
timelock.0
}
}
/// Represent a block height, or block number, expressed in absolute block
/// count. E.g. The transaction was included in block #655123, 655123 block
/// after the genesis block.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BlockHeight(u32);
impl From<BlockHeight> for u32 {
fn from(height: BlockHeight) -> Self {
height.0
}
}
impl BlockHeight {
pub const fn new(block_height: u32) -> Self {
Self(block_height)
}
}
impl Add<Timelock> for BlockHeight {
type Output = BlockHeight;
fn add(self, rhs: Timelock) -> Self::Output {
BlockHeight(self.0 + rhs.0)
}
}

@ -1,6 +1,6 @@
use crate::bitcoin::{
build_shared_output_descriptor, verify_sig, BuildTxLockPsbt, Network, OutPoint, PublicKey,
Txid, TX_FEE,
Timelock, Txid, TX_FEE,
};
use anyhow::{bail, Context, Result};
use bitcoin::{
@ -228,13 +228,13 @@ pub struct TxCancel {
}
impl TxCancel {
pub fn new(tx_lock: &TxLock, cancel_timelock: u32, A: PublicKey, B: PublicKey) -> Self {
pub fn new(tx_lock: &TxLock, cancel_timelock: Timelock, A: PublicKey, B: PublicKey) -> Self {
let cancel_output_descriptor = build_shared_output_descriptor(A.0, B.0);
let tx_in = TxIn {
previous_output: tx_lock.as_outpoint(),
script_sig: Default::default(),
sequence: cancel_timelock,
sequence: cancel_timelock.into(),
witness: Vec::new(),
};
@ -316,14 +316,14 @@ impl TxCancel {
fn build_spend_transaction(
&self,
spend_address: &Address,
sequence: Option<u32>,
sequence: Option<Timelock>,
) -> Transaction {
let previous_output = self.as_outpoint();
let tx_in = TxIn {
previous_output,
script_sig: Default::default(),
sequence: sequence.unwrap_or(0xFFFF_FFFF),
sequence: sequence.map(Into::into).unwrap_or(0xFFFF_FFFF),
witness: Vec::new(),
};
@ -450,7 +450,7 @@ pub struct TxPunish {
}
impl TxPunish {
pub fn new(tx_cancel: &TxCancel, punish_address: &Address, punish_timelock: u32) -> Self {
pub fn new(tx_cancel: &TxCancel, punish_address: &Address, punish_timelock: Timelock) -> Self {
let tx_punish = tx_cancel.build_spend_transaction(punish_address, Some(punish_timelock));
let digest = SigHashCache::new(&tx_punish).signature_hash(

@ -36,7 +36,7 @@ pub mod message;
use crate::{
bitcoin::{
current_epoch, wait_for_cancel_timelock_to_expire, GetBlockHeight, GetRawTransaction,
Network, TransactionBlockHeight,
Network, Timelock, TransactionBlockHeight,
},
monero::{CreateWalletForOutput, WatchForTransfer},
};
@ -357,8 +357,8 @@ pub struct State0 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
}
@ -367,8 +367,8 @@ impl State0 {
rng: &mut R,
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
) -> Self {
let b = bitcoin::SecretKey::new_random(rng);
@ -448,8 +448,8 @@ pub struct State1 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -507,8 +507,8 @@ pub struct State2 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
pub xmr: monero::Amount,
pub cancel_timelock: u32,
pub punish_timelock: u32,
pub cancel_timelock: Timelock,
pub punish_timelock: Timelock,
pub refund_address: bitcoin::Address,
pub redeem_address: bitcoin::Address,
pub punish_address: bitcoin::Address,
@ -574,8 +574,8 @@ pub struct State3 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
pub cancel_timelock: u32,
punish_timelock: u32,
pub cancel_timelock: Timelock,
punish_timelock: Timelock,
pub refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -686,8 +686,8 @@ pub struct State4 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
pub cancel_timelock: u32,
punish_timelock: u32,
pub cancel_timelock: Timelock,
punish_timelock: Timelock,
pub refund_address: bitcoin::Address,
pub redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
@ -879,8 +879,8 @@ pub struct State5 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
cancel_timelock: u32,
punish_timelock: u32,
cancel_timelock: Timelock,
punish_timelock: Timelock,
refund_address: bitcoin::Address,
pub redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,

@ -1,3 +1,4 @@
use crate::bitcoin::Timelock;
use conquer_once::Lazy;
use std::time::Duration;
@ -7,8 +8,8 @@ pub struct Config {
pub bitcoin_finality_confirmations: u32,
pub bitcoin_avg_block_time: Duration,
pub monero_max_finality_time: Duration,
pub bitcoin_cancel_timelock: u32,
pub bitcoin_punish_timelock: u32,
pub bitcoin_cancel_timelock: Timelock,
pub bitcoin_punish_timelock: Timelock,
pub bitcoin_network: ::bitcoin::Network,
}
@ -59,8 +60,8 @@ mod mainnet {
pub static MONERO_AVG_BLOCK_TIME: Lazy<Duration> = Lazy::new(|| Duration::from_secs(2 * 60));
// Set to 12 hours, arbitrary value to be reviewed properly
pub static BITCOIN_CANCEL_TIMELOCK: u32 = 72;
pub static BITCOIN_PUNISH_TIMELOCK: u32 = 72;
pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(72);
pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(72);
}
mod regtest {
@ -77,7 +78,7 @@ mod regtest {
pub static MONERO_AVG_BLOCK_TIME: Lazy<Duration> = Lazy::new(|| Duration::from_secs(60));
pub static BITCOIN_CANCEL_TIMELOCK: u32 = 50;
pub static BITCOIN_CANCEL_TIMELOCK: Timelock = Timelock::new(50);
pub static BITCOIN_PUNISH_TIMELOCK: u32 = 50;
pub static BITCOIN_PUNISH_TIMELOCK: Timelock = Timelock::new(50);
}

Loading…
Cancel
Save