/
transition_frontier_sync_ledger_snarked_state.rs
349 lines (318 loc) · 10.6 KB
/
transition_frontier_sync_ledger_snarked_state.rs
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
use std::collections::{BTreeMap, VecDeque};
use mina_p2p_messages::v2::LedgerHash;
use redux::Timestamp;
use serde::{Deserialize, Serialize};
use crate::ledger::{tree_height_for_num_accounts, LedgerAddress};
use crate::p2p::channels::rpc::P2pRpcId;
use crate::p2p::PeerId;
use crate::rpc::LedgerSyncProgress;
use crate::transition_frontier::sync::ledger::SyncLedgerTarget;
use super::{PeerLedgerQueryError, ACCOUNT_SUBTREE_HEIGHT};
static SYNC_PENDING_EMPTY: BTreeMap<LedgerAddress, LedgerAddressQueryPending> = BTreeMap::new();
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TransitionFrontierSyncLedgerSnarkedState {
/// Doing BFS to sync snarked ledger tree.
Pending {
time: Timestamp,
target: SyncLedgerTarget,
num_accounts: u64,
num_accounts_accepted: u64,
num_hashes_accepted: u64,
/// Queue of addresses to query and the expected contents hash
queue: VecDeque<LedgerQueryQueued>,
/// Pending ongoing address queries and their attempts
#[serde_as(as = "Vec<(_, _)>")]
pending_addresses: BTreeMap<LedgerAddress, LedgerAddressQueryPending>,
/// Pending num account query attempts
pending_num_accounts: Option<LedgerNumAccountsQueryPending>,
},
Success {
time: Timestamp,
target: SyncLedgerTarget,
},
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum LedgerQueryQueued {
NumAccounts,
Address {
address: LedgerAddress,
expected_hash: LedgerHash,
},
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LedgerAddressQueryPending {
pub time: Timestamp,
pub expected_hash: LedgerHash,
pub attempts: BTreeMap<PeerId, PeerRpcState>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LedgerNumAccountsQueryPending {
pub time: Timestamp,
pub attempts: BTreeMap<PeerId, PeerRpcState>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PeerRpcState {
Init {
time: Timestamp,
},
Pending {
time: Timestamp,
rpc_id: P2pRpcId,
},
Error {
time: Timestamp,
rpc_id: P2pRpcId,
error: PeerLedgerQueryError,
},
Success {
time: Timestamp,
rpc_id: P2pRpcId,
},
}
impl PeerRpcState {
pub fn pending_rpc_id(&self) -> Option<P2pRpcId> {
match self {
Self::Pending { rpc_id, .. } => Some(*rpc_id),
_ => None,
}
}
pub fn rpc_id(&self) -> Option<P2pRpcId> {
match self {
Self::Init { .. } => None,
Self::Pending { rpc_id, .. } => Some(*rpc_id),
Self::Error { rpc_id, .. } => Some(*rpc_id),
Self::Success { rpc_id, .. } => Some(*rpc_id),
}
}
pub fn is_pending(&self) -> bool {
matches!(self, Self::Pending { .. })
}
pub fn is_error(&self) -> bool {
matches!(self, Self::Error { .. })
}
pub fn is_success(&self) -> bool {
matches!(self, Self::Success { .. })
}
}
impl TransitionFrontierSyncLedgerSnarkedState {
pub fn pending(time: Timestamp, target: SyncLedgerTarget) -> Self {
Self::Pending {
time,
target,
num_accounts: 0,
num_accounts_accepted: 0,
num_hashes_accepted: 0,
queue: vec![LedgerQueryQueued::NumAccounts].into(),
pending_addresses: Default::default(),
pending_num_accounts: Default::default(),
}
}
pub fn target(&self) -> &SyncLedgerTarget {
match self {
Self::Pending { target, .. } => target,
Self::Success { target, .. } => target,
}
}
pub fn ledger_hash(&self) -> &LedgerHash {
&self.target().snarked_ledger_hash
}
pub fn is_num_accounts_query_next(&self) -> bool {
match self {
Self::Pending { queue, .. } => queue
.front()
.map_or(false, |q| matches!(q, LedgerQueryQueued::NumAccounts)),
_ => false,
}
}
pub fn num_accounts_pending(&self) -> Option<&LedgerNumAccountsQueryPending> {
match self {
Self::Pending {
pending_num_accounts,
..
} => pending_num_accounts.as_ref(),
_ => None,
}
}
pub fn fetch_pending(&self) -> Option<&BTreeMap<LedgerAddress, LedgerAddressQueryPending>> {
match self {
Self::Pending {
pending_addresses, ..
} => Some(pending_addresses),
_ => None,
}
}
pub fn sync_address_retry_iter(&self) -> impl '_ + Iterator<Item = LedgerAddress> {
let pending = match self {
Self::Pending {
pending_addresses, ..
} => pending_addresses,
_ => &SYNC_PENDING_EMPTY,
};
pending
.iter()
.filter(|(_, s)| s.attempts.values().all(|s| s.is_error()))
.map(|(addr, _)| addr.clone())
}
pub fn sync_address_next(&self) -> Option<(LedgerAddress, LedgerHash)> {
match self {
Self::Pending { queue, .. } => match queue.front().map(|a| a.clone()) {
Some(LedgerQueryQueued::Address {
address,
expected_hash,
}) => Some((address, expected_hash)),
_ => None,
},
_ => None,
}
}
pub fn estimation(&self) -> Option<LedgerSyncProgress> {
match self {
TransitionFrontierSyncLedgerSnarkedState::Pending {
num_accounts,
num_accounts_accepted,
num_hashes_accepted,
..
} if *num_accounts > 0 => {
// TODO(tizoc): this approximation is very rough, could be improved
let tree_height = tree_height_for_num_accounts(*num_accounts);
let fill_ratio = (*num_accounts as f64) / 2f64.powf(tree_height as f64);
let num_hashes_estimate = 2u64.pow((tree_height - ACCOUNT_SUBTREE_HEIGHT) as u32);
let num_hashes_estimate = (num_hashes_estimate as f64 * fill_ratio).ceil() as u64;
let fetched = *num_accounts_accepted + num_hashes_accepted;
let estimation = fetched.max(*num_accounts + num_hashes_estimate);
Some(LedgerSyncProgress {
fetched,
estimation,
})
}
TransitionFrontierSyncLedgerSnarkedState::Success { .. } => {
return Some(LedgerSyncProgress {
fetched: 1,
estimation: 1,
})
}
_ => None,
}
}
pub fn peer_num_account_query_get(
&self,
peer_id: &PeerId,
rpc_id: P2pRpcId,
) -> Option<&LedgerNumAccountsQueryPending> {
match self {
Self::Pending {
pending_num_accounts: Some(pending),
..
} => {
let expected_rpc_id = rpc_id;
pending.attempts.get(peer_id).and_then(|s| {
if s.rpc_id()? == expected_rpc_id {
Some(pending)
} else {
None
}
})
}
_ => None,
}
}
pub fn peer_num_account_query_state_get_mut(
&mut self,
peer_id: &PeerId,
rpc_id: P2pRpcId,
) -> Option<&mut PeerRpcState> {
match self {
Self::Pending {
pending_num_accounts,
..
} => {
let expected_rpc_id = rpc_id;
pending_num_accounts
.as_mut()?
.attempts
.get_mut(peer_id)
.filter(|s| match s {
PeerRpcState::Pending { rpc_id, .. } => *rpc_id == expected_rpc_id,
PeerRpcState::Error { rpc_id, .. } => *rpc_id == expected_rpc_id,
PeerRpcState::Success { rpc_id, .. } => *rpc_id == expected_rpc_id,
_ => false,
})
}
_ => None,
}
}
pub fn peer_address_query_get(
&self,
peer_id: &PeerId,
rpc_id: P2pRpcId,
) -> Option<(&LedgerAddress, &LedgerAddressQueryPending)> {
match self {
Self::Pending {
pending_addresses, ..
} => {
let expected_rpc_id = rpc_id;
pending_addresses.iter().find(|(_, s)| {
s.attempts
.get(peer_id)
.map_or(false, |s| s.rpc_id() == Some(expected_rpc_id))
})
}
_ => None,
}
}
pub fn peer_address_query_state_get_mut(
&mut self,
peer_id: &PeerId,
rpc_id: P2pRpcId,
) -> Option<&mut PeerRpcState> {
match self {
Self::Pending {
pending_addresses: pending,
..
} => {
let expected_rpc_id = rpc_id;
pending.iter_mut().find_map(|(_, s)| {
s.attempts.get_mut(peer_id).filter(|s| match s {
PeerRpcState::Pending { rpc_id, .. } => *rpc_id == expected_rpc_id,
PeerRpcState::Error { rpc_id, .. } => *rpc_id == expected_rpc_id,
PeerRpcState::Success { rpc_id, .. } => *rpc_id == expected_rpc_id,
_ => false,
})
})
}
_ => None,
}
}
pub fn peer_address_query_pending_rpc_ids<'a>(
&'a self,
peer_id: &'a PeerId,
) -> impl 'a + Iterator<Item = P2pRpcId> {
let pending = match self {
Self::Pending {
pending_addresses, ..
} => pending_addresses,
_ => &SYNC_PENDING_EMPTY,
};
pending.values().filter_map(move |s| {
s.attempts
.iter()
.find(|(id, _)| *id == peer_id)
.and_then(|(_, s)| s.pending_rpc_id())
})
}
pub fn peer_num_accounts_rpc_id(&self, peer_id: &PeerId) -> Option<P2pRpcId> {
let pending = match self {
Self::Pending {
pending_num_accounts,
..
} => pending_num_accounts.as_ref(),
_ => None,
};
pending?
.attempts
.iter()
.find(|(id, _)| *id == peer_id)
.and_then(|(_, s)| s.pending_rpc_id())
}
}