1
1
use std:: sync:: Arc ;
2
2
3
3
use chia_client:: { Error , Peer , PeerEvent } ;
4
+ use tokio:: sync:: mpsc;
4
5
5
6
use crate :: { CoinStore , DerivationStore } ;
6
7
@@ -25,6 +26,7 @@ pub async fn incremental_sync(
25
26
derivation_store : Arc < impl DerivationStore > ,
26
27
coin_store : Arc < impl CoinStore > ,
27
28
config : SyncConfig ,
29
+ synced_sender : mpsc:: Sender < ( ) > ,
28
30
) -> Result < ( ) , Error < ( ) > > {
29
31
let mut event_receiver = peer. receiver ( ) . resubscribe ( ) ;
30
32
@@ -47,6 +49,8 @@ pub async fn incremental_sync(
47
49
)
48
50
. await ?;
49
51
52
+ synced_sender. send ( ( ) ) . await . ok ( ) ;
53
+
50
54
while let Ok ( event) = event_receiver. recv ( ) . await {
51
55
if let PeerEvent :: CoinStateUpdate ( update) = event {
52
56
coin_store. update_coin_state ( update. items ) . await ;
@@ -57,13 +61,16 @@ pub async fn incremental_sync(
57
61
& config,
58
62
)
59
63
. await ?;
64
+
65
+ synced_sender. send ( ( ) ) . await . ok ( ) ;
60
66
}
61
67
}
62
68
63
69
Ok ( ( ) )
64
70
}
65
71
66
- async fn subscribe (
72
+ /// Subscribe to another set of puzzle hashes.
73
+ pub async fn subscribe (
67
74
peer : & Peer ,
68
75
coin_store : & impl CoinStore ,
69
76
puzzle_hashes : Vec < [ u8 ; 32 ] > ,
@@ -75,7 +82,8 @@ async fn subscribe(
75
82
Ok ( ( ) )
76
83
}
77
84
78
- async fn derive_more (
85
+ /// Create more derivations for a wallet.
86
+ pub async fn derive_more (
79
87
peer : & Peer ,
80
88
derivation_store : & impl DerivationStore ,
81
89
coin_store : & impl CoinStore ,
@@ -93,21 +101,26 @@ async fn derive_more(
93
101
subscribe ( peer, coin_store, puzzle_hashes) . await
94
102
}
95
103
96
- async fn unused_index (
104
+ /// Gets the last unused derivation index for a wallet.
105
+ pub async fn unused_index (
97
106
derivation_store : & impl DerivationStore ,
98
107
coin_store : & impl CoinStore ,
99
108
) -> Option < u32 > {
100
109
let derivations = derivation_store. derivations ( ) . await ;
110
+ let mut unused_index = None ;
101
111
for index in ( 0 ..derivations) . rev ( ) {
102
112
let puzzle_hash = derivation_store. puzzle_hash ( index) . await . unwrap ( ) ;
103
113
if !coin_store. is_used ( puzzle_hash) . await {
104
- return Some ( index) ;
114
+ unused_index = Some ( index) ;
115
+ } else {
116
+ break ;
105
117
}
106
118
}
107
- None
119
+ unused_index
108
120
}
109
121
110
- async fn sync_to_unused_index (
122
+ /// Syncs a wallet such that there are enough unused derivations.
123
+ pub async fn sync_to_unused_index (
111
124
peer : & Peer ,
112
125
derivation_store : & impl DerivationStore ,
113
126
coin_store : & impl CoinStore ,
@@ -132,7 +145,7 @@ async fn sync_to_unused_index(
132
145
133
146
if let Some ( unused_index) = result {
134
147
// Calculate the extra unused derivations after that index.
135
- let extra_indices = derivations - 1 - unused_index;
148
+ let extra_indices = derivations - unused_index;
136
149
137
150
// Make sure at least `gap` indices are available if needed.
138
151
if extra_indices < config. minimum_unused_derivations {
0 commit comments