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
mod contract;
mod multi;
mod safe_contract;
mod simple_list;
#[cfg(test)]
mod test;
use std::sync::Weak;
use bytes::Bytes;
use ethereum_types::{Address, H256};
use ethjson::spec::ValidatorSet as ValidatorSpec;
use machine::{AuxiliaryData, Call, EthereumMachine};
use types::{header::Header, ids::BlockId, BlockNumber};
use client::EngineClient;
use error::Error as EthcoreError;
pub use self::simple_list::SimpleList;
#[cfg(test)]
pub use self::test::TestSet;
use self::{contract::ValidatorContract, multi::Multi, safe_contract::ValidatorSafeContract};
use super::SystemCall;
pub fn new_validator_set_posdao(
spec: ValidatorSpec,
posdao_transition: Option<BlockNumber>,
) -> Box<dyn ValidatorSet> {
match spec {
ValidatorSpec::List(list) => {
Box::new(SimpleList::new(list.into_iter().map(Into::into).collect()))
}
ValidatorSpec::SafeContract(address) => Box::new(ValidatorSafeContract::new(
address.into(),
posdao_transition,
)),
ValidatorSpec::Contract(address) => {
Box::new(ValidatorContract::new(address.into(), posdao_transition))
}
ValidatorSpec::Multi(sequence) => Box::new(Multi::new(
sequence
.into_iter()
.map(|(block, set)| {
(
block.into(),
new_validator_set_posdao(set, posdao_transition),
)
})
.collect(),
)),
}
}
pub fn new_validator_set(spec: ValidatorSpec) -> Box<dyn ValidatorSet> {
new_validator_set_posdao(spec, None)
}
pub trait ValidatorSet: Send + Sync + 'static {
fn default_caller(&self, block_id: BlockId) -> Box<Call>;
fn generate_engine_transactions(
&self,
_first: bool,
_header: &Header,
_call: &mut SystemCall,
) -> Result<Vec<(Address, Bytes)>, EthcoreError>;
fn on_close_block(&self, _header: &Header, _address: &Address) -> Result<(), EthcoreError>;
fn contains(&self, parent: &H256, address: &Address) -> bool {
let default = self.default_caller(BlockId::Hash(*parent));
self.contains_with_caller(parent, address, &*default)
}
fn get(&self, parent: &H256, nonce: usize) -> Address {
let default = self.default_caller(BlockId::Hash(*parent));
self.get_with_caller(parent, nonce, &*default)
}
fn count(&self, parent: &H256) -> usize {
let default = self.default_caller(BlockId::Hash(*parent));
self.count_with_caller(parent, &*default)
}
fn on_epoch_begin(
&self,
_first: bool,
_header: &Header,
_call: &mut SystemCall,
) -> Result<(), ::error::Error> {
Ok(())
}
fn genesis_epoch_data(&self, _header: &Header, _call: &Call) -> Result<Vec<u8>, String> {
Ok(Vec::new())
}
fn is_epoch_end(&self, first: bool, chain_head: &Header) -> Option<Vec<u8>>;
fn signals_epoch_end(
&self,
first: bool,
header: &Header,
aux: AuxiliaryData,
) -> ::engines::EpochChange<EthereumMachine>;
fn epoch_set(
&self,
first: bool,
machine: &EthereumMachine,
number: BlockNumber,
proof: &[u8],
) -> Result<(SimpleList, Option<H256>), ::error::Error>;
fn contains_with_caller(
&self,
parent_block_hash: &H256,
address: &Address,
caller: &Call,
) -> bool;
fn get_with_caller(&self, parent_block_hash: &H256, nonce: usize, caller: &Call) -> Address;
fn count_with_caller(&self, parent_block_hash: &H256, caller: &Call) -> usize;
fn report_malicious(
&self,
_validator: &Address,
_set_block: BlockNumber,
_block: BlockNumber,
_proof: Bytes,
) {
}
fn report_benign(&self, _validator: &Address, _set_block: BlockNumber, _block: BlockNumber) {}
fn register_client(&self, _client: Weak<dyn EngineClient>) {}
}