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
use engines::{Engine, Seal};
use parity_machine::{Machine, Transactions, TotalScoredHeader};
pub struct InstantSeal<M> {
machine: M,
}
impl<M> InstantSeal<M> {
pub fn new(machine: M) -> Self {
InstantSeal {
machine: machine,
}
}
}
impl<M: Machine> Engine<M> for InstantSeal<M>
where M::LiveBlock: Transactions,
M::ExtendedHeader: TotalScoredHeader,
<M::ExtendedHeader as TotalScoredHeader>::Value: Ord
{
fn name(&self) -> &str {
"InstantSeal"
}
fn machine(&self) -> &M { &self.machine }
fn seals_internally(&self) -> Option<bool> { Some(true) }
fn generate_seal(&self, block: &M::LiveBlock, _parent: &M::Header) -> Seal {
if block.transactions().is_empty() { Seal::None } else { Seal::Regular(Vec::new()) }
}
fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> {
Ok(())
}
fn open_block_header_timestamp(&self, parent_timestamp: u64) -> u64 {
use std::{time, cmp};
let now = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap_or_default();
cmp::max(now.as_secs(), parent_timestamp)
}
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
header_timestamp >= parent_timestamp
}
fn fork_choice(&self, new: &M::ExtendedHeader, current: &M::ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use ethereum_types::{H520, Address};
use test_helpers::get_temp_state_db;
use spec::Spec;
use header::Header;
use block::*;
use engines::Seal;
#[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::default(), (3141562.into(), 31415620.into()), vec![], false, &mut Vec::new().into_iter()).unwrap();
let b = b.close_and_lock().unwrap();
if let Seal::Regular(seal) = engine.generate_seal(b.block(), &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![::rlp::encode(&H520::default()).into_vec()]);
assert!(engine.verify_block_unordered(&header).is_ok());
}
}