blockchain_core/
traits.rs

1#[cfg(feature = "std")]
2use std::error as stderror;
3use alloc::vec::Vec;
4use core::hash;
5
6/// A block contains a hash, and reference a parent block via parent hash.
7pub trait Block: Clone {
8	/// Hash type of the block.
9	type Identifier: Clone + Eq + hash::Hash;
10
11	/// Get the block hash.
12	fn id(&self) -> Self::Identifier;
13	/// Get the parent block hash. None if this block is genesis.
14	fn parent_id(&self) -> Option<Self::Identifier>;
15}
16
17/// A value where the key is contained in.
18pub trait Auxiliary<B: Block>: Clone {
19	/// Key type
20	type Key: Clone + Eq + hash::Hash;
21
22	/// Return the key of this object.
23	fn key(&self) -> Self::Key;
24	/// Return block ids associated with this auxiliary. If the backend
25	/// removes any of the blocks listed here, it is expected to remove
26	/// this auxiliary entry, and trigger a recalculation for the
27	/// consensus engine.
28	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
39/// Trait that allows conversion into externalities.
40pub trait AsExternalities<E: ?Sized> {
41	/// Turn this object into externalities.
42	fn as_externalities(&mut self) -> &mut E;
43}
44
45/// Null externalities.
46pub 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
55/// Externalities for reading a key value based storage.
56pub trait StorageExternalities<Error> {
57	/// Read storage value.
58	fn read_storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error>;
59	/// Write storage value.
60	fn write_storage(&mut self, key: Vec<u8>, value: Vec<u8>);
61	/// Remove storage value.
62	fn remove_storage(&mut self, key: &[u8]);
63}
64
65/// Block executor
66pub trait BlockExecutor {
67	#[cfg(feature = "std")]
68	/// Error type
69	type Error: stderror::Error + 'static;
70	#[cfg(not(feature = "std"))]
71	/// Error type
72	type Error: 'static;
73	/// Block type
74	type Block: Block;
75	/// Externalities type
76	type Externalities: ?Sized;
77
78	/// Execute the block via a block object and given state.
79	fn execute_block(
80		&self,
81		block: &Self::Block,
82		state: &mut Self::Externalities
83	) -> Result<(), Self::Error>;
84}
85
86/// Builder executor
87pub trait SimpleBuilderExecutor: BlockExecutor {
88	/// Build block type
89	type BuildBlock;
90	/// Inherent
91	type Inherent;
92	/// Extrinsic
93	type Extrinsic;
94
95	/// Initialize a block from the parent block, and given state.
96	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	/// Apply extrinsic to a given block.
104	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	/// Finalize a block.
112	fn finalize_block(
113		&self,
114		block: &mut Self::BuildBlock,
115		state: &mut Self::Externalities,
116	) -> Result<(), Self::Error>;
117}