Skip to content

Commit

Permalink
accept_0conf: uncomment code and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Nov 24, 2024
1 parent ae69d03 commit 7c873b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
9 changes: 3 additions & 6 deletions tests/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Rgb20>(contract_id);
let spec = contract.spec();
assert_eq!(spec.ticker.to_string(), ticker.to_string());
Expand All @@ -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);
Expand Down Expand Up @@ -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::<Rgb21>(contract_id);
let spec = contract.spec();
assert_eq!(spec.ticker.to_string(), ticker.to_string());
Expand All @@ -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);
Expand Down Expand Up @@ -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::<Rgb25>(contract_id);
assert_eq!(contract.name().to_string(), name.to_string());
assert_eq!(
Expand All @@ -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);
Expand Down
35 changes: 26 additions & 9 deletions tests/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FungibleAllocation> = 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,
);
}
Expand Down
47 changes: 35 additions & 12 deletions tests/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ pub enum AllocationFilter {
Stock,
Wallet,
WalletAll,
WalletTentative,
}

enum Filter<'w> {
NoWallet,
Wallet(&'w RgbWallet<Wallet<XpubDerivable, RgbDescr>>),
WalletAll(&'w RgbWallet<Wallet<XpubDerivable, RgbDescr>>),
WalletTentative(&'w RgbWallet<Wallet<XpubDerivable, RgbDescr>>),
}

impl<'w> AssignmentsFilter for Filter<'w> {
Expand All @@ -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,
}
}
Expand All @@ -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",
}
}
Expand Down Expand Up @@ -957,21 +971,29 @@ impl TestWallet {
.unwrap()
}

pub fn contract_fungible_allocations<S: ContractStateRead>(
pub fn contract_fungible_allocations(
&self,
contract_iface: &ContractIface<S>,
contract_id: ContractId,
iface_type_name: &TypeName,
show_tentative: bool,
) -> Vec<FungibleAllocation> {
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<S: ContractStateRead>(
pub fn contract_data_allocations(
&self,
contract_iface: &ContractIface<S>,
contract_id: ContractId,
iface_type_name: &TypeName,
) -> Vec<DataAllocation> {
contract_iface
self.contract_iface(contract_id, iface_type_name)
.data(fname!("assetOwner"), Filter::Wallet(&self.wallet))
.unwrap()
.collect()
Expand All @@ -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,
};
Expand Down Expand Up @@ -1126,10 +1149,10 @@ impl TestWallet {
expected_fungible_allocations: Vec<u64>,
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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 7c873b5

Please sign in to comment.