Skip to content

Commit

Permalink
Calculate cooperative tx sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Feb 4, 2025
1 parent 1989f43 commit 924ed12
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
18 changes: 13 additions & 5 deletions src/swaps/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,16 +1205,24 @@ impl BtcSwapTx {
}

/// Calculate the size of a transaction.
/// The `preimage` is only required when calculating the claim tx size.
/// Use this before calling drain to help calculate the absolute fees.
/// Multiply the size by the fee_rate to get the absolute fees.
pub fn size(&self, keys: &Keypair, preimage: &Preimage) -> Result<usize, Error> {
let dummy_abs_fee = 0;
// Can only calculate non-coperative claims
pub fn size(
&self,
keys: &Keypair,
preimage: Option<&Preimage>,
is_cooperative: bool,
) -> Result<usize, Error> {
let dummy_abs_fee = 1;
let tx = match self.kind {
SwapTxKind::Claim => {
self.sign_claim(keys, preimage, Fee::Absolute(dummy_abs_fee), None)?
let Some(preimage) = preimage else {
return Err(Error::Protocol("No preimage provided.".to_string()));
};
self.create_claim(keys, preimage, dummy_abs_fee, is_cooperative)?
}
SwapTxKind::Refund => self.sign_refund(keys, Fee::Absolute(dummy_abs_fee), None)?,
SwapTxKind::Refund => self.create_refund(keys, dummy_abs_fee, is_cooperative)?,
};
Ok(tx.vsize())
}
Expand Down
21 changes: 10 additions & 11 deletions src/swaps/liquid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,26 +1262,25 @@ impl LBtcSwapTx {
}

/// Calculate the size of a transaction.
/// The `preimage` is only required when calculating the claim tx size.
/// Use this before calling drain to help calculate the absolute fees.
/// Multiply the size by the fee_rate to get the absolute fees.
pub fn size(
&self,
keys: &Keypair,
preimage: &Preimage,
preimage: Option<&Preimage>,
is_cooperative: bool,
is_discount_ct: bool,
) -> Result<usize, Error> {
let dummy_abs_fee = 0;
let dummy_abs_fee = 1;
let tx = match self.kind {
SwapTxKind::Claim => self.sign_claim(
keys,
preimage,
Fee::Absolute(dummy_abs_fee),
None,
is_discount_ct,
)?,
SwapTxKind::Refund => {
self.sign_refund(keys, Fee::Absolute(dummy_abs_fee), None, is_discount_ct)?
SwapTxKind::Claim => {
let Some(preimage) = preimage else {
return Err(Error::Protocol("No preimage provided.".to_string()));
};
self.create_claim(keys, preimage, dummy_abs_fee, is_cooperative)?
}
SwapTxKind::Refund => self.create_refund(keys, dummy_abs_fee, is_cooperative)?,
};
Ok(tx_size(&tx, is_discount_ct))
}
Expand Down
48 changes: 48 additions & 0 deletions tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ fn prepare_btc_claim() -> (
)
}

#[test]
fn btc_reverse_claim_size() {
let (_test_framework, _scan_request, swap_tx, preimage, recvr_keypair, _utxos) =
prepare_btc_claim();

let coop_claim_tx_size = swap_tx.size(&recvr_keypair, Some(&preimage), true).unwrap();
assert_eq!(coop_claim_tx_size, 84);

let non_coop_claim_tx_size = swap_tx.size(&recvr_keypair, Some(&preimage), false).unwrap();
assert_eq!(non_coop_claim_tx_size, 140);
}

#[test]
fn btc_reverse_claim() {
let (test_framework, scan_request, swap_tx, preimage, recvr_keypair, utxos) =
Expand Down Expand Up @@ -259,6 +271,18 @@ fn prepare_btc_refund() -> (
(test_framework, scan_request, swap_tx, sender_keypair, utxos)
}

#[test]
fn btc_submarine_refund_size() {
let (_test_framework, _scan_request, swap_tx, sender_keypair, _utxos) =
prepare_btc_refund();

let coop_claim_tx_size = swap_tx.size(&sender_keypair, None, true).unwrap();
assert_eq!(coop_claim_tx_size, 84);

let non_coop_claim_tx_size = swap_tx.size(&sender_keypair, None, false).unwrap();
assert_eq!(non_coop_claim_tx_size, 126);
}

#[test]
fn btc_submarine_refund() {
let (test_framework, scan_request, swap_tx, sender_keypair, utxos) = prepare_btc_refund();
Expand Down Expand Up @@ -414,6 +438,18 @@ fn prepare_lbtc_claim() -> (
)
}

#[test]
fn lbtc_reverse_claim_size() {
let (_test_framework, swap_tx, preimage, recvr_keypair, _blinding_keypair, _swap_addrs, _utxos) =
prepare_lbtc_claim();

let coop_claim_tx_size = swap_tx.size(&recvr_keypair, Some(&preimage), true, true).unwrap();
assert_eq!(coop_claim_tx_size, 165);

let non_coop_claim_tx_size = swap_tx.size(&recvr_keypair, Some(&preimage), false, true).unwrap();
assert_eq!(non_coop_claim_tx_size, 221);
}

#[test]
fn lbtc_reverse_claim() {
let (test_framework, swap_tx, preimage, recvr_keypair, blinding_keypair, swap_addrs, utxo) =
Expand Down Expand Up @@ -544,6 +580,18 @@ fn prepare_lbtc_refund() -> (
)
}

#[test]
fn lbtc_submarine_refund_size() {
let (_test_framework, swap_tx, sender_keypair, _blinding_keypair, _swap_addrs, _utxos) =
prepare_lbtc_refund();

let coop_claim_tx_size = swap_tx.size(&sender_keypair, None, true, true).unwrap();
assert_eq!(coop_claim_tx_size, 165);

let non_coop_claim_tx_size = swap_tx.size(&sender_keypair, None, false, true).unwrap();
assert_eq!(non_coop_claim_tx_size, 207);
}

#[test]
fn lbtc_submarine_refund() {
let (test_framework, swap_tx, sender_keypair, blinding_keypair, swap_addrs, utxo) =
Expand Down

0 comments on commit 924ed12

Please sign in to comment.