From 949b9ec8b4834356d524e61574e083ecb177bded Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Tue, 19 Nov 2024 18:41:17 +0000 Subject: [PATCH 1/2] new pub interface sorted_value_transfers --- darkside-tests/tests/advanced_reorg_tests.rs | 40 +++++++++---------- .../tests/network_interruption_tests.rs | 2 +- libtonode-tests/tests/concrete.rs | 28 ++++++------- zingolib/CHANGELOG.md | 4 +- zingolib/src/commands.rs | 8 ++-- zingolib/src/lightclient/describe.rs | 24 ++++++----- .../src/testutils/chain_generics/fixtures.rs | 33 ++++++++------- 7 files changed, 77 insertions(+), 62 deletions(-) diff --git a/darkside-tests/tests/advanced_reorg_tests.rs b/darkside-tests/tests/advanced_reorg_tests.rs index e3cced214..ab7ad2959 100644 --- a/darkside-tests/tests/advanced_reorg_tests.rs +++ b/darkside-tests/tests/advanced_reorg_tests.rs @@ -58,7 +58,7 @@ async fn reorg_changes_incoming_tx_height() { } ); - let before_reorg_transactions = light_client.value_transfers(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -93,7 +93,7 @@ async fn reorg_changes_incoming_tx_height() { } ); - let after_reorg_transactions = light_client.value_transfers(true).await.0; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(after_reorg_transactions.len(), 1); assert_eq!( @@ -214,7 +214,7 @@ async fn reorg_changes_incoming_tx_index() { } ); - let before_reorg_transactions = light_client.value_transfers(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -249,7 +249,7 @@ async fn reorg_changes_incoming_tx_index() { } ); - let after_reorg_transactions = light_client.value_transfers(true).await.0; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(after_reorg_transactions.len(), 1); assert_eq!( @@ -369,7 +369,7 @@ async fn reorg_expires_incoming_tx() { } ); - let before_reorg_transactions = light_client.value_transfers(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -404,7 +404,7 @@ async fn reorg_expires_incoming_tx() { } ); - let after_reorg_transactions = light_client.value_transfers(true).await.0; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(after_reorg_transactions.len(), 0); } @@ -547,7 +547,7 @@ async fn reorg_changes_outgoing_tx_height() { } ); - let before_reorg_transactions = light_client.value_transfers(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -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(true).await); + println!("{:?}", light_client.sorted_value_transfers(true).await); assert_eq!( light_client - .value_transfers(true) + .sorted_value_transfers(true) .await .iter() .find_map(|v| match v.kind() { @@ -661,11 +661,11 @@ async fn reorg_changes_outgoing_tx_height() { expected_after_reorg_balance ); - let after_reorg_transactions = light_client.value_transfers(true).await.0; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(after_reorg_transactions.len(), 3); - println!("{:?}", light_client.value_transfers(true).await); + println!("{:?}", light_client.sorted_value_transfers(true).await); // FIXME: This test is broken because if this issue // https://github.com/zingolabs/zingolib/issues/622 @@ -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(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -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(true).await); + println!("{:?}", light_client.sorted_value_transfers(true).await); let send_height = light_client - .value_transfers(true) + .sorted_value_transfers(true) .await .iter() .find_map(|v| match v.kind() { @@ -872,11 +872,11 @@ 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(true).await.0; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(after_reorg_transactions.len(), 1); - println!("{:?}", light_client.value_transfers(true).await); + println!("{:?}", light_client.sorted_value_transfers(true).await); // FIXME: This test is broken because if this issue // https://github.com/zingolabs/zingolib/issues/622 @@ -964,7 +964,7 @@ async fn reorg_changes_outgoing_tx_index() { } ); - let before_reorg_transactions = light_client.value_transfers(true).await.0; + let before_reorg_transactions = light_client.sorted_value_transfers(true).await.0; assert_eq!(before_reorg_transactions.len(), 1); assert_eq!( @@ -1016,7 +1016,7 @@ async fn reorg_changes_outgoing_tx_index() { assert_eq!( light_client - .value_transfers(true) + .sorted_value_transfers(true) .await .iter() .find_map(|v| match v.kind() { @@ -1039,7 +1039,7 @@ async fn reorg_changes_outgoing_tx_index() { ); println!("pre re-org value transfers:"); - println!("{}", light_client.value_transfers(true).await); + println!("{}", light_client.sorted_value_transfers(true).await); println!("pre re-org tx summaries:"); println!("{}", light_client.transaction_summaries().await); @@ -1087,7 +1087,7 @@ async fn reorg_changes_outgoing_tx_index() { expected_after_reorg_balance ); - let after_reorg_transactions = light_client.value_transfers(true).await; + let after_reorg_transactions = light_client.sorted_value_transfers(true).await; println!("post re-org value transfers:"); println!("{}", after_reorg_transactions); diff --git a/darkside-tests/tests/network_interruption_tests.rs b/darkside-tests/tests/network_interruption_tests.rs index 29b4dce82..255bca40d 100644 --- a/darkside-tests/tests/network_interruption_tests.rs +++ b/darkside-tests/tests/network_interruption_tests.rs @@ -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(true).await); + dbg!(scenario.get_lightclient(0).sorted_value_transfers(true).await); // assert the balance is correct assert_eq!( diff --git a/libtonode-tests/tests/concrete.rs b/libtonode-tests/tests/concrete.rs index 93221903c..6ceeed46c 100644 --- a/libtonode-tests/tests/concrete.rs +++ b/libtonode-tests/tests/concrete.rs @@ -163,7 +163,7 @@ mod fast { .await .unwrap(); - let value_transfers = &recipient.value_transfers(true).await; + let value_transfers = &recipient.sorted_value_transfers(true).await; dbg!(value_transfers); @@ -358,7 +358,7 @@ mod fast { .messages_containing(Some(&charlie.encode(&recipient.config().chain))) .await; - let all_vts = &recipient.value_transfers(true).await; + let all_vts = &recipient.sorted_value_transfers(true).await; let all_messages = &recipient.messages_containing(None).await; for vt in all_vts.0.iter() { @@ -427,11 +427,11 @@ mod fast { 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; + let value_transfers = &recipient.sorted_value_transfers(true).await; + let value_transfers1 = &recipient.sorted_value_transfers(true).await; + let value_transfers2 = &recipient.sorted_value_transfers(true).await; + let mut value_transfers3 = recipient.sorted_value_transfers(false).await; + let mut value_transfers4 = recipient.sorted_value_transfers(false).await; assert_eq!(value_transfers.0[0].memos().len(), 4); @@ -504,7 +504,7 @@ mod fast { .len(), 3usize ); - let val_tranfers = dbg!(sender.value_transfers(true).await); + let val_tranfers = dbg!(sender.sorted_value_transfers(true).await); // This fails, as we don't scan sends to tex correctly yet assert_eq!( val_tranfers.0[0].recipient_address().unwrap(), @@ -1041,7 +1041,7 @@ mod slow { ); println!( "{}", - JsonValue::from(recipient.value_transfers(true).await).pretty(4) + JsonValue::from(recipient.sorted_value_transfers(true).await).pretty(4) ); } #[tokio::test] @@ -1460,7 +1460,7 @@ mod slow { println!("{}", recipient.do_list_transactions().await.pretty(2)); println!( "{}", - JsonValue::from(recipient.value_transfers(true).await).pretty(2) + JsonValue::from(recipient.sorted_value_transfers(true).await).pretty(2) ); recipient.do_rescan().await.unwrap(); println!( @@ -1470,7 +1470,7 @@ mod slow { println!("{}", recipient.do_list_transactions().await.pretty(2)); println!( "{}", - JsonValue::from(recipient.value_transfers(true).await).pretty(2) + JsonValue::from(recipient.sorted_value_transfers(true).await).pretty(2) ); // TODO: Add asserts! } @@ -1893,7 +1893,7 @@ mod slow { println!( "{}", - JsonValue::from(faucet.value_transfers(true).await).pretty(4) + JsonValue::from(faucet.sorted_value_transfers(true).await).pretty(4) ); println!( "{}", @@ -2510,10 +2510,10 @@ mod slow { .await .unwrap(); let pre_rescan_transactions = recipient.do_list_transactions().await; - let pre_rescan_summaries = recipient.value_transfers(true).await; + let pre_rescan_summaries = recipient.sorted_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(true).await; + let post_rescan_summaries = recipient.sorted_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 diff --git a/zingolib/CHANGELOG.md b/zingolib/CHANGELOG.md index 5f8eac93b..3c9813264 100644 --- a/zingolib/CHANGELOG.md +++ b/zingolib/CHANGELOG.md @@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `commands`: - `MessagesFilterCommand`: For listing `ValueTransfers` containing memos related to an optional `sender` parameter. Returns a `JsonValue` object. -- `lightclient::messages_containing`: used by `MessagesFilterCommand`. + - `ValueTransfersCommand`: takes optional reverse sort argument + - `lightclient::messages_containing`: used by `MessagesFilterCommand`. +- `lightclient::sorted_value_transfers` - `tests`: - `message_thread` test. diff --git a/zingolib/src/commands.rs b/zingolib/src/commands.rs index baeff45ef..76382b853 100644 --- a/zingolib/src/commands.rs +++ b/zingolib/src/commands.rs @@ -1286,10 +1286,12 @@ impl Command for ValueTransfersCommand { let newer_first = args .first() .map(|s| s.parse()) - .unwrap_or(Ok(true)) - .unwrap_or(true); + .unwrap_or(Ok(false)) + .unwrap_or(false); - RT.block_on(async move { format!("{}", lightclient.value_transfers(newer_first).await) }) + RT.block_on( + async move { format!("{}", lightclient.sorted_value_transfers(newer_first).await) }, + ) } } diff --git a/zingolib/src/lightclient/describe.rs b/zingolib/src/lightclient/describe.rs index 34c7fc24d..8bda02de6 100644 --- a/zingolib/src/lightclient/describe.rs +++ b/zingolib/src/lightclient/describe.rs @@ -265,7 +265,7 @@ impl LightClient { /// Provides a list of ValueTransfers associated with the sender, or containing the string pub async fn messages_containing(&self, filter: Option<&str>) -> ValueTransfers { - let mut value_transfers = self.value_transfers(true).await.0; + let mut value_transfers = self.sorted_value_transfers(true).await.0; value_transfers.reverse(); match filter { Some(s) => { @@ -292,9 +292,19 @@ impl LightClient { ValueTransfers(value_transfers) } + /// Provides a list of value transfers sorted + /// A value transfer is a group of all notes to a specific receiver in a transaction. + pub async fn sorted_value_transfers(&self, newer_first: bool) -> ValueTransfers { + let mut value_transfers = self.value_transfers().await; + if newer_first { + value_transfers.0.reverse(); + } + value_transfers + } + /// Provides a list of value transfers related to this capability /// A value transfer is a group of all notes to a specific receiver in a transaction. - pub async fn value_transfers(&self, newer_first: bool) -> ValueTransfers { + pub async fn value_transfers(&self) -> ValueTransfers { let mut value_transfers: Vec = Vec::new(); let summaries = self.transaction_summaries().await; @@ -543,16 +553,12 @@ impl LightClient { } }; } - if newer_first { - value_transfers.reverse(); - } - ValueTransfers(value_transfers) } /// TODO: doc comment pub async fn value_transfers_json_string(&self) -> String { - json::JsonValue::from(self.value_transfers(true).await).pretty(2) + json::JsonValue::from(self.sorted_value_transfers(true).await).pretty(2) } /// Provides a list of transaction summaries related to this wallet in order of blockheight @@ -690,7 +696,7 @@ impl LightClient { /// TODO: Add Doc Comment Here! pub async fn do_total_memobytes_to_address(&self) -> finsight::TotalMemoBytesToAddress { - let value_transfers = self.value_transfers(true).await.0; + let value_transfers = self.sorted_value_transfers(true).await.0; let mut memobytes_by_address = HashMap::new(); for value_transfer in value_transfers { if let ValueTransferKind::Sent(SentValueTransfer::Send) = value_transfer.kind() { @@ -945,7 +951,7 @@ impl LightClient { } async fn value_transfer_by_to_address(&self) -> finsight::ValuesSentToAddress { - let value_transfers = self.value_transfers(true).await.0; + let value_transfers = self.sorted_value_transfers(false).await.0; let mut amount_by_address = HashMap::new(); for value_transfer in value_transfers { if let ValueTransferKind::Sent(SentValueTransfer::Send) = value_transfer.kind() { diff --git a/zingolib/src/testutils/chain_generics/fixtures.rs b/zingolib/src/testutils/chain_generics/fixtures.rs index 376bf8b0c..4c7fcda71 100644 --- a/zingolib/src/testutils/chain_generics/fixtures.rs +++ b/zingolib/src/testutils/chain_generics/fixtures.rs @@ -58,30 +58,35 @@ where ) .await; - assert_eq!(sender.value_transfers(true).await.0.len(), 3); + assert_eq!(sender.sorted_value_transfers(true).await.0.len(), 3); assert!(sender - .value_transfers(false) + .sorted_value_transfers(false) .await .0 .iter() .any(|vt| { vt.kind() == ValueTransferKind::Received })); assert!(sender - .value_transfers(false) + .sorted_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!(sender + .sorted_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.sorted_value_transfers(true).await.0.len(), 1); with_assertions::propose_send_bump_sync_all_recipients( &mut environment, @@ -91,18 +96,18 @@ where ) .await; - assert_eq!(sender.value_transfers(true).await.0.len(), 4); + assert_eq!(sender.sorted_value_transfers(true).await.0.len(), 4); assert_eq!( - sender.value_transfers(true).await.0[0].kind(), + sender.sorted_value_transfers(true).await.0[0].kind(), ValueTransferKind::Sent(SentValueTransfer::SendToSelf(SelfSendValueTransfer::Basic)) ); with_assertions::assure_propose_shield_bump_sync(&mut environment, &sender, false) .await .unwrap(); - assert_eq!(sender.value_transfers(true).await.0.len(), 5); + assert_eq!(sender.sorted_value_transfers(true).await.0.len(), 5); assert_eq!( - sender.value_transfers(true).await.0[0].kind(), + sender.sorted_value_transfers(true).await.0[0].kind(), ValueTransferKind::Sent(SentValueTransfer::SendToSelf(SelfSendValueTransfer::Shield)) ); } From 0912cef4e02b96d39d3a578edb572a51fc66d872 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Tue, 19 Nov 2024 18:41:53 +0000 Subject: [PATCH 2/2] cargo fmt --- darkside-tests/tests/network_interruption_tests.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/darkside-tests/tests/network_interruption_tests.rs b/darkside-tests/tests/network_interruption_tests.rs index 255bca40d..23b9c33e6 100644 --- a/darkside-tests/tests/network_interruption_tests.rs +++ b/darkside-tests/tests/network_interruption_tests.rs @@ -197,7 +197,12 @@ 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).sorted_value_transfers(true).await); + dbg!( + scenario + .get_lightclient(0) + .sorted_value_transfers(true) + .await + ); // assert the balance is correct assert_eq!(