-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutxo.rs
More file actions
354 lines (299 loc) · 9.53 KB
/
Copy pathutxo.rs
File metadata and controls
354 lines (299 loc) · 9.53 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! UTXO management for wallet operations
//!
//! This module provides UTXO tracking and management functionality
//! that works with dashcore transaction types.
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::cmp::Ordering;
use crate::Address;
use dashcore::blockdata::transaction::txout::TxOut;
use dashcore::blockdata::transaction::OutPoint;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Unspent Transaction Output
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Utxo {
/// The outpoint (txid + vout)
pub outpoint: OutPoint,
/// The transaction output
pub txout: TxOut,
/// The address this UTXO belongs to
pub address: Address,
/// Block height where this UTXO was created
pub height: u32,
/// Whether this is from a coinbase transaction
pub is_coinbase: bool,
/// Whether this UTXO is confirmed
pub is_confirmed: bool,
/// Whether this UTXO has an InstantLock
pub is_instantlocked: bool,
/// Whether this UTXO is locked (not available for spending)
pub is_locked: bool,
}
impl Utxo {
/// Create a new UTXO
pub fn new(
outpoint: OutPoint,
txout: TxOut,
address: Address,
height: u32,
is_coinbase: bool,
) -> Self {
Self {
outpoint,
txout,
address,
height,
is_coinbase,
is_confirmed: false,
is_instantlocked: false,
is_locked: false,
}
}
/// Get the value of this UTXO in satoshis
pub fn value(&self) -> u64 {
self.txout.value
}
/// Check if this UTXO can be spent at the given height
pub fn is_spendable(&self, current_height: u32) -> bool {
if self.is_locked {
return false;
}
if !self.is_coinbase {
// Regular UTXOs need to be confirmed or InstantLocked
self.is_confirmed || self.is_instantlocked
} else {
// Coinbase outputs require 100 confirmations
current_height >= self.height + 100
}
}
/// Check if this UTXO is mature enough for spending
pub fn is_mature(&self, current_height: u32) -> bool {
if self.is_coinbase {
current_height >= self.height + 100
} else {
true
}
}
/// Lock this UTXO to prevent it from being selected
pub fn lock(&mut self) {
self.is_locked = true;
}
/// Unlock this UTXO to allow it to be selected
pub fn unlock(&mut self) {
self.is_locked = false;
}
}
impl Ord for Utxo {
fn cmp(&self, other: &Self) -> Ordering {
// Order by value (ascending)
self.outpoint.cmp(&other.outpoint)
}
}
impl PartialOrd for Utxo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// UTXO set management
#[derive(Debug, Clone)]
pub struct UtxoSet {
/// UTXOs indexed by outpoint
utxos: BTreeMap<OutPoint, Utxo>,
/// Total balance
total_balance: u64,
/// Confirmed balance
confirmed_balance: u64,
/// Unconfirmed balance
unconfirmed_balance: u64,
/// Locked balance
locked_balance: u64,
}
impl UtxoSet {
/// Create a new empty UTXO set
pub fn new() -> Self {
Self {
utxos: BTreeMap::new(),
total_balance: 0,
confirmed_balance: 0,
unconfirmed_balance: 0,
locked_balance: 0,
}
}
/// Add a UTXO to the set
pub fn add(&mut self, utxo: Utxo) {
let value = utxo.value();
// Update balances
self.total_balance += value;
if utxo.is_confirmed || utxo.is_instantlocked {
self.confirmed_balance += value;
} else {
self.unconfirmed_balance += value;
}
if utxo.is_locked {
self.locked_balance += value;
}
self.utxos.insert(utxo.outpoint, utxo);
}
/// Remove a UTXO from the set
pub fn remove(&mut self, outpoint: &OutPoint) -> Option<Utxo> {
if let Some(utxo) = self.utxos.remove(outpoint) {
let value = utxo.value();
// Update balances
self.total_balance = self.total_balance.saturating_sub(value);
if utxo.is_confirmed || utxo.is_instantlocked {
self.confirmed_balance = self.confirmed_balance.saturating_sub(value);
} else {
self.unconfirmed_balance = self.unconfirmed_balance.saturating_sub(value);
}
if utxo.is_locked {
self.locked_balance = self.locked_balance.saturating_sub(value);
}
Some(utxo)
} else {
None
}
}
/// Get a UTXO by outpoint
pub fn get(&self, outpoint: &OutPoint) -> Option<&Utxo> {
self.utxos.get(outpoint)
}
/// Get a mutable UTXO by outpoint
pub fn get_mut(&mut self, outpoint: &OutPoint) -> Option<&mut Utxo> {
self.utxos.get_mut(outpoint)
}
/// Check if a UTXO exists
pub fn contains(&self, outpoint: &OutPoint) -> bool {
self.utxos.contains_key(outpoint)
}
/// Get all UTXOs
pub fn all(&self) -> Vec<&Utxo> {
self.utxos.values().collect()
}
/// Get all spendable UTXOs
pub fn spendable(&self, current_height: u32) -> Vec<&Utxo> {
self.utxos.values().filter(|utxo| utxo.is_spendable(current_height)).collect()
}
/// Get all UTXOs for a specific address
pub fn for_address(&self, address: &Address) -> Vec<&Utxo> {
self.utxos.values().filter(|utxo| &utxo.address == address).collect()
}
/// Get total balance
pub fn total_balance(&self) -> u64 {
self.total_balance
}
/// Get confirmed balance
pub fn confirmed_balance(&self) -> u64 {
self.confirmed_balance
}
/// Get unconfirmed balance
pub fn unconfirmed_balance(&self) -> u64 {
self.unconfirmed_balance
}
/// Get locked balance
pub fn locked_balance(&self) -> u64 {
self.locked_balance
}
/// Get spendable balance
pub fn spendable_balance(&self, current_height: u32) -> u64 {
self.spendable(current_height).iter().map(|utxo| utxo.value()).sum()
}
/// Clear all UTXOs
pub fn clear(&mut self) {
self.utxos.clear();
self.total_balance = 0;
self.confirmed_balance = 0;
self.unconfirmed_balance = 0;
self.locked_balance = 0;
}
/// Get the number of UTXOs
pub fn len(&self) -> usize {
self.utxos.len()
}
/// Check if the set is empty
pub fn is_empty(&self) -> bool {
self.utxos.is_empty()
}
/// Update confirmation status for a UTXO
pub fn update_confirmation(&mut self, outpoint: &OutPoint, confirmed: bool) {
if let Some(utxo) = self.utxos.get_mut(outpoint) {
let value = utxo.value();
if utxo.is_confirmed != confirmed {
if confirmed {
self.confirmed_balance += value;
self.unconfirmed_balance = self.unconfirmed_balance.saturating_sub(value);
} else {
self.unconfirmed_balance += value;
self.confirmed_balance = self.confirmed_balance.saturating_sub(value);
}
utxo.is_confirmed = confirmed;
}
}
}
/// Lock a UTXO
pub fn lock_utxo(&mut self, outpoint: &OutPoint) -> bool {
if let Some(utxo) = self.utxos.get_mut(outpoint) {
if !utxo.is_locked {
utxo.lock();
self.locked_balance += utxo.value();
return true;
}
}
false
}
/// Unlock a UTXO
pub fn unlock_utxo(&mut self, outpoint: &OutPoint) -> bool {
if let Some(utxo) = self.utxos.get_mut(outpoint) {
if utxo.is_locked {
utxo.unlock();
self.locked_balance = self.locked_balance.saturating_sub(utxo.value());
return true;
}
}
false
}
}
impl Default for UtxoSet {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::test_utils::test_utxo_full;
#[test]
fn test_utxo_spendability() {
let mut utxo = test_utxo_full(100000, 100, 0, false);
// Unconfirmed UTXO should not be spendable
assert!(!utxo.is_spendable(200));
// Confirmed UTXO should be spendable
utxo.is_confirmed = true;
assert!(utxo.is_spendable(200));
// Locked UTXO should not be spendable
utxo.lock();
assert!(!utxo.is_spendable(200));
}
#[test]
fn test_utxo_set_operations() {
let mut set = UtxoSet::new();
let utxo1 = test_utxo_full(100000, 100, 0, false);
let utxo2 = test_utxo_full(200000, 150, 1, false); // Different vout to ensure unique OutPoint
set.add(utxo1.clone());
set.add(utxo2.clone());
assert_eq!(set.len(), 2);
assert_eq!(set.total_balance(), 300000);
assert_eq!(set.unconfirmed_balance(), 300000);
assert_eq!(set.confirmed_balance(), 0);
// Update confirmation
set.update_confirmation(&utxo1.outpoint, true);
assert_eq!(set.confirmed_balance(), 100000);
assert_eq!(set.unconfirmed_balance(), 200000);
// Remove UTXO
let removed = set.remove(&utxo1.outpoint);
assert!(removed.is_some());
assert_eq!(set.len(), 1);
assert_eq!(set.total_balance(), 200000);
}
}