Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Add POSDAO transition and malice report queue. #11245

Merged
merged 12 commits into from
Jan 29, 2020
4 changes: 2 additions & 2 deletions ethcore/engines/validator-set/res/validator_set.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"constant": true,
"inputs": [
{
"name": "_reportingMiningAddress",
"name": "_reportingValidator",
"type": "address"
},
{
"name": "_maliciousMiningAddress",
"name": "_maliciousValidator",
"type": "address"
},
{
Expand Down
2 changes: 1 addition & 1 deletion ethcore/res/validator_contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"0000000000000000000000000000000000000005": {
"balance": "1",
"constructor": "60c0604052737d577a597b2742b498cb5cf0c26cdcd726d39e6e60809081527382a978b3f5962a5b0957d9ee9eef472ee55b42f160a0526100449060009060026100a3565b5034801561005157600080fd5b5060005b60005481101561009d57806001600080848154811061007057fe5b6000918252602080832090910154600160a060020a03168352820192909252604001902055600101610055565b5061012f565b8280548282559060005260206000209081019282156100f8579160200282015b828111156100f85782518254600160a060020a031916600160a060020a039091161782556020909201916001909101906100c3565b50610104929150610108565b5090565b61012c91905b80821115610104578054600160a060020a031916815560010161010e565b90565b61099d8061013e6000396000f3fe608060405234801561001057600080fd5b50600436106100d1576000357c010000000000000000000000000000000000000000000000000000000090048063b56b366b1161008e578063b56b366b14610206578063b7ab4db514610282578063c476dd401461028a578063cbd2d5281461030f578063d69f13bb14610345578063d8f2e0bf14610371576100d1565b806335aa2e44146100d65780633d3b54581461010f578063752862111461012b5780639300c9261461013557806393b4e25e146101d8578063a92252ae146101e0575b600080fd5b6100f3600480360360208110156100ec57600080fd5b5035610379565b60408051600160a060020a039092168252519081900360200190f35b6101176103a0565b604080519115158252519081900360200190f35b6101336103a7565b005b6101336004803603602081101561014b57600080fd5b81019060208101813564010000000081111561016657600080fd5b82018360208201111561017857600080fd5b8035906020019184602083028401116401000000008311171561019a57600080fd5b9190808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152509295506103a9945050505050565b610133610445565b610117600480360360208110156101f657600080fd5b5035600160a060020a03166104cc565b6102326004803603604081101561021c57600080fd5b50600160a060020a038135169060200135610512565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561026e578181015183820152602001610256565b505050509050019250505060405180910390f35b6102326105b1565b610133600480360360608110156102a057600080fd5b600160a060020a03823516916020810135918101906060810160408201356401000000008111156102d057600080fd5b8201836020820111156102e257600080fd5b8035906020019184600183028401116401000000008311171561030457600080fd5b509092509050610613565b6101176004803603606081101561032557600080fd5b50600160a060020a0381358116916020810135909116906040013561078b565b6101336004803603604081101561035b57600080fd5b50600160a060020a038135169060200135610843565b6100f3610873565b6000818154811061038657fe5b600091825260209091200154600160a060020a0316905081565b4315155b90565b565b80516103bc906000906020840190610882565b5060014303407f55252fa6eee4741b4e24a74a70e9c11fd2c2281df8d6ea13126ff845f7825c8960006040518080602001828103825283818154815260200191508054801561043457602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610416575b50509250505060405180910390a250565b60014303407f55252fa6eee4741b4e24a74a70e9c11fd2c2281df8d6ea13126ff845f7825c896000604051808060200182810382528381815481526020019150805480156104bc57602002820191906000526020600020905b8154600160a060020a0316815260019091019060200180831161049e575b50509250505060405180910390a2565b600160a060020a038116600081815260016020526040812054815491929183919081106104f557fe5b600091825260209091200154600160a060020a0316141592915050565b60408051600160a060020a0384166020808301919091528183018490528251808303840181526060838101808652825192840192909220600090815260028452859020805460809481028601850190965285835290949193909201828280156105a457602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610586575b5050505050905092915050565b6060600080548060200260200160405190810160405280929190818152602001828054801561060957602002820191906000526020600020905b8154600160a060020a031681526001909101906020018083116105eb575b5050505050905090565b60408051600160a060020a03861660208083018290528284018790528351808403850181526060909301845282519281019290922060009081526002835283812080546001818101835591835284832001805473ffffffffffffffffffffffffffffffffffffffff191633179055828252909252918120548154811061069557fe5b600091825260209091200154600160a060020a03161415610785576000805460001981019081106106c257fe5b6000918252602080832090910154600160a060020a03878116845260019092526040832054835492909116929181106106f757fe5b6000918252602080832091909101805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0394851617905591861681526001909152604081208190558054600019810190811061074d57fe5b60009182526020822001805473ffffffffffffffffffffffffffffffffffffffff191690558054906107839060001983016108f4565b505b50505050565b600043808311156107a057600091505061083c565b6064811180156107b257508260648203115b156107c157600091505061083c565b6107ca846104cc565b156107d957600091505061083c565b60606107e58585610512565b905060005b81518110156108345786600160a060020a031682828151811061080957fe5b6020026020010151600160a060020a0316141561082c576000935050505061083c565b6001016107ea565b506001925050505b9392505050565b506003805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b600354600160a060020a031681565b8280548282559060005260206000209081019282156108e4579160200282015b828111156108e4578251825473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039091161782556020909201916001909101906108a2565b506108f092915061091d565b5090565b8154818355818111156109185760008381526020902061091891810190830161094e565b505050565b6103a491905b808211156108f057805473ffffffffffffffffffffffffffffffffffffffff19168155600101610923565b6103a491905b808211156108f0576000815560010161095456fea265627a7a723158209f485615bb2aaf495c79b1b72f547c2d243daee4478054ad11f5d46116fdd76d64736f6c634300050c0032"
"constructor": "60c0604052737d577a597b2742b498cb5cf0c26cdcd726d39e6e60809081527382a978b3f5962a5b0957d9ee9eef472ee55b42f160a05261004390600290816100a3565b5034801561005057600080fd5b5060005b60025481101561009d5780600560006002848154811061007057fe5b6000918252602080832090910154600160a060020a03168352820192909252604001902055600101610054565b5061012f565b8280548282559060005260206000209081019282156100f8579160200282015b828111156100f85782518254600160a060020a031916600160a060020a039091161782556020909201916001909101906100c3565b50610104929150610108565b5090565b61012c91905b80821115610104578054600160a060020a031916815560010161010e565b90565b6108e28061013e6000396000f3fe608060405234801561001057600080fd5b50600436106100d1576000357c010000000000000000000000000000000000000000000000000000000090048063b56b366b1161008e578063b56b366b14610206578063b7ab4db514610282578063c476dd401461028a578063cbd2d5281461030f578063d69f13bb14610345578063d8f2e0bf14610371576100d1565b806335aa2e44146100d65780633d3b54581461010f578063752862111461012b5780639300c9261461013557806393b4e25e146101d8578063a92252ae146101e0575b600080fd5b6100f3600480360360208110156100ec57600080fd5b5035610379565b60408051600160a060020a039092168252519081900360200190f35b6101176103a0565b604080519115158252519081900360200190f35b6101336103a7565b005b6101336004803603602081101561014b57600080fd5b81019060208101813564010000000081111561016657600080fd5b82018360208201111561017857600080fd5b8035906020019184602083028401116401000000008311171561019a57600080fd5b9190808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152509295506103a9945050505050565b6101336103c8565b610117600480360360208110156101f657600080fd5b5035600160a060020a031661044f565b6102326004803603604081101561021c57600080fd5b50600160a060020a038135169060200135610464565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561026e578181015183820152602001610256565b505050509050019250505060405180910390f35b6102326104e3565b610133600480360360608110156102a057600080fd5b600160a060020a03823516916020810135918101906060810160408201356401000000008111156102d057600080fd5b8201836020820111156102e257600080fd5b8035906020019184600183028401116401000000008311171561030457600080fd5b509092509050610545565b6101176004803603606081101561032557600080fd5b50600160a060020a038135811691602081013590911690604001356106eb565b6101336004803603604081101561035b57600080fd5b50600160a060020a038135169060200135610788565b6100f36107b8565b6002818154811061038657fe5b600091825260209091200154600160a060020a0316905081565b4315155b90565b565b80516103bc9060029060208401906107c7565b506103c56103c8565b50565b60014303407f55252fa6eee4741b4e24a74a70e9c11fd2c2281df8d6ea13126ff845f7825c8960026040518080602001828103825283818154815260200191508054801561043f57602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610421575b50509250505060405180910390a2565b60016020526000908152604090205460ff1681565b600160a060020a03821660009081526003602090815260408083208484528252918290208054835181840281018401909452808452606093928301828280156104d657602002820191906000526020600020905b8154600160a060020a031681526001909101906020018083116104b8575b5050505050905092915050565b6060600280548060200260200160405190810160405280929190818152602001828054801561053b57602002820191906000526020600020905b8154600160a060020a0316815260019091019060200180831161051d575b5050505050905090565b600160a060020a0384166000818152600360209081526040808320878452825280832080546001818101835591855283852001805473ffffffffffffffffffffffffffffffffffffffff1916339081179091558585526004845282852089865284528285208186528452828520805460ff19908116841790915586865282855283862080549091169092179091556005909252909120546002805492939290919081106105ee57fe5b600091825260209091200154600160a060020a031614156106e45760028054600019810190811061061b57fe5b6000918252602080832090910154600160a060020a038881168452600590925260409092205460028054929093169291811061065357fe5b6000918252602080832091909101805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039485161790559187168152600590915260408120556002805460001981019081106106a957fe5b6000918252602090912001805473ffffffffffffffffffffffffffffffffffffffff1916905560028054906106e2906000198301610839565b505b5050505050565b60004380831115610700576000915050610781565b60648111801561071257508260648203115b15610721576000915050610781565b600160a060020a03841660009081526001602052604090205460ff161561074c576000915050610781565b5050600160a060020a03808316600090815260046020908152604080832085845282528083209387168352929052205460ff16155b9392505050565b506000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b600054600160a060020a031681565b828054828255906000526020600020908101928215610829579160200282015b82811115610829578251825473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039091161782556020909201916001909101906107e7565b50610835929150610862565b5090565b81548183558181111561085d5760008381526020902061085d918101908301610893565b505050565b6103a491905b8082111561083557805473ffffffffffffffffffffffffffffffffffffffff19168155600101610868565b6103a491905b80821115610835576000815560010161089956fea265627a7a7231582073e77a97d79ab4382d5a28644654c335489310e72aa6c8f737e424bdb6c2bbd664736f6c63430005100032"
},
"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
Expand Down
135 changes: 75 additions & 60 deletions ethcore/res/validator_contract.sol
Original file line number Diff line number Diff line change
@@ -1,62 +1,103 @@
// Source for the test AuRa validator set contract.
varasev marked this conversation as resolved.
Show resolved Hide resolved
//
// Contains POSDAO features. The full POSDAO ValidatorSet contract production code is available at
// https://github.com/poanetwork/posdao-contracts/blob/master/contracts/ValidatorSetAuRa.sol
//
// 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 {
contract TestValidatorSet {

address public disliked; // contains the address of validator reported by `reportBenign`
mapping(address => bool) public isValidatorBanned; // if the validator is banned by `reportMalicious`

// The initial set of validators
address[] public validators = [
0x7d577a597B2742b498Cb5Cf0C26cDCD726d39E6e,
0x82A978B3f5962A5b0957d9ee9eEf472EE55B42F1
];

mapping(address => uint) indices;
mapping(bytes32 => address[]) maliceReported;
address public disliked;
// The mappings used by POSDAO features testing (see `reportMalicious` and `shouldValidatorReport` functions below)
mapping(address => mapping(uint256 => address[])) private _maliceReportedForBlock;
mapping(address => mapping(uint256 => mapping(address => bool))) private _maliceReportedForBlockMapped;
mapping(address => uint256) private _validatorIndex;

// The standard event to notify the engine about the validator set changing in the contract
event InitiateChange(bytes32 indexed parentHash, address[] newSet);

constructor() public {
// Initialize validator indices to be able to correctly remove
// a malicious validator from the validator set later
for (uint i = 0; i < validators.length; i++) {
indices[validators[i]] = i;
_validatorIndex[validators[i]] = i;
}
}

// Called on every block to update node validator list.
function getValidators() view public returns (address[] memory) {
return validators;
// Emits an `InitiateChange` event with the current (or new) validator set
function emitInitiateChange() public {
emit InitiateChange(blockhash(block.number - 1), validators);
}

function setValidators(address[] memory _validators) public {
validators = _validators;
emit InitiateChange(blockhash(block.number - 1), validators);
// Applies a validator set change in production code. Does nothing in the test
function finalizeChange() pure public {}

// Benign validator behaviour report. Kept here for regression testing
function reportBenign(address _validator, uint256) public {
disliked = _validator;
}

// Removes a validator from the list.
function reportMalicious(address validator, uint256 blockNum, bytes calldata) external {
maliceReported[keccak256(abi.encode(validator, blockNum))].push(msg.sender);
if (validators[indices[validator]] == validator) {
validators[indices[validator]] = validators[validators.length-1];
delete indices[validator];
delete validators[validators.length-1];
// Removes a malicious validator from the list
function reportMalicious(address _validator, uint256 _blockNum, bytes calldata) external {
address reportingValidator = msg.sender;

// Mark the `_validator` as reported by `reportingValidator` for the block `_blockNum`
_maliceReportedForBlock[_validator][_blockNum].push(reportingValidator);
_maliceReportedForBlockMapped[_validator][_blockNum][reportingValidator] = true;
isValidatorBanned[_validator] = true;

// If the passed validator is in the validator set
if (validators[_validatorIndex[_validator]] == _validator) {
// Remove the validator from the set
validators[_validatorIndex[_validator]] = validators[validators.length - 1];
delete _validatorIndex[_validator];
delete validators[validators.length - 1];
validators.length--;
}
}

// Returns the list of all validators that reported the given validator as malicious for the given block.
function maliceReportedForBlock(address validator, uint256 blockNum) public view returns(address[] memory) {
return maliceReported[keccak256(abi.encode(validator, blockNum))];
// Tests validator set changing and emitting the `InitiateChange` event
function setValidators(address[] memory _validators) public {
validators = _validators;
emitInitiateChange();
}

// Checks if `emitInitiateChange` can be called (used by POSDAO tests)
function emitInitiateChangeCallable() view public returns(bool) {
return block.number > 0;
}

// Returns the current validator set
function getValidators() public view returns(address[] memory) {
return validators;
}

// Returns the list of all validators that reported the given validator
// as malicious for the given block. Used by POSDAO tests
function maliceReportedForBlock(address _validator, uint256 _blockNum) public view returns(address[] memory) {
return _maliceReportedForBlock[_validator][_blockNum];
}

/// Returns a boolean flag indicating whether the specified validator
/// should report about some validator's misbehaviour at the specified block.
/// @param _reportingMiningAddress The mining address of validator who reports.
/// @param _maliciousMiningAddress The mining address of malicious validator.
/// @param _blockNumber The block number at which the validator misbehaved.
// Returns a boolean flag indicating whether the specified validator
// should report about some validator's misbehaviour at the specified block.
// Used by POSDAO tests.
// `_reportingValidator` is the address of validator who reports.
// `_maliciousValidator` is the address of malicious validator.
// `_blockNumber` is the block number at which the malicious validator misbehaved.
function shouldValidatorReport(
address _reportingMiningAddress,
address _maliciousMiningAddress,
address _reportingValidator,
address _maliciousValidator,
uint256 _blockNumber
) public view returns(bool) {
uint256 currentBlock = block.number;
Expand All @@ -66,39 +107,13 @@ contract TestList {
if (currentBlock > 100 && currentBlock - 100 > _blockNumber) {
return false;
}
if (isValidatorBanned(_maliciousMiningAddress)) {
return false; // already banned
}
address[] memory reports = maliceReportedForBlock(_maliciousMiningAddress, _blockNumber);
for (uint256 i = 0; i < reports.length; i++) {
if (reports[i] == _reportingMiningAddress) {
return false; // already reported
}
if (isValidatorBanned[_maliciousValidator]) {
// We shouldn't report of the malicious validator
varasev marked this conversation as resolved.
Show resolved Hide resolved
// as it has already been reported and banned
return false;
}
return true;
}

// Benign validator behaviour report. Kept here for regression testing.
function reportBenign(address validator, uint256) public {
disliked = validator;
}

// Checks if `emitInitiateChange` can be called.
function emitInitiateChangeCallable() view public returns (bool) {
return block.number > 0;
}

// Checks if a validator has been removed.
function isValidatorBanned(address validator) view public returns (bool) {
return validators[indices[validator]] != validator;
// Return `false` if already reported by the same `_reportingValidator` for the same `_blockNumber`
return !_maliceReportedForBlockMapped[_maliciousValidator][_blockNumber][_reportingValidator];
}

// Emits an `InitiateChange` event.
function emitInitiateChange() public {
emit InitiateChange(blockhash(block.number - 1), validators);
}

// Applies a validator set change in production code. Does nothing in the test.
function finalizeChange() pure public {}
}