Skip to content

Instantly share code, notes, and snippets.

@vkomenda
Created March 21, 2019 17:51
Show Gist options
  • Save vkomenda/a15aafb59016bf6d001f4caf737c5e2e to your computer and use it in GitHub Desktop.
Save vkomenda/a15aafb59016bf6d001f4caf737c5e2e to your computer and use it in GitHub Desktop.
// Source for the test AuRa validator set contract.
//
// The bytecode of this contract is included in `validator_contract.json` as the
// constructor of address `0x0000..0005`.
pragma solidity ^0.5.0;
contract TestList {
address[] public validators = [
0x7d577a597B2742b498Cb5Cf0C26cDCD726d39E6e,
0x82A978B3f5962A5b0957d9ee9eEf472EE55B42F1
];
mapping(address => uint) indices;
mapping(bytes32 => address[]) validatorChanges;
event InitiateChange(bytes32 indexed parentHash, address[] newSet);
constructor() public {
for (uint i = 0; i < validators.length; i++) {
indices[validators[i]] = i;
}
}
// Called on every block to update node validator list.
function getValidators() view public returns (address[] memory) {
return validators;
}
// Removes a validator from the list.
function reportMalicious(address validator, uint256 _blockNumber, bytes memory _proof) public {
validators[indices[validator]] = validators[validators.length-1];
delete indices[validator];
delete validators[validators.length-1];
validators.length--;
// Record the validator set change.
bytes32 h = blockhash(block.number);
validatorChanges[h] = new address[](validators.length);
for (uint i = 0; i < validators.length; i++) {
validatorChanges[h][i] = validators[i];
}
}
// Checks if `emitInitiateChange` can be called.
function emitInitiateChangeCallable() pure public returns (bool) {
return true;
}
// Emits an `InitiateChange` event in production code. Does nothing in the test.
function emitInitiateChange() external {
bytes32 h = blockhash(block.number - 1);
if (validatorChanges[h].length > 0) {
emit InitiateChange(h, validators);
}
}
// Applies a validator set change in production code. Does nothing in the test.
function finalizeChange() pure public {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment