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
use std::time::Instant;
use bytes::Bytes;
use vapory_types::H256;
use tetsy_rlp::{Rlp, RlpStream, DecoderError};
pub enum Snapshotting {
Unsupported,
PoW {
blocks: u64,
max_restore_blocks: u64
},
PoA,
}
#[derive(Debug)]
pub struct Progress {
accounts: u64,
prev_accounts: u64,
pub blocks: u64,
bytes: u64,
prev_bytes: u64,
pub done: bool,
pub abort: bool,
last_tick: Instant,
}
impl Progress {
pub fn new() -> Progress {
Progress {
accounts: 0,
prev_accounts: 0,
blocks: 0,
bytes: 0,
prev_bytes: 0,
abort: false,
done: false,
last_tick: Instant::now(),
}
}
pub fn accounts(&self) -> u64 { self.accounts }
pub fn blocks(&self) -> u64 { self.blocks }
pub fn bytes(&self) -> u64 { self.bytes }
pub fn done(&self) -> bool { self.done }
pub fn rate(&self) -> (f64, f64) {
let dt = self.last_tick.elapsed().as_secs_f64();
if dt < 1.0 {
return (0f64, 0f64);
}
let delta_acc = self.accounts.saturating_sub(self.prev_accounts);
let delta_bytes = self.bytes.saturating_sub(self.prev_bytes);
(delta_acc as f64 / dt, delta_bytes as f64 / dt)
}
pub fn update(&mut self, accounts_delta: u64, bytes_delta: u64) {
self.last_tick = Instant::now();
self.prev_accounts = self.accounts;
self.prev_bytes = self.bytes;
self.accounts += accounts_delta;
self.bytes += bytes_delta;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ManifestData {
pub version: u64,
pub state_hashes: Vec<H256>,
pub block_hashes: Vec<H256>,
pub state_root: H256,
pub block_number: u64,
pub block_hash: H256,
}
impl ManifestData {
pub fn into_rlp(self) -> Bytes {
let mut stream = RlpStream::new_list(6);
stream.append(&self.version);
stream.append_list(&self.state_hashes);
stream.append_list(&self.block_hashes);
stream.append(&self.state_root);
stream.append(&self.block_number);
stream.append(&self.block_hash);
stream.out()
}
pub fn from_rlp(raw: &[u8]) -> Result<Self, DecoderError> {
let decoder = Rlp::new(raw);
let (start, version) = if decoder.item_count()? == 5 {
(0, 1)
} else {
(1, decoder.val_at(0)?)
};
let state_hashes: Vec<H256> = decoder.list_at(start + 0)?;
let block_hashes: Vec<H256> = decoder.list_at(start + 1)?;
let state_root: H256 = decoder.val_at(start + 2)?;
let block_number: u64 = decoder.val_at(start + 3)?;
let block_hash: H256 = decoder.val_at(start + 4)?;
Ok(ManifestData {
version,
state_hashes,
block_hashes,
state_root,
block_number,
block_hash,
})
}
}
pub type ChunkSink<'a> = dyn FnMut(&[u8]) -> std::io::Result<()> + 'a;
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum RestorationStatus {
Inactive,
Initializing {
state_chunks: u32,
block_chunks: u32,
chunks_done: u32,
},
Ongoing {
state_chunks: u32,
block_chunks: u32,
state_chunks_done: u32,
block_chunks_done: u32,
},
Finalizing,
Failed,
}