-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_feeds.js
390 lines (381 loc) · 12.8 KB
/
data_feeds.js
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*jslint node: true */
"use strict";
var async = require('async');
var _ = require('lodash');
var constants = require('./constants.js');
var kvstore = require('./kvstore.js');
var string_utils = require('./string_utils.js');
var storage = require('./storage.js');
var ValidationUtils = require("./validation_utils.js");
function dataFeedExists(arrAddresses, feed_name, relation, value, min_mci, max_mci, bAA, handleResult){
var start_time = Date.now();
var bLimitedPrecision = (max_mci < constants.aa2UpgradeMci);
if (bAA) {
var bFound = false;
function relationSatisfied(v1, v2) {
switch (relation) {
case '<': return (v1 < v2);
case '<=': return (v1 <= v2);
case '>': return (v1 > v2);
case '>=': return (v1 >= v2);
default: throw Error("unknown relation: " + relation);
}
}
for (var unit in storage.assocUnstableMessages) {
var objUnit = storage.assocUnstableUnits[unit] || storage.assocStableUnits[unit];
if (!objUnit)
throw Error("unstable unit " + unit + " not in assoc");
if (!objUnit.bAA)
continue;
if (objUnit.latest_included_mc_index < min_mci || objUnit.latest_included_mc_index > max_mci)
continue;
if (_.intersection(arrAddresses, objUnit.author_addresses).length === 0)
continue;
storage.assocUnstableMessages[unit].forEach(function (message) {
if (message.app !== 'data_feed')
return;
var payload = message.payload;
if (!ValidationUtils.hasOwnProperty(payload, feed_name))
return;
var feed_value = payload[feed_name];
if (relation === '=') {
if (value === feed_value || value.toString() === feed_value.toString())
bFound = true;
return;
}
if (relation === '!=') {
if (value.toString() !== feed_value.toString())
bFound = true;
return;
}
if (typeof value === 'number' && typeof feed_value === 'number') {
if (relationSatisfied(feed_value, value))
bFound = true;
return;
}
var f_value = (typeof value === 'string') ? string_utils.toNumber(value, bLimitedPrecision) : value;
var f_feed_value = (typeof feed_value === 'string') ? string_utils.toNumber(feed_value, bLimitedPrecision) : feed_value;
if (f_value === null && f_feed_value === null) { // both are strings that don't look like numbers
if (relationSatisfied(feed_value, value))
bFound = true;
return;
}
if (f_value !== null && f_feed_value !== null) { // both are either numbers or strings that look like numbers
if (relationSatisfied(f_feed_value, f_value))
bFound = true;
return;
}
if (typeof value === 'string' && typeof feed_value === 'string') { // only one string looks like a number
if (relationSatisfied(feed_value, value))
bFound = true;
return;
}
// else they are incomparable e.g. 'abc' > 123
});
if (bFound)
break;
}
if (bFound)
return handleResult(true);
}
async.eachSeries(
arrAddresses,
function(address, cb){
dataFeedByAddressExists(address, feed_name, relation, value, min_mci, max_mci, cb);
},
function(bFound){
console.log('data feed by '+arrAddresses+' '+feed_name+relation+value+': '+bFound+', df took '+(Date.now()-start_time)+'ms');
handleResult(!!bFound);
}
);
}
function dataFeedByAddressExists(address, feed_name, relation, value, min_mci, max_mci, handleResult){
if (relation === '!='){
return dataFeedByAddressExists(address, feed_name, '>', value, min_mci, max_mci, function(bFound){
if (bFound)
return handleResult(true);
dataFeedByAddressExists(address, feed_name, '<', value, min_mci, max_mci, handleResult);
});
}
var prefixed_value;
var type;
if (typeof value === 'string'){
var bLimitedPrecision = (max_mci < constants.aa2UpgradeMci);
var float = string_utils.toNumber(value, bLimitedPrecision);
if (float !== null){
prefixed_value = 'n\n'+string_utils.encodeDoubleInLexicograpicOrder(float);
type = 'n';
}
else{
prefixed_value = 's\n'+value;
type = 's';
}
}
else{
prefixed_value = 'n\n'+string_utils.encodeDoubleInLexicograpicOrder(value);
type= 'n';
}
var strMinMci = string_utils.encodeMci(min_mci);
var strMaxMci = string_utils.encodeMci(max_mci);
var key_prefix = 'df\n'+address+'\n'+feed_name+'\n'+prefixed_value;
var bFound = false;
var options = {};
switch (relation){
case '=':
options.gte = key_prefix+'\n'+strMaxMci;
options.lte = key_prefix+'\n'+strMinMci;
options.limit = 1;
break;
case '>=':
options.gte = key_prefix;
options.lt = 'df\n'+address+'\n'+feed_name+'\n'+type+'\r'; // \r is next after \n
break;
case '>':
options.gt = key_prefix+'\nffffffff';
options.lt = 'df\n'+address+'\n'+feed_name+'\n'+type+'\r'; // \r is next after \n
break;
case '<=':
options.lte = key_prefix+'\nffffffff';
options.gt = 'df\n'+address+'\n'+feed_name+'\n'+type+'\n';
break;
case '<':
options.lt = key_prefix;
options.gt = 'df\n'+address+'\n'+feed_name+'\n'+type+'\n';
break;
}
var count = 0;
var count_before_found = 0;
var handleData;
if (relation === '=')
handleData = function(data){
count++;
count_before_found++;
bFound = true;
};
else
handleData = function(data){
count++;
if (bFound)
return;
count_before_found++;
var mci = string_utils.getMciFromDataFeedKey(data);
if (mci >= min_mci && mci <= max_mci){
bFound = true;
console.log('destroying stream prematurely');
stream.destroy();
onEnd();
}
};
var bOnEndCalled = false;
function onEnd(){
if (bOnEndCalled)
throw Error("second call of onEnd");
bOnEndCalled = true;
console.log('data feed by '+address+' '+feed_name+relation+value+': '+bFound+', '+count_before_found+' / '+count+' records inspected');
handleResult(bFound);
}
var stream = kvstore.createKeyStream(options);
stream.on('data', handleData)
.on('end', onEnd)
.on('error', function(error){
throw Error('error from data stream: '+error);
});
}
// timestamp is for light only
function readDataFeedValue(arrAddresses, feed_name, value, min_mci, max_mci, unstable_opts, ifseveral, timestamp, handleResult){
var bLimitedPrecision = (max_mci < constants.aa2UpgradeMci);
var start_time = Date.now();
var objResult = { bAbortedBecauseOfSeveral: false, value: undefined, unit: undefined, mci: undefined };
var bIncludeUnstableAAs = !!unstable_opts;
var bIncludeAllUnstable = (unstable_opts === 'all_unstable');
if (bIncludeUnstableAAs) {
var arrCandidates = [];
for (var unit in storage.assocUnstableMessages) {
var objUnit = storage.assocUnstableUnits[unit] || storage.assocStableUnits[unit];
if (!objUnit)
throw Error("unstable unit " + unit + " not in assoc");
if (!objUnit.bAA && !bIncludeAllUnstable)
continue;
if (objUnit.latest_included_mc_index < min_mci || objUnit.latest_included_mc_index > max_mci)
continue;
if (_.intersection(arrAddresses, objUnit.author_addresses).length === 0)
continue;
storage.assocUnstableMessages[unit].forEach(function (message) {
if (message.app !== 'data_feed')
return;
var payload = message.payload;
if (!ValidationUtils.hasOwnProperty(payload, feed_name))
return;
var feed_value = payload[feed_name];
if (value === null || value === feed_value || value.toString() === feed_value.toString())
arrCandidates.push({
value: string_utils.getFeedValue(feed_value, bLimitedPrecision),
latest_included_mc_index: objUnit.latest_included_mc_index,
level: objUnit.level,
unit: objUnit.unit,
mci: max_mci // it doesn't matter
});
});
}
if (arrCandidates.length === 1) {
var feed = arrCandidates[0];
objResult.value = feed.value;
objResult.unit = feed.unit;
objResult.mci = feed.mci;
if (ifseveral === 'last')
return handleResult(objResult);
}
else if (arrCandidates.length > 1) {
if (ifseveral === 'abort') {
objResult.bAbortedBecauseOfSeveral = true;
return handleResult(objResult);
}
arrCandidates.sort(function (a, b) {
if (a.latest_included_mc_index < b.latest_included_mc_index)
return -1;
if (a.latest_included_mc_index > b.latest_included_mc_index)
return 1;
if (a.level < b.level)
return -1;
if (a.level > b.level)
return 1;
throw Error("can't sort candidates "+a+" and "+b);
});
var feed = arrCandidates[arrCandidates.length - 1];
objResult.value = feed.value;
objResult.unit = feed.unit;
objResult.mci = feed.mci;
return handleResult(objResult);
}
}
async.eachSeries(
arrAddresses,
function(address, cb){
readDataFeedByAddress(address, feed_name, value, min_mci, max_mci, ifseveral, objResult, cb);
},
function(err){ // err passed here if aborted because of several
console.log('data feed by '+arrAddresses+' '+feed_name+', val='+value+': '+objResult.value+', dfv took '+(Date.now()-start_time)+'ms');
handleResult(objResult);
}
);
}
function readDataFeedByAddress(address, feed_name, value, min_mci, max_mci, ifseveral, objResult, handleResult){
var bLimitedPrecision = (max_mci < constants.aa2UpgradeMci);
var bAbortIfSeveral = (ifseveral === 'abort');
var key_prefix;
if (value === null){
key_prefix = 'dfv\n'+address+'\n'+feed_name;
}
else{
var prefixed_value;
if (typeof value === 'string'){
var float = string_utils.toNumber(value, bLimitedPrecision);
if (float !== null)
prefixed_value = 'n\n'+string_utils.encodeDoubleInLexicograpicOrder(float);
else
prefixed_value = 's\n'+value;
}
else
prefixed_value = 'n\n'+string_utils.encodeDoubleInLexicograpicOrder(value);
key_prefix = 'df\n'+address+'\n'+feed_name+'\n'+prefixed_value;
}
var options = {
gte: key_prefix+'\n'+string_utils.encodeMci(max_mci),
lte: key_prefix+'\n'+string_utils.encodeMci(min_mci),
limit: bAbortIfSeveral ? 2 : 1
};
var handleData = function(data){
if (bAbortIfSeveral && objResult.value !== undefined){
objResult.bAbortedBecauseOfSeveral = true;
return;
}
var mci = string_utils.getMciFromDataFeedKey(data.key);
if (objResult.value === undefined || ifseveral === 'last' && mci > objResult.mci){
if (value !== null){
objResult.value = string_utils.getValueFromDataFeedKey(data.key);
objResult.unit = data.value;
}
else{
var arrParts = data.value.split('\n');
objResult.value = string_utils.getFeedValue(arrParts[0], bLimitedPrecision); // may convert to number
objResult.unit = arrParts[1];
}
objResult.mci = mci;
}
};
kvstore.createReadStream(options)
.on('data', handleData)
.on('end', function(){
handleResult(objResult.bAbortedBecauseOfSeveral);
})
.on('error', function(error){
throw Error('error from data stream: '+error);
});
}
function readDataFeedValueByParams(params, max_mci, unstable_opts, cb) {
var oracles = params.oracles;
if (!oracles)
return cb("no params in light/get_profiles_units");
if (!ValidationUtils.isNonemptyArray(oracles))
return cb("oracles must be non-empty array");
if (!oracles.every(ValidationUtils.isValidAddress))
return cb("some oracle addresses are not valid");
if (oracles.length > 10)
return cb("too many oracles");
var feed_name = params.feed_name;
if (!feed_name || typeof feed_name !== 'string')
return cb("empty feed_name or not a string");
var value = null;
if ('feed_value' in params) {
value = params.feed_value;
if (!isValidValue(value))
return cb("bad feed_value: " + value);
}
var min_mci = 0;
if ('min_mci' in params) {
min_mci = params.min_mci;
if (!ValidationUtils.isNonnegativeInteger(min_mci))
return cb("bad min_mci: " + min_mci);
}
var ifseveral = 'last';
if ('ifseveral' in params) {
ifseveral = params.ifseveral;
if (ifseveral !== 'abort' && ifseveral !== 'last')
return cb("bad ifseveral: " + ifseveral);
}
var what = 'value';
if ('what' in params) {
what = params.what;
if (what !== 'unit' && what !== 'value')
return cb("bad what: " + what);
}
var type = 'auto';
if ('type' in params) {
type = params.type;
if (type !== 'string' && type !== 'auto')
return cb("bad df type: " + type);
}
if ('ifnone' in params && !isValidValue(params.ifnone))
return cb("bad ifnone: " + params.ifnone);
readDataFeedValue(oracles, feed_name, value, min_mci, max_mci, unstable_opts, ifseveral, Math.round(Date.now() / 1000), function (objResult) {
if (objResult.bAbortedBecauseOfSeveral)
return cb("several values found");
if (objResult.value !== undefined) {
if (what === 'unit')
return cb(null, objResult.unit);
if (type === 'string')
return cb(null, objResult.value.toString());
return cb(null, objResult.value);
}
if ('ifnone' in params && params.ifnone !== 'abort') {
return cb(null, params.ifnone); // the type of ifnone (string, number, boolean) is preserved
}
cb("data feed " + feed_name + " not found");
});
}
function isValidValue(val){
return (typeof val === 'string' || typeof val === 'boolean' || typeof val === 'number');
}
exports.dataFeedExists = dataFeedExists;
exports.readDataFeedValue = readDataFeedValue;
exports.readDataFeedValueByParams = readDataFeedValueByParams;