Skip to content

Commit

Permalink
Merge pull request #1532 from dorianvp/test-vts-in-tx
Browse files Browse the repository at this point in the history
Test ValueTransfers order inside a Transaction
  • Loading branch information
dorianvp authored Nov 19, 2024
2 parents a5a17af + ebe1418 commit bc3c525
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
67 changes: 66 additions & 1 deletion libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ mod fast {
use zingo_status::confirmation_status::ConfirmationStatus;
use zingolib::{
config::ZENNIES_FOR_ZINGO_REGTEST_ADDRESS,
testutils::lightclient::{from_inputs, get_base_address},
testutils::{
chain_generics::{conduct_chain::ConductChain, libtonode::LibtonodeEnvironment},
lightclient::{from_inputs, get_base_address},
},
utils::conversion::txid_from_hex_encoded_str,
wallet::{
data::summaries::{SelfSendValueTransfer, SentValueTransfer, ValueTransferKind},
Expand Down Expand Up @@ -379,6 +382,68 @@ mod fast {
.all(|pair| { pair[0].blockheight() >= pair[1].blockheight() }));
}

/// Tests that value transfers are properly sorted by block height and index.
/// It also tests that retrieving the value transfers multiple times in a row returns the same results.
#[tokio::test]
async fn value_transfers() {
let mut environment = LibtonodeEnvironment::setup().await;

let faucet = environment.create_faucet().await;
let recipient = environment.create_client().await;

environment.bump_chain().await;
faucet.do_sync(false).await.unwrap();

check_client_balances!(faucet, o: 0 s: 2_500_000_000u64 t: 0u64);

from_inputs::quick_send(
&faucet,
vec![
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some("Message #1"),
),
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some("Message #2"),
),
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some("Message #3"),
),
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some("Message #4"),
),
],
)
.await
.unwrap();

environment.bump_chain().await;
recipient.do_sync(false).await.unwrap();

let value_transfers = &recipient.value_transfers(true).await;
let value_transfers1 = &recipient.value_transfers(true).await;
let value_transfers2 = &recipient.value_transfers(true).await;
let mut value_transfers3 = recipient.value_transfers(false).await;
let mut value_transfers4 = recipient.value_transfers(false).await;

assert_eq!(value_transfers.0[0].memos().len(), 4);

value_transfers3.0.reverse();
value_transfers4.0.reverse();

assert_eq!(value_transfers, value_transfers1);
assert_eq!(value_transfers, value_transfers2);
assert_eq!(value_transfers.0, value_transfers3.0);
assert_eq!(value_transfers.0, value_transfers4.0);
}

pub mod tex {
use super::*;
fn first_taddr_to_tex(client: &LightClient) -> ZcashAddress {
Expand Down
9 changes: 4 additions & 5 deletions zingolib/src/lightclient/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,7 @@ impl LightClient {
let mut value_transfers: Vec<ValueTransfer> = Vec::new();
let summaries = self.transaction_summaries().await;

let transaction_summaries = if newer_first {
Box::new(summaries.iter().rev()) as Box<dyn Iterator<Item = &TransactionSummary>>
} else {
Box::new(summaries.iter()) as Box<dyn Iterator<Item = &TransactionSummary>>
};
let transaction_summaries = summaries.iter();

for tx in transaction_summaries {
match tx.kind() {
Expand Down Expand Up @@ -547,6 +543,9 @@ impl LightClient {
}
};
}
if newer_first {
value_transfers.reverse();
}

ValueTransfers(value_transfers)
}
Expand Down
40 changes: 22 additions & 18 deletions zingolib/src/testutils/chain_generics/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,29 @@ where
.await;

assert_eq!(sender.value_transfers(true).await.0.len(), 3);
assert_eq!(
sender.value_transfers(true).await.0[0].kind(),
ValueTransferKind::Sent(SentValueTransfer::Send)
);
assert_eq!(
sender.value_transfers(true).await.0[1].kind(),
ValueTransferKind::Sent(SentValueTransfer::SendToSelf(
SelfSendValueTransfer::MemoToSelf
))
);
assert_eq!(
sender.value_transfers(true).await.0[2].kind(),
ValueTransferKind::Received
);

assert!(sender
.value_transfers(false)
.await
.0
.iter()
.any(|vt| { vt.kind() == ValueTransferKind::Received }));

assert!(sender
.value_transfers(false)
.await
.0
.iter()
.any(|vt| { vt.kind() == ValueTransferKind::Sent(SentValueTransfer::Send) }));

assert!(sender.value_transfers(false).await.0.iter().any(|vt| {
vt.kind()
== ValueTransferKind::Sent(SentValueTransfer::SendToSelf(
SelfSendValueTransfer::MemoToSelf,
))
}));

assert_eq!(recipient.value_transfers(true).await.0.len(), 1);
assert_eq!(
recipient.value_transfers(true).await.0[0].kind(),
ValueTransferKind::Received
);

with_assertions::propose_send_bump_sync_all_recipients(
&mut environment,
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ pub mod summaries {
addresses.insert(address, outgoing_tx_data.output_index);
});
let mut addresses_vec = addresses.into_iter().collect::<Vec<_>>();
addresses_vec.sort_by_key(|x| x.1);
addresses_vec.sort_by_key(|(_address, output_index)| *output_index);
addresses_vec.iter().for_each(|(address, _output_index)| {
let outgoing_data_to_address: Vec<OutgoingTxData> = transaction_summary
.outgoing_tx_data()
Expand Down

0 comments on commit bc3c525

Please sign in to comment.