-
Notifications
You must be signed in to change notification settings - Fork 23
/
Witnet.sol
372 lines (360 loc) · 10.8 KB
/
Witnet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;
import "../interfaces/IWitnetRequest.sol";
library Witnet {
/// @notice Witnet function that computes the hash of a CBOR-encoded Data Request.
/// @param _bytecode CBOR-encoded RADON.
function hash(bytes memory _bytecode) internal pure returns (bytes32) {
return sha256(_bytecode);
}
/// Struct containing both request and response data related to every query posted to the Witnet Request Board
struct Query {
Request request;
Response response;
}
/// Possible status of a Witnet query.
enum QueryStatus {
Unknown,
Posted,
Reported,
Deleted
}
/// Data kept in EVM-storage for every Request posted to the Witnet Request Board.
struct Request {
IWitnetRequest addr; // The contract containing the Data Request which execution has been requested.
address requester; // Address from which the request was posted.
bytes32 hash; // Hash of the Data Request whose execution has been requested.
uint256 gasprice; // Minimum gas price the DR resolver should pay on the solving tx.
uint256 reward; // Escrowed reward to be paid to the DR resolver.
}
/// Data kept in EVM-storage containing Witnet-provided response metadata and result.
struct Response {
address reporter; // Address from which the result was reported.
uint256 timestamp; // Timestamp of the Witnet-provided result.
bytes32 drTxHash; // Hash of the Witnet transaction that solved the queried Data Request.
bytes cborBytes; // Witnet-provided result CBOR-bytes to the queried Data Request.
}
/// Data struct containing the Witnet-provided result to a Data Request.
struct Result {
bool success; // Flag stating whether the request could get solved successfully, or not.
CBOR value; // Resulting value, in CBOR-serialized bytes.
}
/// Data struct following the RFC-7049 standard: Concise Binary Object Representation.
struct CBOR {
Buffer buffer;
uint8 initialByte;
uint8 majorType;
uint8 additionalInformation;
uint64 len;
uint64 tag;
}
/// Iterable bytes buffer.
struct Buffer {
bytes data;
uint32 cursor;
}
/// Witnet error codes table.
enum ErrorCodes {
// 0x00: Unknown error. Something went really bad!
Unknown,
// Script format errors
/// 0x01: At least one of the source scripts is not a valid CBOR-encoded value.
SourceScriptNotCBOR,
/// 0x02: The CBOR value decoded from a source script is not an Array.
SourceScriptNotArray,
/// 0x03: The Array value decoded form a source script is not a valid Data Request.
SourceScriptNotRADON,
/// Unallocated
ScriptFormat0x04,
ScriptFormat0x05,
ScriptFormat0x06,
ScriptFormat0x07,
ScriptFormat0x08,
ScriptFormat0x09,
ScriptFormat0x0A,
ScriptFormat0x0B,
ScriptFormat0x0C,
ScriptFormat0x0D,
ScriptFormat0x0E,
ScriptFormat0x0F,
// Complexity errors
/// 0x10: The request contains too many sources.
RequestTooManySources,
/// 0x11: The script contains too many calls.
ScriptTooManyCalls,
/// Unallocated
Complexity0x12,
Complexity0x13,
Complexity0x14,
Complexity0x15,
Complexity0x16,
Complexity0x17,
Complexity0x18,
Complexity0x19,
Complexity0x1A,
Complexity0x1B,
Complexity0x1C,
Complexity0x1D,
Complexity0x1E,
Complexity0x1F,
// Operator errors
/// 0x20: The operator does not exist.
UnsupportedOperator,
/// Unallocated
Operator0x21,
Operator0x22,
Operator0x23,
Operator0x24,
Operator0x25,
Operator0x26,
Operator0x27,
Operator0x28,
Operator0x29,
Operator0x2A,
Operator0x2B,
Operator0x2C,
Operator0x2D,
Operator0x2E,
Operator0x2F,
// Retrieval-specific errors
/// 0x30: At least one of the sources could not be retrieved, but returned HTTP error.
HTTP,
/// 0x31: Retrieval of at least one of the sources timed out.
RetrievalTimeout,
/// Unallocated
Retrieval0x32,
Retrieval0x33,
Retrieval0x34,
Retrieval0x35,
Retrieval0x36,
Retrieval0x37,
Retrieval0x38,
Retrieval0x39,
Retrieval0x3A,
Retrieval0x3B,
Retrieval0x3C,
Retrieval0x3D,
Retrieval0x3E,
Retrieval0x3F,
// Math errors
/// 0x40: Math operator caused an underflow.
Underflow,
/// 0x41: Math operator caused an overflow.
Overflow,
/// 0x42: Tried to divide by zero.
DivisionByZero,
/// Unallocated
Math0x43,
Math0x44,
Math0x45,
Math0x46,
Math0x47,
Math0x48,
Math0x49,
Math0x4A,
Math0x4B,
Math0x4C,
Math0x4D,
Math0x4E,
Math0x4F,
// Other errors
/// 0x50: Received zero reveals
NoReveals,
/// 0x51: Insufficient consensus in tally precondition clause
InsufficientConsensus,
/// 0x52: Received zero commits
InsufficientCommits,
/// 0x53: Generic error during tally execution
TallyExecution,
/// Unallocated
OtherError0x54,
OtherError0x55,
OtherError0x56,
OtherError0x57,
OtherError0x58,
OtherError0x59,
OtherError0x5A,
OtherError0x5B,
OtherError0x5C,
OtherError0x5D,
OtherError0x5E,
OtherError0x5F,
/// 0x60: Invalid reveal serialization (malformed reveals are converted to this value)
MalformedReveal,
/// Unallocated
OtherError0x61,
OtherError0x62,
OtherError0x63,
OtherError0x64,
OtherError0x65,
OtherError0x66,
OtherError0x67,
OtherError0x68,
OtherError0x69,
OtherError0x6A,
OtherError0x6B,
OtherError0x6C,
OtherError0x6D,
OtherError0x6E,
OtherError0x6F,
// Access errors
/// 0x70: Tried to access a value from an index using an index that is out of bounds
ArrayIndexOutOfBounds,
/// 0x71: Tried to access a value from a map using a key that does not exist
MapKeyNotFound,
/// Unallocated
OtherError0x72,
OtherError0x73,
OtherError0x74,
OtherError0x75,
OtherError0x76,
OtherError0x77,
OtherError0x78,
OtherError0x79,
OtherError0x7A,
OtherError0x7B,
OtherError0x7C,
OtherError0x7D,
OtherError0x7E,
OtherError0x7F,
OtherError0x80,
OtherError0x81,
OtherError0x82,
OtherError0x83,
OtherError0x84,
OtherError0x85,
OtherError0x86,
OtherError0x87,
OtherError0x88,
OtherError0x89,
OtherError0x8A,
OtherError0x8B,
OtherError0x8C,
OtherError0x8D,
OtherError0x8E,
OtherError0x8F,
OtherError0x90,
OtherError0x91,
OtherError0x92,
OtherError0x93,
OtherError0x94,
OtherError0x95,
OtherError0x96,
OtherError0x97,
OtherError0x98,
OtherError0x99,
OtherError0x9A,
OtherError0x9B,
OtherError0x9C,
OtherError0x9D,
OtherError0x9E,
OtherError0x9F,
OtherError0xA0,
OtherError0xA1,
OtherError0xA2,
OtherError0xA3,
OtherError0xA4,
OtherError0xA5,
OtherError0xA6,
OtherError0xA7,
OtherError0xA8,
OtherError0xA9,
OtherError0xAA,
OtherError0xAB,
OtherError0xAC,
OtherError0xAD,
OtherError0xAE,
OtherError0xAF,
OtherError0xB0,
OtherError0xB1,
OtherError0xB2,
OtherError0xB3,
OtherError0xB4,
OtherError0xB5,
OtherError0xB6,
OtherError0xB7,
OtherError0xB8,
OtherError0xB9,
OtherError0xBA,
OtherError0xBB,
OtherError0xBC,
OtherError0xBD,
OtherError0xBE,
OtherError0xBF,
OtherError0xC0,
OtherError0xC1,
OtherError0xC2,
OtherError0xC3,
OtherError0xC4,
OtherError0xC5,
OtherError0xC6,
OtherError0xC7,
OtherError0xC8,
OtherError0xC9,
OtherError0xCA,
OtherError0xCB,
OtherError0xCC,
OtherError0xCD,
OtherError0xCE,
OtherError0xCF,
OtherError0xD0,
OtherError0xD1,
OtherError0xD2,
OtherError0xD3,
OtherError0xD4,
OtherError0xD5,
OtherError0xD6,
OtherError0xD7,
OtherError0xD8,
OtherError0xD9,
OtherError0xDA,
OtherError0xDB,
OtherError0xDC,
OtherError0xDD,
OtherError0xDE,
OtherError0xDF,
// Bridge errors: errors that only belong in inter-client communication
/// 0xE0: Requests that cannot be parsed must always get this error as their result.
/// However, this is not a valid result in a Tally transaction, because invalid requests
/// are never included into blocks and therefore never get a Tally in response.
BridgeMalformedRequest,
/// 0xE1: Witnesses exceeds 100
BridgePoorIncentives,
/// 0xE2: The request is rejected on the grounds that it may cause the submitter to spend or stake an
/// amount of value that is unjustifiably high when compared with the reward they will be getting
BridgeOversizedResult,
/// Unallocated
OtherError0xE3,
OtherError0xE4,
OtherError0xE5,
OtherError0xE6,
OtherError0xE7,
OtherError0xE8,
OtherError0xE9,
OtherError0xEA,
OtherError0xEB,
OtherError0xEC,
OtherError0xED,
OtherError0xEE,
OtherError0xEF,
OtherError0xF0,
OtherError0xF1,
OtherError0xF2,
OtherError0xF3,
OtherError0xF4,
OtherError0xF5,
OtherError0xF6,
OtherError0xF7,
OtherError0xF8,
OtherError0xF9,
OtherError0xFA,
OtherError0xFB,
OtherError0xFC,
OtherError0xFD,
OtherError0xFE,
// This should not exist:
/// 0xFF: Some tally error is not intercepted but should
UnhandledIntercept
}
}