Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 186 additions & 6 deletions key-wallet-manager/src/event_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ use crate::wallet_interface::WalletInterface;
use dashcore::block::{Block, Header, Version};
use dashcore::blockdata::script::Builder;
use dashcore::blockdata::transaction::special_transaction::asset_lock::AssetLockPayload;
use dashcore::blockdata::transaction::special_transaction::provider_registration::{
ProviderMasternodeType, ProviderRegistrationPayload,
};
use dashcore::blockdata::transaction::special_transaction::TransactionPayload;
use dashcore::bls_sig_utils::BLSSignature;
use dashcore::bls_sig_utils::{BLSPublicKey, BLSSignature};
use dashcore::ephemerealdata::chain_lock::ChainLock;
use dashcore::ephemerealdata::instant_lock::InstantLock;
use dashcore::hash_types::CycleHash;
use dashcore::hash_types::{CycleHash, InputsHash};
use dashcore::hashes::Hash;
use dashcore::opcodes;
use dashcore::{
BlockHash, CompactTarget, OutPoint, PublicKey, ScriptBuf, TxIn, TxMerkleNode, TxOut, Txid,
Witness,
BlockHash, CompactTarget, OutPoint, PubkeyHash, PublicKey, ScriptBuf, TxIn, TxMerkleNode,
TxOut, Txid, Witness,
};
use key_wallet::account::StandardAccountType;
use key_wallet::managed_account::address_pool::{AddressPoolType, PublicKeyType};
use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait;
use key_wallet::managed_account::managed_account_type::ManagedAccountType;
use key_wallet::transaction_checking::{TransactionRouter, TransactionType};
use key_wallet::transaction_checking::{BlockInfo, TransactionRouter, TransactionType};
use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference;
use key_wallet::AccountType;
use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};

fn make_block(txdata: Vec<Transaction>, seed: u8, time: u32) -> Block {
Block {
Expand Down Expand Up @@ -1270,6 +1273,183 @@ async fn test_update_wallet_synced_height_does_not_re_emit_when_unchanged() {
);
}

// ---------------------------------------------------------------------------
// Masternode collateral locks
// ---------------------------------------------------------------------------

/// A ProRegTx registering `collateral` and paying `outputs`. Its fee input and
/// keys belong to no one the test wallet knows; `seed` varies the txid.
fn masternode_registration(collateral: OutPoint, outputs: Vec<TxOut>, seed: u8) -> Transaction {
let key_hash = PubkeyHash::from_byte_array([0x70; 20]);
Transaction {
version: 3,
lock_time: 0,
input: vec![TxIn {
previous_output: OutPoint::new(Txid::from_byte_array([seed; 32]), 0),
..Default::default()
}],
output: outputs,
special_transaction_payload: Some(TransactionPayload::ProviderRegistrationPayloadType(
ProviderRegistrationPayload {
version: 1,
masternode_type: ProviderMasternodeType::Regular,
masternode_mode: 0,
collateral_outpoint: collateral,
service_address: "127.0.0.1:19999".parse().expect("socket address"),
owner_key_hash: key_hash,
operator_public_key: BLSPublicKey::from([0x11; 48]),
voting_key_hash: key_hash,
operator_reward: 0,
script_payout: ScriptBuf::new(),
inputs_hash: InputsHash::all_zeros(),
signature: vec![0x33; 65],
platform_node_id: None,
platform_p2p_port: None,
platform_http_port: None,
},
)),
}
}

fn bip44_account_0() -> AccountType {
AccountType::Standard {
index: 0,
standard_account_type: StandardAccountType::BIP44Account,
}
}

/// A wallet holding one coin of `TX_AMOUNT`, mined at height 100.
async fn manager_holding_a_coin() -> (WalletManager, WalletId, OutPoint) {
let (mut manager, wallet_id, addr) = setup_manager_with_wallet();
let funding = create_tx_paying_to(&addr, 0xc0);
let block = make_block(vec![funding.clone()], 0xc0, 1_000);
manager
.process_block_for_wallets(&block, block.block_hash(), 100, &BTreeSet::from([wallet_id]))
.await;
(manager, wallet_id, OutPoint::new(funding.txid(), 0))
}

/// A ProRegTx locks a collateral it names at the block that holds it, not
/// from the mempool. The mempool sighting changes nothing and emits nothing;
/// the block's `BlockProcessed` carries the coin in the locked balance.
#[tokio::test]
async fn test_registration_locks_a_named_collateral_at_its_block_not_from_the_mempool() {
let (mut manager, wallet_id, collateral) = manager_holding_a_coin().await;
let registration = masternode_registration(collateral, vec![], 0xc1);
let mut rx = manager.subscribe_events();

let result = manager.process_mempool_transaction(&registration, None).await;

assert!(!result.is_relevant);
assert_no_events(&mut rx);
let info = manager.get_wallet_info(&wallet_id).expect("wallet info");
assert!(!info.is_outpoint_locked(&collateral));
assert_eq!(info.balance().spendable(), TX_AMOUNT);
assert_eq!(info.balance().locked(), 0);

let block = make_block(vec![registration], 0xc1, 1_001);
manager
.process_block_for_wallets(&block, block.block_hash(), 101, &BTreeSet::from([wallet_id]))
.await;

assert!(manager
.get_wallet_info(&wallet_id)
.expect("wallet info")
.is_outpoint_locked(&collateral));
let events = drain_events(&mut rx);
assert_eq!(events.len(), 1, "one BlockProcessed expected, got {:?}", events);
match &events[0] {
WalletEvent::BlockProcessed {
inserted,
updated,
balance,
account_balances,
..
} => {
assert!(inserted.is_empty() && updated.is_empty(), "the ProRegTx records nothing");
assert_eq!(balance.locked(), TX_AMOUNT);
assert_eq!(balance.spendable(), 0);
let account = account_balances.get(&bip44_account_0()).expect("the coin's account");
assert_eq!(account.locked(), TX_AMOUNT);
}
other => panic!("expected BlockProcessed, got {:?}", other),
}
}

/// An InstantSend lock does not make a ProRegTx a registration: the coin it
/// names stays spendable until a block holds the ProRegTx.
#[tokio::test]
async fn test_instant_locked_registration_locks_no_collateral_it_names() {
let (mut manager, wallet_id, collateral) = manager_holding_a_coin().await;
let registration = masternode_registration(collateral, vec![], 0xc2);
let mut rx = manager.subscribe_events();

manager
.process_mempool_transaction(&registration, Some(dummy_instant_lock(registration.txid())))
.await;

assert_no_events(&mut rx);
let info = manager.get_wallet_info(&wallet_id).expect("wallet info");
assert!(!info.is_outpoint_locked(&collateral));
assert_eq!(info.balance().spendable(), TX_AMOUNT);
assert_eq!(info.balance().locked(), 0);
}

/// A ProRegTx that creates its collateral locks it from the mempool, and the
/// `TransactionDetected` for the coin carries it in the locked balance.
#[tokio::test]
async fn test_mempool_registration_creating_its_collateral_emits_the_locked_balance() {
let (mut manager, wallet_id, addr) = setup_manager_with_wallet();
let to_wallet = TxOut {
value: TX_AMOUNT,
script_pubkey: addr.script_pubkey(),
};
// A null collateral txid: the collateral is this ProRegTx's output 0.
let registration =
masternode_registration(OutPoint::new(Txid::all_zeros(), 0), vec![to_wallet], 0xc3);
let collateral = OutPoint::new(registration.txid(), 0);
let mut rx = manager.subscribe_events();

let result = manager.process_mempool_transaction(&registration, None).await;

assert!(result.is_relevant);
let events = drain_events(&mut rx);
assert_eq!(events.len(), 1, "one TransactionDetected expected, got {:?}", events);
match &events[0] {
WalletEvent::TransactionDetected {
record,
balance,
account_balances,
..
} => {
assert_eq!(record.txid, registration.txid());
assert_eq!(balance.locked(), TX_AMOUNT);
assert_eq!(balance.spendable(), 0, "the collateral is not spendable balance");
let account = account_balances.get(&bip44_account_0()).expect("the coin's account");
assert_eq!(account.locked(), TX_AMOUNT);
}
other => panic!("expected TransactionDetected, got {:?}", other),
}
let info = manager.get_wallet_info(&wallet_id).expect("wallet info");
assert!(info.is_outpoint_locked(&collateral));
assert_eq!(info.balance().locked(), TX_AMOUNT);
}

/// A check reports the locks it took per wallet, including for a wallet the
/// transaction is otherwise irrelevant to, so a caller that checks with
/// `update_balance = false` knows which balances to refresh.
#[tokio::test]
async fn test_check_transaction_reports_a_lock_for_a_wallet_it_is_otherwise_irrelevant_to() {
let (mut manager, wallet_id, collateral) = manager_holding_a_coin().await;
let registration = masternode_registration(collateral, vec![], 0xc4);
let mined = TransactionContext::InBlock(BlockInfo::new(101, BlockHash::all_zeros(), 1_001));

let result = manager.check_transaction_in_all_wallets(&registration, mined, true, false).await;

assert!(result.affected_wallets.is_empty());
assert_eq!(result.per_wallet_locked_outpoints, BTreeMap::from([(wallet_id, vec![collateral])]));
}

// ---------------------------------------------------------------------------
// Dry run and irrelevant paths
// ---------------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions key-wallet-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ pub struct CheckTransactionsResult {
/// see [`crate::events::WalletEvent::TransactionsSwept`] for why a
/// consumer needs this set named explicitly instead of re-deriving it.
pub per_wallet_released_outpoints: BTreeMap<WalletId, Vec<OutPoint>>,
/// Outpoints this check added to each wallet's lock set, grouped by
/// wallet: the collateral of a ProRegTx (see
/// [`ManagedWalletInfo::locked_outpoints`]). Gathered whether or not the
/// transaction is relevant to the wallet, since a ProRegTx in a block
/// locks a coin the wallet holds even when nothing else ties it to the
/// wallet. A lock moves that coin from the spendable to the locked
/// balance, so a caller that checked with `update_balance = false`
/// refreshes these wallets as well as the affected ones.
pub per_wallet_locked_outpoints: BTreeMap<WalletId, Vec<OutPoint>>,
}

impl CheckTransactionsResult {
Expand Down Expand Up @@ -662,6 +671,14 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
.extend(check_result.released_outpoints);
}

if !check_result.locked_outpoints.is_empty() {
result
.per_wallet_locked_outpoints
.entry(*wallet_id)
.or_default()
.extend(check_result.locked_outpoints);
}

if !check_result.new_addresses.is_empty() {
result
.new_addresses
Expand Down
14 changes: 13 additions & 1 deletion key-wallet-manager/src/process_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,23 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletM
// changed. The cached `.balance` field is stale until
// `update_balance()` runs, so the pre-snapshot taken here captures
// the pre-transaction state.
//
// A wallet whose lock set this check grew is refreshed too, relevant
// or not, since a lock moves a held coin to the locked balance.
// Off-chain only a ProRegTx's own output locks, and only as that coin
// arrives, so the lock rides the `TransactionDetected` of the wallet
// the ProRegTx pays.
let refreshed: BTreeSet<WalletId> = check_result
.affected_wallets
.iter()
.chain(check_result.per_wallet_locked_outpoints.keys())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 16c1795 this has no effect: off-chain a lock only lands on the ProRegTx's own output, so the wallet is either already in affected_wallets or doesn't hold the coin. Today it only triggers an update_balance() with no event for the irrelevant registrations above. I'd remove it.

.copied()
.collect();
let mut per_wallet_account_diff: BTreeMap<
WalletId,
BTreeMap<AccountType, WalletCoreBalance>,
> = BTreeMap::new();
for wallet_id in &check_result.affected_wallets {
for wallet_id in &refreshed {
if let Some(info) = self.wallet_infos.get_mut(wallet_id) {
let prior = info.account_balances();
info.update_balance();
Expand Down
6 changes: 3 additions & 3 deletions key-wallet/src/managed_account/managed_core_funds_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ impl ManagedCoreFundsAccount {
// scratch, so carry forward flags that must not regress. An
// InstantSend lock is permanent for a txid (DIP-0010) and
// trust only ever settles, so both latch monotonically. A
// coin reservation is orthogonal to chain context and is kept
// as-is. `is_confirmed` stays freshly derived so a reorg can
// still downgrade it.
// coin lock follows the wallet's lock set, not chain context,
// and is kept as-is. `is_confirmed` stays freshly derived so a
// reorg can still downgrade it.
if let Some(prior) = self.utxos.get(&outpoint) {
utxo.is_instantlocked |= prior.is_instantlocked;
utxo.is_trusted |= prior.is_trusted;
Expand Down
9 changes: 7 additions & 2 deletions key-wallet/src/tests/balance_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ fn test_locked_utxos_in_locked_balance() {
let mut wallet_info = ManagedWalletInfo::dummy(3);
let mut account = ManagedCoreFundsAccount::dummy_bip44();

let mut utxo = Utxo::dummy(1, 100_000, 1000, false, true);
utxo.is_locked = true;
let utxo = Utxo::dummy(1, 100_000, 1000, false, true);
let outpoint = utxo.outpoint;
account.utxos.insert(utxo.outpoint, utxo);
wallet_info.accounts.insert(account).unwrap();

assert_eq!(wallet_info.balance, WalletCoreBalance::default());
wallet_info.update_last_processed_height(1100);
assert_eq!(wallet_info.balance, WalletCoreBalance::new(100_000, 0, 0, 0));

// A held coin is locked through the wallet's lock set, which moves its
// value to the locked bucket.
assert!(wallet_info.lock_outpoint(outpoint));
let expected = WalletCoreBalance::new(0, 0, 0, 100_000);
assert_eq!(wallet_info.balance, expected);
}
Expand Down
Loading
Loading