Skip to content

Commit

Permalink
Merge pull request #1525 from dorianvp/fix-vt-message-order
Browse files Browse the repository at this point in the history
Restore order for value_transfers and messages
  • Loading branch information
zancas authored Nov 18, 2024
2 parents acc422d + fdc6ba7 commit cceca3e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 69 deletions.
46 changes: 23 additions & 23 deletions darkside-tests/tests/advanced_reorg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async fn reorg_changes_incoming_tx_height() {
}
);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -93,7 +93,7 @@ async fn reorg_changes_incoming_tx_height() {
}
);

let after_reorg_transactions = light_client.value_transfers().await.0;
let after_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(after_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -214,7 +214,7 @@ async fn reorg_changes_incoming_tx_index() {
}
);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -249,7 +249,7 @@ async fn reorg_changes_incoming_tx_index() {
}
);

let after_reorg_transactions = light_client.value_transfers().await.0;
let after_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(after_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -369,7 +369,7 @@ async fn reorg_expires_incoming_tx() {
}
);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -404,7 +404,7 @@ async fn reorg_expires_incoming_tx() {
}
);

let after_reorg_transactions = light_client.value_transfers().await.0;
let after_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(after_reorg_transactions.len(), 0);
}
Expand Down Expand Up @@ -547,7 +547,7 @@ async fn reorg_changes_outgoing_tx_height() {
}
);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -597,11 +597,11 @@ async fn reorg_changes_outgoing_tx_height() {
// check that the outgoing transaction has the correct height before
// the reorg is triggered

println!("{:?}", light_client.value_transfers().await);
println!("{:?}", light_client.value_transfers(true).await);

assert_eq!(
light_client
.value_transfers()
.value_transfers(true)
.await
.iter()
.find_map(|v| match v.kind() {
Expand Down Expand Up @@ -661,18 +661,18 @@ async fn reorg_changes_outgoing_tx_height() {
expected_after_reorg_balance
);

let after_reorg_transactions = light_client.value_transfers().await.0;
let after_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(after_reorg_transactions.len(), 3);

println!("{:?}", light_client.value_transfers().await);
println!("{:?}", light_client.value_transfers(true).await);

// FIXME: This test is broken because if this issue
// https://github.com/zingolabs/zingolib/issues/622
// verify that the reorged transaction is in the new height
// assert_eq!(
// light_client
// .list_value_transfers()
// .list_value_transfers(true)
// .await
// .into_iter()
// .find_map(|v| match v.kind {
Expand Down Expand Up @@ -786,7 +786,7 @@ async fn reorg_expires_outgoing_tx_height() {
light_client.do_sync(true).await.unwrap();
assert_eq!(light_client.do_balance().await, expected_initial_balance);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -829,10 +829,10 @@ async fn reorg_expires_outgoing_tx_height() {
// check that the outgoing transaction has the correct height before
// the reorg is triggered

println!("{:?}", light_client.value_transfers().await);
println!("{:?}", light_client.value_transfers(true).await);

let send_height = light_client
.value_transfers()
.value_transfers(true)
.await
.iter()
.find_map(|v| match v.kind() {
Expand Down Expand Up @@ -872,18 +872,18 @@ async fn reorg_expires_outgoing_tx_height() {
// sent transaction was never mined and has expired.
assert_eq!(light_client.do_balance().await, expected_initial_balance);

let after_reorg_transactions = light_client.value_transfers().await.0;
let after_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(after_reorg_transactions.len(), 1);

println!("{:?}", light_client.value_transfers().await);
println!("{:?}", light_client.value_transfers(true).await);

// FIXME: This test is broken because if this issue
// https://github.com/zingolabs/zingolib/issues/622
// verify that the reorged transaction is in the new height
// assert_eq!(
// light_client
// .list_value_transfers()
// .list_value_transfers(true)
// .await
// .into_iter()
// .find_map(|v| match v.kind {
Expand Down Expand Up @@ -964,7 +964,7 @@ async fn reorg_changes_outgoing_tx_index() {
}
);

let before_reorg_transactions = light_client.value_transfers().await.0;
let before_reorg_transactions = light_client.value_transfers(true).await.0;

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -1016,7 +1016,7 @@ async fn reorg_changes_outgoing_tx_index() {

assert_eq!(
light_client
.value_transfers()
.value_transfers(true)
.await
.iter()
.find_map(|v| match v.kind() {
Expand All @@ -1039,7 +1039,7 @@ async fn reorg_changes_outgoing_tx_index() {
);

println!("pre re-org value transfers:");
println!("{}", light_client.value_transfers().await);
println!("{}", light_client.value_transfers(true).await);
println!("pre re-org tx summaries:");
println!("{}", light_client.transaction_summaries().await);

Expand Down Expand Up @@ -1087,7 +1087,7 @@ async fn reorg_changes_outgoing_tx_index() {
expected_after_reorg_balance
);

let after_reorg_transactions = light_client.value_transfers().await;
let after_reorg_transactions = light_client.value_transfers(true).await;

println!("post re-org value transfers:");
println!("{}", after_reorg_transactions);
Expand All @@ -1102,7 +1102,7 @@ async fn reorg_changes_outgoing_tx_index() {
// verify that the reorged transaction is in the new height
// assert_eq!(
// light_client
// .list_value_transfers()
// .list_value_transfers(true)
// .await
// .into_iter()
// .find_map(|v| match v.kind {
Expand Down
2 changes: 1 addition & 1 deletion darkside-tests/tests/network_interruption_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async fn shielded_note_marked_as_change_test() {
json::stringify_pretty(scenario.get_lightclient(0).do_list_notes(true).await, 4)
);
println!("do list tx summaries:");
dbg!(scenario.get_lightclient(0).value_transfers().await);
dbg!(scenario.get_lightclient(0).value_transfers(true).await);

// assert the balance is correct
assert_eq!(
Expand Down
65 changes: 44 additions & 21 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod fast {
.await
.unwrap();

let value_transfers = &recipient.value_transfers().await;
let value_transfers = &recipient.value_transfers(true).await;

dbg!(value_transfers);

Expand All @@ -173,6 +173,18 @@ mod fast {
&& vt.recipient_address() == Some(ZENNIES_FOR_ZINGO_REGTEST_ADDRESS)));
}

/// Test sending and receiving messages between three parties.
///
/// This test case consists of the following sequence of events:
///
/// 1. Alice sends a message to Bob.
/// 2. Alice sends another message to Bob.
/// 3. Bob sends a message to Alice.
/// 4. Alice sends a message to Charlie.
/// 5. Charlie sends a message to Alice.
///
/// After the messages are sent, the test checks that the `messages_containing` method
/// returns the expected messages for each party in the correct order.
#[tokio::test]
async fn message_thread() {
let (regtest_manager, _cph, faucet, recipient, _txid) =
Expand Down Expand Up @@ -205,12 +217,6 @@ mod fast {
)
.unwrap();

println!(
"Addresses: {}, {}",
alice,
bob.encode(&faucet.config().chain),
);

let alice_to_bob = TransactionRequest::new(vec![Payment::new(
ZcashAddress::from_str(&bob.encode(&faucet.config().chain)).unwrap(),
NonNegativeAmount::from_u64(1_000).unwrap(),
Expand Down Expand Up @@ -348,12 +354,29 @@ mod fast {
let value_transfers_charlie = &recipient
.messages_containing(Some(&charlie.encode(&recipient.config().chain)))
.await;
let all_vts = &recipient.messages_containing(None).await;

println!("ALL VTS");
dbg!(all_vts);
let all_vts = &recipient.value_transfers(true).await;
let all_messages = &recipient.messages_containing(None).await;

for vt in all_vts.0.iter() {
dbg!(vt.blockheight());
}

assert_eq!(value_transfers_bob.0.len(), 3);
assert_eq!(value_transfers_charlie.0.len(), 2);

// Also asserting the order now (sorry juanky)
// ALL MESSAGES (First one should be the oldest one)
assert!(all_messages
.0
.windows(2)
.all(|pair| { pair[0].blockheight() <= pair[1].blockheight() }));

// ALL VTS (First one should be the most recent one)
assert!(all_vts
.0
.windows(2)
.all(|pair| { pair[0].blockheight() >= pair[1].blockheight() }));
}

pub mod tex {
Expand Down Expand Up @@ -416,10 +439,10 @@ mod fast {
.len(),
3usize
);
let val_tranfers = dbg!(sender.value_transfers().await);
let val_tranfers = dbg!(sender.value_transfers(true).await);
// This fails, as we don't scan sends to tex correctly yet
assert_eq!(
val_tranfers.0[2].recipient_address().unwrap(),
val_tranfers.0[0].recipient_address().unwrap(),
tex_addr_from_first.encode()
);
}
Expand Down Expand Up @@ -953,7 +976,7 @@ mod slow {
);
println!(
"{}",
JsonValue::from(recipient.value_transfers().await).pretty(4)
JsonValue::from(recipient.value_transfers(true).await).pretty(4)
);
}
#[tokio::test]
Expand Down Expand Up @@ -1372,7 +1395,7 @@ mod slow {
println!("{}", recipient.do_list_transactions().await.pretty(2));
println!(
"{}",
JsonValue::from(recipient.value_transfers().await).pretty(2)
JsonValue::from(recipient.value_transfers(true).await).pretty(2)
);
recipient.do_rescan().await.unwrap();
println!(
Expand All @@ -1382,7 +1405,7 @@ mod slow {
println!("{}", recipient.do_list_transactions().await.pretty(2));
println!(
"{}",
JsonValue::from(recipient.value_transfers().await).pretty(2)
JsonValue::from(recipient.value_transfers(true).await).pretty(2)
);
// TODO: Add asserts!
}
Expand Down Expand Up @@ -1805,7 +1828,7 @@ mod slow {

println!(
"{}",
JsonValue::from(faucet.value_transfers().await).pretty(4)
JsonValue::from(faucet.value_transfers(true).await).pretty(4)
);
println!(
"{}",
Expand Down Expand Up @@ -2422,10 +2445,10 @@ mod slow {
.await
.unwrap();
let pre_rescan_transactions = recipient.do_list_transactions().await;
let pre_rescan_summaries = recipient.value_transfers().await;
let pre_rescan_summaries = recipient.value_transfers(true).await;
recipient.do_rescan().await.unwrap();
let post_rescan_transactions = recipient.do_list_transactions().await;
let post_rescan_summaries = recipient.value_transfers().await;
let post_rescan_summaries = recipient.value_transfers(true).await;
assert_eq!(pre_rescan_transactions, post_rescan_transactions);
assert_eq!(pre_rescan_summaries, post_rescan_summaries);
let mut outgoing_metadata = pre_rescan_transactions
Expand Down Expand Up @@ -3712,7 +3735,7 @@ mod slow {
zingolib::testutils::increase_server_height(&regtest_manager, 1).await;

let _synciiyur = recipient.do_sync(false).await;
// let summ_sim = recipient.list_value_transfers().await;
// let summ_sim = recipient.list_value_transfers(true).await;
let bala_sim = recipient.do_balance().await;

recipient.clear_state().await;
Expand All @@ -3731,10 +3754,10 @@ mod slow {
}
}

// let summ_int = recipient.list_value_transfers().await;
// let summ_int = recipient.list_value_transfers(true).await;
// let bala_int = recipient.do_balance().await;
let _synciiyur = recipient.do_sync(false).await;
// let summ_syn = recipient.list_value_transfers().await;
// let summ_syn = recipient.list_value_transfers(true).await;
let bala_syn = recipient.do_balance().await;

dbg!(
Expand Down
12 changes: 9 additions & 3 deletions zingolib/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ impl Command for ValueTransfersCommand {
A value transfer is a group of all notes to a specific receiver in a transaction.
Usage:
valuetransfers
valuetransfers [bool]
"#}
}

Expand All @@ -1278,12 +1278,18 @@ impl Command for ValueTransfersCommand {
}

fn exec(&self, args: &[&str], lightclient: &LightClient) -> String {
if !args.is_empty() {
if args.len() > 1 {
return "Error: invalid arguments\nTry 'help valuetransfers' for correct usage and examples"
.to_string();
}

RT.block_on(async move { format!("{}", lightclient.value_transfers().await) })
let newer_first = args
.first()
.map(|s| s.parse())
.unwrap_or(Ok(true))
.unwrap_or(true);

RT.block_on(async move { format!("{}", lightclient.value_transfers(newer_first).await) })
}
}

Expand Down
Loading

0 comments on commit cceca3e

Please sign in to comment.