Skip to content

Commit

Permalink
Merge pull request #1539 from fluidvanadium/value_transfers
Browse files Browse the repository at this point in the history
new pub interface sorted_value_transfers
  • Loading branch information
dorianvp authored Nov 19, 2024
2 parents 40c20b7 + c98b5ca commit e415707
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 62 deletions.
40 changes: 20 additions & 20 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(true).await.0;
let before_reorg_transactions = light_client.sorted_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(true).await.0;
let after_reorg_transactions = light_client.sorted_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(true).await.0;
let before_reorg_transactions = light_client.sorted_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(true).await.0;
let after_reorg_transactions = light_client.sorted_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(true).await.0;
let before_reorg_transactions = light_client.sorted_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(true).await.0;
let after_reorg_transactions = light_client.sorted_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(true).await.0;
let before_reorg_transactions = light_client.sorted_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(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() {
Expand Down Expand Up @@ -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
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(true).await.0;
let before_reorg_transactions = light_client.sorted_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(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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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() {
Expand All @@ -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);

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(true).await;
let after_reorg_transactions = light_client.sorted_value_transfers(true).await;

println!("post re-org value transfers:");
println!("{}", after_reorg_transactions);
Expand Down
7 changes: 6 additions & 1 deletion darkside-tests/tests/network_interruption_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).value_transfers(true).await);
dbg!(
scenario
.get_lightclient(0)
.sorted_value_transfers(true)
.await
);

// assert the balance is correct
assert_eq!(
Expand Down
28 changes: 14 additions & 14 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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!(
Expand All @@ -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!
}
Expand Down Expand Up @@ -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!(
"{}",
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion zingolib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 5 additions & 3 deletions zingolib/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
)
}
}

Expand Down
24 changes: 15 additions & 9 deletions zingolib/src/lightclient/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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<ValueTransfer> = Vec::new();
let summaries = self.transaction_summaries().await;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit e415707

Please sign in to comment.