From 7c873b51cb6f8b2bfed616ba648c2f7bced25470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Wed, 11 Sep 2024 11:49:39 +0200 Subject: [PATCH] accept_0conf: uncomment code and improve --- tests/issuance.rs | 9 +++----- tests/transfers.rs | 35 +++++++++++++++++++++++-------- tests/utils/helpers.rs | 47 +++++++++++++++++++++++++++++++----------- tests/utils/mod.rs | 2 +- 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/tests/issuance.rs b/tests/issuance.rs index 4742615..9f99c8e 100644 --- a/tests/issuance.rs +++ b/tests/issuance.rs @@ -43,7 +43,6 @@ fn issue_nia(wallet_desc: DescriptorType, close_method: CloseMethod) { ); let (contract_id, iface_type_name) = wallet.issue_with_info(asset_info, close_method, None); - let contract_iface = wallet.contract_iface(contract_id, &iface_type_name); let contract = wallet.contract_iface_class::(contract_id); let spec = contract.spec(); assert_eq!(spec.ticker.to_string(), ticker.to_string()); @@ -59,7 +58,7 @@ fn issue_nia(wallet_desc: DescriptorType, close_method: CloseMethod) { ); assert_eq!(contract.total_issued_supply().value(), issued_supply); - let allocations = wallet.contract_fungible_allocations(&contract_iface); + let allocations = wallet.contract_fungible_allocations(contract_id, &iface_type_name, false); assert_eq!(allocations.len(), 1); let allocation = allocations[0]; assert_eq!(allocation.seal.method(), close_method); @@ -117,7 +116,6 @@ fn issue_uda(wallet_desc: DescriptorType, close_method: CloseMethod) { ); let (contract_id, iface_type_name) = wallet.issue_with_info(asset_info, close_method, None); - let contract_iface = wallet.contract_iface(contract_id, &iface_type_name); let contract = wallet.contract_iface_class::(contract_id); let spec = contract.spec(); assert_eq!(spec.ticker.to_string(), ticker.to_string()); @@ -144,7 +142,7 @@ fn issue_uda(wallet_desc: DescriptorType, close_method: CloseMethod) { ); assert_eq!(token_data.reserves.unwrap(), token_data_reserves); - let allocations = wallet.contract_data_allocations(&contract_iface); + let allocations = wallet.contract_data_allocations(contract_id, &iface_type_name); assert_eq!(allocations.len(), 1); let allocation = &allocations[0]; assert_eq!(allocation.seal.method(), close_method); @@ -175,7 +173,6 @@ fn issue_cfa(wallet_desc: DescriptorType, close_method: CloseMethod) { ); let (contract_id, iface_type_name) = wallet.issue_with_info(asset_info, close_method, None); - let contract_iface = wallet.contract_iface(contract_id, &iface_type_name); let contract = wallet.contract_iface_class::(contract_id); assert_eq!(contract.name().to_string(), name.to_string()); assert_eq!( @@ -193,7 +190,7 @@ fn issue_cfa(wallet_desc: DescriptorType, close_method: CloseMethod) { ); assert_eq!(contract.total_issued_supply().value(), issued_supply); - let allocations = wallet.contract_fungible_allocations(&contract_iface); + let allocations = wallet.contract_fungible_allocations(contract_id, &iface_type_name, false); assert_eq!(allocations.len(), 1); let allocation = allocations[0]; assert_eq!(allocation.seal.method(), close_method); diff --git a/tests/transfers.rs b/tests/transfers.rs index c246241..63e4a7d 100644 --- a/tests/transfers.rs +++ b/tests/transfers.rs @@ -495,26 +495,43 @@ fn accept_0conf() { wlt_2.close_method(), InvoiceType::Witness, ); - let (consignment, _) = wlt_1.transfer(invoice.clone(), None, None, true, None); + let (consignment, tx) = wlt_1.transfer(invoice.clone(), None, None, true, None); + let txid = tx.txid(); wlt_2.accept_transfer(consignment.clone(), None); - // TODO: check if it's correct that sender sees 2 allocations - /* - wlt_1.sync(); - wlt_1.check_allocations( + // wlt_2 sees the allocation even if TX has not been mined + wlt_2.check_allocations( contract_id, &iface_type_name, AssetSchema::Nia, - vec![issue_supply - amt], + vec![amt], false, ); - */ - wlt_2.check_allocations( + + wlt_1.sync(); + + let wlt_1_change_amt = issue_supply - amt; + + // wlt_1 needs to get tentative allocations to see its change from the unmined TX + let allocations: Vec = wlt_1 + .contract_fungible_allocations(contract_id, &iface_type_name, true) + .into_iter() + .filter(|fa| fa.seal.txid() == Some(txid)) + .collect(); + assert_eq!(allocations.len(), 1); + assert!(allocations + .iter() + .any(|fa| fa.state == Amount::from(wlt_1_change_amt))); + + // after mining, wlt_1 doesn't need to get tentative allocations to see the change + mine(false); + wlt_1.sync(); + wlt_1.check_allocations( contract_id, &iface_type_name, AssetSchema::Nia, - vec![amt], + vec![wlt_1_change_amt], false, ); } diff --git a/tests/utils/helpers.rs b/tests/utils/helpers.rs index 8978ba6..2dfe3e6 100644 --- a/tests/utils/helpers.rs +++ b/tests/utils/helpers.rs @@ -16,12 +16,14 @@ pub enum AllocationFilter { Stock, Wallet, WalletAll, + WalletTentative, } enum Filter<'w> { NoWallet, Wallet(&'w RgbWallet>), WalletAll(&'w RgbWallet>), + WalletTentative(&'w RgbWallet>), } impl<'w> AssignmentsFilter for Filter<'w> { @@ -31,6 +33,10 @@ impl<'w> AssignmentsFilter for Filter<'w> { .wallet() .filter_unspent() .should_include(outpoint, id), + Filter::WalletTentative(wallet) => wallet + .wallet() + .filter_outpoints() + .should_include(outpoint, id), _ => true, } } @@ -43,8 +49,16 @@ impl<'w> Filter<'w> { .expect("liquid is not yet supported"); match self { Filter::Wallet(rgb) if rgb.wallet().is_unspent(outpoint) => "", - Filter::WalletAll(rgb) if rgb.wallet().is_unspent(outpoint) => "-- unspent", - Filter::WalletAll(rgb) if rgb.wallet().has_outpoint(outpoint) => "-- spent", + Filter::WalletAll(rgb) | Filter::WalletTentative(rgb) + if rgb.wallet().is_unspent(outpoint) => + { + "-- unspent" + } + Filter::WalletAll(rgb) | Filter::WalletTentative(rgb) + if rgb.wallet().has_outpoint(outpoint) => + { + "-- spent" + } _ => "-- third-party", } } @@ -957,21 +971,29 @@ impl TestWallet { .unwrap() } - pub fn contract_fungible_allocations( + pub fn contract_fungible_allocations( &self, - contract_iface: &ContractIface, + contract_id: ContractId, + iface_type_name: &TypeName, + show_tentative: bool, ) -> Vec { - contract_iface - .fungible(fname!("assetOwner"), Filter::Wallet(&self.wallet)) + let filter = if show_tentative { + Filter::WalletTentative(&self.wallet) + } else { + Filter::Wallet(&self.wallet) + }; + self.contract_iface(contract_id, iface_type_name) + .fungible(fname!("assetOwner"), filter) .unwrap() .collect() } - pub fn contract_data_allocations( + pub fn contract_data_allocations( &self, - contract_iface: &ContractIface, + contract_id: ContractId, + iface_type_name: &TypeName, ) -> Vec { - contract_iface + self.contract_iface(contract_id, iface_type_name) .data(fname!("assetOwner"), Filter::Wallet(&self.wallet)) .unwrap() .collect() @@ -985,6 +1007,7 @@ impl TestWallet { ) { let filter = match filter { AllocationFilter::WalletAll => Filter::WalletAll(&self.wallet), + AllocationFilter::WalletTentative => Filter::WalletTentative(&self.wallet), AllocationFilter::Wallet => Filter::Wallet(&self.wallet), AllocationFilter::Stock => Filter::NoWallet, }; @@ -1126,10 +1149,10 @@ impl TestWallet { expected_fungible_allocations: Vec, nonfungible_allocation: bool, ) { - let contract_iface = self.contract_iface(contract_id, iface_type_name); match asset_schema { AssetSchema::Nia | AssetSchema::Cfa => { - let allocations = self.contract_fungible_allocations(&contract_iface); + let allocations = + self.contract_fungible_allocations(contract_id, iface_type_name, false); assert_eq!(allocations.len(), expected_fungible_allocations.len()); assert!(allocations .iter() @@ -1145,7 +1168,7 @@ impl TestWallet { } } AssetSchema::Uda => { - let allocations = self.contract_data_allocations(&contract_iface); + let allocations = self.contract_data_allocations(contract_id, iface_type_name); let expected_allocations = if nonfungible_allocation { assert_eq!( allocations diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index 735ac7c..a70f820 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -65,7 +65,7 @@ pub use rand::RngCore; pub use rgb::{ interface::AssignmentsFilter, invoice::Pay2Vout, - persistence::{ContractStateRead, MemContract, MemContractState, Stock}, + persistence::{MemContract, MemContractState, Stock}, resolvers::AnyResolver, stl::ContractTerms, validation::{Failure, ResolveWitness, Scripts, Validity, WitnessResolverError},