diff options
author | chriseth <chris@ethereum.org> | 2017-07-12 21:46:33 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2017-07-12 21:55:11 +0800 |
commit | b1c1fb6c8314a8f756cb367bdaa73f4459f397f2 (patch) | |
tree | 7e9d18c3e48d0a90eb75cf3453354f3699d528d1 /test/compilationTests/gnosis/Oracles/MajorityOracle.sol | |
parent | 91f17a366202f5cac21a5a469c682ad86fe9ede8 (diff) | |
download | dexon-solidity-b1c1fb6c8314a8f756cb367bdaa73f4459f397f2.tar.gz dexon-solidity-b1c1fb6c8314a8f756cb367bdaa73f4459f397f2.tar.zst dexon-solidity-b1c1fb6c8314a8f756cb367bdaa73f4459f397f2.zip |
Gnosis compilation contracts.
Diffstat (limited to 'test/compilationTests/gnosis/Oracles/MajorityOracle.sol')
-rw-r--r-- | test/compilationTests/gnosis/Oracles/MajorityOracle.sol | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/test/compilationTests/gnosis/Oracles/MajorityOracle.sol b/test/compilationTests/gnosis/Oracles/MajorityOracle.sol new file mode 100644 index 00000000..1562ce48 --- /dev/null +++ b/test/compilationTests/gnosis/Oracles/MajorityOracle.sol @@ -0,0 +1,89 @@ +pragma solidity ^0.4.11; +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 + function MajorityOracle(Oracle[] _oracles) + 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 + require(address(_oracles[i]) != 0); + oracles = _oracles; + } + + /// @dev Allows to registers oracles for a majority vote + /// @return Is outcome set? + /// @return Outcome + function getStatusAndOutcome() + public + 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 + constant + returns (bool) + { + var (outcomeSet, ) = getStatusAndOutcome(); + return outcomeSet; + } + + /// @dev Returns winning outcome + /// @return Outcome + function getOutcome() + public + constant + returns (int) + { + var (, winningOutcome) = getStatusAndOutcome(); + return winningOutcome; + } +} |