-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_chain.js
1339 lines (1242 loc) · 50.5 KB
/
main_chain.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint node: true */
"use strict";
var _ = require('lodash');
var async = require('async');
var db = require('./db.js');
var constants = require("./constants.js");
var storage = require('./storage.js');
var graph = require('./graph.js');
var objectHash = require("./object_hash.js");
var paid_witnessing = require("./paid_witnessing.js");
var headers_commission = require("./headers_commission.js");
var mutex = require('./mutex.js');
var eventBus = require('./event_bus.js');
var profiler = require('./profiler.js');
var breadcrumbs = require('./breadcrumbs.js');
// override when adding units which caused witnessed level to significantly retreat
var arrRetreatingUnits = [
'+5ntioHT58jcFb8oVc+Ff4UvO5UvYGRcrGfYIofGUW8=',
'C/aPdM0sODPLC3NqJPWdZlqmV8B4xxf2N/+HSEi0sKU=',
'sSev6hvQU86SZBemy9CW2lJIko2jZDoY55Lm3zf2QU4=',
'19GglT3uZx1WmfWstLb3yIa85jTic+t01Kpe6s5gTTA=',
'Hyi2XVdZ/5D3H/MhwDL/jRWHp3F/dQTmwemyUHW+Urg=',
'xm0kFeKh6uqSXx6UUmc2ucgsNCU5h/e6wxSMWirhOTo='
];
function updateMainChain(conn, from_unit, last_added_unit, onDone){
var arrAllParents = [];
var arrNewMcUnits = [];
// if unit === null, read free balls
function findNextUpMainChainUnit(unit, handleUnit){
function handleProps(props){
if (props.best_parent_unit === null)
throw Error("best parent is null");
console.log("unit "+unit+", best parent "+props.best_parent_unit+", wlevel "+props.witnessed_level);
handleUnit(props.best_parent_unit);
}
function readLastUnitProps(handleLastUnitProps){
conn.query("SELECT unit AS best_parent_unit, witnessed_level \n\
FROM units WHERE is_free=1 \n\
ORDER BY witnessed_level DESC, \n\
level-witnessed_level ASC, \n\
unit ASC \n\
LIMIT 5",
function(rows){
if (rows.length === 0)
throw Error("no free units?");
if (rows.length > 1){
var arrParents = rows.map(function(row){ return row.best_parent_unit; });
arrAllParents = arrParents;
for (var i=0; i<arrRetreatingUnits.length; i++){
var n = arrParents.indexOf(arrRetreatingUnits[i]);
if (n >= 0)
return handleLastUnitProps(rows[n]);
}
}
/*
// override when adding +5ntioHT58jcFb8oVc+Ff4UvO5UvYGRcrGfYIofGUW8= which caused witnessed level to significantly retreat
if (rows.length === 2 && (rows[1].best_parent_unit === '+5ntioHT58jcFb8oVc+Ff4UvO5UvYGRcrGfYIofGUW8=' || rows[1].best_parent_unit === 'C/aPdM0sODPLC3NqJPWdZlqmV8B4xxf2N/+HSEi0sKU=' || rows[1].best_parent_unit === 'sSev6hvQU86SZBemy9CW2lJIko2jZDoY55Lm3zf2QU4=') && (rows[0].best_parent_unit === '3XJT1iK8FpFeGjwWXd9+Yu7uJp7hM692Sfbb5zdqWCE=' || rows[0].best_parent_unit === 'TyY/CY8xLGvJhK6DaBumj2twaf4y4jPC6umigAsldIA=' || rows[0].best_parent_unit === 'VKX2Nsx2W1uQYT6YajMGHAntwNuSMpAAlxF7Y98tKj8='))
return handleLastUnitProps(rows[1]);
*/
handleLastUnitProps(rows[0]);
}
);
}
unit ? storage.readStaticUnitProps(conn, unit, handleProps) : readLastUnitProps(handleProps);
}
function goUpFromUnit(unit){
if (storage.isGenesisUnit(unit))
return checkNotRebuildingStableMainChainAndGoDown(0, unit);
profiler.start();
findNextUpMainChainUnit(unit, function(best_parent_unit){
storage.readUnitProps(conn, best_parent_unit, function(objBestParentUnitProps){
var objBestParentUnitProps2 = storage.assocUnstableUnits[best_parent_unit];
if (!objBestParentUnitProps2){
if (storage.isGenesisUnit(best_parent_unit))
objBestParentUnitProps2 = storage.assocStableUnits[best_parent_unit];
else
throw Error("unstable unit not found: "+best_parent_unit);
}
var objBestParentUnitPropsForCheck = _.cloneDeep(objBestParentUnitProps2);
delete objBestParentUnitPropsForCheck.parent_units;
if (!storage.isGenesisUnit(best_parent_unit))
delete objBestParentUnitPropsForCheck.earned_headers_commission_recipients;
if (!_.isEqual(objBestParentUnitPropsForCheck, objBestParentUnitProps))
throwError("different props, db: "+JSON.stringify(objBestParentUnitProps)+", unstable: "+JSON.stringify(objBestParentUnitProps2));
if (!objBestParentUnitProps.is_on_main_chain)
conn.query("UPDATE units SET is_on_main_chain=1, main_chain_index=NULL WHERE unit=?", [best_parent_unit], function(){
objBestParentUnitProps2.is_on_main_chain = 1;
objBestParentUnitProps2.main_chain_index = null;
arrNewMcUnits.push(best_parent_unit);
profiler.stop('mc-goUpFromUnit');
goUpFromUnit(best_parent_unit);
});
else{
profiler.stop('mc-goUpFromUnit');
if (unit === null)
updateLatestIncludedMcIndex(objBestParentUnitProps.main_chain_index, false);
else
checkNotRebuildingStableMainChainAndGoDown(objBestParentUnitProps.main_chain_index, best_parent_unit);
}
});
});
}
function checkNotRebuildingStableMainChainAndGoDown(last_main_chain_index, last_main_chain_unit){
console.log("checkNotRebuildingStableMainChainAndGoDown "+from_unit);
profiler.start();
conn.query(
"SELECT unit FROM units WHERE is_on_main_chain=1 AND main_chain_index>? AND is_stable=1",
[last_main_chain_index],
function(rows){
profiler.stop('mc-checkNotRebuilding');
if (rows.length > 0)
throw Error("removing stable units "+rows.map(function(row){return row.unit}).join(', ')+" from MC after adding "+last_added_unit+" with all parents "+arrAllParents.join(', '));
goDownAndUpdateMainChainIndex(last_main_chain_index, last_main_chain_unit);
}
);
}
function goDownAndUpdateMainChainIndex(last_main_chain_index, last_main_chain_unit){
profiler.start();
conn.query(
//"UPDATE units SET is_on_main_chain=0, main_chain_index=NULL WHERE is_on_main_chain=1 AND main_chain_index>?",
"UPDATE units SET is_on_main_chain=0, main_chain_index=NULL WHERE main_chain_index>?",
[last_main_chain_index],
function(){
for (var unit in storage.assocUnstableUnits){
var o = storage.assocUnstableUnits[unit];
if (o.main_chain_index > last_main_chain_index){
o.is_on_main_chain = 0;
o.main_chain_index = null;
}
}
var main_chain_index = last_main_chain_index;
var main_chain_unit = last_main_chain_unit;
conn.query(
"SELECT unit FROM units WHERE is_on_main_chain=1 AND main_chain_index IS NULL ORDER BY level",
function(rows){
if (rows.length === 0){
//if (last_main_chain_index > 0)
throw Error("no unindexed MC units after adding "+last_added_unit);
//else{
// console.log("last MC=0, no unindexed MC units");
// return updateLatestIncludedMcIndex(last_main_chain_index, true);
//}
}
var arrDbNewMcUnits = rows.map(function(row){ return row.unit; });
arrNewMcUnits.reverse();
if (!_.isEqual(arrNewMcUnits, arrDbNewMcUnits))
throwError("different new MC units, arr: "+JSON.stringify(arrNewMcUnits)+", db: "+JSON.stringify(arrDbNewMcUnits));
async.eachSeries(
rows,
function(row, cb){
main_chain_index++;
var arrUnits = [row.unit];
function goUp(arrStartUnits){
conn.query(
"SELECT DISTINCT unit \n\
FROM parenthoods JOIN units ON parent_unit=unit \n\
WHERE child_unit IN(?) AND main_chain_index IS NULL",
[arrStartUnits],
function(rows){
var arrNewStartUnits = rows.map(function(row){ return row.unit; });
var arrNewStartUnits2 = [];
arrStartUnits.forEach(function(start_unit){
storage.assocUnstableUnits[start_unit].parent_units.forEach(function(parent_unit){
if (storage.assocUnstableUnits[parent_unit] && storage.assocUnstableUnits[parent_unit].main_chain_index === null && arrNewStartUnits2.indexOf(parent_unit) === -1)
arrNewStartUnits2.push(parent_unit);
});
});
if (!_.isEqual(arrNewStartUnits.sort(), arrNewStartUnits2.sort()))
throwError("different new start units, arr: "+JSON.stringify(arrNewStartUnits2)+", db: "+JSON.stringify(arrNewStartUnits));
if (arrNewStartUnits.length === 0)
return updateMc();
arrUnits = arrUnits.concat(arrNewStartUnits);
goUp(arrNewStartUnits);
}
);
}
function updateMc(){
arrUnits.forEach(function(unit){
storage.assocUnstableUnits[unit].main_chain_index = main_chain_index;
});
var strUnitList = arrUnits.map(db.escape).join(', ');
conn.query("UPDATE units SET main_chain_index=? WHERE unit IN("+strUnitList+")", [main_chain_index], function(){
conn.query("UPDATE unit_authors SET _mci=? WHERE unit IN("+strUnitList+")", [main_chain_index], function(){
cb();
});
});
}
goUp(arrUnits);
},
function(err){
console.log("goDownAndUpdateMainChainIndex done");
if (err)
throw Error("goDownAndUpdateMainChainIndex eachSeries failed");
conn.query(
"UPDATE unit_authors SET _mci=NULL WHERE unit IN(SELECT unit FROM units WHERE main_chain_index IS NULL)",
function(){
profiler.stop('mc-goDown');
updateLatestIncludedMcIndex(last_main_chain_index, true);
}
);
}
);
}
);
}
);
}
function updateLatestIncludedMcIndex(last_main_chain_index, bRebuiltMc){
function checkAllLatestIncludedMcIndexesAreSet(){
profiler.start();
if (!_.isEqual(assocDbLimcisByUnit, assocLimcisByUnit))
throwError("different LIMCIs, mem: "+JSON.stringify(assocLimcisByUnit)+", db: "+JSON.stringify(assocDbLimcisByUnit));
conn.query("SELECT unit FROM units WHERE latest_included_mc_index IS NULL AND level!=0", function(rows){
if (rows.length > 0)
throw Error(rows.length+" units have latest_included_mc_index=NULL, e.g. unit "+rows[0].unit);
profiler.stop('mc-limci-check');
updateStableMcFlag();
});
}
function propagateLIMCI(){
console.log("propagateLIMCI "+last_main_chain_index);
profiler.start();
// the 1st condition in WHERE is the same that was used 2 queries ago to NULL limcis
conn.query(
/*
"UPDATE units AS punits \n\
JOIN parenthoods ON punits.unit=parent_unit \n\
JOIN units AS chunits ON child_unit=chunits.unit \n\
SET chunits.latest_included_mc_index=punits.latest_included_mc_index \n\
WHERE (chunits.main_chain_index > ? OR chunits.main_chain_index IS NULL) \n\
AND (chunits.latest_included_mc_index IS NULL OR chunits.latest_included_mc_index < punits.latest_included_mc_index)",
[last_main_chain_index],
function(result){
(result.affectedRows > 0) ? propagateLIMCI() : checkAllLatestIncludedMcIndexesAreSet();
}
*/
"SELECT punits.latest_included_mc_index, chunits.unit \n\
FROM units AS punits \n\
JOIN parenthoods ON punits.unit=parent_unit \n\
JOIN units AS chunits ON child_unit=chunits.unit \n\
WHERE (chunits.main_chain_index > ? OR chunits.main_chain_index IS NULL) \n\
AND (chunits.latest_included_mc_index IS NULL OR chunits.latest_included_mc_index < punits.latest_included_mc_index)",
[last_main_chain_index],
function(rows){
profiler.stop('mc-limci-select-propagate');
if (rows.length === 0)
return checkAllLatestIncludedMcIndexesAreSet();
profiler.start();
async.eachSeries(
rows,
function(row, cb){
assocDbLimcisByUnit[row.unit] = row.latest_included_mc_index;
conn.query("UPDATE units SET latest_included_mc_index=? WHERE unit=?", [row.latest_included_mc_index, row.unit], function(){cb();});
},
function(){
profiler.stop('mc-limci-update-propagate');
propagateLIMCI();
}
);
}
);
}
function loadUnitProps(unit, handleProps){
if (storage.assocUnstableUnits[unit])
return handleProps(storage.assocUnstableUnits[unit]);
storage.readUnitProps(conn, unit, handleProps);
}
function calcLIMCIs(onUpdated){
var arrFilledUnits = [];
async.forEachOfSeries(
assocChangedUnits,
function(props, unit, cb){
var max_limci = -1;
async.eachSeries(
props.parent_units,
function(parent_unit, cb2){
loadUnitProps(parent_unit, function(parent_props){
if (parent_props.is_on_main_chain){
props.latest_included_mc_index = parent_props.main_chain_index;
assocLimcisByUnit[unit] = props.latest_included_mc_index;
arrFilledUnits.push(unit);
return cb2('done');
}
if (parent_props.latest_included_mc_index === null)
return cb2('parent limci not known yet');
if (parent_props.latest_included_mc_index > max_limci)
max_limci = parent_props.latest_included_mc_index;
cb2();
});
},
function(err){
if (err)
return cb();
if (max_limci < 0)
throw Error("max limci < 0 for unit "+unit);
props.latest_included_mc_index = max_limci;
assocLimcisByUnit[unit] = props.latest_included_mc_index;
arrFilledUnits.push(unit);
cb();
}
);
},
function(){
arrFilledUnits.forEach(function(unit){
delete assocChangedUnits[unit];
});
if (Object.keys(assocChangedUnits).length > 0)
calcLIMCIs(onUpdated);
else
onUpdated();
}
);
}
console.log("updateLatestIncludedMcIndex "+last_main_chain_index);
profiler.start();
var assocChangedUnits = {};
var assocLimcisByUnit = {};
var assocDbLimcisByUnit = {};
for (var unit in storage.assocUnstableUnits){
var o = storage.assocUnstableUnits[unit];
if (o.main_chain_index > last_main_chain_index || o.main_chain_index === null){
o.latest_included_mc_index = null;
assocChangedUnits[unit] = o;
}
}
calcLIMCIs(function(){
conn.query("UPDATE units SET latest_included_mc_index=NULL WHERE main_chain_index>? OR main_chain_index IS NULL", [last_main_chain_index], function(res){
console.log("update LIMCI=NULL done, matched rows: "+res.affectedRows);
profiler.stop('mc-limci-set-null');
profiler.start();
conn.query(
// if these units have other parents, they cannot include later MC units (otherwise, the parents would've been redundant).
// the 2nd condition in WHERE is the same that was used 1 query ago to NULL limcis.
// I had to rewrite this single query because sqlite doesn't support JOINs in UPDATEs
/*
"UPDATE units AS punits \n\
JOIN parenthoods ON punits.unit=parent_unit \n\
JOIN units AS chunits ON child_unit=chunits.unit \n\
SET chunits.latest_included_mc_index=punits.main_chain_index \n\
WHERE punits.is_on_main_chain=1 \n\
AND (chunits.main_chain_index > ? OR chunits.main_chain_index IS NULL) \n\
AND chunits.latest_included_mc_index IS NULL",
[last_main_chain_index],
function(result){
if (result.affectedRows === 0 && bRebuiltMc)
throw "no latest_included_mc_index updated";
propagateLIMCI();
}
*/
"SELECT chunits.unit, punits.main_chain_index \n\
FROM units AS punits \n\
JOIN parenthoods ON punits.unit=parent_unit \n\
JOIN units AS chunits ON child_unit=chunits.unit \n\
WHERE punits.is_on_main_chain=1 \n\
AND (chunits.main_chain_index > ? OR chunits.main_chain_index IS NULL) \n\
AND chunits.latest_included_mc_index IS NULL",
[last_main_chain_index],
function(rows){
console.log(rows.length+" rows");
profiler.stop('mc-limci-select-initial');
profiler.start();
if (rows.length === 0 && bRebuiltMc)
throw Error("no latest_included_mc_index updated, last_mci="+last_main_chain_index+", affected="+res.affectedRows);
async.eachSeries(
rows,
function(row, cb){
console.log(row.main_chain_index, row.unit);
assocDbLimcisByUnit[row.unit] = row.main_chain_index;
conn.query("UPDATE units SET latest_included_mc_index=? WHERE unit=?", [row.main_chain_index, row.unit], function(){ cb(); });
},
function(){
profiler.stop('mc-limci-update-initial');
propagateLIMCI();
}
);
}
);
});
});
}
function readLastStableMcUnit(handleLastStableMcUnit){
conn.query("SELECT unit FROM units WHERE is_on_main_chain=1 AND is_stable=1 ORDER BY main_chain_index DESC LIMIT 1", function(rows){
if (rows.length === 0)
throw Error("no units on stable MC?");
handleLastStableMcUnit(rows[0].unit);
});
}
function updateStableMcFlag(){
console.log("updateStableMcFlag");
profiler.start();
readLastStableMcUnit(function(last_stable_mc_unit){
console.log("last stable mc unit "+last_stable_mc_unit);
storage.readWitnesses(conn, last_stable_mc_unit, function(arrWitnesses){
conn.query("SELECT unit, is_on_main_chain, main_chain_index, level FROM units WHERE best_parent_unit=?", [last_stable_mc_unit], function(rows){
if (rows.length === 0){
//if (isGenesisUnit(last_stable_mc_unit))
// return finish();
throw Error("no best children of last stable MC unit "+last_stable_mc_unit+"?");
}
var arrMcRows = rows.filter(function(row){ return (row.is_on_main_chain === 1); }); // only one element
var arrAltRows = rows.filter(function(row){ return (row.is_on_main_chain === 0); });
if (arrMcRows.length !== 1)
throw Error("not a single MC child?");
var first_unstable_mc_unit = arrMcRows[0].unit;
var first_unstable_mc_index = arrMcRows[0].main_chain_index;
var first_unstable_mc_level = arrMcRows[0].level;
var arrAltBranchRootUnits = arrAltRows.map(function(row){ return row.unit; });
function advanceLastStableMcUnitAndTryNext(){
profiler.stop('mc-stableFlag');
markMcIndexStable(conn, first_unstable_mc_index, updateStableMcFlag);
}
conn.query("SELECT witnessed_level FROM units WHERE is_free=1 AND is_on_main_chain=1", function(wl_rows){
if (wl_rows.length !== 1)
throw Error("not a single mc wl");
// this is the level when we colect 7 witnesses if walking up the MC from its end
var mc_end_witnessed_level = wl_rows[0].witnessed_level;
conn.query(
// among these 7 witnesses, find min wl
"SELECT MIN(witnessed_level) AS min_mc_wl FROM units LEFT JOIN unit_authors USING(unit) \n\
WHERE is_on_main_chain=1 AND level>=? AND address IN(?)", // _left_ join enforces the best query plan in sqlite
[mc_end_witnessed_level, arrWitnesses],
function(min_wl_rows){
if (min_wl_rows.length !== 1)
throw Error("not a single min mc wl");
var min_mc_wl = min_wl_rows[0].min_mc_wl;
if (arrAltBranchRootUnits.length === 0){ // no alt branches
if (min_mc_wl >= first_unstable_mc_level)
return advanceLastStableMcUnitAndTryNext();
return finish();
/*
// if there are 12 witnesses on the MC, the next unit is stable
// This is not reliable. Adding a new unit after this one (not descending from this one)
// could change the MC near the tip and route the MC away from a witness-authored unit, thus decreasing the count below 12
conn.query(
"SELECT COUNT(DISTINCT address) AS count_witnesses FROM units JOIN unit_authors USING(unit) \n\
WHERE is_on_main_chain=1 AND main_chain_index>=? AND address IN(?)",
[first_unstable_mc_index, arrWitnesses],
function(count_witnesses_rows){
(count_witnesses_rows[0].count_witnesses === constants.COUNT_WITNESSES)
? advanceLastStableMcUnitAndTryNext() : finish();
}
);
return;
*/
}
createListOfBestChildren(arrAltBranchRootUnits, function(arrAltBestChildren){
determineMaxAltLevel(
conn, first_unstable_mc_index, first_unstable_mc_level, arrAltBestChildren, arrWitnesses,
function(max_alt_level){
(min_mc_wl > max_alt_level) ? advanceLastStableMcUnitAndTryNext() : finish();
}
);
});
}
);
});
});
});
});
}
// also includes arrParentUnits
function createListOfBestChildren(arrParentUnits, handleBestChildrenList){
if (arrParentUnits.length === 0)
return handleBestChildrenList([]);
var arrBestChildren = arrParentUnits.slice();
function goDownAndCollectBestChildren(arrStartUnits, cb){
conn.query("SELECT unit, is_free FROM units WHERE best_parent_unit IN(?)", [arrStartUnits], function(rows){
if (rows.length === 0)
return cb();
//console.log("unit", arrStartUnits, "best children:", rows.map(function(row){ return row.unit; }), "free units:", rows.reduce(function(sum, row){ return sum+row.is_free; }, 0));
async.eachSeries(
rows,
function(row, cb2){
arrBestChildren.push(row.unit);
if (row.is_free === 1)
cb2();
else
goDownAndCollectBestChildren([row.unit], cb2);
},
cb
);
});
}
goDownAndCollectBestChildren(arrParentUnits, function(){
handleBestChildrenList(arrBestChildren);
});
}
function finish(){
profiler.stop('mc-stableFlag');
console.log("done updating MC\n");
if (onDone)
onDone();
}
console.log("\nwill update MC");
/*if (from_unit === null && arrRetreatingUnits.indexOf(last_added_unit) >= 0){
conn.query("UPDATE units SET is_on_main_chain=1, main_chain_index=NULL WHERE unit=?", [last_added_unit], function(){
goUpFromUnit(last_added_unit);
});
}
else*/
goUpFromUnit(from_unit);
}
/*
// climbs up along best parent links up, returns list of units encountered with level >= min_level
function createListOfPrivateMcUnits(start_unit, min_level, handleList){
var arrUnits = [];
function goUp(unit){
conn.query(
"SELECT best_parent_unit, level FROM units WHERE unit=?", [unit],
function(rows){
if (rows.length !== 1)
throw "createListOfPrivateMcUnits: not 1 row";
var row = rows[0];
if (row.level < min_level)
return handleList(arrUnits);
arrUnits.push(unit);
goUp(row.best_parent_unit);
}
);
}
goUp(start_unit);
}
*/
function readBestChildrenProps(conn, arrUnits, handleResult){
if (arrUnits.every(function(unit){ return !!storage.assocUnstableUnits[unit]; })){
var arrProps = [];
arrUnits.forEach(function(unit){
if (storage.assocBestChildren[unit])
arrProps = arrProps.concat(storage.assocBestChildren[unit]);
});
return handleResult(arrProps);
}
conn.query("SELECT unit, is_on_main_chain, main_chain_index, level, is_free FROM units WHERE best_parent_unit IN(?)", [arrUnits], function(rows){
if (arrUnits.every(function(unit){ return !!storage.assocUnstableUnits[unit]; })){
var arrProps = [];
arrUnits.forEach(function(unit){
if (storage.assocBestChildren[unit])
arrProps = arrProps.concat(storage.assocBestChildren[unit]);
});
if (!arraysEqual(_.sortBy(rows, 'unit'), _.sortBy(arrProps, 'unit'), ['unit', 'is_on_main_chain', 'main_chain_index', 'level', 'is_free']))
throwError("different best children of "+arrUnits.join(', ')+": db "+JSON.stringify(rows)+", mem "+JSON.stringify(arrProps));
}
handleResult(rows);
});
}
function arraysEqual(arr1, arr2, fields){
if (arr1.length !== arr2.length)
return false;
for (var i=0; i<arr1.length; i++)
for (var j=0; j<fields.length; j++)
if (arr1[i][fields[i]] !== arr2[i][fields[i]])
return false;
return true;
}
function determineMaxAltLevel(conn, first_unstable_mc_index, first_unstable_mc_level, arrAltBestChildren, arrWitnesses, handleResult){
// console.log('============= alt branch children\n', arrAltBestChildren.join('\n'));
// Compose a set S of units that increase WL, that is their own WL is greater than that of every parent.
// In this set, find max L. Alt WL will never reach it. If min_mc_wl > L, next MC unit is stable.
// Also filter the set S to include only those units that are conformant with the last stable MC unit.
if (first_unstable_mc_index >= constants.altBranchByBestParentUpgradeMci){
conn.query(
"SELECT MAX(units.level) AS max_alt_level \n\
FROM units \n\
CROSS JOIN units AS bpunits \n\
ON units.best_parent_unit=bpunits.unit AND bpunits.witnessed_level < units.witnessed_level \n\
WHERE units.unit IN("+arrAltBestChildren.map(db.escape).join(', ')+")",
function(max_alt_rows){
var max_alt_level = max_alt_rows[0].max_alt_level; // can be null
// console.log('===== min_mc_wl='+min_mc_wl+', max_alt_level='+max_alt_level+", first_unstable_mc_level="+first_unstable_mc_level);
handleResult(max_alt_level || first_unstable_mc_level);
}
);
}
else{
// this sql query is totally wrong but we still leave it for compatibility
conn.query(
"SELECT MAX(units.level) AS max_alt_level \n\
FROM units \n\
LEFT JOIN parenthoods ON units.unit=child_unit \n\
LEFT JOIN units AS punits ON parent_unit=punits.unit AND punits.witnessed_level >= units.witnessed_level \n\
WHERE units.unit IN("+arrAltBestChildren.map(db.escape).join(', ')+") AND punits.unit IS NULL AND ( \n\
SELECT COUNT(*) \n\
FROM unit_witnesses \n\
WHERE unit_witnesses.unit IN(units.unit, units.witness_list_unit) AND unit_witnesses.address IN(?) \n\
)>=?",
[arrWitnesses, constants.COUNT_WITNESSES - constants.MAX_WITNESS_LIST_MUTATIONS],
function(max_alt_rows){
if (max_alt_rows.length !== 1)
throw Error("not a single max alt level");
var max_alt_level = max_alt_rows[0].max_alt_level;
// console.log('===== min_mc_wl='+min_mc_wl+', max_alt_level='+max_alt_level+", first_unstable_mc_level="+first_unstable_mc_level);
handleResult(max_alt_level);
}
);
}
}
function determineIfStableInLaterUnits(conn, earlier_unit, arrLaterUnits, handleResult){
if (storage.isGenesisUnit(earlier_unit))
return handleResult(true);
// hack to workaround past validation error
if (earlier_unit === 'LGFzduLJNQNzEqJqUXdkXr58wDYx77V8WurDF3+GIws=' && arrLaterUnits.join(',') === '6O4t3j8kW0/Lo7n2nuS8ITDv2UbOhlL9fF1M6j/PrJ4=')
return handleResult(true);
var start_time = Date.now();
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; }));
var max_later_level = Math.max.apply(
null, arrLaterUnitProps.map(function(objLaterUnitProps){ return objLaterUnitProps.level; }));
readBestParentAndItsWitnesses(conn, earlier_unit, function(best_parent_unit, arrWitnesses){
conn.query("SELECT unit, is_on_main_chain, main_chain_index, level FROM units WHERE best_parent_unit=?", [best_parent_unit], function(rows){
if (rows.length === 0)
throw Error("no best children of "+best_parent_unit+"?");
var arrMcRows = rows.filter(function(row){ return (row.is_on_main_chain === 1); }); // only one element
var arrAltRows = rows.filter(function(row){ return (row.is_on_main_chain === 0); });
if (arrMcRows.length !== 1)
throw Error("not a single MC child?");
var first_unstable_mc_unit = arrMcRows[0].unit;
if (first_unstable_mc_unit !== earlier_unit)
throw Error("first unstable MC unit is not our input unit");
var first_unstable_mc_index = arrMcRows[0].main_chain_index;
var first_unstable_mc_level = arrMcRows[0].level;
var arrAltBranchRootUnits = arrAltRows.map(function(row){ return row.unit; });
//console.log("first_unstable_mc_index", first_unstable_mc_index);
//console.log("first_unstable_mc_level", first_unstable_mc_level);
//console.log("alt", arrAltBranchRootUnits);
function findMinMcWitnessedLevel(handleMinMcWl){
createListOfBestChildrenIncludedByLaterUnits([first_unstable_mc_unit], function(arrBestChildren){
conn.query( // if 2 witnesses authored the same unit, unit_authors will be joined 2 times and counted twice
"SELECT witnessed_level, address \n\
FROM units \n\
CROSS JOIN unit_authors USING(unit) \n\
WHERE unit IN("+arrBestChildren.map(db.escape).join(', ')+") AND address IN(?) \n\
ORDER BY witnessed_level DESC",
[arrWitnesses],
function(rows){
var arrCollectedWitnesses = [];
var min_mc_wl = -1;
for (var i=0; i<rows.length; i++){
var row = rows[i];
if (arrCollectedWitnesses.indexOf(row.address) === -1){
arrCollectedWitnesses.push(row.address);
if (arrCollectedWitnesses.length >= constants.MAJORITY_OF_WITNESSES){
min_mc_wl = row.witnessed_level;
break;
}
}
}
// var min_mc_wl = rows[constants.MAJORITY_OF_WITNESSES-1].witnessed_level;
if (first_unstable_mc_index > constants.branchedMinMcWlUpgradeMci){
if (min_mc_wl === -1)
throw Error("couldn't collect 7 witnesses, earlier unit "+earlier_unit+", best children "+arrBestChildren.join(', '));
return handleMinMcWl(min_mc_wl);
}
// it might be more optimistic because it collects 7 witness units, not 7 units posted by _different_ witnesses
findMinMcWitnessedLevelOld(function(old_min_mc_wl){
var diff = min_mc_wl - old_min_mc_wl;
console.log("---------- new min_mc_wl="+min_mc_wl+", old min_mc_wl="+old_min_mc_wl+", diff="+diff+", later "+arrLaterUnits.join(', '));
// if (diff < 0)
// throw Error("new min_mc_wl="+min_mc_wl+", old min_mc_wl="+old_min_mc_wl+", diff="+diff+" for earlier "+earlier_unit+", later "+arrLaterUnits.join(', '));
handleMinMcWl(old_min_mc_wl);
});
}
);
});
}
function findMinMcWitnessedLevelOld(handleMinMcWl){
var min_mc_wl = Number.MAX_VALUE;
var count = 0;
function goUp(start_unit){
conn.query(
"SELECT best_parent_unit, witnessed_level, \n\
(SELECT COUNT(*) FROM unit_authors WHERE unit_authors.unit=units.unit AND address IN(?)) AS count \n\
FROM units WHERE unit=?", [arrWitnesses, start_unit],
function(rows){
if (rows.length !== 1)
throw Error("findMinMcWitnessedLevelOld: not 1 row");
var row = rows[0];
if (row.count > 0 && row.witnessed_level < min_mc_wl)
min_mc_wl = row.witnessed_level;
count += row.count; // this is a bug, should count only unique witnesses
(count < constants.MAJORITY_OF_WITNESSES) ? goUp(row.best_parent_unit) : handleMinMcWl(min_mc_wl);
}
);
}
conn.query(
"SELECT witnessed_level, best_parent_unit, \n\
(SELECT COUNT(*) FROM unit_authors WHERE unit_authors.unit=units.unit AND address IN(?)) AS count \n\
FROM units \n\
WHERE unit IN(?) \n\
ORDER BY witnessed_level DESC, \n\
level-witnessed_level ASC, \n\
unit ASC \n\
LIMIT 1",
[arrWitnesses, arrLaterUnits],
function(rows){
var row = rows[0];
if (row.count > 0)
min_mc_wl = row.witnessed_level;
count += row.count;
goUp(row.best_parent_unit);
}
);
}
function determineIfHasAltBranches(handleHasAltBranchesResult){
if (arrAltBranchRootUnits.length === 0)
return handleHasAltBranchesResult(false);
// check if alt branches are included by later units
async.eachSeries(
arrAltBranchRootUnits,
function(alt_root_unit, cb){
graph.determineIfIncludedOrEqual(conn, alt_root_unit, arrLaterUnits, function(bIncluded){
bIncluded ? cb("included") : cb();
});
},
function(err){
handleHasAltBranchesResult(err ? true : false);
}
);
}
// also includes arrAltBranchRootUnits
function createListOfBestChildrenIncludedByLaterUnits(arrAltBranchRootUnits, handleBestChildrenList){
if (arrAltBranchRootUnits.length === 0)
return handleBestChildrenList([]);
var arrBestChildren = [];
var arrTips = [];
var arrNotIncludedTips = [];
var arrRemovedBestChildren = [];
function goDownAndCollectBestChildren(arrStartUnits, cb){
conn.query("SELECT unit, is_free, main_chain_index FROM units WHERE best_parent_unit IN(?)", [arrStartUnits], function(rows){
if (rows.length === 0)
return cb();
async.eachSeries(
rows,
function(row, cb2){
function addUnit(){
arrBestChildren.push(row.unit);
if (row.is_free === 1 || arrLaterUnits.indexOf(row.unit) >= 0)
cb2();
else
goDownAndCollectBestChildren([row.unit], cb2);
}
if (row.main_chain_index !== null && row.main_chain_index <= max_later_limci)
addUnit();
else
graph.determineIfIncludedOrEqual(conn, row.unit, arrLaterUnits, function(bIncluded){
bIncluded ? addUnit() : cb2();
});
},
cb
);
});
}
function goDownAndCollectBestChildrenFast(arrStartUnits, cb){
readBestChildrenProps(conn, arrStartUnits, function(rows){
if (rows.length === 0){
arrStartUnits.forEach(function(start_unit){
arrTips.push(start_unit);
});
return cb();
}
async.eachSeries(
rows,
function(row, cb2){
arrBestChildren.push(row.unit);
if (arrLaterUnits.indexOf(row.unit) >= 0)
cb2();
else if (row.is_free === 1 || row.level >= max_later_level || row.is_on_main_chain && row.main_chain_index > max_later_limci){
arrTips.push(row.unit);
arrNotIncludedTips.push(row.unit);
cb2();
}
else
goDownAndCollectBestChildrenFast([row.unit], cb2);
},
cb
);
});
}
function findBestChildrenNotIncludedInLaterUnits(arrUnits, cb){
var arrUnitsToRemove = [];
async.eachSeries(
arrUnits,
function(unit, cb2){
if (arrRemovedBestChildren.indexOf(unit) >= 0)
return cb2();
if (arrNotIncludedTips.indexOf(unit) >= 0){
arrUnitsToRemove.push(unit);
return cb2();
}
graph.determineIfIncludedOrEqual(conn, unit, arrLaterUnits, function(bIncluded){
if (!bIncluded)
arrUnitsToRemove.push(unit);
cb2();
});
},
function(){
if (arrUnitsToRemove.length === 0)
return cb();
arrRemovedBestChildren = arrRemovedBestChildren.concat(arrUnitsToRemove);
goUp(arrUnitsToRemove, cb);
}
);
}
function goUp(arrCurrentTips, cb){
var arrUnits = [];
async.eachSeries(
arrCurrentTips,
function(unit, cb2){
storage.readStaticUnitProps(conn, unit, function(props){
if (arrUnits.indexOf(props.best_parent_unit) === -1)
arrUnits.push(props.best_parent_unit);
cb2();
});
},
function(){
findBestChildrenNotIncludedInLaterUnits(arrUnits, cb);
}
);
}
function collectBestChildren(arrFilteredAltBranchRootUnits, cb){
goDownAndCollectBestChildrenFast(arrFilteredAltBranchRootUnits, function(){
if (arrTips.length === 0)
return cb();
var start_time = Date.now();
findBestChildrenNotIncludedInLaterUnits(arrTips, function(){
console.log("findBestChildrenNotIncludedInLaterUnits took "+(Date.now()-start_time)+"ms");
arrBestChildren = _.difference(arrBestChildren, arrRemovedBestChildren);
cb();
});
});
}
// leaves only those roots that are included by later units
function filterAltBranchRootUnits(cb){
//console.log('===== before filtering:', arrAltBranchRootUnits);
var arrFilteredAltBranchRootUnits = [];
conn.query("SELECT unit, is_free, main_chain_index FROM units WHERE unit IN(?)", [arrAltBranchRootUnits], function(rows){
if (rows.length === 0)
throw Error("no alt branch root units?");
async.eachSeries(
rows,
function(row, cb2){
function addUnit(){
arrBestChildren.push(row.unit);
// if (row.is_free === 0) // seems no reason to exclude
arrFilteredAltBranchRootUnits.push(row.unit);
cb2();
}
if (row.main_chain_index !== null && row.main_chain_index <= max_later_limci)
addUnit();
else
graph.determineIfIncludedOrEqual(conn, row.unit, arrLaterUnits, function(bIncluded){
bIncluded ? addUnit() : cb2();
});
},
function(){
//console.log('filtered:', arrFilteredAltBranchRootUnits);
if (arrFilteredAltBranchRootUnits.length === 0)
return handleBestChildrenList([]);
var arrInitialBestChildren = _.clone(arrBestChildren);
var start_time = Date.now();
goDownAndCollectBestChildren(arrFilteredAltBranchRootUnits, function(){
console.log("goDownAndCollectBestChildren took "+(Date.now()-start_time)+"ms");
var arrBestChildren1 = _.clone(arrBestChildren.sort());
arrBestChildren = arrInitialBestChildren;
start_time = Date.now();
collectBestChildren(arrFilteredAltBranchRootUnits, function(){
console.log("collectBestChildren took "+(Date.now()-start_time)+"ms");
arrBestChildren.sort();
if (!_.isEqual(arrBestChildren, arrBestChildren1)){
throwError("different best children, old "+arrBestChildren1.join(', ')+'; new '+arrBestChildren.join(', '));
arrBestChildren = arrBestChildren1;
}
cb();
});
});
}
);
});
}
filterAltBranchRootUnits(function(){
//console.log('best children:', arrBestChildren);
handleBestChildrenList(arrBestChildren);
});
}
findMinMcWitnessedLevel(function(min_mc_wl){
//console.log("min mc wl", min_mc_wl);
determineIfHasAltBranches(function(bHasAltBranches){
if (!bHasAltBranches){
console.log("determineIfStableInLaterUnits no alt took "+(Date.now()-start_time)+"ms");
if (min_mc_wl >= first_unstable_mc_level)
return handleResult(true);
return handleResult(false);
/*
// Wrong. See the comment above
// if there are 12 witnesses on the MC, the next unit is stable
conn.query(
"SELECT COUNT(DISTINCT address) AS count_witnesses FROM units JOIN unit_authors USING(unit) \n\
WHERE is_on_main_chain=1 AND main_chain_index>=? AND address IN(?)",
[first_unstable_mc_index, arrWitnesses],
function(count_witnesses_rows){
console.log(count_witnesses_rows[0]);
handleResult(count_witnesses_rows[0].count_witnesses === constants.COUNT_WITNESSES);
}
);
return;
*/
}
// has alt branches
if (first_unstable_mc_index >= constants.altBranchByBestParentUpgradeMci && min_mc_wl < first_unstable_mc_level){
console.log("determineIfStableInLaterUnits min_mc_wl < first_unstable_mc_level with branches: not stable took "+(Date.now()-start_time)+"ms");
return handleResult(false);
}
createListOfBestChildrenIncludedByLaterUnits(arrAltBranchRootUnits, function(arrAltBestChildren){
determineMaxAltLevel(
conn, first_unstable_mc_index, first_unstable_mc_level, arrAltBestChildren, arrWitnesses,
function(max_alt_level){
console.log("determineIfStableInLaterUnits with branches took "+(Date.now()-start_time)+"ms");
// allow '=' since alt WL will *never* reach max_alt_level.
// The comparison when moving the stability point above is still strict for compatibility
handleResult(min_mc_wl >= max_alt_level);
}
);
});
});
});
});
});