-
Notifications
You must be signed in to change notification settings - Fork 992
/
Copy pathmasp.rs
217 lines (194 loc) · 5.79 KB
/
masp.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
use std::fmt::Debug;
use color_eyre::owo_colors::OwoColorize;
use masp_primitives::sapling::ViewingKey;
use masp_primitives::zip32::ExtendedSpendingKey;
use namada_sdk::error::Error;
use namada_sdk::io::Io;
use namada_sdk::masp::{
IndexedNoteEntry, ProgressLogger, ProgressType, ShieldedContext,
ShieldedUtils,
};
use namada_sdk::queries::Client;
use namada_sdk::storage::BlockHeight;
use namada_sdk::{display, display_line, MaybeSend, MaybeSync};
pub async fn syncing_with_height_increment<
U: ShieldedUtils + MaybeSend + MaybeSync,
C: Client + Sync,
IO: Io,
>(
mut shielded: ShieldedContext<U>,
client: &C,
io: &IO,
batch_size: u64,
mut last_query_height: Option<BlockHeight>,
sks: &[ExtendedSpendingKey],
fvks: &[ViewingKey],
) -> Result<ShieldedContext<U>, Error> {
let shutdown_signal = async {
let (tx, rx) = tokio::sync::oneshot::channel();
namada_sdk::control_flow::shutdown_send(tx).await;
rx.await
};
// add function to check if new spending key populates on
// wallet list
display_line!(io, "{}", "==== Shielded sync started first step ====".on_white());
display_line!(io, "\n\n");
let logger = CliLogger::new(io);
// Increment the height based on the current value
if let Some(mut height) = last_query_height {
if height < BlockHeight(1000) {
height += BlockHeight(1000);
} else if height < BlockHeight(10000) {
height += BlockHeight(10000);
} else {
height += BlockHeight(100000);
}
last_query_height = Some(height);
}
let sync = async move {
shielded
.fetch(client, &logger, last_query_height, batch_size, sks, fvks)
.await
.map(|_| shielded)
};
tokio::select! {
sync = sync => {
let shielded = sync?;
display!(io, "Syncing finished\n");
Ok(shielded)
},
sig = shutdown_signal => {
sig.map_err(|e| Error::Other(e.to_string()))?;
display!(io, "\n");
Ok(ShieldedContext::default())
},
}
}
pub async fn syncing<
U: ShieldedUtils + MaybeSend + MaybeSync,
C: Client + Sync,
IO: Io,
>(
mut shielded: ShieldedContext<U>,
client: &C,
io: &IO,
batch_size: u64,
last_query_height: Option<BlockHeight>,
sks: &[ExtendedSpendingKey],
fvks: &[ViewingKey],
) -> Result<ShieldedContext<U>, Error> {
let shutdown_signal = async {
let (tx, rx) = tokio::sync::oneshot::channel();
namada_sdk::control_flow::shutdown_send(tx).await;
rx.await
};
display_line!(io, "{}", "==== Shielded sync started ====".on_white());
display_line!(io, "\n\n");
let logger = CliLogger::new(io);
let sync = async move {
shielded
.fetch(client, &logger, last_query_height, batch_size, sks, fvks)
.await
.map(|_| shielded)
};
tokio::select! {
sync = sync => {
let shielded = sync?;
display!(io, "Syncing finished\n");
Ok(shielded)
},
sig = shutdown_signal => {
sig.map_err(|e| Error::Other(e.to_string()))?;
display!(io, "\n");
Ok(ShieldedContext::default())
},
}
}
pub struct CliLogging<'io, T, IO: Io> {
items: Vec<T>,
index: usize,
length: usize,
io: &'io IO,
r#type: ProgressType,
}
impl<'io, T: Debug, IO: Io> CliLogging<'io, T, IO> {
fn new<I>(items: I, io: &'io IO, r#type: ProgressType) -> Self
where
I: IntoIterator<Item = T>,
{
let items: Vec<_> = items.into_iter().collect();
Self {
length: items.len(),
items,
index: 0,
io,
r#type,
}
}
}
impl<'io, T: Debug, IO: Io> Iterator for CliLogging<'io, T, IO> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.index == 0 {
self.items = {
let mut new_items = vec![];
std::mem::swap(&mut new_items, &mut self.items);
new_items.into_iter().rev().collect()
};
}
if self.items.is_empty() {
return None;
}
self.index += 1;
let percent = (100 * self.index) / self.length;
let completed: String = vec!['#'; percent].iter().collect();
let incomplete: String = vec!['.'; 100 - percent].iter().collect();
display_line!(self.io, "\x1b[2A\x1b[J");
match self.r#type {
ProgressType::Fetch => display_line!(
self.io,
"Fetched block {:?} of {:?}",
self.items.last().unwrap(),
self.items[0]
),
ProgressType::Scan => display_line!(
self.io,
"Scanning {} of {}",
self.index,
self.length
),
}
display!(self.io, "[{}{}] ~~ {} %", completed, incomplete, percent);
self.io.flush();
self.items.pop()
}
}
/// A progress logger for the CLI
#[derive(Debug, Clone)]
pub struct CliLogger<'io, IO: Io> {
io: &'io IO,
}
impl<'io, IO: Io> CliLogger<'io, IO> {
pub fn new(io: &'io IO) -> Self {
Self { io }
}
}
impl<'io, IO: Io> ProgressLogger<IO> for CliLogger<'io, IO> {
type Fetch = CliLogging<'io, u64, IO>;
type Scan = CliLogging<'io, IndexedNoteEntry, IO>;
fn io(&self) -> &IO {
self.io
}
fn fetch<I>(&self, items: I) -> Self::Fetch
where
I: IntoIterator<Item = u64>,
{
CliLogging::new(items, self.io, ProgressType::Fetch)
}
fn scan<I>(&self, items: I) -> Self::Scan
where
I: IntoIterator<Item = IndexedNoteEntry>,
{
CliLogging::new(items, self.io, ProgressType::Scan)
}
}