-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprocess_block.rs
More file actions
170 lines (144 loc) · 5.96 KB
/
process_block.rs
File metadata and controls
170 lines (144 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::wallet_interface::{BlockProcessingResult, WalletInterface};
use crate::WalletManager;
use alloc::string::String;
use alloc::vec::Vec;
use async_trait::async_trait;
use core::fmt::Write as _;
use dashcore::prelude::CoreBlockHeight;
use dashcore::{Address, Block, Transaction};
use key_wallet::transaction_checking::transaction_router::TransactionRouter;
use key_wallet::transaction_checking::TransactionContext;
use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface;
#[async_trait]
impl<T: WalletInfoInterface + Send + Sync + 'static> WalletInterface for WalletManager<T> {
async fn process_block(
&mut self,
block: &Block,
height: CoreBlockHeight,
) -> BlockProcessingResult {
let mut result = BlockProcessingResult::default();
let block_hash = Some(block.block_hash());
let timestamp = block.header.time;
// Process each transaction using the base manager
for tx in &block.txdata {
let context = TransactionContext::InBlock {
height,
block_hash,
timestamp: Some(timestamp),
};
let check_result = self.check_transaction_in_all_wallets(tx, context, true).await;
if !check_result.affected_wallets.is_empty() {
if check_result.is_new_transaction {
result.new_txids.push(tx.txid());
} else {
result.existing_txids.push(tx.txid());
}
}
result.new_addresses.extend(check_result.new_addresses);
}
self.update_synced_height(height);
result
}
async fn process_mempool_transaction(&mut self, tx: &Transaction) {
let context = TransactionContext::Mempool;
// Check transaction against all wallets
self.check_transaction_in_all_wallets(
tx, context, true, // update state
)
.await;
}
fn monitored_addresses(&self) -> Vec<Address> {
self.monitored_addresses()
}
async fn transaction_effect(&self, tx: &Transaction) -> Option<(i64, Vec<String>)> {
// Aggregate across all managed wallets. If any wallet considers it relevant,
// compute net = total_received - total_sent and collect involved addresses.
let mut total_received: u64 = 0;
let mut total_sent: u64 = 0;
let mut addresses: Vec<String> = Vec::new();
let mut is_relevant_any = false;
for info in self.wallet_infos.values() {
let collection = info.accounts();
// Reuse the same routing/check logic used in normal processing
let tx_type = TransactionRouter::classify_transaction(tx);
let account_types = TransactionRouter::get_relevant_account_types(&tx_type);
let result = collection.check_transaction(tx, &account_types);
if result.is_relevant {
is_relevant_any = true;
total_received = total_received.saturating_add(result.total_received);
total_sent = total_sent.saturating_add(result.total_sent);
// Collect involved addresses from affected accounts
for account_match in result.affected_accounts {
for addr_info in account_match.account_type_match.all_involved_addresses() {
addresses.push(addr_info.address.to_string());
}
}
}
}
if is_relevant_any {
// Deduplicate addresses while preserving order
let mut seen = alloc::collections::BTreeSet::new();
addresses.retain(|a| seen.insert(a.clone()));
let net = (total_received as i64) - (total_sent as i64);
Some((net, addresses))
} else {
None
}
}
async fn earliest_required_height(&self) -> CoreBlockHeight {
self.wallet_infos.values().map(|info| info.birth_height()).min().unwrap_or(0)
}
fn synced_height(&self) -> CoreBlockHeight {
self.synced_height
}
fn update_synced_height(&mut self, height: CoreBlockHeight) {
self.synced_height = height;
for info in self.wallet_infos.values_mut() {
info.update_synced_height(height);
}
}
async fn describe(&self) -> String {
let wallet_count = self.wallet_infos.len();
if wallet_count == 0 {
return format!("WalletManager: 0 wallets (network {})", self.network);
}
let mut details = Vec::with_capacity(wallet_count);
for (wallet_id, info) in &self.wallet_infos {
let name = info.name().unwrap_or("unnamed");
let mut wallet_id_hex = String::with_capacity(wallet_id.len() * 2);
for byte in wallet_id {
let _ = write!(&mut wallet_id_hex, "{:02x}", byte);
}
let script_count = info.monitored_addresses().len();
let summary = format!("{} scripts", script_count);
details.push(format!("{} ({}): {}", name, wallet_id_hex, summary));
}
format!(
"WalletManager: {} wallet(s) on {}\n{}",
wallet_count,
self.network,
details.join("\n")
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use dashcore::Network;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
#[tokio::test]
async fn test_synced_height() {
let mut manager: WalletManager<ManagedWalletInfo> = WalletManager::new(Network::Testnet);
// Initial state
assert_eq!(manager.synced_height(), 0);
// Inrease synced height
manager.update_synced_height(1000);
assert_eq!(manager.synced_height(), 1000);
//Increase synced height again
manager.update_synced_height(5000);
assert_eq!(manager.synced_height(), 5000);
// Decrease synced height
manager.update_synced_height(10);
assert_eq!(manager.synced_height(), 10);
}
}