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
use std::error as stderror;
use std::hash;
pub trait Block: Clone {
type Identifier: Copy + Eq + hash::Hash;
fn id(&self) -> Self::Identifier;
fn parent_id(&self) -> Option<Self::Identifier>;
}
pub trait Auxiliary<B: Block>: Clone {
type Key: Copy + Eq + hash::Hash;
fn key(&self) -> Self::Key;
fn associated(&self) -> Vec<B::Identifier> {
Vec::new()
}
}
impl<B: Block> Auxiliary<B> for () {
type Key = ();
fn key(&self) -> () { () }
}
pub trait AsExternalities<E: ?Sized> {
fn as_externalities(&mut self) -> &mut E;
}
pub trait NullExternalities { }
pub trait StorageExternalities<Error> {
fn read_storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error>;
fn write_storage(&mut self, key: Vec<u8>, value: Vec<u8>);
fn remove_storage(&mut self, key: &[u8]);
}
pub struct ImportOperation<B, S> {
pub block: B,
pub state: S,
}
pub struct Operation<B: Block, S, A: Auxiliary<B>> {
pub import_block: Vec<ImportOperation<B, S>>,
pub set_head: Option<B::Identifier>,
pub insert_auxiliaries: Vec<A>,
pub remove_auxiliaries: Vec<A::Key>,
}
impl<B: Block, S, A: Auxiliary<B>> Default for Operation<B, S, A> {
fn default() -> Self {
Self {
import_block: Vec::new(),
set_head: None,
insert_auxiliaries: Vec::new(),
remove_auxiliaries: Vec::new(),
}
}
}
pub trait Backend {
type Block: Block;
type State;
type Auxiliary: Auxiliary<Self::Block>;
type Error: stderror::Error + 'static;
fn commit(
&mut self,
operation: Operation<Self::Block, Self::State, Self::Auxiliary>,
) -> Result<(), Self::Error>;
}
pub trait ChainQuery: Backend {
fn genesis(&self) -> <Self::Block as Block>::Identifier;
fn head(&self) -> <Self::Block as Block>::Identifier;
fn contains(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<bool, Self::Error>;
fn is_canon(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<bool, Self::Error>;
fn lookup_canon_depth(
&self,
depth: usize,
) -> Result<Option<<Self::Block as Block>::Identifier>, Self::Error>;
fn auxiliary(
&self,
key: &<Self::Auxiliary as Auxiliary<Self::Block>>::Key,
) -> Result<Option<Self::Auxiliary>, Self::Error>;
fn depth_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<usize, Self::Error>;
fn children_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Vec<<Self::Block as Block>::Identifier>, Self::Error>;
fn state_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Self::State, Self::Error>;
fn block_at(
&self,
hash: &<Self::Block as Block>::Identifier,
) -> Result<Self::Block, Self::Error>;
}
pub trait ImportBlock {
type Block: Block;
type Error: stderror::Error + 'static;
fn import_block(&mut self, block: Self::Block) -> Result<(), Self::Error>;
}
pub trait BlockExecutor {
type Error: stderror::Error + 'static;
type Block: Block;
type Externalities: ?Sized;
fn execute_block(
&self,
block: &Self::Block,
state: &mut Self::Externalities
) -> Result<(), Self::Error>;
}
pub trait BuilderExecutor {
type Error: stderror::Error + 'static;
type Block: Block;
type BuildBlock;
type Externalities: ?Sized;
type Inherent;
type Extrinsic;
fn initialize_block(
&self,
parent_block: &Self::Block,
state: &mut Self::Externalities,
inherent: Self::Inherent,
) -> Result<Self::BuildBlock, Self::Error>;
fn apply_extrinsic(
&self,
block: &mut Self::BuildBlock,
extrinsic: Self::Extrinsic,
state: &mut Self::Externalities,
) -> Result<(), Self::Error>;
fn finalize_block(
&self,
block: &mut Self::BuildBlock,
state: &mut Self::Externalities,
) -> Result<(), Self::Error>;
}