2022-01-28 17:33:06 +01:00
|
|
|
// Author: Mohamed Sohail <mohamedsohailazim@gmail.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
pragma solidity >=0.6.12;
|
|
|
|
|
|
|
|
contract KitabuValidators {
|
|
|
|
address constant SYSTEM_ADDRESS =
|
|
|
|
0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE;
|
|
|
|
address public admissionController =
|
2022-01-28 18:05:27 +01:00
|
|
|
0x9F57B4A25638F3bdfFcB1F3d2902860601a1Aa65;
|
2022-01-28 17:33:06 +01:00
|
|
|
|
2022-01-28 18:05:27 +01:00
|
|
|
address[] validators;
|
2022-01-28 17:33:06 +01:00
|
|
|
|
|
|
|
bool public finalized;
|
|
|
|
|
|
|
|
mapping(address => uint256) validatorSetIndex;
|
|
|
|
|
|
|
|
event NewAdmissionController(
|
|
|
|
address indexed old,
|
|
|
|
address indexed newController
|
|
|
|
);
|
|
|
|
event InitiateChange(bytes32 indexed parentHash, address[] newSet);
|
|
|
|
event ChangeFinalized(address[] currentSet);
|
|
|
|
|
|
|
|
modifier onlySystemChange() {
|
|
|
|
require(msg.sender == SYSTEM_ADDRESS);
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:05:27 +01:00
|
|
|
modifier alreadyFinalized() {
|
2022-01-28 17:33:06 +01:00
|
|
|
require(finalized);
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifier onlyAdmissionController() {
|
|
|
|
require(msg.sender == admissionController);
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:05:27 +01:00
|
|
|
constructor() public {
|
2022-01-28 17:33:06 +01:00
|
|
|
validators.push(admissionController);
|
|
|
|
validatorSetIndex[admissionController] = 0;
|
2022-01-28 18:05:27 +01:00
|
|
|
finalized = true;
|
2022-01-28 17:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setNewAdmissionController(address newController)
|
|
|
|
public
|
|
|
|
onlyAdmissionController
|
|
|
|
{
|
|
|
|
emit NewAdmissionController(admissionController, newController);
|
|
|
|
admissionController = newController;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValidators() public view returns (address[] memory) {
|
|
|
|
return validators;
|
|
|
|
}
|
|
|
|
|
|
|
|
function initiateChange() private {
|
|
|
|
finalized = false;
|
|
|
|
emit InitiateChange(blockhash(block.number - 1), getValidators());
|
|
|
|
}
|
|
|
|
|
|
|
|
function finalizeChange() public onlySystemChange {
|
|
|
|
finalized = true;
|
|
|
|
emit ChangeFinalized(validators);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addValidator(address newValidator)
|
|
|
|
public
|
|
|
|
onlyAdmissionController
|
2022-01-28 18:05:27 +01:00
|
|
|
alreadyFinalized
|
2022-01-28 17:33:06 +01:00
|
|
|
{
|
|
|
|
validators.push(newValidator);
|
|
|
|
validatorSetIndex[newValidator] = validators.length - 1;
|
|
|
|
initiateChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeValidator(address exValidator)
|
|
|
|
public
|
|
|
|
onlyAdmissionController
|
2022-01-28 18:05:27 +01:00
|
|
|
alreadyFinalized
|
2022-01-28 17:33:06 +01:00
|
|
|
{
|
|
|
|
orderedRemoval(validatorSetIndex[exValidator]);
|
|
|
|
delete validatorSetIndex[exValidator];
|
|
|
|
initiateChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
function orderedRemoval(uint256 index) private {
|
|
|
|
for (uint256 i = index; i < validators.length - 1; i++) {
|
|
|
|
validators[i] = validators[i + 1];
|
|
|
|
}
|
|
|
|
validators.pop();
|
|
|
|
}
|
|
|
|
}
|