1use std::collections::{HashMap, HashSet};
22
23use parking_lot::RwLock;
24
25use sp_api::CallContext;
26use sp_consensus::BlockOrigin;
27use sp_core::{offchain::OffchainStorage, H256};
28use sp_runtime::{
29 traits::{Block as BlockT, HashingFor, NumberFor},
30 Justification, Justifications, StateVersion, Storage,
31};
32use sp_state_machine::{
33 backend::AsTrieBackend, ChildStorageCollection, IndexOperation, IterArgs,
34 OffchainChangesCollection, StorageCollection, StorageIterator,
35};
36use sp_storage::{ChildInfo, StorageData, StorageKey};
37pub use sp_trie::MerkleValue;
38
39use crate::{blockchain::Backend as BlockchainBackend, UsageInfo};
40
41pub use sp_state_machine::{Backend as StateBackend, BackendTransaction, KeyValueStates};
42
43pub type StateBackendFor<B, Block> = <B as Backend<Block>>::State;
45
46#[derive(Debug, Clone, Copy)]
48pub enum ImportNotificationAction {
49 RecentBlock,
51 EveryBlock,
53 Both,
55 None,
57}
58
59pub struct ImportSummary<Block: BlockT> {
64 pub hash: Block::Hash,
66 pub origin: BlockOrigin,
68 pub header: Block::Header,
70 pub is_new_best: bool,
72 pub storage_changes: Option<(StorageCollection, ChildStorageCollection)>,
74 pub tree_route: Option<sp_blockchain::TreeRoute<Block>>,
78 pub import_notification_action: ImportNotificationAction,
80}
81
82#[derive(Clone, Debug)]
84pub struct StaleBlock<Block: BlockT> {
85 pub hash: Block::Hash,
87 pub is_head: bool,
89}
90
91pub struct FinalizeSummary<Block: BlockT> {
96 pub header: Block::Header,
98 pub finalized: Vec<Block::Hash>,
102 pub stale_blocks: Vec<StaleBlock<Block>>,
104}
105
106pub struct ClientImportOperation<Block: BlockT, B: Backend<Block>> {
108 pub op: B::BlockImportOperation,
110 pub notify_imported: Option<ImportSummary<Block>>,
112 pub notify_finalized: Option<FinalizeSummary<Block>>,
114}
115
116pub fn apply_aux<'a, 'b: 'a, 'c: 'a, B, Block, D, I>(
118 operation: &mut ClientImportOperation<Block, B>,
119 insert: I,
120 delete: D,
121) -> sp_blockchain::Result<()>
122where
123 Block: BlockT,
124 B: Backend<Block>,
125 I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
126 D: IntoIterator<Item = &'a &'b [u8]>,
127{
128 operation.op.insert_aux(
129 insert
130 .into_iter()
131 .map(|(k, v)| (k.to_vec(), Some(v.to_vec())))
132 .chain(delete.into_iter().map(|k| (k.to_vec(), None))),
133 )
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub enum NewBlockState {
139 Normal,
141 Best,
143 Final,
145}
146
147impl NewBlockState {
148 pub fn is_best(self) -> bool {
150 match self {
151 NewBlockState::Best | NewBlockState::Final => true,
152 NewBlockState::Normal => false,
153 }
154 }
155
156 pub fn is_final(self) -> bool {
158 match self {
159 NewBlockState::Final => true,
160 NewBlockState::Best | NewBlockState::Normal => false,
161 }
162 }
163}
164
165#[derive(Default, Debug, Clone)]
167pub struct PrefetchedIndexedTransactions {
168 pub ops: Vec<IndexOperation>,
170
171 pub renew_payloads: HashMap<H256, Vec<u8>>,
173}
174
175pub trait BlockImportOperation<Block: BlockT> {
179 type State: StateBackend<HashingFor<Block>>;
181
182 fn state(&self) -> sp_blockchain::Result<Option<&Self::State>>;
186
187 fn set_block_data(
199 &mut self,
200 header: Block::Header,
201 body: Option<Vec<Block::Extrinsic>>,
202 indexed_body: Option<Vec<Vec<u8>>>,
203 justifications: Option<Justifications>,
204 state: NewBlockState,
205 register_as_leaf: bool,
206 ) -> sp_blockchain::Result<()>;
207
208 fn update_db_storage(
210 &mut self,
211 update: BackendTransaction<HashingFor<Block>>,
212 ) -> sp_blockchain::Result<()>;
213
214 fn set_genesis_state(
217 &mut self,
218 storage: Storage,
219 commit: bool,
220 state_version: StateVersion,
221 ) -> sp_blockchain::Result<Block::Hash>;
222
223 fn reset_storage(
225 &mut self,
226 storage: Storage,
227 state_version: StateVersion,
228 ) -> sp_blockchain::Result<Block::Hash>;
229
230 fn update_storage(
232 &mut self,
233 update: StorageCollection,
234 child_update: ChildStorageCollection,
235 ) -> sp_blockchain::Result<()>;
236
237 fn update_offchain_storage(
239 &mut self,
240 _offchain_update: OffchainChangesCollection,
241 ) -> sp_blockchain::Result<()> {
242 Ok(())
243 }
244
245 fn insert_aux<I>(&mut self, ops: I) -> sp_blockchain::Result<()>
249 where
250 I: IntoIterator<Item = (Vec<u8>, Option<Vec<u8>>)>;
251
252 fn mark_finalized(
255 &mut self,
256 hash: Block::Hash,
257 justification: Option<Justification>,
258 ) -> sp_blockchain::Result<()>;
259
260 fn mark_head(&mut self, hash: Block::Hash) -> sp_blockchain::Result<()>;
263
264 fn update_transaction_index(&mut self, index: Vec<IndexOperation>)
266 -> sp_blockchain::Result<()>;
267
268 fn set_renew_payloads(&mut self, payloads: HashMap<H256, Vec<u8>>)
271 -> sp_blockchain::Result<()>;
272
273 fn set_create_gap(&mut self, create_gap: bool);
275}
276
277pub trait LockImportRun<Block: BlockT, B: Backend<Block>> {
279 fn lock_import_and_run<R, Err, F>(&self, f: F) -> Result<R, Err>
281 where
282 F: FnOnce(&mut ClientImportOperation<Block, B>) -> Result<R, Err>,
283 Err: From<sp_blockchain::Error>;
284}
285
286pub trait Finalizer<Block: BlockT, B: Backend<Block>> {
288 fn apply_finality(
298 &self,
299 operation: &mut ClientImportOperation<Block, B>,
300 block: Block::Hash,
301 justification: Option<Justification>,
302 notify: bool,
303 ) -> sp_blockchain::Result<()>;
304
305 fn finalize_block(
319 &self,
320 block: Block::Hash,
321 justification: Option<Justification>,
322 notify: bool,
323 ) -> sp_blockchain::Result<()>;
324}
325
326pub trait AuxStore {
332 fn insert_aux<
336 'a,
337 'b: 'a,
338 'c: 'a,
339 I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
340 D: IntoIterator<Item = &'a &'b [u8]>,
341 >(
342 &self,
343 insert: I,
344 delete: D,
345 ) -> sp_blockchain::Result<()>;
346
347 fn get_aux(&self, key: &[u8]) -> sp_blockchain::Result<Option<Vec<u8>>>;
349}
350
351pub struct KeysIter<State, Block>
353where
354 State: StateBackend<HashingFor<Block>>,
355 Block: BlockT,
356{
357 inner: <State as StateBackend<HashingFor<Block>>>::RawIter,
358 state: State,
359}
360
361impl<State, Block> KeysIter<State, Block>
362where
363 State: StateBackend<HashingFor<Block>>,
364 Block: BlockT,
365{
366 pub fn new(
368 state: State,
369 prefix: Option<&StorageKey>,
370 start_at: Option<&StorageKey>,
371 ) -> Result<Self, State::Error> {
372 let mut args = IterArgs::default();
373 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
374 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
375 args.start_at_exclusive = true;
376
377 Ok(Self { inner: state.raw_iter(args)?, state })
378 }
379
380 pub fn new_child(
382 state: State,
383 child_info: ChildInfo,
384 prefix: Option<&StorageKey>,
385 start_at: Option<&StorageKey>,
386 ) -> Result<Self, State::Error> {
387 let mut args = IterArgs::default();
388 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
389 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
390 args.child_info = Some(child_info);
391 args.start_at_exclusive = true;
392
393 Ok(Self { inner: state.raw_iter(args)?, state })
394 }
395}
396
397impl<State, Block> Iterator for KeysIter<State, Block>
398where
399 Block: BlockT,
400 State: StateBackend<HashingFor<Block>>,
401{
402 type Item = StorageKey;
403
404 fn next(&mut self) -> Option<Self::Item> {
405 self.inner.next_key(&self.state)?.ok().map(StorageKey)
406 }
407}
408
409pub struct PairsIter<State, Block>
411where
412 State: StateBackend<HashingFor<Block>>,
413 Block: BlockT,
414{
415 inner: <State as StateBackend<HashingFor<Block>>>::RawIter,
416 state: State,
417}
418
419impl<State, Block> Iterator for PairsIter<State, Block>
420where
421 Block: BlockT,
422 State: StateBackend<HashingFor<Block>>,
423{
424 type Item = (StorageKey, StorageData);
425
426 fn next(&mut self) -> Option<Self::Item> {
427 self.inner
428 .next_pair(&self.state)?
429 .ok()
430 .map(|(key, value)| (StorageKey(key), StorageData(value)))
431 }
432}
433
434impl<State, Block> PairsIter<State, Block>
435where
436 State: StateBackend<HashingFor<Block>>,
437 Block: BlockT,
438{
439 pub fn new(
441 state: State,
442 prefix: Option<&StorageKey>,
443 start_at: Option<&StorageKey>,
444 ) -> Result<Self, State::Error> {
445 let mut args = IterArgs::default();
446 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
447 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
448 args.start_at_exclusive = true;
449
450 Ok(Self { inner: state.raw_iter(args)?, state })
451 }
452}
453
454pub trait StorageProvider<Block: BlockT, B: Backend<Block>> {
456 fn storage(
458 &self,
459 hash: Block::Hash,
460 key: &StorageKey,
461 ) -> sp_blockchain::Result<Option<StorageData>>;
462
463 fn storage_hash(
465 &self,
466 hash: Block::Hash,
467 key: &StorageKey,
468 ) -> sp_blockchain::Result<Option<Block::Hash>>;
469
470 fn storage_keys(
473 &self,
474 hash: Block::Hash,
475 prefix: Option<&StorageKey>,
476 start_key: Option<&StorageKey>,
477 ) -> sp_blockchain::Result<KeysIter<B::State, Block>>;
478
479 fn storage_pairs(
482 &self,
483 hash: <Block as BlockT>::Hash,
484 prefix: Option<&StorageKey>,
485 start_key: Option<&StorageKey>,
486 ) -> sp_blockchain::Result<PairsIter<B::State, Block>>;
487
488 fn child_storage(
491 &self,
492 hash: Block::Hash,
493 child_info: &ChildInfo,
494 key: &StorageKey,
495 ) -> sp_blockchain::Result<Option<StorageData>>;
496
497 fn child_storage_keys(
500 &self,
501 hash: Block::Hash,
502 child_info: ChildInfo,
503 prefix: Option<&StorageKey>,
504 start_key: Option<&StorageKey>,
505 ) -> sp_blockchain::Result<KeysIter<B::State, Block>>;
506
507 fn child_storage_hash(
510 &self,
511 hash: Block::Hash,
512 child_info: &ChildInfo,
513 key: &StorageKey,
514 ) -> sp_blockchain::Result<Option<Block::Hash>>;
515
516 fn closest_merkle_value(
518 &self,
519 hash: Block::Hash,
520 key: &StorageKey,
521 ) -> sp_blockchain::Result<Option<MerkleValue<Block::Hash>>>;
522
523 fn child_closest_merkle_value(
525 &self,
526 hash: Block::Hash,
527 child_info: &ChildInfo,
528 key: &StorageKey,
529 ) -> sp_blockchain::Result<Option<MerkleValue<Block::Hash>>>;
530}
531
532#[derive(Debug, Clone, Copy)]
536pub enum TrieCacheContext {
537 Trusted,
545 Untrusted,
549}
550
551impl From<CallContext> for TrieCacheContext {
552 fn from(call_context: CallContext) -> Self {
553 match call_context {
554 CallContext::Onchain { .. } => TrieCacheContext::Trusted,
555 CallContext::Offchain => TrieCacheContext::Untrusted,
556 }
557 }
558}
559
560pub trait Backend<Block: BlockT>: AuxStore + Send + Sync {
583 type BlockImportOperation: BlockImportOperation<Block, State = Self::State>;
585 type Blockchain: BlockchainBackend<Block>;
587 type State: StateBackend<HashingFor<Block>>
589 + Send
590 + AsTrieBackend<
591 HashingFor<Block>,
592 TrieBackendStorage = <Self::State as StateBackend<HashingFor<Block>>>::TrieBackendStorage,
593 >;
594 type OffchainStorage: OffchainStorage;
596
597 fn begin_operation(&self) -> sp_blockchain::Result<Self::BlockImportOperation>;
601
602 fn begin_state_operation(
604 &self,
605 operation: &mut Self::BlockImportOperation,
606 block: Block::Hash,
607 ) -> sp_blockchain::Result<()>;
608
609 fn commit_operation(
611 &self,
612 transaction: Self::BlockImportOperation,
613 ) -> sp_blockchain::Result<()>;
614
615 fn finalize_block(
619 &self,
620 hash: Block::Hash,
621 justification: Option<Justification>,
622 ) -> sp_blockchain::Result<()>;
623
624 fn append_justification(
628 &self,
629 hash: Block::Hash,
630 justification: Justification,
631 ) -> sp_blockchain::Result<()>;
632
633 fn blockchain(&self) -> &Self::Blockchain;
635
636 fn usage_info(&self) -> Option<UsageInfo>;
638
639 fn offchain_storage(&self) -> Option<Self::OffchainStorage>;
641
642 fn pin_block(&self, hash: Block::Hash) -> sp_blockchain::Result<()>;
646
647 fn unpin_block(&self, hash: Block::Hash);
649
650 fn have_state_at(&self, hash: Block::Hash, _number: NumberFor<Block>) -> bool {
652 self.state_at(hash, TrieCacheContext::Untrusted).is_ok()
653 }
654
655 fn state_at(
657 &self,
658 hash: Block::Hash,
659 trie_cache_context: TrieCacheContext,
660 ) -> sp_blockchain::Result<Self::State>;
661
662 fn revert(
670 &self,
671 n: NumberFor<Block>,
672 revert_finalized: bool,
673 ) -> sp_blockchain::Result<(NumberFor<Block>, HashSet<Block::Hash>)>;
674
675 fn remove_leaf_block(&self, hash: Block::Hash) -> sp_blockchain::Result<()>;
677
678 fn insert_aux<
680 'a,
681 'b: 'a,
682 'c: 'a,
683 I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
684 D: IntoIterator<Item = &'a &'b [u8]>,
685 >(
686 &self,
687 insert: I,
688 delete: D,
689 ) -> sp_blockchain::Result<()> {
690 AuxStore::insert_aux(self, insert, delete)
691 }
692 fn get_aux(&self, key: &[u8]) -> sp_blockchain::Result<Option<Vec<u8>>> {
694 AuxStore::get_aux(self, key)
695 }
696
697 fn get_import_lock(&self) -> &RwLock<()>;
704
705 fn requires_full_sync(&self) -> bool;
707}
708
709pub trait LocalBackend<Block: BlockT>: Backend<Block> {}