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
mod transition;
mod vote_collector;
mod null_engine;
mod instant_seal;
mod basic_authority;
mod authority_round;
mod tendermint;
mod validator_set;
mod signer;
pub use self::null_engine::NullEngine;
pub use self::instant_seal::InstantSeal;
pub use self::basic_authority::BasicAuthority;
pub use self::authority_round::AuthorityRound;
pub use self::tendermint::Tendermint;
use std::sync::Weak;
use util::*;
use ethkey::Signature;
use account_provider::AccountProvider;
use block::ExecutedBlock;
use builtin::Builtin;
use env_info::EnvInfo;
use error::Error;
use spec::CommonParams;
use evm::Schedule;
use header::Header;
use transaction::{UnverifiedTransaction, SignedTransaction};
use client::Client;
#[derive(Debug)]
pub enum EngineError {
NotAuthorized(Address),
DoubleVote(Address),
NotProposer(Mismatch<Address>),
UnexpectedMessage,
BadSealFieldSize(OutOfBounds<usize>),
}
impl fmt::Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::EngineError::*;
let msg = match *self {
DoubleVote(ref address) => format!("Author {} issued too many blocks.", address),
NotProposer(ref mis) => format!("Author is not a current proposer: {}", mis),
NotAuthorized(ref address) => format!("Signer {} is not authorized.", address),
UnexpectedMessage => "This Engine should not be fed messages.".into(),
BadSealFieldSize(ref oob) => format!("Seal field has an unexpected length: {}", oob),
};
f.write_fmt(format_args!("Engine error ({})", msg))
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum Seal {
Proposal(Vec<Bytes>),
Regular(Vec<Bytes>),
None,
}
pub trait Engine : Sync + Send {
fn name(&self) -> &str;
fn version(&self) -> SemanticVersion { SemanticVersion::new(0, 0, 0) }
fn seal_fields(&self) -> usize { 0 }
fn extra_info(&self, _header: &Header) -> BTreeMap<String, String> { BTreeMap::new() }
fn additional_params(&self) -> HashMap<String, String> { HashMap::new() }
fn params(&self) -> &CommonParams;
fn schedule(&self, env_info: &EnvInfo) -> Schedule;
fn builtins(&self) -> &BTreeMap<Address, Builtin>;
fn maximum_extra_data_size(&self) -> usize { self.params().maximum_extra_data_size }
fn maximum_uncle_count(&self) -> usize { 2 }
fn maximum_uncle_age(&self) -> usize { 6 }
fn account_start_nonce(&self) -> U256 { self.params().account_start_nonce }
fn on_new_block(&self, _block: &mut ExecutedBlock) {}
fn on_close_block(&self, _block: &mut ExecutedBlock) {}
fn is_sealer(&self, _author: &Address) -> Option<bool> { None }
fn is_default_sealer(&self) -> Option<bool> { self.is_sealer(&Default::default()) }
fn generate_seal(&self, _block: &ExecutedBlock) -> Seal { Seal::None }
fn verify_block_basic(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
fn verify_block_family(&self, _header: &Header, _parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
fn verify_transaction_basic(&self, t: &UnverifiedTransaction, _header: &Header) -> Result<(), Error> {
t.check_low_s()?;
Ok(())
}
fn verify_transaction(&self, t: UnverifiedTransaction, _header: &Header) -> Result<SignedTransaction, Error> {
SignedTransaction::new(t)
}
fn signing_network_id(&self, _env_info: &EnvInfo) -> Option<u64> { None }
fn verify_block_seal(&self, header: &Header) -> Result<(), Error> {
self.verify_block_basic(header, None).and_then(|_| self.verify_block_unordered(header, None))
}
fn populate_from_parent(&self, header: &mut Header, parent: &Header, _gas_floor_target: U256, _gas_ceil_target: U256) {
header.set_difficulty(parent.difficulty().clone());
header.set_gas_limit(parent.gas_limit().clone());
}
fn handle_message(&self, _message: &[u8]) -> Result<(), Error> { Err(EngineError::UnexpectedMessage.into()) }
fn is_builtin(&self, a: &Address) -> bool { self.builtins().contains_key(a) }
fn cost_of_builtin(&self, a: &Address, input: &[u8]) -> U256 {
self.builtins().get(a).expect("queried cost of nonexistent builtin").cost(input.len())
}
fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut BytesRef) {
self.builtins().get(a).expect("attempted to execute nonexistent builtin").execute(input, output);
}
fn is_proposal(&self, _verified_header: &Header) -> bool { false }
fn set_signer(&self, _account_provider: Arc<AccountProvider>, _address: Address, _password: String) {}
fn sign(&self, _hash: H256) -> Result<Signature, Error> { unimplemented!() }
fn register_client(&self, _client: Weak<Client>) {}
fn step(&self) {}
fn stop(&self) {}
}