-
Notifications
You must be signed in to change notification settings - Fork 47
/
DifficultyOracle.sol
111 lines (100 loc) · 3.45 KB
/
DifficultyOracle.sol
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
pragma solidity ^0.4.0;
import "Oracles/AbstractOracle.sol";
/// @title Difficulty oracle contract - On chain oracle to resolve difficulty events at given block.
/// @author Stefan George - <[email protected]>
/// @author Martin Koeppelmann - <[email protected]>
contract DifficultyOracle is Oracle {
/*
* Constants
*/
// Oracle meta data
string constant public name = "Difficulty Oracle";
/*
* Data structures
*/
// block number => result
mapping (uint => uint) difficultyResults;
// event identifier => block number
mapping (bytes32 => uint) eventIdentifiers;
/*
* Read and write functions
*/
/// @dev Sets difficulty as winning outcome for a specific block. Returns success.
/// @param eventIdentifier Hash identifying an event.
/// @param data Encodes data used to resolve event. In this case block number.
function setOutcome(bytes32 eventIdentifier, bytes32[] data)
external
{
uint blockNumber = eventIdentifiers[eventIdentifier];
if (block.number < blockNumber || difficultyResults[blockNumber] != 0) {
// Block number was not reached yet or it was set already
throw;
}
difficultyResults[blockNumber] = block.difficulty;
}
/// @dev Validates and registers event. Returns event identifier.
/// @param data Array of oracle addresses used for event resolution.
/// @return eventIdentifier Returns event identifier.
function registerEvent(bytes32[] data)
public
returns (bytes32 eventIdentifier)
{
uint blockNumber = uint(data[0]);
if (blockNumber <= block.number) {
// Block number was already reached
throw;
}
eventIdentifier = sha3(data);
eventIdentifiers[eventIdentifier] = blockNumber;
EventRegistration(msg.sender, eventIdentifier);
}
/*
* Read functions
*/
/// @dev Returns if winning outcome is set for given event.
/// @param eventIdentifier Hash identifying an event.
/// @return isSet Returns if outcome is set.
function isOutcomeSet(bytes32 eventIdentifier)
constant
public
returns (bool isSet)
{
uint blockNumber = eventIdentifiers[eventIdentifier];
// Difficulty will never be == 0
return difficultyResults[blockNumber] > 0;
}
/// @dev Returns winning outcome for given event.
/// @param eventIdentifier Hash identifying an event.
/// @return outcome Returns outcome.
function getOutcome(bytes32 eventIdentifier)
constant
public
returns (int outcome)
{
uint blockNumber = eventIdentifiers[eventIdentifier];
return int(difficultyResults[blockNumber]);
}
/// @dev Returns data needed to identify an event.
/// @param eventIdentifier Hash identifying an event.
/// @return data Returns event data.
function getEventData(bytes32 eventIdentifier)
constant
public
returns (bytes32[] data)
{
data = new bytes32[](1);
data[0] = bytes32(eventIdentifiers[eventIdentifier]);
}
/// @dev Returns total fees for oracle.
/// @param data Event data used for event resolution.
/// @return fee Returns fee.
/// @return token Returns token.
function getFee(bytes32[] data)
constant
public
returns (uint fee, address token)
{
fee = 0;
token = 0;
}
}