blockchain_core/
traits.rs1#[cfg(feature = "std")]
2use std::error as stderror;
3use alloc::vec::Vec;
4use core::hash;
5
6pub trait Block: Clone {
8 type Identifier: Clone + Eq + hash::Hash;
10
11 fn id(&self) -> Self::Identifier;
13 fn parent_id(&self) -> Option<Self::Identifier>;
15}
16
17pub trait Auxiliary<B: Block>: Clone {
19 type Key: Clone + Eq + hash::Hash;
21
22 fn key(&self) -> Self::Key;
24 fn associated(&self) -> Vec<B::Identifier> {
29 Vec::new()
30 }
31}
32
33impl<B: Block> Auxiliary<B> for () {
34 type Key = ();
35
36 fn key(&self) -> () { () }
37}
38
39pub trait AsExternalities<E: ?Sized> {
41 fn as_externalities(&mut self) -> &mut E;
43}
44
45pub trait NullExternalities { }
47
48impl NullExternalities for () { }
49impl AsExternalities<dyn NullExternalities> for () {
50 fn as_externalities(&mut self) -> &mut (dyn NullExternalities + 'static) {
51 self
52 }
53}
54
55pub trait StorageExternalities<Error> {
57 fn read_storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error>;
59 fn write_storage(&mut self, key: Vec<u8>, value: Vec<u8>);
61 fn remove_storage(&mut self, key: &[u8]);
63}
64
65pub trait BlockExecutor {
67 #[cfg(feature = "std")]
68 type Error: stderror::Error + 'static;
70 #[cfg(not(feature = "std"))]
71 type Error: 'static;
73 type Block: Block;
75 type Externalities: ?Sized;
77
78 fn execute_block(
80 &self,
81 block: &Self::Block,
82 state: &mut Self::Externalities
83 ) -> Result<(), Self::Error>;
84}
85
86pub trait SimpleBuilderExecutor: BlockExecutor {
88 type BuildBlock;
90 type Inherent;
92 type Extrinsic;
94
95 fn initialize_block(
97 &self,
98 parent_block: &Self::Block,
99 state: &mut Self::Externalities,
100 inherent: Self::Inherent,
101 ) -> Result<Self::BuildBlock, Self::Error>;
102
103 fn apply_extrinsic(
105 &self,
106 block: &mut Self::BuildBlock,
107 extrinsic: Self::Extrinsic,
108 state: &mut Self::Externalities,
109 ) -> Result<(), Self::Error>;
110
111 fn finalize_block(
113 &self,
114 block: &mut Self::BuildBlock,
115 state: &mut Self::Externalities,
116 ) -> Result<(), Self::Error>;
117}