Skip to content

Commit

Permalink
Merge pull request #1564 from zancas/make_value_transfers_more_idiomatic
Browse files Browse the repository at this point in the history
Make value transfers more idiomatic
  • Loading branch information
zancas authored Nov 30, 2024
2 parents be6c159 + aaa545d commit 6c0e7d2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 128 deletions.
22 changes: 11 additions & 11 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.sorted_value_transfers(true).await.0;
let before_reorg_transactions = light_client.sorted_value_transfers(true).await;

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

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

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

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

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

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

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

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

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

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

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -872,7 +872,7 @@ 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.sorted_value_transfers(true).await.0;
let after_reorg_transactions = light_client.sorted_value_transfers(true).await;

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

Expand Down Expand Up @@ -964,7 +964,7 @@ async fn reorg_changes_outgoing_tx_index() {
}
);

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

assert_eq!(before_reorg_transactions.len(), 1);
assert_eq!(
Expand Down
126 changes: 41 additions & 85 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ mod fast {

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

dbg!(value_transfers);

assert!(value_transfers.iter().any(|vt| vt.kind()
== ValueTransferKind::Sent(SentValueTransfer::SendToSelf(
SelfSendValueTransfer::Basic
Expand Down Expand Up @@ -503,7 +501,7 @@ mod fast {

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

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

from_inputs::quick_send(
&faucet,
Expand All @@ -528,7 +526,7 @@ mod fast {

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

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

/// Test sending and receiving messages between three parties.
Expand All @@ -545,9 +543,25 @@ mod fast {
/// returns the expected messages for each party in the correct order.
#[tokio::test]
async fn message_thread() {
// Begin test setup
let (regtest_manager, _cph, faucet, recipient, _txid) =
scenarios::orchard_funded_recipient(10_000_000).await;

macro_rules! send_and_sync {
($client:ident, $message:ident) => {
// Propose sending the message
$client.propose_send($message.clone()).await.unwrap();
// Complete and broadcast the stored proposal
$client
.complete_and_broadcast_stored_proposal()
.await
.unwrap();
// Increase the height and wait for the client
increase_height_and_wait_for_client(&regtest_manager, &$client, 1)
.await
.unwrap();
};
}
// Addresses: alice, bob, charlie
let alice = get_base_address(&recipient, PoolType::ORCHARD).await;
let bob = faucet
.wallet
Expand All @@ -561,7 +575,6 @@ mod fast {
false,
)
.unwrap();

let charlie = faucet
.wallet
.wallet_capability()
Expand All @@ -575,6 +588,7 @@ mod fast {
)
.unwrap();

// messages
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 All @@ -587,7 +601,6 @@ mod fast {
)
.unwrap()])
.unwrap();

let alice_to_bob_2 = TransactionRequest::new(vec![Payment::new(
ZcashAddress::from_str(&bob.encode(&faucet.config().chain)).unwrap(),
NonNegativeAmount::from_u64(1_000).unwrap(),
Expand All @@ -600,7 +613,6 @@ mod fast {
)
.unwrap()])
.unwrap();

let alice_to_charlie = TransactionRequest::new(vec![Payment::new(
ZcashAddress::from_str(&charlie.encode(&faucet.config().chain)).unwrap(),
NonNegativeAmount::from_u64(1_000).unwrap(),
Expand All @@ -613,7 +625,6 @@ mod fast {
)
.unwrap()])
.unwrap();

let charlie_to_alice = TransactionRequest::new(vec![Payment::new(
ZcashAddress::from_str(&alice).unwrap(),
NonNegativeAmount::from_u64(1_000).unwrap(),
Expand All @@ -630,7 +641,6 @@ mod fast {
)
.unwrap()])
.unwrap();

let bob_to_alice = TransactionRequest::new(vec![Payment::new(
ZcashAddress::from_str(&alice).unwrap(),
NonNegativeAmount::from_u64(1_000).unwrap(),
Expand All @@ -647,92 +657,40 @@ mod fast {
)
.unwrap()])
.unwrap();

recipient.propose_send(alice_to_bob.clone()).await.unwrap();

recipient
.complete_and_broadcast_stored_proposal()
.await
.unwrap();

increase_height_and_wait_for_client(&regtest_manager, &recipient, 1)
.await
.unwrap();

recipient
.propose_send(alice_to_bob_2.clone())
.await
.unwrap();

recipient
.complete_and_broadcast_stored_proposal()
.await
.unwrap();

increase_height_and_wait_for_client(&regtest_manager, &recipient, 1)
.await
.unwrap();

faucet.propose_send(bob_to_alice.clone()).await.unwrap();

faucet
.complete_and_broadcast_stored_proposal()
.await
.unwrap();

increase_height_and_wait_for_client(&regtest_manager, &recipient, 1)
.await
.unwrap();

recipient
.propose_send(alice_to_charlie.clone())
.await
.unwrap();

recipient
.complete_and_broadcast_stored_proposal()
.await
.unwrap();

faucet.propose_send(charlie_to_alice.clone()).await.unwrap();

faucet
.complete_and_broadcast_stored_proposal()
.await
.unwrap();

// Complete test setup

// Message Sending
send_and_sync!(recipient, alice_to_bob);
send_and_sync!(recipient, alice_to_bob_2);
send_and_sync!(faucet, bob_to_alice);
send_and_sync!(recipient, alice_to_charlie);
send_and_sync!(faucet, charlie_to_alice);
// Final sync of recipient
increase_height_and_wait_for_client(&regtest_manager, &recipient, 1)
.await
.unwrap();

// Collect observations
let value_transfers_bob = &recipient
.messages_containing(Some(&bob.encode(&recipient.config().chain)))
.await;

let value_transfers_charlie = &recipient
.messages_containing(Some(&charlie.encode(&recipient.config().chain)))
.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() {
dbg!(vt.blockheight());
}

assert_eq!(value_transfers_bob.0.len(), 3);
assert_eq!(value_transfers_charlie.0.len(), 2);
// Make assertions
assert_eq!(value_transfers_bob.len(), 3);
assert_eq!(value_transfers_charlie.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() }));
}
Expand Down Expand Up @@ -788,15 +746,15 @@ mod fast {
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);
assert_eq!(value_transfers[0].memos().len(), 4);

value_transfers3.0.reverse();
value_transfers4.0.reverse();
value_transfers3.reverse();
value_transfers4.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);
assert_eq!(value_transfers, &value_transfers3);
assert_eq!(value_transfers, &value_transfers4);
}

pub mod tex {
Expand Down Expand Up @@ -834,11 +792,11 @@ mod fast {

let proposal = sender.propose_send(transaction_request).await.unwrap();
assert_eq!(proposal.steps().len(), 2usize);
let sent_txids_according_to_broadcast = sender
let _sent_txids_according_to_broadcast = sender
.complete_and_broadcast_stored_proposal()
.await
.unwrap();
let txids = sender
let _txids = sender
.wallet
.transactions()
.read()
Expand All @@ -847,8 +805,6 @@ mod fast {
.keys()
.cloned()
.collect::<Vec<TxId>>();
dbg!(&txids);
dbg!(sent_txids_according_to_broadcast);
assert_eq!(
sender
.wallet
Expand All @@ -862,7 +818,7 @@ mod fast {
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(),
val_tranfers[0].recipient_address().unwrap(),
tex_addr_from_first.encode()
);
}
Expand Down
Loading

0 comments on commit 6c0e7d2

Please sign in to comment.