From 8cba7a4861b0e8461b19043770cf13430e842bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Tue, 21 Jan 2025 14:56:26 +0100 Subject: [PATCH] add WitnessOrd::Ignored --- rgb-core | 2 +- rgb-std | 2 +- tests/transfers.rs | 110 +++++++++++++++++++++++++++++++++-------- tests/utils/helpers.rs | 14 ++++-- 4 files changed, 103 insertions(+), 25 deletions(-) diff --git a/rgb-core b/rgb-core index 4990bb8..d647635 160000 --- a/rgb-core +++ b/rgb-core @@ -1 +1 @@ -Subproject commit 4990bb82e8ed9d167098680593f50b90f1b7f1dc +Subproject commit d647635a978b1f74da70d11d15c668d9254324b1 diff --git a/rgb-std b/rgb-std index 45ad2d7..3f79897 160000 --- a/rgb-std +++ b/rgb-std @@ -1 +1 @@ -Subproject commit 45ad2d79c1397164f080cf90799c60ab4e2da7aa +Subproject commit 3f798976792f25b375842f24a28d1457ad045b2a diff --git a/tests/transfers.rs b/tests/transfers.rs index 7bb5766..e63fa16 100644 --- a/tests/transfers.rs +++ b/tests/transfers.rs @@ -736,7 +736,6 @@ fn accept_0conf() { #[cfg(not(feature = "altered"))] #[rstest] #[case(false)] -#[ignore = "fix needed"] // https://github.com/RGB-WG/rgb-std/issues/292 #[case(true)] fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { initialize(); @@ -745,21 +744,39 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { let wlt_2 = get_wallet(&DescriptorType::Wpkh); let pre_funding_height = get_height(); - let utxo = wlt_1.get_utxo(Some(10_000)); - let (contract_id, iface_type_name) = wlt_1.issue_nia(600, wlt_1.close_method(), Some(&utxo)); + let utxo_1 = wlt_1.get_utxo(Some(10_000)); + let utxo_2 = wlt_1.get_utxo(Some(20_000)); + let amounts = vec![600, 600]; + let outpoints = vec![Some(utxo_1), Some(utxo_2)]; + let asset_info = AssetInfo::default_nia(amounts.clone()); + let (contract_id, iface_type_name) = + wlt_1.issue_with_info(asset_info, wlt_1.close_method(), outpoints); + + struct LNFasciaResolver {} + impl ResolveWitness for LNFasciaResolver { + fn resolve_pub_witness(&self, _: XWitnessId) -> Result { + unreachable!() + } + fn resolve_pub_witness_ord( + &self, + _: XWitnessId, + ) -> Result { + Ok(WitnessOrd::Ignored) + } + } println!("\n1. fake commitment TX (no HTLCs)"); let beneficiaries = vec![ (wlt_2.get_address(), Some(2000)), (wlt_1.get_address(), None), ]; - let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo], beneficiaries, None); + let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo_1], beneficiaries, None); let coloring_info = ColoringInfo { asset_info_map: HashMap::from([( contract_id, AssetColoringInfo { iface: iface_type_name.clone(), - input_outpoints: vec![utxo], + input_outpoints: vec![utxo_1], output_map: HashMap::from([(0, 100), (1, 500)]), static_blinding: Some(666), }, @@ -768,7 +785,7 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX - 1), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info.clone()); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); let htlc_vout = 2; @@ -785,13 +802,13 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { (wlt_1.get_address(), None), (htlc_derived_addr.addr, Some(htlc_btc_amt)), ]; - let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo], beneficiaries, None); + let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo_1], beneficiaries, None); let coloring_info = ColoringInfo { asset_info_map: HashMap::from([( contract_id, AssetColoringInfo { iface: iface_type_name.clone(), - input_outpoints: vec![utxo], + input_outpoints: vec![utxo_1], output_map: HashMap::from([(0, 100), (1, 300), (htlc_vout, htlc_rgb_amt)]), static_blinding: Some(666), }, @@ -800,7 +817,7 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX - 1), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); if update_witnesses_before_htlc { @@ -831,7 +848,7 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); println!("\n4. fake commitment TX (no HTLCs)"); @@ -839,13 +856,13 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { (wlt_2.get_address(), Some(3000)), (wlt_1.get_address(), None), ]; - let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo], beneficiaries, None); + let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo_1], beneficiaries, None); let coloring_info = ColoringInfo { asset_info_map: HashMap::from([( contract_id, AssetColoringInfo { iface: iface_type_name.clone(), - input_outpoints: vec![utxo], + input_outpoints: vec![utxo_1], output_map: HashMap::from([(0, 100), (1, 500)]), static_blinding: Some(666), }, @@ -854,7 +871,7 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX - 1), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); let mut old_psbt = psbt.clone(); @@ -865,13 +882,13 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { (wlt_1.get_address(), None), (htlc_derived_addr.addr, Some(htlc_btc_amt)), ]; - let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo], beneficiaries, None); + let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo_1], beneficiaries, None); let coloring_info = ColoringInfo { asset_info_map: HashMap::from([( contract_id, AssetColoringInfo { iface: iface_type_name.clone(), - input_outpoints: vec![utxo], + input_outpoints: vec![utxo_1], output_map: HashMap::from([(0, 122), (1, 298), (htlc_vout, htlc_rgb_amt)]), static_blinding: Some(666), }, @@ -880,7 +897,7 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX - 1), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info.clone()); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); if update_witnesses_before_htlc { @@ -911,17 +928,43 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { nonce: Some(u64::MAX), }; let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); - wlt_1.consume_fascia(fascia.clone(), psbt.txid()); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); // no problem: since the force-close tx will be updated to mined soon wlt_1.sync_and_update_witnesses(Some(pre_funding_height)); - println!("\n7. broadcast old PSBT"); + println!("\n7. fake commitment TX (1 HTLC) on 2nd channel"); + let beneficiaries = vec![ + (wlt_2.get_address(), Some(2000)), + (wlt_1.get_address(), None), + (htlc_derived_addr.addr, Some(htlc_btc_amt)), + ]; + let (mut psbt, _meta) = wlt_1.construct_psbt(vec![utxo_2], beneficiaries, None); + let coloring_info = ColoringInfo { + asset_info_map: HashMap::from([( + contract_id, + AssetColoringInfo { + iface: iface_type_name.clone(), + input_outpoints: vec![utxo_2], + output_map: HashMap::from([(0, 100), (1, 300), (htlc_vout, htlc_rgb_amt)]), + static_blinding: Some(666), + }, + )]), + static_blinding: Some(666), + nonce: Some(u64::MAX - 1), + }; + let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); + wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); + + println!("\n8. broadcast old PSBT"); let tx = wlt_1.sign_finalize_extract(&mut old_psbt); wlt_1.broadcast_tx(&tx); - wlt_1.mine_tx(&tx.txid(), false); - wlt_1.sync_and_update_witnesses(Some(pre_funding_height)); + let txid = tx.txid(); + wlt_1.mine_tx(&txid, false); + wlt_1.sync(); + wlt_1.update_witnesses(pre_funding_height, vec![XChain::Bitcoin(txid)]); let mut wlt_3 = get_wallet(&DescriptorType::Wpkh); wlt_1.send( &mut wlt_3, @@ -932,6 +975,33 @@ fn ln_transfers(#[case] update_witnesses_before_htlc: bool) { 1000, None, ); + + println!("\n9. fake HTLC TX on 2nd channel"); + let witness_id = fascia.witness_id(); + let txid = witness_id.as_reduced_unsafe(); + let input_outpoint = Outpoint::new(*txid, htlc_vout); + let beneficiaries = vec![(wlt_1.get_address(), None)]; + let (mut psbt, _meta) = wlt_1.construct_psbt_offchain( + vec![(input_outpoint, htlc_btc_amt, htlc_derived_addr.terminal)], + beneficiaries, + None, + ); + let coloring_info = ColoringInfo { + asset_info_map: HashMap::from([( + contract_id, + AssetColoringInfo { + iface: iface_type_name.clone(), + input_outpoints: vec![input_outpoint], + output_map: HashMap::from([(0, htlc_rgb_amt)]), + static_blinding: Some(666), + }, + )]), + static_blinding: Some(666), + nonce: Some(u64::MAX), + }; + let (fascia, _asset_beneficiaries) = wlt_1.color_psbt(&mut psbt, coloring_info); + wlt_1.consume_fascia_custom_resolver(fascia.clone(), LNFasciaResolver {}); + wlt_1.debug_logs(contract_id, &iface_type_name, AllocationFilter::WalletAll); } #[cfg(not(feature = "altered"))] diff --git a/tests/utils/helpers.rs b/tests/utils/helpers.rs index 4680bdf..415fb28 100644 --- a/tests/utils/helpers.rs +++ b/tests/utils/helpers.rs @@ -743,7 +743,7 @@ impl TestWallet { pub fn sync_and_update_witnesses(&mut self, after_height: Option) { self.sync(); - self.update_witnesses(after_height.unwrap_or(1)); + self.update_witnesses(after_height.unwrap_or(1), vec![]); } pub fn switch_to_instance(&mut self, instance: u8) { @@ -1643,17 +1643,25 @@ impl TestWallet { witness_id: XChain::Bitcoin(witness_txid), }; + self.consume_fascia_custom_resolver(fascia, resolver); + } + + pub fn consume_fascia_custom_resolver( + &mut self, + fascia: Fascia, + resolver: impl ResolveWitness, + ) { self.wallet .stock_mut() .consume_fascia(fascia, resolver) .unwrap(); } - pub fn update_witnesses(&mut self, after_height: u32) { + pub fn update_witnesses(&mut self, after_height: u32, force_witnesses: Vec) { let resolver = self.get_resolver(); self.wallet .stock_mut() - .update_witnesses(resolver, after_height) + .update_witnesses(resolver, after_height, force_witnesses) .unwrap(); }