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
use std::sync::atomic::{AtomicU64, Ordering};
use common_types::{
header::Header,
engines::{
Seal,
SealingState,
params::CommonParams,
},
errors::VapcoreError as Error,
};
use enjen::Engine;
use vapjson;
use mashina::{
ExecutedBlock,
Machine
};
#[derive(Default, Debug, PartialEq)]
pub struct InstantSealParams {
pub millisecond_timestamp: bool,
}
impl From<vapjson::spec::InstantSealParams> for InstantSealParams {
fn from(p: vapjson::spec::InstantSealParams) -> Self {
InstantSealParams {
millisecond_timestamp: p.millisecond_timestamp,
}
}
}
pub struct InstantSeal {
params: InstantSealParams,
mashina: Machine,
last_sealed_block: AtomicU64,
}
impl InstantSeal {
pub fn new(params: InstantSealParams, mashina: Machine) -> Self {
InstantSeal {
params,
mashina,
last_sealed_block: AtomicU64::new(0),
}
}
}
impl Engine for InstantSeal {
fn name(&self) -> &str { "InstantSeal" }
fn mashina(&self) -> &Machine { &self.mashina }
fn sealing_state(&self) -> SealingState { SealingState::Ready }
fn should_reseal_on_update(&self) -> bool {
true
}
fn generate_seal(&self, block: &ExecutedBlock, _parent: &Header) -> Seal {
if !block.transactions.is_empty() {
let block_number = block.header.number();
let last_sealed_block = self.last_sealed_block.load(Ordering::SeqCst);
if block_number > last_sealed_block {
let prev_last_sealed_block = self.last_sealed_block.compare_and_swap(last_sealed_block, block_number, Ordering::SeqCst);
if prev_last_sealed_block == last_sealed_block {
return Seal::Regular(Vec::new())
}
}
}
Seal::None
}
fn verify_local_seal(&self, _header: &Header) -> Result<(), Error> {
Ok(())
}
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
use std::{time, cmp};
let dur = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
let mut now = dur.as_secs();
if self.params.millisecond_timestamp {
now = now * 1000 + dur.subsec_millis() as u64;
}
cmp::max(now, parent_timestamp)
}
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
header_timestamp >= parent_timestamp
}
fn params(&self) -> &CommonParams {
self.mashina.params()
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use common_types::{
header::Header,
engines::Seal,
};
use vapory_types::{H520, Address};
use vapcore::{
test_helpers::get_temp_state_db,
block::*,
};
use spec;
#[test]
fn instant_can_seal() {
let spec = spec::new_instant();
let engine = &*spec.engine;
let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
let genesis_header = spec.genesis_header();
let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
let b = b.close_and_lock().unwrap();
if let Seal::Regular(seal) = engine.generate_seal(&b, &genesis_header) {
assert!(b.try_seal(engine, seal).is_ok());
}
}
#[test]
fn instant_cant_verify() {
let engine = spec::new_instant().engine;
let mut header: Header = Header::default();
assert!(engine.verify_block_basic(&header).is_ok());
header.set_seal(vec![tetsy_rlp::encode(&H520::default())]);
assert!(engine.verify_block_unordered(&header).is_ok());
}
}