aboutsummaryrefslogtreecommitdiffstats
path: root/test/compilationTests/gnosis/Oracles/MajorityOracle.sol
blob: d809737005d60f33d28c4dd4a13895a5b424627c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
    constructor(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]) != address(0));
        oracles = _oracles;
    }

    /// @dev Allows to registers oracles for a majority vote
    /// @return Is outcome set?
    /// @return Outcome
    function getStatusAndOutcome()
        public
        view
        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
        view
        returns (bool)
    {
        (bool outcomeSet, ) = getStatusAndOutcome();
        return outcomeSet;
    }

    /// @dev Returns winning outcome
    /// @return Outcome
    function getOutcome()
        public
        view
        returns (int)
    {
        (, int winningOutcome) = getStatusAndOutcome();
        return winningOutcome;
    }
}