1use std::collections::HashSet;
22
23use parking_lot::RwLock;
24
25use pezsp_api::CallContext;
26use pezsp_consensus::BlockOrigin;
27use pezsp_core::offchain::OffchainStorage;
28use pezsp_runtime::{
29 traits::{Block as BlockT, HashingFor, NumberFor},
30 Justification, Justifications, StateVersion, Storage,
31};
32use pezsp_state_machine::{
33 backend::AsTrieBackend, ChildStorageCollection, IndexOperation, IterArgs,
34 OffchainChangesCollection, StorageCollection, StorageIterator,
35};
36use pezsp_storage::{ChildInfo, StorageData, StorageKey};
37pub use pezsp_trie::MerkleValue;
38
39use crate::{blockchain::Backend as BlockchainBackend, UsageInfo};
40
41pub use pezsp_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<pezsp_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) -> pezsp_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
165pub trait BlockImportOperation<Block: BlockT> {
169 type State: StateBackend<HashingFor<Block>>;
171
172 fn state(&self) -> pezsp_blockchain::Result<Option<&Self::State>>;
176
177 fn set_block_data(
179 &mut self,
180 header: Block::Header,
181 body: Option<Vec<Block::Extrinsic>>,
182 indexed_body: Option<Vec<Vec<u8>>>,
183 justifications: Option<Justifications>,
184 state: NewBlockState,
185 ) -> pezsp_blockchain::Result<()>;
186
187 fn update_db_storage(
189 &mut self,
190 update: BackendTransaction<HashingFor<Block>>,
191 ) -> pezsp_blockchain::Result<()>;
192
193 fn set_genesis_state(
196 &mut self,
197 storage: Storage,
198 commit: bool,
199 state_version: StateVersion,
200 ) -> pezsp_blockchain::Result<Block::Hash>;
201
202 fn reset_storage(
204 &mut self,
205 storage: Storage,
206 state_version: StateVersion,
207 ) -> pezsp_blockchain::Result<Block::Hash>;
208
209 fn update_storage(
211 &mut self,
212 update: StorageCollection,
213 child_update: ChildStorageCollection,
214 ) -> pezsp_blockchain::Result<()>;
215
216 fn update_offchain_storage(
218 &mut self,
219 _offchain_update: OffchainChangesCollection,
220 ) -> pezsp_blockchain::Result<()> {
221 Ok(())
222 }
223
224 fn insert_aux<I>(&mut self, ops: I) -> pezsp_blockchain::Result<()>
228 where
229 I: IntoIterator<Item = (Vec<u8>, Option<Vec<u8>>)>;
230
231 fn mark_finalized(
234 &mut self,
235 hash: Block::Hash,
236 justification: Option<Justification>,
237 ) -> pezsp_blockchain::Result<()>;
238
239 fn mark_head(&mut self, hash: Block::Hash) -> pezsp_blockchain::Result<()>;
242
243 fn update_transaction_index(
245 &mut self,
246 index: Vec<IndexOperation>,
247 ) -> pezsp_blockchain::Result<()>;
248
249 fn set_create_gap(&mut self, create_gap: bool);
251}
252
253pub trait LockImportRun<Block: BlockT, B: Backend<Block>> {
255 fn lock_import_and_run<R, Err, F>(&self, f: F) -> Result<R, Err>
257 where
258 F: FnOnce(&mut ClientImportOperation<Block, B>) -> Result<R, Err>,
259 Err: From<pezsp_blockchain::Error>;
260}
261
262pub trait Finalizer<Block: BlockT, B: Backend<Block>> {
264 fn apply_finality(
274 &self,
275 operation: &mut ClientImportOperation<Block, B>,
276 block: Block::Hash,
277 justification: Option<Justification>,
278 notify: bool,
279 ) -> pezsp_blockchain::Result<()>;
280
281 fn finalize_block(
295 &self,
296 block: Block::Hash,
297 justification: Option<Justification>,
298 notify: bool,
299 ) -> pezsp_blockchain::Result<()>;
300}
301
302pub trait AuxStore {
308 fn insert_aux<
312 'a,
313 'b: 'a,
314 'c: 'a,
315 I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
316 D: IntoIterator<Item = &'a &'b [u8]>,
317 >(
318 &self,
319 insert: I,
320 delete: D,
321 ) -> pezsp_blockchain::Result<()>;
322
323 fn get_aux(&self, key: &[u8]) -> pezsp_blockchain::Result<Option<Vec<u8>>>;
325}
326
327pub struct KeysIter<State, Block>
329where
330 State: StateBackend<HashingFor<Block>>,
331 Block: BlockT,
332{
333 inner: <State as StateBackend<HashingFor<Block>>>::RawIter,
334 state: State,
335}
336
337impl<State, Block> KeysIter<State, Block>
338where
339 State: StateBackend<HashingFor<Block>>,
340 Block: BlockT,
341{
342 pub fn new(
344 state: State,
345 prefix: Option<&StorageKey>,
346 start_at: Option<&StorageKey>,
347 ) -> Result<Self, State::Error> {
348 let mut args = IterArgs::default();
349 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
350 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
351 args.start_at_exclusive = true;
352
353 Ok(Self { inner: state.raw_iter(args)?, state })
354 }
355
356 pub fn new_child(
358 state: State,
359 child_info: ChildInfo,
360 prefix: Option<&StorageKey>,
361 start_at: Option<&StorageKey>,
362 ) -> Result<Self, State::Error> {
363 let mut args = IterArgs::default();
364 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
365 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
366 args.child_info = Some(child_info);
367 args.start_at_exclusive = true;
368
369 Ok(Self { inner: state.raw_iter(args)?, state })
370 }
371}
372
373impl<State, Block> Iterator for KeysIter<State, Block>
374where
375 Block: BlockT,
376 State: StateBackend<HashingFor<Block>>,
377{
378 type Item = StorageKey;
379
380 fn next(&mut self) -> Option<Self::Item> {
381 self.inner.next_key(&self.state)?.ok().map(StorageKey)
382 }
383}
384
385pub struct PairsIter<State, Block>
387where
388 State: StateBackend<HashingFor<Block>>,
389 Block: BlockT,
390{
391 inner: <State as StateBackend<HashingFor<Block>>>::RawIter,
392 state: State,
393}
394
395impl<State, Block> Iterator for PairsIter<State, Block>
396where
397 Block: BlockT,
398 State: StateBackend<HashingFor<Block>>,
399{
400 type Item = (StorageKey, StorageData);
401
402 fn next(&mut self) -> Option<Self::Item> {
403 self.inner
404 .next_pair(&self.state)?
405 .ok()
406 .map(|(key, value)| (StorageKey(key), StorageData(value)))
407 }
408}
409
410impl<State, Block> PairsIter<State, Block>
411where
412 State: StateBackend<HashingFor<Block>>,
413 Block: BlockT,
414{
415 pub fn new(
417 state: State,
418 prefix: Option<&StorageKey>,
419 start_at: Option<&StorageKey>,
420 ) -> Result<Self, State::Error> {
421 let mut args = IterArgs::default();
422 args.prefix = prefix.as_ref().map(|prefix| prefix.0.as_slice());
423 args.start_at = start_at.as_ref().map(|start_at| start_at.0.as_slice());
424 args.start_at_exclusive = true;
425
426 Ok(Self { inner: state.raw_iter(args)?, state })
427 }
428}
429
430pub trait StorageProvider<Block: BlockT, B: Backend<Block>> {
432 fn storage(
434 &self,
435 hash: Block::Hash,
436 key: &StorageKey,
437 ) -> pezsp_blockchain::Result<Option<StorageData>>;
438
439 fn storage_hash(
441 &self,
442 hash: Block::Hash,
443 key: &StorageKey,
444 ) -> pezsp_blockchain::Result<Option<Block::Hash>>;
445
446 fn storage_keys(
449 &self,
450 hash: Block::Hash,
451 prefix: Option<&StorageKey>,
452 start_key: Option<&StorageKey>,
453 ) -> pezsp_blockchain::Result<KeysIter<B::State, Block>>;
454
455 fn storage_pairs(
458 &self,
459 hash: <Block as BlockT>::Hash,
460 prefix: Option<&StorageKey>,
461 start_key: Option<&StorageKey>,
462 ) -> pezsp_blockchain::Result<PairsIter<B::State, Block>>;
463
464 fn child_storage(
467 &self,
468 hash: Block::Hash,
469 child_info: &ChildInfo,
470 key: &StorageKey,
471 ) -> pezsp_blockchain::Result<Option<StorageData>>;
472
473 fn child_storage_keys(
476 &self,
477 hash: Block::Hash,
478 child_info: ChildInfo,
479 prefix: Option<&StorageKey>,
480 start_key: Option<&StorageKey>,
481 ) -> pezsp_blockchain::Result<KeysIter<B::State, Block>>;
482
483 fn child_storage_hash(
486 &self,
487 hash: Block::Hash,
488 child_info: &ChildInfo,
489 key: &StorageKey,
490 ) -> pezsp_blockchain::Result<Option<Block::Hash>>;
491
492 fn closest_merkle_value(
494 &self,
495 hash: Block::Hash,
496 key: &StorageKey,
497 ) -> pezsp_blockchain::Result<Option<MerkleValue<Block::Hash>>>;
498
499 fn child_closest_merkle_value(
501 &self,
502 hash: Block::Hash,
503 child_info: &ChildInfo,
504 key: &StorageKey,
505 ) -> pezsp_blockchain::Result<Option<MerkleValue<Block::Hash>>>;
506}
507
508#[derive(Debug, Clone, Copy)]
512pub enum TrieCacheContext {
513 Trusted,
521 Untrusted,
525}
526
527impl From<CallContext> for TrieCacheContext {
528 fn from(call_context: CallContext) -> Self {
529 match call_context {
530 CallContext::Onchain => TrieCacheContext::Trusted,
531 CallContext::Offchain => TrieCacheContext::Untrusted,
532 }
533 }
534}
535
536pub trait Backend<Block: BlockT>: AuxStore + Send + Sync {
559 type BlockImportOperation: BlockImportOperation<Block, State = Self::State>;
561 type Blockchain: BlockchainBackend<Block>;
563 type State: StateBackend<HashingFor<Block>>
565 + Send
566 + AsTrieBackend<
567 HashingFor<Block>,
568 TrieBackendStorage = <Self::State as StateBackend<HashingFor<Block>>>::TrieBackendStorage,
569 >;
570 type OffchainStorage: OffchainStorage;
572
573 fn begin_operation(&self) -> pezsp_blockchain::Result<Self::BlockImportOperation>;
577
578 fn begin_state_operation(
580 &self,
581 operation: &mut Self::BlockImportOperation,
582 block: Block::Hash,
583 ) -> pezsp_blockchain::Result<()>;
584
585 fn commit_operation(
587 &self,
588 transaction: Self::BlockImportOperation,
589 ) -> pezsp_blockchain::Result<()>;
590
591 fn finalize_block(
595 &self,
596 hash: Block::Hash,
597 justification: Option<Justification>,
598 ) -> pezsp_blockchain::Result<()>;
599
600 fn append_justification(
604 &self,
605 hash: Block::Hash,
606 justification: Justification,
607 ) -> pezsp_blockchain::Result<()>;
608
609 fn blockchain(&self) -> &Self::Blockchain;
611
612 fn usage_info(&self) -> Option<UsageInfo>;
614
615 fn offchain_storage(&self) -> Option<Self::OffchainStorage>;
617
618 fn pin_block(&self, hash: Block::Hash) -> pezsp_blockchain::Result<()>;
622
623 fn unpin_block(&self, hash: Block::Hash);
625
626 fn have_state_at(&self, hash: Block::Hash, _number: NumberFor<Block>) -> bool {
628 self.state_at(hash, TrieCacheContext::Untrusted).is_ok()
629 }
630
631 fn state_at(
633 &self,
634 hash: Block::Hash,
635 trie_cache_context: TrieCacheContext,
636 ) -> pezsp_blockchain::Result<Self::State>;
637
638 fn revert(
646 &self,
647 n: NumberFor<Block>,
648 revert_finalized: bool,
649 ) -> pezsp_blockchain::Result<(NumberFor<Block>, HashSet<Block::Hash>)>;
650
651 fn remove_leaf_block(&self, hash: Block::Hash) -> pezsp_blockchain::Result<()>;
653
654 fn insert_aux<
656 'a,
657 'b: 'a,
658 'c: 'a,
659 I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
660 D: IntoIterator<Item = &'a &'b [u8]>,
661 >(
662 &self,
663 insert: I,
664 delete: D,
665 ) -> pezsp_blockchain::Result<()> {
666 AuxStore::insert_aux(self, insert, delete)
667 }
668 fn get_aux(&self, key: &[u8]) -> pezsp_blockchain::Result<Option<Vec<u8>>> {
670 AuxStore::get_aux(self, key)
671 }
672
673 fn get_import_lock(&self) -> &RwLock<()>;
680
681 fn requires_full_sync(&self) -> bool;
683}
684
685pub trait LocalBackend<Block: BlockT>: Backend<Block> {}