Skip to content

Commit

Permalink
Merge pull request zingolabs#1542 from dorianvp/empty-memo
Browse files Browse the repository at this point in the history
Filter empty messages in messages_containing function
  • Loading branch information
fluidvanadium authored Nov 21, 2024
2 parents 679dd33 + ae67335 commit 7bc2d8b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
64 changes: 64 additions & 0 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,70 @@ mod fast {
&& vt.recipient_address() == Some(ZENNIES_FOR_ZINGO_REGTEST_ADDRESS)));
}

/// This tests checks that messages_containing returns an empty vector when empty memos are included.
#[tokio::test]
async fn filter_empty_messages() {
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(""),
),
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some(""),
),
],
)
.await
.unwrap();

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

let no_messages = &recipient.messages_containing(None).await;

assert_eq!(no_messages.0.len(), 0);

from_inputs::quick_send(
&faucet,
vec![
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some("Hello"),
),
(
get_base_address_macro!(recipient, "unified").as_str(),
5_000,
Some(""),
),
],
)
.await
.unwrap();

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

let single_message = &recipient.messages_containing(None).await;

assert_eq!(single_message.0.len(), 1);
}

/// Test sending and receiving messages between three parties.
///
/// This test case consists of the following sequence of events:
Expand Down
6 changes: 5 additions & 1 deletion zingolib/src/lightclient/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,14 @@ impl LightClient {
}
}

/// Provides a list of ValueTransfers associated with the sender, or containing the string
/// 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.sorted_value_transfers(true).await.0;
value_transfers.reverse();

// Filter out VTs where all memos are empty.
value_transfers.retain(|vt| vt.memos().iter().any(|memo| !memo.is_empty()));

match filter {
Some(s) => {
value_transfers.retain(|vt| {
Expand Down

0 comments on commit 7bc2d8b

Please sign in to comment.