-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathPoaNetworkConsensus.sol
256 lines (224 loc) · 8.65 KB
/
PoaNetworkConsensus.sol
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
pragma solidity ^0.4.24;
import "./interfaces/IPoaNetworkConsensus.sol";
import "./interfaces/IProxyStorage.sol";
contract PoaNetworkConsensus is IPoaNetworkConsensus {
/// Issue this log event to signal a desired change in validator set.
/// This will not lead to a change in active validator set until
/// finalizeChange is called.
///
/// Only the last log event of any block can take effect.
/// If a signal is issued while another is being finalized it may never
/// take effect.
///
/// parentHash here should be the parent block hash, or the
/// signal will not be recognized.
event InitiateChange(bytes32 indexed parentHash, address[] newSet);
event ChangeFinalized(address[] newSet);
event MoCInitializedProxyStorage(address proxyStorage);
struct ValidatorState {
// Is this a validator.
bool isValidator;
// Is a validator finalized.
bool isValidatorFinalized;
// Index in the currentValidators.
uint256 index;
}
IProxyStorage public proxyStorage;
address public systemAddress = 0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE;
address[] public currentValidators;
address[] public pendingList;
mapping(address => ValidatorState) public validatorsState;
address internal _moc;
address internal _mocPending;
address internal _owner;
bool internal _isMoCRemoved = false;
bool internal _isMoCRemovedPending = false;
bool public finalized = false;
bool public wasProxyStorageSet = false;
modifier onlySystemAndNotFinalized() {
require(msg.sender == systemAddress && !finalized);
_;
}
modifier onlyKeysManager() {
require(msg.sender == getKeysManager());
_;
}
constructor(address _masterOfCeremony, address[] validators) public {
// TODO: When you deploy this contract, make sure you hardcode items below
// Make sure you have those addresses defined in spec.json
require(_masterOfCeremony != address(0));
_moc = _masterOfCeremony;
currentValidators = [_masterOfCeremony];
for (uint256 y = 0; y < validators.length; y++) {
require(validators[y] != address(0));
currentValidators.push(validators[y]);
}
for (uint256 i = 0; i < currentValidators.length; i++) {
address validator = currentValidators[i];
require(!isValidator(validator));
validatorsState[validator] = ValidatorState({
isValidator: true,
isValidatorFinalized: true,
index: i
});
}
pendingList = currentValidators;
_owner = msg.sender;
}
function isMasterOfCeremonyRemoved() public view returns(bool) {
return _isMoCRemoved;
}
function isMasterOfCeremonyRemovedPending() public view returns(bool) {
return _isMoCRemovedPending;
}
function masterOfCeremony() public view returns(address) {
return _moc;
}
function masterOfCeremonyPending() public view returns(address) {
return _mocPending;
}
/// Get current validator set (last enacted or initial if no changes ever made)
function getValidators() public view returns(address[]) {
if (currentValidators.length == 1 && currentValidators[0] == _moc) {
return pendingList;
} else {
return currentValidators;
}
}
function getPendingList() public view returns(address[]) {
return pendingList;
}
/// Called when an initiated change reaches finality and is activated.
/// Only valid when msg.sender == SUPER_USER (EIP96, 2**160 - 2)
///
/// Also called when the contract is first enabled for consensus. In this case,
/// the "change" finalized is the activation of the initial set.
function finalizeChange() public onlySystemAndNotFinalized {
finalized = true;
for (uint256 i = 0; i < pendingList.length; i++) {
ValidatorState storage state = validatorsState[pendingList[i]];
if (!state.isValidatorFinalized) {
state.isValidatorFinalized = true;
}
}
currentValidators = pendingList;
if (_mocPending != address(0)) {
_moc = _mocPending;
_mocPending = address(0);
}
if (_isMoCRemovedPending) {
_isMoCRemoved = true;
_isMoCRemovedPending = false;
}
emit ChangeFinalized(getValidators());
}
function addValidator(address _validator, bool _shouldFireEvent)
public
onlyKeysManager
returns(bool)
{
if (_addValidatorAllowed(_validator)) {
_addValidator(_validator, _shouldFireEvent);
return true;
}
return false;
}
function removeValidator(
address _validator,
bool _shouldFireEvent
) public onlyKeysManager returns(bool) {
if (_removeValidatorAllowed(_validator)) {
_removeValidator(_validator, _shouldFireEvent);
return true;
}
return false;
}
function swapValidatorKey(address _newKey, address _oldKey)
public
onlyKeysManager
returns(bool)
{
if (!_removeValidatorAllowed(_oldKey) || !_addValidatorAllowed(_newKey)) return false;
_removeValidator(_oldKey, false);
_addValidator(_newKey, false);
if (_oldKey == _moc) {
_mocPending = _newKey;
}
emit InitiateChange(blockhash(block.number - 1), pendingList);
return true;
}
function setProxyStorage(address _newAddress) public {
require(msg.sender == _moc && !_isMoCRemoved || msg.sender == _owner);
require(!wasProxyStorageSet);
require(_newAddress != address(0));
proxyStorage = IProxyStorage(_newAddress);
wasProxyStorageSet = true;
emit MoCInitializedProxyStorage(proxyStorage);
}
function isValidator(address _someone) public view returns(bool) {
return validatorsState[_someone].isValidator;
}
function isValidatorFinalized(address _someone) public view returns(bool) {
bool _isValidator = validatorsState[_someone].isValidator;
bool _isFinalized = validatorsState[_someone].isValidatorFinalized;
return _isValidator && _isFinalized;
}
function getKeysManager() public view returns(address) {
return proxyStorage.getKeysManager();
}
function getCurrentValidatorsLength() public view returns(uint256) {
return currentValidators.length;
}
function getCurrentValidatorsLengthWithoutMoC() public view returns(uint256) {
if (_isMoCRemoved) {
return currentValidators.length;
}
if (currentValidators.length == 0) {
return 0;
}
return currentValidators.length - 1; // exclude MoC
}
function _addValidatorAllowed(address _validator) private view returns(bool) {
if (_validator == address(0)) return false;
if (isValidator(_validator)) return false;
return true;
}
function _addValidator(address _validator, bool _shouldFireEvent) private {
validatorsState[_validator] = ValidatorState({
isValidator: true,
isValidatorFinalized: false,
index: pendingList.length
});
pendingList.push(_validator);
finalized = false;
if (_shouldFireEvent) {
emit InitiateChange(blockhash(block.number - 1), pendingList);
}
}
function _removeValidatorAllowed(address _validator) private view returns(bool) {
if (pendingList.length == 0) return false;
if (!isValidator(_validator)) return false;
return true;
}
function _removeValidator(address _validator, bool _shouldFireEvent) private {
uint256 removedIndex = validatorsState[_validator].index;
// Can not remove the last validator.
uint256 lastIndex = pendingList.length - 1;
address lastValidator = pendingList[lastIndex];
// Override the removed validator with the last one.
pendingList[removedIndex] = lastValidator;
// Update the index of the last validator.
validatorsState[lastValidator].index = removedIndex;
pendingList.length--;
validatorsState[_validator].index = 0;
validatorsState[_validator].isValidator = false;
validatorsState[_validator].isValidatorFinalized = false;
finalized = false;
if (_shouldFireEvent) {
if (_validator == _moc) {
_isMoCRemovedPending = true;
}
emit InitiateChange(blockhash(block.number - 1), pendingList);
}
}
}