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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use byteorder::{ByteOrder, BigEndian};
use std::mem;
use crypto::Hash;
use messages::{RawMessage, Precommit, CONSENSUS};
use storage::{Snapshot, Fork, StorageKey, StorageValue, ListIndex, MapIndex, ProofListIndex,
ProofMapIndex, MapProof};
use helpers::Height;
use super::{Block, BlockProof, Blockchain};
use super::config::StoredConfiguration;
pub fn gen_prefix<K: StorageKey>(service_id: u16, ord: u8, suffix: &K) -> Vec<u8> {
let pos = mem::size_of::<u16>();
let mut res = vec![0; pos + 1 + suffix.size()];
suffix.write(&mut res[pos + 1..]);
BigEndian::write_u16(&mut res[0..pos], service_id);
res[pos] = ord;
res
}
encoding_struct! (
struct ConfigReference {
const SIZE = 40;
field actual_from: Height [00 => 08]
field cfg_hash: &Hash [08 => 40]
}
);
encoding_struct! (
struct TxLocation {
const SIZE = 16;
field block_height: Height [00 => 08]
field position_in_block: u64 [08 => 16]
}
);
#[derive(Debug)]
pub struct Schema<T> {
view: T,
}
impl<T> Schema<T>
where
T: AsRef<Snapshot>,
{
pub fn new(snapshot: T) -> Schema<T> {
Schema { view: snapshot }
}
pub fn transactions(&self) -> MapIndex<&T, Hash, RawMessage> {
MapIndex::new(gen_prefix(CONSENSUS, 0, &()), &self.view)
}
pub fn tx_location_by_tx_hash(&self) -> MapIndex<&T, Hash, TxLocation> {
MapIndex::new(gen_prefix(CONSENSUS, 1, &()), &self.view)
}
pub fn blocks(&self) -> MapIndex<&T, Hash, Block> {
MapIndex::new(gen_prefix(CONSENSUS, 2, &()), &self.view)
}
pub fn block_hashes_by_height(&self) -> ListIndex<&T, Hash> {
ListIndex::new(gen_prefix(CONSENSUS, 3, &()), &self.view)
}
pub fn block_txs(&self, height: Height) -> ProofListIndex<&T, Hash> {
let height: u64 = height.into();
ProofListIndex::new(gen_prefix(CONSENSUS, 4, &height), &self.view)
}
pub fn precommits(&self, hash: &Hash) -> ListIndex<&T, Precommit> {
ListIndex::new(gen_prefix(CONSENSUS, 5, hash), &self.view)
}
pub fn configs(&self) -> ProofMapIndex<&T, Hash, StoredConfiguration> {
ProofMapIndex::new(gen_prefix(CONSENSUS, 6, &()), &self.view)
}
pub fn configs_actual_from(&self) -> ListIndex<&T, ConfigReference> {
ListIndex::new(gen_prefix(CONSENSUS, 7, &()), &self.view)
}
pub fn state_hash_aggregator(&self) -> ProofMapIndex<&T, Hash, Hash> {
ProofMapIndex::new(gen_prefix(CONSENSUS, 8, &()), &self.view)
}
pub fn block_hash_by_height(&self, height: Height) -> Option<Hash> {
self.block_hashes_by_height().get(height.into())
}
pub fn block_and_precommits(&self, height: Height) -> Option<BlockProof> {
let block_hash = match self.block_hash_by_height(height) {
None => return None,
Some(block_hash) => block_hash,
};
let block = self.blocks().get(&block_hash).unwrap();
let precommits_table = self.precommits(&block_hash);
let precommits = precommits_table.iter().collect();
let res = BlockProof {
block: block,
precommits: precommits,
};
Some(res)
}
pub fn last_block(&self) -> Option<Block> {
match self.block_hashes_by_height().last() {
Some(hash) => Some(self.blocks().get(&hash).unwrap()),
None => None,
}
}
pub fn last_height(&self) -> Option<Height> {
let block_opt = self.last_block();
block_opt.map(|block| block.height())
}
pub fn current_height(&self) -> Height {
let last_height = self.last_height();
match last_height {
Some(last_height) => last_height.next(),
None => Height::zero(),
}
}
pub fn actual_configuration(&self) -> StoredConfiguration {
let current_height = self.current_height();
let res = self.configuration_by_height(current_height);
trace!("Retrieved actual_config: {:?}", res);
res
}
pub fn following_configuration(&self) -> Option<StoredConfiguration> {
let current_height = self.current_height();
let idx = self.find_configurations_index_by_height(current_height);
match self.configs_actual_from().get(idx + 1) {
Some(cfg_ref) => {
let cfg_hash = cfg_ref.cfg_hash();
let cfg = self.configuration_by_hash(cfg_hash).expect(&format!(
"Config with hash {:?} is absent in configs table",
cfg_hash
));
Some(cfg)
}
None => None,
}
}
pub fn previous_configuration(&self) -> Option<StoredConfiguration> {
let current_height = self.current_height();
let idx = self.find_configurations_index_by_height(current_height);
if idx > 0 {
let cfg_ref = self.configs_actual_from().get(idx - 1).expect(&format!(
"Configuration at index {} not found",
idx
));
let cfg_hash = cfg_ref.cfg_hash();
let cfg = self.configuration_by_hash(cfg_hash).expect(&format!(
"Config with hash {:?} is absent in configs table",
cfg_hash
));
Some(cfg)
} else {
None
}
}
pub fn configuration_by_height(&self, height: Height) -> StoredConfiguration {
let idx = self.find_configurations_index_by_height(height);
let cfg_ref = self.configs_actual_from().get(idx).expect(&format!(
"Configuration at index {} not found",
idx
));
let cfg_hash = cfg_ref.cfg_hash();
self.configuration_by_hash(cfg_hash).expect(&format!(
"Config with hash {:?} is absent in configs table",
cfg_hash
))
}
pub fn configuration_by_hash(&self, hash: &Hash) -> Option<StoredConfiguration> {
self.configs().get(hash)
}
pub fn core_state_hash(&self) -> Vec<Hash> {
vec![self.configs().root_hash()]
}
pub fn get_proof_to_service_table(&self, service_id: u16, table_idx: usize) -> MapProof<Hash> {
let key = Blockchain::service_table_unique_key(service_id, table_idx);
let sum_table = self.state_hash_aggregator();
sum_table.get_proof(&key)
}
fn find_configurations_index_by_height(&self, height: Height) -> u64 {
let actual_from = self.configs_actual_from();
for i in (0..actual_from.len()).rev() {
if actual_from.get(i).unwrap().actual_from() <= height {
return i as u64;
}
}
panic!(
"Couldn't not find any config for height {}, \
that means that genesis block was created incorrectly.",
height
)
}
}
impl<'a> Schema<&'a mut Fork> {
pub fn transactions_mut(&mut self) -> MapIndex<&mut Fork, Hash, RawMessage> {
MapIndex::new(gen_prefix(CONSENSUS, 0, &()), &mut self.view)
}
pub fn tx_location_by_tx_hash_mut(&mut self) -> MapIndex<&mut Fork, Hash, TxLocation> {
MapIndex::new(gen_prefix(CONSENSUS, 1, &()), &mut self.view)
}
pub fn blocks_mut(&mut self) -> MapIndex<&mut Fork, Hash, Block> {
MapIndex::new(gen_prefix(CONSENSUS, 2, &()), &mut self.view)
}
pub fn block_hashes_by_height_mut(&mut self) -> ListIndex<&mut Fork, Hash> {
ListIndex::new(gen_prefix(CONSENSUS, 3, &()), &mut self.view)
}
pub fn block_hash_by_height_mut(&mut self, height: Height) -> Option<Hash> {
self.block_hashes_by_height().get(height.into())
}
pub fn block_txs_mut(&mut self, height: Height) -> ProofListIndex<&mut Fork, Hash> {
let height: u64 = height.into();
ProofListIndex::new(gen_prefix(CONSENSUS, 4, &height), &mut self.view)
}
pub fn precommits_mut(&mut self, hash: &Hash) -> ListIndex<&mut Fork, Precommit> {
ListIndex::new(gen_prefix(CONSENSUS, 5, hash), &mut self.view)
}
pub fn configs_mut(&mut self) -> ProofMapIndex<&mut Fork, Hash, StoredConfiguration> {
ProofMapIndex::new(gen_prefix(CONSENSUS, 6, &()), &mut self.view)
}
pub fn configs_actual_from_mut(&mut self) -> ListIndex<&mut Fork, ConfigReference> {
ListIndex::new(gen_prefix(CONSENSUS, 7, &()), &mut self.view)
}
pub fn state_hash_aggregator_mut(&mut self) -> ProofMapIndex<&mut Fork, Hash, Hash> {
ProofMapIndex::new(gen_prefix(CONSENSUS, 8, &()), &mut self.view)
}
pub fn commit_configuration(&mut self, config_data: StoredConfiguration) {
let actual_from = config_data.actual_from;
if let Some(last_cfg) = self.configs_actual_from().last() {
if last_cfg.cfg_hash() != &config_data.previous_cfg_hash {
panic!(
"Attempting to commit configuration with incorrect previous hash: {:?}, \
expected: {:?}",
config_data.previous_cfg_hash,
last_cfg.cfg_hash()
);
}
if actual_from <= last_cfg.actual_from() {
panic!(
"Attempting to commit configuration with actual_from {} less than the last \
committed the last committed actual_from {}",
actual_from,
last_cfg.actual_from()
);
}
}
let cfg_hash = config_data.hash();
self.configs_mut().put(&cfg_hash, config_data.clone());
let cfg_ref = ConfigReference::new(actual_from, &cfg_hash);
self.configs_actual_from_mut().push(cfg_ref);
info!(
"Scheduled the following configuration for acceptance: {:?}",
config_data
);
}
}