2018-10-24 12:52:11 +00:00
|
|
|
pragma solidity >=0.0;
|
2017-07-12 13:46:33 +00:00
|
|
|
import "../Oracles/Oracle.sol";
|
|
|
|
|
|
|
|
|
|
|
|
/// @title Majority oracle contract - Allows to resolve an event based on multiple oracles with majority vote
|
|
|
|
/// @author Stefan George - <stefan@gnosis.pm>
|
|
|
|
contract MajorityOracle is Oracle {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Storage
|
|
|
|
*/
|
|
|
|
Oracle[] public oracles;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public functions
|
|
|
|
*/
|
|
|
|
/// @dev Allows to create an oracle for a majority vote based on other oracles
|
|
|
|
/// @param _oracles List of oracles taking part in the majority vote
|
2018-07-14 21:42:01 +00:00
|
|
|
constructor(Oracle[] memory _oracles)
|
2017-07-12 13:46:33 +00:00
|
|
|
public
|
|
|
|
{
|
|
|
|
// At least 2 oracles should be defined
|
|
|
|
require(_oracles.length > 2);
|
|
|
|
for (uint i = 0; i < _oracles.length; i++)
|
|
|
|
// Oracle address cannot be null
|
2018-06-12 09:05:49 +00:00
|
|
|
require(address(_oracles[i]) != address(0));
|
2017-07-12 13:46:33 +00:00
|
|
|
oracles = _oracles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @dev Allows to registers oracles for a majority vote
|
2019-10-21 22:05:34 +00:00
|
|
|
/// @return outcomeSet Is outcome set?
|
|
|
|
/// @return outcome Outcome
|
2017-07-12 13:46:33 +00:00
|
|
|
function getStatusAndOutcome()
|
|
|
|
public
|
2018-07-02 09:14:28 +00:00
|
|
|
view
|
2017-07-12 13:46:33 +00:00
|
|
|
returns (bool outcomeSet, int outcome)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
int[] memory outcomes = new int[](oracles.length);
|
|
|
|
uint[] memory validations = new uint[](oracles.length);
|
|
|
|
for (i = 0; i < oracles.length; i++)
|
|
|
|
if (oracles[i].isOutcomeSet()) {
|
|
|
|
int _outcome = oracles[i].getOutcome();
|
|
|
|
for (uint j = 0; j <= i; j++)
|
|
|
|
if (_outcome == outcomes[j]) {
|
|
|
|
validations[j] += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (validations[j] == 0) {
|
|
|
|
outcomes[j] = _outcome;
|
|
|
|
validations[j] = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint outcomeValidations = 0;
|
|
|
|
uint outcomeIndex = 0;
|
|
|
|
for (i = 0; i < oracles.length; i++)
|
|
|
|
if (validations[i] > outcomeValidations) {
|
|
|
|
outcomeValidations = validations[i];
|
|
|
|
outcomeIndex = i;
|
|
|
|
}
|
|
|
|
// There is a majority vote
|
|
|
|
if (outcomeValidations * 2 > oracles.length) {
|
|
|
|
outcomeSet = true;
|
|
|
|
outcome = outcomes[outcomeIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @dev Returns if winning outcome is set
|
|
|
|
/// @return Is outcome set?
|
|
|
|
function isOutcomeSet()
|
|
|
|
public
|
2019-09-16 12:33:43 +00:00
|
|
|
override
|
2018-07-02 09:14:28 +00:00
|
|
|
view
|
2017-07-12 13:46:33 +00:00
|
|
|
returns (bool)
|
|
|
|
{
|
2018-06-21 11:58:38 +00:00
|
|
|
(bool outcomeSet, ) = getStatusAndOutcome();
|
2017-07-12 13:46:33 +00:00
|
|
|
return outcomeSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @dev Returns winning outcome
|
|
|
|
/// @return Outcome
|
|
|
|
function getOutcome()
|
|
|
|
public
|
2019-09-16 12:33:43 +00:00
|
|
|
override
|
2018-07-02 09:14:28 +00:00
|
|
|
view
|
2017-07-12 13:46:33 +00:00
|
|
|
returns (int)
|
|
|
|
{
|
2018-06-21 11:58:38 +00:00
|
|
|
(, int winningOutcome) = getStatusAndOutcome();
|
2017-07-12 13:46:33 +00:00
|
|
|
return winningOutcome;
|
|
|
|
}
|
|
|
|
}
|