Skip to content

Commit

Permalink
Use a stub preimage
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Feb 5, 2025
1 parent cc2762f commit 622eac3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
13 changes: 3 additions & 10 deletions src/swaps/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,19 +1208,12 @@ impl BtcSwapTx {
/// 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: Option<&Preimage>,
is_cooperative: bool,
) -> Result<usize, Error> {
pub fn size(&self, keys: &Keypair, is_cooperative: bool) -> Result<usize, Error> {
let dummy_abs_fee = 1;
let tx = match self.kind {
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)?
let preimage = Preimage::from_vec([0; 32].to_vec())?;
self.create_claim(keys, &preimage, dummy_abs_fee, is_cooperative)?
}
SwapTxKind::Refund => self.create_refund(keys, dummy_abs_fee, is_cooperative)?,
};
Expand Down
7 changes: 2 additions & 5 deletions src/swaps/liquid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,17 +1268,14 @@ impl LBtcSwapTx {
pub fn size(
&self,
keys: &Keypair,
preimage: Option<&Preimage>,
is_cooperative: bool,
is_discount_ct: bool,
) -> Result<usize, Error> {
let dummy_abs_fee = 1;
let tx = match self.kind {
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)?
let preimage = Preimage::from_vec([0; 32].to_vec())?;
self.create_claim(keys, &preimage, dummy_abs_fee, is_cooperative)?
}
SwapTxKind::Refund => self.create_refund(keys, dummy_abs_fee, is_cooperative)?,
};
Expand Down
35 changes: 18 additions & 17 deletions tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ fn prepare_btc_claim() -> (

#[test]
fn btc_reverse_claim_size() {
let (_test_framework, _scan_request, swap_tx, preimage, recvr_keypair, _utxos) =
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();
let coop_claim_tx_size = swap_tx.size(&recvr_keypair, true).unwrap();
assert_eq!(coop_claim_tx_size, 84);

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

Expand Down Expand Up @@ -277,10 +275,10 @@ fn prepare_btc_refund() -> (
fn btc_submarine_refund_size() {
let (_test_framework, _scan_request, swap_tx, sender_keypair, _utxos) = prepare_btc_refund();

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

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

Expand Down Expand Up @@ -441,17 +439,20 @@ 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 (
_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();
let coop_claim_tx_size = swap_tx.size(&recvr_keypair, 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();
let non_coop_claim_tx_size = swap_tx.size(&recvr_keypair, false, true).unwrap();
assert_eq!(non_coop_claim_tx_size, 221);
}

Expand Down Expand Up @@ -590,10 +591,10 @@ fn lbtc_submarine_refund_size() {
let (_test_framework, swap_tx, sender_keypair, _blinding_keypair, _swap_addrs, _utxos) =
prepare_lbtc_refund();

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

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

Expand Down

0 comments on commit 622eac3

Please sign in to comment.