|
| 1 | +//! Regression test for DIP-15 contact address pool maintenance (issue #1032). |
| 2 | +
|
| 3 | +use dashcore::hashes::Hash; |
| 4 | +use dashcore::{Address, BlockHash, Transaction}; |
| 5 | + |
| 6 | +use crate::account::AccountType; |
| 7 | +use crate::managed_account::address_pool::AddressPool; |
| 8 | +use crate::managed_account::managed_account_trait::ManagedAccountTrait; |
| 9 | +use crate::test_utils::TestWalletContext; |
| 10 | +use crate::transaction_checking::{BlockInfo, TransactionContext}; |
| 11 | +use crate::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; |
| 12 | + |
| 13 | +const US: [u8; 32] = [0xaa; 32]; |
| 14 | +const THEM: [u8; 32] = [0xbb; 32]; |
| 15 | + |
| 16 | +/// The two chains of one friendship. DIP-15 derives them from the same pair of |
| 17 | +/// identity ids in opposite order, and the wallet files them in separate |
| 18 | +/// collections, so each reaches gap maintenance by its own route. |
| 19 | +const CONTACT_CHAINS: [AccountType; 2] = [ |
| 20 | + AccountType::DashpayReceivingFunds { |
| 21 | + index: 0, |
| 22 | + user_identity_id: US, |
| 23 | + friend_identity_id: THEM, |
| 24 | + }, |
| 25 | + AccountType::DashpayExternalAccount { |
| 26 | + index: 0, |
| 27 | + user_identity_id: US, |
| 28 | + friend_identity_id: THEM, |
| 29 | + }, |
| 30 | +]; |
| 31 | + |
| 32 | +/// The pool a contact chain monitors, looked up by its fully-keyed account type. |
| 33 | +fn contact_pool(ctx: &TestWalletContext, contact: AccountType) -> &AddressPool { |
| 34 | + ctx.managed_wallet |
| 35 | + .accounts |
| 36 | + .dashpay_receival_accounts |
| 37 | + .values() |
| 38 | + .chain(ctx.managed_wallet.accounts.dashpay_external_accounts.values()) |
| 39 | + .find(|account| account.managed_account_type().to_account_type() == contact) |
| 40 | + .expect("contact account") |
| 41 | + .managed_account_type() |
| 42 | + .address_pools()[0] |
| 43 | +} |
| 44 | + |
| 45 | +/// Pays `address` in a block, and reports whether the wallet saw it. |
| 46 | +async fn pay(ctx: &mut TestWalletContext, address: &Address, height: u32) -> bool { |
| 47 | + let tx = Transaction::dummy(address, 0..1, &[29_000]); |
| 48 | + let context = TransactionContext::InBlock(BlockInfo::new( |
| 49 | + height, |
| 50 | + BlockHash::from_byte_array([height as u8; 32]), |
| 51 | + 1_700_000_000, |
| 52 | + )); |
| 53 | + ctx.check_transaction(&tx, context).await.is_relevant |
| 54 | +} |
| 55 | + |
| 56 | +/// A contact chain must extend as its payments arrive. Without that, the first |
| 57 | +/// payment past the addresses built at construction lands on an address the |
| 58 | +/// wallet never derived, and is never seen. |
| 59 | +#[tokio::test] |
| 60 | +async fn contact_payments_extend_the_contact_pool() { |
| 61 | + let mut ctx = TestWalletContext::new_random(); |
| 62 | + for contact in CONTACT_CHAINS { |
| 63 | + ctx.wallet.add_account(contact, None).expect("contact account is addable"); |
| 64 | + ctx.managed_wallet |
| 65 | + .add_managed_account(&ctx.wallet, contact) |
| 66 | + .expect("contact account is manageable"); |
| 67 | + } |
| 68 | + assert_ne!( |
| 69 | + contact_pool(&ctx, CONTACT_CHAINS[0]).address_at_index(0), |
| 70 | + contact_pool(&ctx, CONTACT_CHAINS[1]).address_at_index(0), |
| 71 | + "reversing the identity ids must give the two directions distinct chains" |
| 72 | + ); |
| 73 | + |
| 74 | + for (chain, contact) in CONTACT_CHAINS.into_iter().enumerate() { |
| 75 | + let height = 1_555_196 + chain as u32 * 2; |
| 76 | + let direction = if chain == 0 { |
| 77 | + "receiving" |
| 78 | + } else { |
| 79 | + "external" |
| 80 | + }; |
| 81 | + let pool = contact_pool(&ctx, contact); |
| 82 | + let gap = pool.gap_limit; |
| 83 | + let last_built = pool.address_at_index(gap - 1).expect("construction fills the window"); |
| 84 | + assert!( |
| 85 | + pool.address_at_index(gap).is_none(), |
| 86 | + "{direction}: nothing is derived past that window yet" |
| 87 | + ); |
| 88 | + |
| 89 | + assert!( |
| 90 | + pay(&mut ctx, &last_built, height).await, |
| 91 | + "{direction}: payment to a monitored address is seen" |
| 92 | + ); |
| 93 | + assert_eq!( |
| 94 | + contact_pool(&ctx, contact).highest_generated, |
| 95 | + Some(gap - 1 + gap), |
| 96 | + "{direction}: using an address must leave a full window monitored ahead of it" |
| 97 | + ); |
| 98 | + |
| 99 | + let past_the_window = |
| 100 | + contact_pool(&ctx, contact).address_at_index(gap).expect("the window moved"); |
| 101 | + assert!( |
| 102 | + pay(&mut ctx, &past_the_window, height + 1).await, |
| 103 | + "{direction}: the payment past the construction window must be seen too" |
| 104 | + ); |
| 105 | + assert_eq!( |
| 106 | + contact_pool(&ctx, contact).highest_generated, |
| 107 | + Some(gap * 2), |
| 108 | + "{direction}: the window must keep moving, not widen once" |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments