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
use sp_runtime::traits::{Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor, HashFor};
use sp_runtime::Justification;
use serde::{Serialize, Deserialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::any::Any;
use crate::Error;
use crate::import_queue::CacheKeyId;
#[derive(Debug, PartialEq, Eq)]
pub enum ImportResult {
Imported(ImportedAux),
AlreadyInChain,
KnownBad,
UnknownParent,
MissingState,
}
#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportedAux {
pub header_only: bool,
pub clear_justification_requests: bool,
pub needs_justification: bool,
pub bad_justification: bool,
pub is_new_best: bool,
}
impl ImportResult {
pub fn imported(is_new_best: bool) -> ImportResult {
let mut aux = ImportedAux::default();
aux.is_new_best = is_new_best;
ImportResult::Imported(aux)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum BlockOrigin {
Genesis,
NetworkInitialSync,
NetworkBroadcast,
ConsensusBroadcast,
Own,
File,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ForkChoiceStrategy {
LongestChain,
Custom(bool),
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BlockCheckParams<Block: BlockT> {
pub hash: Block::Hash,
pub number: NumberFor<Block>,
pub parent_hash: Block::Hash,
pub allow_missing_state: bool,
pub import_existing: bool,
}
#[non_exhaustive]
pub struct BlockImportParams<Block: BlockT, Transaction> {
pub origin: BlockOrigin,
pub header: Block::Header,
pub justification: Option<Justification>,
pub post_digests: Vec<DigestItemFor<Block>>,
pub body: Option<Vec<Block::Extrinsic>>,
pub storage_changes: Option<
sp_state_machine::StorageChanges<Transaction, HashFor<Block>, NumberFor<Block>>
>,
pub finalized: bool,
pub intermediates: HashMap<Cow<'static, [u8]>, Box<dyn Any>>,
pub auxiliary: Vec<(Vec<u8>, Option<Vec<u8>>)>,
pub fork_choice: Option<ForkChoiceStrategy>,
pub allow_missing_state: bool,
pub import_existing: bool,
pub post_hash: Option<Block::Hash>,
}
impl<Block: BlockT, Transaction> BlockImportParams<Block, Transaction> {
pub fn new(
origin: BlockOrigin,
header: Block::Header,
) -> Self {
Self {
origin, header,
justification: None,
post_digests: Vec::new(),
body: None,
storage_changes: None,
finalized: false,
intermediates: HashMap::new(),
auxiliary: Vec::new(),
fork_choice: None,
allow_missing_state: false,
import_existing: false,
post_hash: None,
}
}
pub fn post_hash(&self) -> Block::Hash {
if let Some(hash) = self.post_hash {
hash
} else {
if self.post_digests.is_empty() {
self.header.hash()
} else {
let mut hdr = self.header.clone();
for digest_item in &self.post_digests {
hdr.digest_mut().push(digest_item.clone());
}
hdr.hash()
}
}
}
pub fn convert_transaction<Transaction2>(self) -> BlockImportParams<Block, Transaction2> {
BlockImportParams {
origin: self.origin,
header: self.header,
justification: self.justification,
post_digests: self.post_digests,
body: self.body,
storage_changes: None,
finalized: self.finalized,
auxiliary: self.auxiliary,
intermediates: self.intermediates,
allow_missing_state: self.allow_missing_state,
fork_choice: self.fork_choice,
import_existing: self.import_existing,
post_hash: self.post_hash,
}
}
pub fn take_intermediate<T: 'static>(&mut self, key: &[u8]) -> Result<Box<T>, Error> {
let (k, v) = self.intermediates.remove_entry(key).ok_or(Error::NoIntermediate)?;
match v.downcast::<T>() {
Ok(v) => Ok(v),
Err(v) => {
self.intermediates.insert(k, v);
Err(Error::InvalidIntermediate)
},
}
}
pub fn intermediate<T: 'static>(&self, key: &[u8]) -> Result<&T, Error> {
self.intermediates.get(key)
.ok_or(Error::NoIntermediate)?
.downcast_ref::<T>()
.ok_or(Error::InvalidIntermediate)
}
pub fn intermediate_mut<T: 'static>(&mut self, key: &[u8]) -> Result<&mut T, Error> {
self.intermediates.get_mut(key)
.ok_or(Error::NoIntermediate)?
.downcast_mut::<T>()
.ok_or(Error::InvalidIntermediate)
}
}
pub trait BlockImport<B: BlockT> {
type Error: std::error::Error + Send + 'static;
type Transaction;
fn check_block(
&mut self,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error>;
fn import_block(
&mut self,
block: BlockImportParams<B, Self::Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error>;
}
impl<B: BlockT, Transaction> BlockImport<B> for crate::import_queue::BoxBlockImport<B, Transaction> {
type Error = crate::error::Error;
type Transaction = Transaction;
fn check_block(
&mut self,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
(**self).check_block(block)
}
fn import_block(
&mut self,
block: BlockImportParams<B, Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(**self).import_block(block, cache)
}
}
impl<B: BlockT, T, E: std::error::Error + Send + 'static, Transaction> BlockImport<B> for Arc<T>
where for<'r> &'r T: BlockImport<B, Error = E, Transaction = Transaction>
{
type Error = E;
type Transaction = Transaction;
fn check_block(
&mut self,
block: BlockCheckParams<B>,
) -> Result<ImportResult, Self::Error> {
(&**self).check_block(block)
}
fn import_block(
&mut self,
block: BlockImportParams<B, Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(&**self).import_block(block, cache)
}
}
pub trait JustificationImport<B: BlockT> {
type Error: std::error::Error + Send + 'static;
fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }
fn import_justification(
&mut self,
hash: B::Hash,
number: NumberFor<B>,
justification: Justification,
) -> Result<(), Self::Error>;
}