-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathqueries.rs
More file actions
154 lines (139 loc) · 5.92 KB
/
queries.rs
File metadata and controls
154 lines (139 loc) · 5.92 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
//! Query methods for peers, masternodes, and balances.
//!
//! This module contains:
//! - Peer queries (count, info, disconnect)
//! - Masternode queries (engine, list, quorums)
//! - Balance queries
//! - Filter availability checks
use crate::error::{Result, SpvError};
use crate::network::NetworkManager;
use crate::storage::StorageManager;
use crate::types::AddressBalance;
use dashcore::sml::llmq_type::LLMQType;
use dashcore::sml::masternode_list::MasternodeList;
use dashcore::sml::masternode_list_engine::MasternodeListEngine;
use dashcore::sml::quorum_entry::qualified_quorum_entry::QualifiedQuorumEntry;
use dashcore::QuorumHash;
use key_wallet_manager::wallet_interface::WalletInterface;
use super::DashSpvClient;
impl<
W: WalletInterface + Send + Sync + 'static,
N: NetworkManager + Send + Sync + 'static,
S: StorageManager + Send + Sync + 'static,
> DashSpvClient<W, N, S>
{
// ============ Peer Queries ============
/// Get the number of connected peers.
pub fn peer_count(&self) -> usize {
self.network.peer_count()
}
/// Get information about connected peers.
pub fn peer_info(&self) -> Vec<crate::types::PeerInfo> {
self.network.peer_info()
}
/// Get the number of connected peers (async version).
pub async fn get_peer_count(&self) -> usize {
self.network.peer_count()
}
// ============ Masternode Queries ============
/// Get a reference to the masternode list engine.
/// Returns None if masternode sync is not enabled in config.
pub fn masternode_list_engine(&self) -> Option<&MasternodeListEngine> {
self.sync_manager.masternode_list_engine()
}
/// Get the masternode list at a specific block height.
/// Returns None if the masternode list for that height is not available.
pub fn get_masternode_list_at_height(&self, height: u32) -> Option<&MasternodeList> {
self.masternode_list_engine().and_then(|engine| engine.masternode_lists.get(&height))
}
/// Get a quorum entry by type and hash at a specific block height.
/// Returns None if the quorum is not found.
pub fn get_quorum_at_height(
&self,
height: u32,
quorum_type: LLMQType,
quorum_hash: QuorumHash,
) -> Result<QualifiedQuorumEntry> {
// First check if we have the masternode list at this height
match self.get_masternode_list_at_height(height) {
Some(ml) => {
// We have the masternode list, now look for the quorum
match ml.quorums.get(&quorum_type) {
Some(quorums) => match quorums.get(&quorum_hash) {
Some(quorum) => {
tracing::debug!(
"Found quorum type {} at height {} with hash {}",
quorum_type,
height,
hex::encode(quorum_hash)
);
Ok(quorum.clone())
}
None => {
let message = format!("Quorum not found: type {} at height {} with hash {} (masternode list exists with {} quorums of this type)",
quorum_type,
height,
hex::encode(quorum_hash),
quorums.len());
tracing::warn!(message);
Err(SpvError::QuorumLookupError(message))
}
},
None => {
tracing::warn!(
"No quorums of type {} found at height {} (masternode list exists)",
quorum_type,
height
);
Err(SpvError::QuorumLookupError(format!(
"No quorums of type {} found at height {}",
quorum_type, height
)))
}
}
}
None => {
tracing::warn!(
"No masternode list found at height {} - cannot retrieve quorum",
height
);
Err(SpvError::QuorumLookupError(format!(
"No masternode list found at height {}",
height
)))
}
}
}
// ============ Balance Queries ============
/// Get balance for a specific address.
///
/// This method is deprecated - use the wallet's balance query methods instead.
pub async fn get_address_balance(
&self,
_address: &dashcore::Address,
) -> Result<AddressBalance> {
// This method requires wallet-specific functionality not in WalletInterface
// The wallet should expose balance info through its own interface
Err(SpvError::Config(
"Address balance queries should be made directly to the wallet implementation"
.to_string(),
))
}
/// Get balances for all watched addresses.
///
/// This method is deprecated - use the wallet's balance query methods instead.
pub async fn get_all_balances(
&self,
) -> Result<std::collections::HashMap<dashcore::Address, AddressBalance>> {
// TODO: Get balances from wallet instead of tracking separately
// Will be implemented when wallet integration is complete
Ok(std::collections::HashMap::new())
}
// ============ Filter Queries ============
/// Check if filter sync is available (any peer supports compact filters).
pub async fn is_filter_sync_available(&self) -> bool {
self.network
.has_peer_with_service(dashcore::network::constants::ServiceFlags::COMPACT_FILTERS)
.await
}
}