forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
393 lines (347 loc) · 16.8 KB
/
graph.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
391
392
393
/*jslint node: true */
"use strict";
var _ = require('lodash');
var async = require('async');
var constants = require('./constants.js');
var storage = require('./storage.js');
var db = require('./db.js');
var profiler = require('./profiler.js');
var conf = require('./conf.js');
function compareUnits(conn, unit1, unit2, handleResult){
if (unit1 === unit2)
return handleResult(0);
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free FROM units WHERE unit IN(?)",
[[unit1, unit2]],
function(rows){
if (rows.length !== 2)
throw Error("not 2 rows");
var objUnitProps1 = (rows[0].unit === unit1) ? rows[0] : rows[1];
var objUnitProps2 = (rows[0].unit === unit2) ? rows[0] : rows[1];
compareUnitsByProps(conn, objUnitProps1, objUnitProps2, handleResult);
}
);
}
function compareUnitsByProps(conn, objUnitProps1, objUnitProps2, handleResult){
if (objUnitProps1.unit === objUnitProps2.unit)
return handleResult(0);
if (objUnitProps1.level === objUnitProps2.level)
return handleResult(null);
if (objUnitProps1.is_free === 1 && objUnitProps2.is_free === 1) // free units
return handleResult(null);
// genesis
if (objUnitProps1.latest_included_mc_index === null)
return handleResult(-1);
if (objUnitProps2.latest_included_mc_index === null)
return handleResult(+1);
if (objUnitProps1.latest_included_mc_index >= objUnitProps2.main_chain_index && objUnitProps2.main_chain_index !== null)
return handleResult(+1);
if (objUnitProps2.latest_included_mc_index >= objUnitProps1.main_chain_index && objUnitProps1.main_chain_index !== null)
return handleResult(-1);
if (objUnitProps1.level <= objUnitProps2.level
&& objUnitProps1.latest_included_mc_index <= objUnitProps2.latest_included_mc_index
&& (objUnitProps1.main_chain_index <= objUnitProps2.main_chain_index
&& objUnitProps1.main_chain_index !== null && objUnitProps2.main_chain_index !== null
|| objUnitProps1.main_chain_index === null || objUnitProps2.main_chain_index === null)
||
objUnitProps1.level >= objUnitProps2.level
&& objUnitProps1.latest_included_mc_index >= objUnitProps2.latest_included_mc_index
&& (objUnitProps1.main_chain_index >= objUnitProps2.main_chain_index
&& objUnitProps1.main_chain_index !== null && objUnitProps2.main_chain_index !== null
|| objUnitProps1.main_chain_index === null || objUnitProps2.main_chain_index === null)
){
// still can be comparable
}
else
return handleResult(null);
var objEarlierUnit = (objUnitProps1.level < objUnitProps2.level) ? objUnitProps1 : objUnitProps2;
var objLaterUnit = (objUnitProps1.level < objUnitProps2.level) ? objUnitProps2 : objUnitProps1;
var resultIfFound = (objUnitProps1.level < objUnitProps2.level) ? -1 : 1;
// can be negative if main_chain_index === null but that doesn't matter
var earlier_unit_delta = objEarlierUnit.main_chain_index - objEarlierUnit.latest_included_mc_index;
var later_unit_delta = objLaterUnit.main_chain_index - objLaterUnit.latest_included_mc_index;
var arrKnownUnits = [];
function goUp(arrStartUnits){
//console.log('compare', arrStartUnits);
//console.log('compare goUp', objUnitProps1.unit, objUnitProps2.unit);
arrKnownUnits = arrKnownUnits.concat(arrStartUnits);
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain \n\
FROM parenthoods JOIN units ON parent_unit=unit \n\
WHERE child_unit IN(?)",
[arrStartUnits],
function(rows){
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
if (objUnitProps.unit === objEarlierUnit.unit)
return handleResult(resultIfFound);
if (objUnitProps.main_chain_index !== null && objUnitProps.main_chain_index <= objEarlierUnit.latest_included_mc_index)
continue;
if (objUnitProps.is_on_main_chain === 0 && objUnitProps.level > objEarlierUnit.level)
arrNewStartUnits.push(objUnitProps.unit);
}
arrNewStartUnits = _.uniq(arrNewStartUnits);
arrNewStartUnits = _.difference(arrNewStartUnits, arrKnownUnits);
(arrNewStartUnits.length > 0) ? goUp(arrNewStartUnits) : handleResult(null);
}
);
}
function goDown(arrStartUnits){
arrKnownUnits = arrKnownUnits.concat(arrStartUnits);
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain \n\
FROM parenthoods JOIN units ON child_unit=unit \n\
WHERE parent_unit IN(?)",
[arrStartUnits],
function(rows){
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
if (objUnitProps.unit === objLaterUnit.unit)
return handleResult(resultIfFound);
if (objLaterUnit.main_chain_index !== null && objLaterUnit.main_chain_index <= objUnitProps.latest_included_mc_index)
continue;
if (objUnitProps.is_on_main_chain === 0 && objUnitProps.level < objLaterUnit.level)
arrNewStartUnits.push(objUnitProps.unit);
}
arrNewStartUnits = _.uniq(arrNewStartUnits);
arrNewStartUnits = _.difference(arrNewStartUnits, arrKnownUnits);
(arrNewStartUnits.length > 0) ? goDown(arrNewStartUnits) : handleResult(null);
}
);
}
(later_unit_delta > earlier_unit_delta) ? goUp([objLaterUnit.unit]) : goDown([objEarlierUnit.unit]);
}
// determines if earlier_unit is included by at least one of arrLaterUnits
function determineIfIncluded(conn, earlier_unit, arrLaterUnits, handleResult){
// console.log('determineIfIncluded', earlier_unit, arrLaterUnits, new Error().stack);
if (!earlier_unit)
throw Error("no earlier_unit");
if (storage.isGenesisUnit(earlier_unit))
return handleResult(true);
storage.readPropsOfUnits(conn, earlier_unit, arrLaterUnits, function(objEarlierUnitProps, arrLaterUnitProps){
if (objEarlierUnitProps.is_free === 1)
return handleResult(false);
var max_later_limci = Math.max.apply(
null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.latest_included_mc_index; }));
//console.log("max limci "+max_later_limci+", earlier mci "+objEarlierUnitProps.main_chain_index);
if (objEarlierUnitProps.main_chain_index !== null && max_later_limci >= objEarlierUnitProps.main_chain_index)
return handleResult(true);
if (max_later_limci < objEarlierUnitProps.latest_included_mc_index)
return handleResult(false);
var max_later_level = Math.max.apply(
null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.level; }));
if (max_later_level < objEarlierUnitProps.level)
return handleResult(false);
var max_later_wl = Math.max.apply(
null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.witnessed_level; }));
if (max_later_wl < objEarlierUnitProps.witnessed_level && objEarlierUnitProps.main_chain_index > constants.witnessedLevelMustNotRetreatFromAllParentsUpgradeMci)
return handleResult(false);
var bAllLaterUnitsAreWithMci = !arrLaterUnitProps.find(function(objLaterUnitProps){ return (objLaterUnitProps.main_chain_index === null); });
if (bAllLaterUnitsAreWithMci){
if (objEarlierUnitProps.main_chain_index === null){
console.log('all later are with mci, earlier is null mci', objEarlierUnitProps, arrLaterUnitProps);
return handleResult(false);
}
var max_later_mci = Math.max.apply(
null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.main_chain_index; }));
if (max_later_mci < objEarlierUnitProps.main_chain_index)
return handleResult(false);
}
var arrKnownUnits = [];
function goUp(arrStartUnits){
// console.log('determine goUp', earlier_unit, arrLaterUnits/*, arrStartUnits*/);
arrKnownUnits = arrKnownUnits.concat(arrStartUnits);
var arrDbStartUnits = [];
var arrParents = [];
arrStartUnits.forEach(function(unit){
var props = storage.assocUnstableUnits[unit] || storage.assocStableUnits[unit];
if (!props || !props.parent_units){
arrDbStartUnits.push(unit);
return;
}
props.parent_units.forEach(function(parent_unit){
var objParent = storage.assocUnstableUnits[parent_unit] || storage.assocStableUnits[parent_unit];
if (!objParent){
if (arrDbStartUnits.indexOf(unit) === -1)
arrDbStartUnits.push(unit);
return;
}
/*objParent = _.cloneDeep(objParent);
for (var key in objParent)
if (['unit', 'level', 'latest_included_mc_index', 'main_chain_index', 'is_on_main_chain'].indexOf(key) === -1)
delete objParent[key];*/
arrParents.push(objParent);
});
});
if (arrDbStartUnits.length > 0){
console.log('failed to find all parents in memory, will query the db, earlier '+earlier_unit+', later '+arrLaterUnits+', not found '+arrDbStartUnits);
arrParents = [];
}
function handleParents(rows){
// var sort_fun = function(row){ return row.unit; };
// if (arrParents.length > 0 && !_.isEqual(_.sortBy(rows, sort_fun), _.sortBy(arrParents, sort_fun)))
// throw Error("different parents");
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
if (objUnitProps.unit === earlier_unit)
return handleResult(true);
if (objUnitProps.main_chain_index !== null && objUnitProps.main_chain_index <= objEarlierUnitProps.latest_included_mc_index)
continue;
if (objUnitProps.main_chain_index !== null && objEarlierUnitProps.main_chain_index !== null && objUnitProps.main_chain_index < objEarlierUnitProps.main_chain_index)
continue;
if (objUnitProps.main_chain_index !== null && objEarlierUnitProps.main_chain_index === null)
continue;
if (objUnitProps.latest_included_mc_index < objEarlierUnitProps.latest_included_mc_index)
continue;
if (objUnitProps.witnessed_level < objEarlierUnitProps.witnessed_level && objEarlierUnitProps.main_chain_index > constants.witnessedLevelMustNotRetreatFromAllParentsUpgradeMci)
continue;
if (objUnitProps.is_on_main_chain === 0 && objUnitProps.level > objEarlierUnitProps.level)
arrNewStartUnits.push(objUnitProps.unit);
}
arrNewStartUnits = _.uniq(arrNewStartUnits);
arrNewStartUnits = _.difference(arrNewStartUnits, arrKnownUnits);
(arrNewStartUnits.length > 0) ? goUp(arrNewStartUnits) : handleResult(false);
}
if (arrParents.length)
return setImmediate(handleParents, arrParents);
conn.query(
"SELECT unit, level, witnessed_level, latest_included_mc_index, main_chain_index, is_on_main_chain \n\
FROM parenthoods JOIN units ON parent_unit=unit \n\
WHERE child_unit IN(?)",
[arrStartUnits],
handleParents
);
}
goUp(arrLaterUnits);
});
}
function determineIfIncludedOrEqual(conn, earlier_unit, arrLaterUnits, handleResult){
if (arrLaterUnits.indexOf(earlier_unit) >= 0)
return handleResult(true);
determineIfIncluded(conn, earlier_unit, arrLaterUnits, handleResult);
}
// excludes earlier unit
function readDescendantUnitsByAuthorsBeforeMcIndex(conn, objEarlierUnitProps, arrAuthorAddresses, to_main_chain_index, handleUnits){
var arrUnits = [];
var arrKnownUnits = [];
function goDown(arrStartUnits){
profiler.start();
arrKnownUnits = arrKnownUnits.concat(arrStartUnits);
var indexMySQL = conf.storage == "mysql" ? "USE INDEX (PRIMARY)" : "";
conn.query(
"SELECT units.unit, unit_authors.address AS author_in_list \n\
FROM parenthoods \n\
JOIN units ON child_unit=units.unit \n\
LEFT JOIN unit_authors "+ indexMySQL + " ON unit_authors.unit=units.unit AND address IN(?) \n\
WHERE parent_unit IN(?) AND latest_included_mc_index<? AND main_chain_index<=?",
[arrAuthorAddresses, arrStartUnits, objEarlierUnitProps.main_chain_index, to_main_chain_index],
function(rows){
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
arrNewStartUnits.push(objUnitProps.unit);
if (objUnitProps.author_in_list)
arrUnits.push(objUnitProps.unit);
}
profiler.stop('mc-wc-descendants-goDown');
arrNewStartUnits = _.difference(arrNewStartUnits, arrKnownUnits);
(arrNewStartUnits.length > 0) ? goDown(arrNewStartUnits) : handleUnits(arrUnits);
}
);
}
profiler.start();
var indexMySQL = conf.storage == "mysql" ? "USE INDEX (PRIMARY)" : "";
conn.query( // _left_ join forces use of indexes in units
"SELECT unit FROM units "+db.forceIndex("byMcIndex")+" LEFT JOIN unit_authors " + indexMySQL + " USING(unit) \n\
WHERE latest_included_mc_index>=? AND main_chain_index>? AND main_chain_index<=? AND latest_included_mc_index<? AND address IN(?)",
[objEarlierUnitProps.main_chain_index, objEarlierUnitProps.main_chain_index, to_main_chain_index, to_main_chain_index, arrAuthorAddresses],
// "SELECT unit FROM units WHERE latest_included_mc_index>=? AND main_chain_index<=?",
// [objEarlierUnitProps.main_chain_index, to_main_chain_index],
function(rows){
arrUnits = rows.map(function(row) { return row.unit; });
profiler.stop('mc-wc-descendants-initial');
goDown([objEarlierUnitProps.unit]);
}
);
}
// excludes earlier unit
function readDescendantUnitsBeforeLandingOnMc(conn, objEarlierUnitProps, arrLaterUnitProps, handleUnits){
var max_later_limci = Math.max.apply(null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.latest_included_mc_index; }));
var max_later_level = Math.max.apply(null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.level; }));
var arrLandedUnits = []; // units that landed on MC before max_later_limci, they are already included in at least one of later units
var arrUnlandedUnits = []; // direct shoots to later units, without touching the MC
function goDown(arrStartUnits){
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain \n\
FROM parenthoods JOIN units ON child_unit=unit \n\
WHERE parent_unit IN(?) AND latest_included_mc_index<? AND level<=?",
[arrStartUnits, objEarlierUnitProps.main_chain_index, max_later_level],
function(rows){
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
//if (objUnitProps.latest_included_mc_index >= objEarlierUnitProps.main_chain_index)
// continue;
//if (objUnitProps.level > max_later_level)
// continue;
arrNewStartUnits.push(objUnitProps.unit);
if (objUnitProps.main_chain_index !== null && objUnitProps.main_chain_index <= max_later_limci) // exclude free balls!
arrLandedUnits.push(objUnitProps.unit);
else
arrUnlandedUnits.push(objUnitProps.unit);
}
(arrNewStartUnits.length > 0) ? goDown(arrNewStartUnits) : handleUnits(arrLandedUnits, arrUnlandedUnits);
}
);
}
goDown([objEarlierUnitProps.unit]);
}
// includes later units
function readAscendantUnitsAfterTakingOffMc(conn, objEarlierUnitProps, arrLaterUnitProps, handleUnits){
var arrLaterUnits = arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.unit; });
var max_later_limci = Math.max.apply(null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.latest_included_mc_index; }));
var arrLandedUnits = []; // units that took off MC after earlier unit's MCI, they already include the earlier unit
var arrUnlandedUnits = []; // direct shoots from earlier units, without touching the MC
arrLaterUnitProps.forEach(function(objUnitProps){
if (objUnitProps.latest_included_mc_index >= objEarlierUnitProps.main_chain_index)
arrLandedUnits.push(objUnitProps.unit);
else
arrUnlandedUnits.push(objUnitProps.unit);
});
function goUp(arrStartUnits){
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain \n\
FROM parenthoods JOIN units ON parent_unit=unit \n\
WHERE child_unit IN(?) AND (main_chain_index>? OR main_chain_index IS NULL) AND level>=?",
[arrStartUnits, max_later_limci, objEarlierUnitProps.level],
function(rows){
var arrNewStartUnits = [];
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
//if (objUnitProps.main_chain_index <= max_later_limci)
// continue;
//if (objUnitProps.level < objEarlierUnitProps.level)
// continue;
arrNewStartUnits.push(objUnitProps.unit);
if (objUnitProps.latest_included_mc_index >= objEarlierUnitProps.main_chain_index)
arrLandedUnits.push(objUnitProps.unit);
else
arrUnlandedUnits.push(objUnitProps.unit);
}
(arrNewStartUnits.length > 0) ? goUp(arrNewStartUnits) : handleUnits(arrLandedUnits, arrUnlandedUnits);
}
);
}
goUp(arrLaterUnits);
}
exports.compareUnitsByProps = compareUnitsByProps;
exports.compareUnits = compareUnits;
exports.determineIfIncluded = determineIfIncluded;
exports.determineIfIncludedOrEqual = determineIfIncludedOrEqual;
exports.readDescendantUnitsByAuthorsBeforeMcIndex = readDescendantUnitsByAuthorsBeforeMcIndex;
// used only in majority_witnessing.js which is not used itself
exports.readDescendantUnitsBeforeLandingOnMc = readDescendantUnitsBeforeLandingOnMc;
exports.readAscendantUnitsAfterTakingOffMc = readAscendantUnitsAfterTakingOffMc;