-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.js
1251 lines (1145 loc) · 62.6 KB
/
storage.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 async = require('async');
var _ = require('lodash');
var db = require('./db.js');
var conf = require('./conf.js');
var objectHash = require("./object_hash.js");
var constants = require("./constants.js");
var archiving = require('./archiving.js');
var MAX_INT32 = Math.pow(2, 31) - 1;
var assocKnownUnits = {};
var assocCachedUnits = {};
var assocCachedUnitAuthors = {};
var assocCachedUnitWitnesses = {};
var assocCachedAssetInfos = {};
var assocUnstableUnits = {};
var assocStableUnits = {};
var assocStableUnitsByMci = {};
var assocBestChildren = {};
var min_retrievable_mci = null;
//initializeMinRetrievableMci();
function readJoint(conn, unit, callbacks) {
if (!conf.bSaveJointJson)
return readJointDirectly(conn, unit, callbacks);
conn.query("SELECT json FROM joints WHERE unit=?", [unit], function(rows){
if (rows.length === 0)
return readJointDirectly(conn, unit, callbacks);
var objJoint = JSON.parse(rows[0].json);
if (!objJoint.ball){ // got there because of an old bug
conn.query("DELETE FROM joints WHERE unit=?", [unit]);
return readJointDirectly(conn, unit, callbacks);
}
callbacks.ifFound(objJoint);
});
}
function readJointDirectly(conn, unit, callbacks, bRetrying) {
console.log("\nreading unit "+unit);
if (min_retrievable_mci === null){
console.log("min_retrievable_mci not known yet");
setTimeout(function(){
readJointDirectly(conn, unit, callbacks);
}, 1000);
return;
}
//profiler.start();
conn.query(
"SELECT units.unit, version, alt, witness_list_unit, last_ball_unit, balls.ball AS last_ball, is_stable, \n\
content_hash, headers_commission, payload_commission, main_chain_index, "+conn.getUnixTimestamp("units.creation_date")+" AS timestamp \n\
FROM units LEFT JOIN balls ON last_ball_unit=balls.unit WHERE units.unit=?",
[unit],
function(unit_rows){
if (unit_rows.length === 0){
//profiler.stop('read');
return callbacks.ifNotFound();
}
var objUnit = unit_rows[0];
var objJoint = {unit: objUnit};
var main_chain_index = objUnit.main_chain_index;
//delete objUnit.main_chain_index;
objUnit.timestamp = parseInt(objUnit.timestamp);
var bFinalBad = !!objUnit.content_hash;
var bStable = objUnit.is_stable;
delete objUnit.is_stable;
objectHash.cleanNulls(objUnit);
var bVoided = (objUnit.content_hash && main_chain_index < min_retrievable_mci);
var bRetrievable = (main_chain_index >= min_retrievable_mci || main_chain_index === null);
if (!conf.bLight && !objUnit.last_ball && !isGenesisUnit(unit))
throw Error("no last ball in unit "+JSON.stringify(objUnit));
// unit hash verification below will fail if:
// 1. the unit was received already voided, i.e. its messages are stripped and content_hash is set
// 2. the unit is still retrievable (e.g. we are syncing)
// In this case, bVoided=false hence content_hash will be deleted but the messages are missing
if (bVoided){
//delete objUnit.last_ball;
//delete objUnit.last_ball_unit;
delete objUnit.headers_commission;
delete objUnit.payload_commission;
}
else
delete objUnit.content_hash;
async.series([
function(callback){ // parents
conn.query(
"SELECT parent_unit \n\
FROM parenthoods \n\
WHERE child_unit=? \n\
ORDER BY parent_unit",
[unit],
function(rows){
if (rows.length === 0)
return callback();
objUnit.parent_units = rows.map(function(row){ return row.parent_unit; });
callback();
}
);
},
function(callback){ // ball
if (bRetrievable && !isGenesisUnit(unit))
return callback();
// include the .ball field even if it is not stable yet, because its parents might have been changed
// and the receiver should not attempt to verify them
conn.query("SELECT ball FROM balls WHERE unit=?", [unit], function(rows){
if (rows.length === 0)
return callback();
objJoint.ball = rows[0].ball;
callback();
});
},
function(callback){ // skiplist
if (bRetrievable)
return callback();
conn.query("SELECT skiplist_unit FROM skiplist_units WHERE unit=? ORDER BY skiplist_unit", [unit], function(rows){
if (rows.length === 0)
return callback();
objJoint.skiplist_units = rows.map(function(row){ return row.skiplist_unit; });
callback();
});
},
function(callback){ // witnesses
conn.query("SELECT address FROM unit_witnesses WHERE unit=? ORDER BY address", [unit], function(rows){
if (rows.length > 0)
objUnit.witnesses = rows.map(function(row){ return row.address; });
callback();
});
},
function(callback){ // earned_headers_commission_recipients
if (bVoided)
return callback();
conn.query("SELECT address, earned_headers_commission_share FROM earned_headers_commission_recipients \
WHERE unit=? ORDER BY address",
[unit],
function(rows){
if (rows.length > 0)
objUnit.earned_headers_commission_recipients = rows;
callback();
}
);
},
function(callback){ // authors
conn.query("SELECT address, definition_chash FROM unit_authors WHERE unit=? ORDER BY address", [unit], function(rows){
objUnit.authors = [];
async.eachSeries(
rows,
function(row, cb){
var author = {address: row.address};
function onAuthorDone(){
objUnit.authors.push(author);
cb();
}
if (bVoided)
return onAuthorDone();
author.authentifiers = {};
conn.query(
"SELECT path, authentifier FROM authentifiers WHERE unit=? AND address=?",
[unit, author.address],
function(sig_rows){
for (var i=0; i<sig_rows.length; i++)
author.authentifiers[sig_rows[i].path] = sig_rows[i].authentifier;
// if definition_chash is defined:
if (row.definition_chash){
readDefinition(conn, row.definition_chash, {
ifFound: function(arrDefinition){
author.definition = arrDefinition;
onAuthorDone();
},
ifDefinitionNotFound: function(definition_chash){
throw Error("definition "+definition_chash+" not defined");
}
});
}
else
onAuthorDone();
}
);
},
function(){
callback();
}
);
});
},
function(callback){ // messages
if (bVoided)
return callback();
conn.query(
"SELECT app, payload_hash, payload_location, payload, payload_uri, payload_uri_hash, message_index \n\
FROM messages WHERE unit=? ORDER BY message_index", [unit],
function(rows){
if (rows.length === 0){
if (conf.bLight)
throw new Error("no messages in unit "+unit);
return callback(); // any errors will be caught by verifying unit hash
}
objUnit.messages = [];
async.eachSeries(
rows,
function(row, cb){
var objMessage = row;
var message_index = row.message_index;
delete objMessage.message_index;
objectHash.cleanNulls(objMessage);
objUnit.messages.push(objMessage);
function addSpendProofs(){
conn.query(
"SELECT spend_proof, address FROM spend_proofs WHERE unit=? AND message_index=? ORDER BY spend_proof_index",
[unit, message_index],
function(proof_rows){
if (proof_rows.length === 0)
return cb();
objMessage.spend_proofs = [];
for (var i=0; i<proof_rows.length; i++){
var objSpendProof = proof_rows[i];
if (objUnit.authors.length === 1) // single-authored
delete objSpendProof.address;
objMessage.spend_proofs.push(objSpendProof);
}
cb();
}
);
}
if (objMessage.payload_location !== "inline")
return addSpendProofs();
switch(objMessage.app){
case "address_definition_change":
conn.query(
"SELECT definition_chash, address FROM address_definition_changes WHERE unit=? AND message_index=?",
[unit, message_index],
function(dch_rows){
if (dch_rows.length === 0)
throw Error("no definition change?");
objMessage.payload = dch_rows[0];
if (objUnit.authors.length === 1) // single-authored
delete objMessage.payload.address;
addSpendProofs();
}
);
break;
case "poll":
conn.query(
"SELECT question FROM polls WHERE unit=? AND message_index=?", [unit, message_index],
function(poll_rows){
if (poll_rows.length !== 1)
throw Error("no poll question or too many?");
objMessage.payload = {question: poll_rows[0].question};
conn.query("SELECT choice FROM poll_choices WHERE unit=? ORDER BY choice_index", [unit], function(ch_rows){
if (ch_rows.length === 0)
throw Error("no choices?");
objMessage.payload.choices = ch_rows.map(function(choice_row){ return choice_row.choice; });
addSpendProofs();
});
}
);
break;
case "vote":
conn.query(
"SELECT poll_unit, choice FROM votes WHERE unit=? AND message_index=?", [unit, message_index],
function(vote_rows){
if (vote_rows.length !== 1)
throw Error("no vote choice or too many?");
objMessage.payload = {unit: vote_rows[0].poll_unit, choice: vote_rows[0].choice};
addSpendProofs();
}
);
break;
case "asset":
conn.query(
"SELECT cap, is_private, is_transferrable, auto_destroy, fixed_denominations, \n\
issued_by_definer_only, cosigned_by_definer, spender_attested, \n\
issue_condition, transfer_condition \n\
FROM assets WHERE unit=? AND message_index=?",
[unit, message_index],
function(asset_rows){
if (asset_rows.length !== 1)
throw Error("no asset or too many?");
objMessage.payload = asset_rows[0];
objectHash.cleanNulls(objMessage.payload);
objMessage.payload.is_private = !!objMessage.payload.is_private;
objMessage.payload.is_transferrable = !!objMessage.payload.is_transferrable;
objMessage.payload.auto_destroy = !!objMessage.payload.auto_destroy;
objMessage.payload.fixed_denominations = !!objMessage.payload.fixed_denominations;
objMessage.payload.issued_by_definer_only = !!objMessage.payload.issued_by_definer_only;
objMessage.payload.cosigned_by_definer = !!objMessage.payload.cosigned_by_definer;
objMessage.payload.spender_attested = !!objMessage.payload.spender_attested;
if (objMessage.payload.issue_condition)
objMessage.payload.issue_condition = JSON.parse(objMessage.payload.issue_condition);
if (objMessage.payload.transfer_condition)
objMessage.payload.transfer_condition = JSON.parse(objMessage.payload.transfer_condition);
var addAttestors = function(next){
if (!objMessage.payload.spender_attested)
return next();
conn.query(
"SELECT attestor_address FROM asset_attestors \n\
WHERE unit=? AND message_index=? ORDER BY attestor_address",
[unit, message_index],
function(att_rows){
if (att_rows.length === 0)
throw Error("no attestors?");
objMessage.payload.attestors = att_rows.map(function(att_row){
return att_row.attestor_address;
});
next();
}
);
};
var addDenominations = function(next){
if (!objMessage.payload.fixed_denominations)
return next();
conn.query(
"SELECT denomination, count_coins FROM asset_denominations \n\
WHERE asset=? ORDER BY denomination",
[unit],
function(denom_rows){
if (denom_rows.length === 0)
throw Error("no denominations?");
objMessage.payload.denominations = denom_rows.map(function(denom_row){
var denom = {denomination: denom_row.denomination};
if (denom_row.count_coins)
denom.count_coins = denom_row.count_coins;
return denom;
});
next();
}
);
};
async.series([addAttestors, addDenominations], addSpendProofs);
}
);
break;
case "asset_attestors":
conn.query(
"SELECT attestor_address, asset FROM asset_attestors \n\
WHERE unit=? AND message_index=? ORDER BY attestor_address",
[unit, message_index],
function(att_rows){
if (att_rows.length === 0)
throw Error("no attestors?");
objMessage.payload = {asset: att_rows[0].asset};
if (att_rows.length > 1
&& att_rows.some(function(att_row){ return (att_row.asset !== objMessage.payload.asset) }))
throw Error("different assets in attestor list");
objMessage.payload.attestors = att_rows.map(function(att_row){ return att_row.attestor_address;});
addSpendProofs();
}
);
break;
case "data_feed":
conn.query(
"SELECT feed_name, `value`, int_value FROM data_feeds WHERE unit=? AND message_index=?", [unit, message_index],
function(df_rows){
if (df_rows.length === 0)
throw Error("no data feed?");
objMessage.payload = {};
df_rows.forEach(function(df_row){
objMessage.payload[df_row.feed_name] =
(typeof df_row.value === 'string') ? df_row.value : Number(df_row.int_value);
});
addSpendProofs();
}
);
break;
case "profile":
case "attestation": // maybe later we'll store profiles and attestations in some structured form
case "data":
case "definition_template":
objMessage.payload = JSON.parse(objMessage.payload);
addSpendProofs();
break;
case "payment":
objMessage.payload = {};
var prev_asset;
var prev_denomination;
var readInputs = function(cb2){
conn.query(
"SELECT type, denomination, assets.fixed_denominations, \n\
src_unit AS unit, src_message_index AS message_index, src_output_index AS output_index, \n\
from_main_chain_index, to_main_chain_index, serial_number, amount, address, asset \n\
FROM inputs \n\
LEFT JOIN assets ON asset=assets.unit \n\
WHERE inputs.unit=? AND inputs.message_index=? \n\
ORDER BY input_index",
[unit, message_index],
function(input_rows){
objMessage.payload.inputs = [];
for (var i=0; i<input_rows.length; i++){
var input = input_rows[i];
if (!input.address && !conf.bLight) // may be NULL for light (light clients are reading units e.g. after receiving payment notification)
throw Error("readJoint: input address is NULL");
var asset = input.asset;
var denomination = input.denomination;
if (i>0){
if (asset !== prev_asset)
throw Error("different assets in inputs?");
if (denomination !== prev_denomination)
throw Error("different denominations in inputs?");
}
if (i === 0 && asset){
objMessage.payload.asset = asset;
if (input.fixed_denominations)
objMessage.payload.denomination = denomination;
}
delete input.asset;
delete input.denomination;
delete input.fixed_denominations;
objectHash.cleanNulls(input);
if (input.type === "transfer" || objUnit.authors.length === 1)
delete input.address;
if (input.type === "transfer")
delete input.type;
objMessage.payload.inputs.push(input);
if (i === 0){
prev_asset = asset;
prev_denomination = denomination;
}
}
cb2();
}
);
};
var readOutputs = function(cb2){
objMessage.payload.outputs = [];
conn.query( // we don't select blinding because it's absent on public payments
"SELECT address, amount, asset, denomination \n\
FROM outputs WHERE unit=? AND message_index=? ORDER BY output_index",
[unit, message_index],
function(output_rows){
for (var i=0; i<output_rows.length; i++){
var output = output_rows[i];
if (output.asset !== prev_asset)
throw Error("different assets in outputs?");
if (output.denomination !== prev_denomination)
throw Error("different denominations in outputs?");
delete output.asset;
delete output.denomination;
objMessage.payload.outputs.push(output);
}
cb2();
}
);
};
async.series(
[readInputs, readOutputs],
addSpendProofs
);
break;
default:
addSpendProofs();
}
},
callback
);
} // message rows
);
}
], function(){
//profiler.stop('read');
// verify unit hash. Might fail if the unit was archived while reading, in this case retry
// light wallets don't have last_ball, don't verify their hashes
if (!conf.bLight && !isCorrectHash(objUnit, unit)){
if (bRetrying)
throw Error("unit hash verification failed, unit: "+unit+", objUnit: "+JSON.stringify(objUnit));
console.log("unit hash verification failed, will retry");
return setTimeout(function(){
readJointDirectly(conn, unit, callbacks, true);
}, 60*1000);
}
if (!conf.bSaveJointJson || !bStable || (bFinalBad && bRetrievable) || bRetrievable)
return callbacks.ifFound(objJoint);
conn.query("INSERT "+db.getIgnore()+" INTO joints (unit, json) VALUES (?,?)", [unit, JSON.stringify(objJoint)], function(){
callbacks.ifFound(objJoint);
});
});
}
);
}
function isCorrectHash(objUnit, unit){
try{
return (objectHash.getUnitHash(objUnit) === unit);
}
catch(e){
return false;
}
}
// add .ball even if it is not retrievable
function readJointWithBall(conn, unit, handleJoint) {
readJoint(conn, unit, {
ifNotFound: function(){
throw Error("joint not found, unit "+unit);
},
ifFound: function(objJoint){
if (objJoint.ball)
return handleJoint(objJoint);
conn.query("SELECT ball FROM balls WHERE unit=?", [unit], function(rows){
if (rows.length === 1)
objJoint.ball = rows[0].ball;
handleJoint(objJoint);
});
}
});
}
function readWitnessList(conn, unit, handleWitnessList, bAllowEmptyList){
var arrWitnesses = assocCachedUnitWitnesses[unit];
if (arrWitnesses)
return handleWitnessList(arrWitnesses);
conn.query("SELECT address FROM unit_witnesses WHERE unit=? ORDER BY address", [unit], function(rows){
if (!bAllowEmptyList && rows.length === 0)
throw Error("witness list of unit "+unit+" not found");
if (rows.length > 0 && rows.length !== constants.COUNT_WITNESSES)
throw Error("wrong number of witnesses in unit "+unit);
arrWitnesses = rows.map(function(row){ return row.address; });
if (rows.length > 0)
assocCachedUnitWitnesses[unit] = arrWitnesses;
handleWitnessList(arrWitnesses);
});
}
function readWitnesses(conn, unit, handleWitnessList){
var arrWitnesses = assocCachedUnitWitnesses[unit];
if (arrWitnesses)
return handleWitnessList(arrWitnesses);
conn.query("SELECT witness_list_unit FROM units WHERE unit=?", [unit], function(rows){
if (rows.length === 0)
throw Error("unit "+unit+" not found");
var witness_list_unit = rows[0].witness_list_unit;
readWitnessList(conn, witness_list_unit ? witness_list_unit : unit, function(arrWitnesses){
assocCachedUnitWitnesses[unit] = arrWitnesses;
handleWitnessList(arrWitnesses);
});
});
}
function determineIfWitnessAddressDefinitionsHaveReferences(conn, arrWitnesses, handleResult){
conn.query(
"SELECT 1 FROM address_definition_changes JOIN definitions USING(definition_chash) \n\
WHERE address IN(?) AND has_references=1 \n\
UNION \n\
SELECT 1 FROM definitions WHERE definition_chash IN(?) AND has_references=1 \n\
LIMIT 1",
[arrWitnesses, arrWitnesses],
function(rows){
handleResult(rows.length > 0);
}
);
}
function determineWitnessedLevelAndBestParent(conn, arrParentUnits, arrWitnesses, handleWitnessedLevelAndBestParent){
var arrCollectedWitnesses = [];
var my_best_parent_unit;
function addWitnessesAndGoUp(start_unit){
readStaticUnitProps(conn, start_unit, function(props){
var best_parent_unit = props.best_parent_unit;
var level = props.level;
if (level === null)
throw Error("null level in updateWitnessedLevel");
if (level === 0) // genesis
return handleWitnessedLevelAndBestParent(0, my_best_parent_unit);
readUnitAuthors(conn, start_unit, function(arrAuthors){
for (var i=0; i<arrAuthors.length; i++){
var address = arrAuthors[i];
if (arrWitnesses.indexOf(address) !== -1 && arrCollectedWitnesses.indexOf(address) === -1)
arrCollectedWitnesses.push(address);
}
(arrCollectedWitnesses.length < constants.MAJORITY_OF_WITNESSES)
? addWitnessesAndGoUp(best_parent_unit) : handleWitnessedLevelAndBestParent(level, my_best_parent_unit);
});
});
}
determineBestParent(conn, {parent_units: arrParentUnits, witness_list_unit: 'none'}, arrWitnesses, function(best_parent_unit){
if (!best_parent_unit)
throw Error("no best parent of "+arrParentUnits.join(', ')+", witnesses "+arrWitnesses.join(', '));
my_best_parent_unit = best_parent_unit;
addWitnessesAndGoUp(best_parent_unit);
});
}
// max_mci must be stable
function readDefinitionByAddress(conn, address, max_mci, callbacks){
if (max_mci === null)
max_mci = MAX_INT32;
// try to find last definition change, otherwise definition_chash=address
conn.query(
"SELECT definition_chash FROM address_definition_changes CROSS JOIN units USING(unit) \n\
WHERE address=? AND is_stable=1 AND sequence='good' AND main_chain_index<=? ORDER BY level DESC LIMIT 1",
[address, max_mci],
function(rows){
var definition_chash = (rows.length > 0) ? rows[0].definition_chash : address;
readDefinitionAtMci(conn, definition_chash, max_mci, callbacks);
}
);
}
// max_mci must be stable
function readDefinitionAtMci(conn, definition_chash, max_mci, callbacks){
var sql = "SELECT definition FROM definitions CROSS JOIN unit_authors USING(definition_chash) CROSS JOIN units USING(unit) \n\
WHERE definition_chash=? AND is_stable=1 AND sequence='good' AND main_chain_index<=?";
var params = [definition_chash, max_mci];
conn.query(sql, params, function(rows){
if (rows.length === 0)
return callbacks.ifDefinitionNotFound(definition_chash);
callbacks.ifFound(JSON.parse(rows[0].definition));
});
}
function readDefinition(conn, definition_chash, callbacks){
conn.query("SELECT definition FROM definitions WHERE definition_chash=?", [definition_chash], function(rows){
if (rows.length === 0)
return callbacks.ifDefinitionNotFound(definition_chash);
callbacks.ifFound(JSON.parse(rows[0].definition));
});
}
function isGenesisUnit(unit){
return (unit === constants.GENESIS_UNIT);
}
function readUnitProps(conn, unit, handleProps){
if (assocStableUnits[unit])
return handleProps(assocStableUnits[unit]);
var stack = new Error().stack;
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free, is_stable, witnessed_level, headers_commission, payload_commission, sequence, GROUP_CONCAT(address) AS author_addresses, COALESCE(witness_list_unit, unit) AS witness_list_unit\n\
FROM units \n\
JOIN unit_authors USING(unit) \n\
WHERE unit=? \n\
GROUP BY +unit",
[unit],
function(rows){
if (rows.length !== 1)
throw Error("not 1 row");
var props = rows[0];
props.author_addresses = props.author_addresses.split(',');
if (props.is_stable) {
if (props.sequence === 'good') // we don't cache final-bads as they can be voided later
assocStableUnits[unit] = props;
// we don't add it to assocStableUnitsByMci as all we need there is already there
}
else{
if (!assocUnstableUnits[unit])
throw Error("no unstable props of "+unit);
var props2 = _.cloneDeep(assocUnstableUnits[unit]);
delete props2.parent_units;
delete props2.earned_headers_commission_recipients;
if (!_.isEqual(props, props2)) {
debugger;
throw Error("different props of "+unit+", mem: "+JSON.stringify(props2)+", db: "+JSON.stringify(props)+", stack "+stack);
}
}
handleProps(props);
}
);
}
function readPropsOfUnits(conn, earlier_unit, arrLaterUnits, handleProps){
var objEarlierUnitProps2 = assocUnstableUnits[earlier_unit] || assocStableUnits[earlier_unit];
var arrLaterUnitProps2 = arrLaterUnits.map(function(later_unit){ return assocUnstableUnits[later_unit] || assocStableUnits[later_unit]; });
// if (objEarlierUnitProps2 && arrLaterUnitProps2.every(function(p){ return !!p; }))
// return handleProps(objEarlierUnitProps2, arrLaterUnitProps2);
var bEarlierInLaterUnits = (arrLaterUnits.indexOf(earlier_unit) !== -1);
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free FROM units WHERE unit IN(?, ?)",
[earlier_unit, arrLaterUnits],
function(rows){
if (rows.length !== arrLaterUnits.length + (bEarlierInLaterUnits ? 0 : 1))
throw Error("wrong number of rows for earlier "+earlier_unit+", later "+arrLaterUnits);
var objEarlierUnitProps, arrLaterUnitProps = [];
for (var i=0; i<rows.length; i++){
if (rows[i].unit === earlier_unit)
objEarlierUnitProps = rows[i];
else
arrLaterUnitProps.push(rows[i]);
}
if (bEarlierInLaterUnits)
arrLaterUnitProps.push(objEarlierUnitProps);
if (objEarlierUnitProps2 && arrLaterUnitProps2.every(function(p){ return !!p; })){
console.log('have earlier and later in cache, earlier '+earlier_unit);
var objEarlierUnitProps2cmp = _.cloneDeep(objEarlierUnitProps2);
var arrLaterUnitProps2cmp = _.cloneDeep(arrLaterUnitProps2);
var arrAllProps2cmp = arrLaterUnitProps2cmp.concat([objEarlierUnitProps2cmp]);
arrAllProps2cmp.forEach(function(props){
delete props.parent_units;
delete props.earned_headers_commission_recipients;
delete props.author_addresses;
delete props.is_stable;
delete props.witnessed_level;
delete props.headers_commission;
delete props.payload_commission;
delete props.sequence;
delete props.witness_list_unit;
});
if (!_.isEqual(objEarlierUnitProps, objEarlierUnitProps2cmp))
throwError("different earlier, db "+JSON.stringify(objEarlierUnitProps)+", mem "+JSON.stringify(objEarlierUnitProps2cmp));
if (!_.isEqual(_.sortBy(arrLaterUnitProps, 'unit'), _.sortBy(arrLaterUnitProps2cmp, 'unit')))
throwError("different later, db "+JSON.stringify(arrLaterUnitProps)+", mem "+JSON.stringify(arrLaterUnitProps2cmp));
}
else
console.log('have earlier or later not in cache');
handleProps(objEarlierUnitProps, arrLaterUnitProps);
}
);
}
function throwError(msg){
var eventBus = require('./event_bus.js');
debugger;
if (typeof window === 'undefined')
throw Error(msg);
else
eventBus.emit('nonfatal_error', msg, new Error());
}
function readLastStableMcUnitProps(conn, handleLastStableMcUnitProps){
conn.query(
"SELECT units.*, ball FROM units LEFT JOIN balls USING(unit) WHERE is_on_main_chain=1 AND is_stable=1 ORDER BY main_chain_index DESC LIMIT 1",
function(rows){
if (rows.length === 0)
return handleLastStableMcUnitProps(null); // empty database
//throw "readLastStableMcUnitProps: no units on stable MC?";
if (!rows[0].ball)
throw Error("no ball for last stable unit "+rows[0].unit);
handleLastStableMcUnitProps(rows[0]);
}
);
}
function readLastStableMcIndex(conn, handleLastStableMcIndex){
readLastStableMcUnitProps(conn, function(objLastStableMcUnitProps){
handleLastStableMcIndex(objLastStableMcUnitProps ? objLastStableMcUnitProps.main_chain_index : 0);
});
}
function findLastBallMciOfMci(conn, mci, handleLastBallMci){
if (mci === 0)
throw Error("findLastBallMciOfMci called with mci=0");
conn.query(
"SELECT lb_units.main_chain_index, lb_units.is_on_main_chain \n\
FROM units JOIN units AS lb_units ON units.last_ball_unit=lb_units.unit \n\
WHERE units.is_on_main_chain=1 AND units.main_chain_index=?",
[mci],
function(rows){
if (rows.length !== 1)
throw Error("last ball's mci count "+rows.length+" !== 1, mci = "+mci);
if (rows[0].is_on_main_chain !== 1)
throw Error("lb is not on mc?");
handleLastBallMci(rows[0].main_chain_index);
}
);
}
function updateMinRetrievableMciAfterStabilizingMci(conn, last_stable_mci, handleMinRetrievableMci){
console.log("updateMinRetrievableMciAfterStabilizingMci "+last_stable_mci);
findLastBallMciOfMci(conn, last_stable_mci, function(last_ball_mci){
if (last_ball_mci <= min_retrievable_mci) // nothing new
return handleMinRetrievableMci(min_retrievable_mci);
var prev_min_retrievable_mci = min_retrievable_mci;
min_retrievable_mci = last_ball_mci;
// strip content off units older than min_retrievable_mci
conn.query(
// 'JOIN messages' filters units that are not stripped yet
"SELECT DISTINCT unit, content_hash FROM units "+db.forceIndex('byMcIndex')+" CROSS JOIN messages USING(unit) \n\
WHERE main_chain_index<=? AND main_chain_index>=? AND sequence='final-bad'",
[min_retrievable_mci, prev_min_retrievable_mci],
function(unit_rows){
var arrQueries = [];
async.eachSeries(
unit_rows,
function(unit_row, cb){
var unit = unit_row.unit;
console.log('voiding unit '+unit);
if (!unit_row.content_hash)
throw Error("no content hash in bad unit "+unit);
readJoint(conn, unit, {
ifNotFound: function(){
throw Error("bad unit not found: "+unit);
},
ifFound: function(objJoint){
archiving.generateQueriesToArchiveJoint(conn, objJoint, 'voided', arrQueries, cb);
}
});
},
function(){
if (arrQueries.length === 0)
return handleMinRetrievableMci(min_retrievable_mci);
async.series(arrQueries, function(){
unit_rows.forEach(function(unit_row){
// don't forget, can be still used when calculating witnessing commissions
// forgetUnit(unit_row.unit);
});
handleMinRetrievableMci(min_retrievable_mci);
});
}
);
}
);
});
}
// function initializeMinRetrievableMci(conn, onDone){
// var conn = conn || db;
// conn.query(
// "SELECT MAX(lb_units.main_chain_index) AS min_retrievable_mci \n\
// FROM units JOIN units AS lb_units ON units.last_ball_unit=lb_units.unit \n\
// WHERE units.is_on_main_chain=1 AND units.is_stable=1",
// function(rows){
// if (rows.length !== 1)
// throw Error("MAX() no rows?");
// min_retrievable_mci = rows[0].min_retrievable_mci;
// if (min_retrievable_mci === null)
// min_retrievable_mci = 0;
// if (onDone)
// onDone();
// }
// );
// }
function archiveJointAndDescendantsIfExists(from_unit){
console.log('will archive if exists from unit '+from_unit);
db.query("SELECT 1 FROM units WHERE unit=?", [from_unit], function(rows){
if (rows.length > 0)
archiveJointAndDescendants(from_unit);
});
}
function archiveJointAndDescendants(from_unit){
db.executeInTransaction(function doWork(conn, cb){
function addChildren(arrParentUnits){
conn.query("SELECT DISTINCT child_unit FROM parenthoods WHERE parent_unit IN(?)", [arrParentUnits], function(rows){
if (rows.length === 0)
return archive();
var arrChildUnits = rows.map(function(row){ return row.child_unit; });
arrUnits = arrUnits.concat(arrChildUnits);
addChildren(arrChildUnits);
});
}
function archive(){
arrUnits = _.uniq(arrUnits); // does not affect the order
arrUnits.reverse();
console.log('will archive', arrUnits);
var arrQueries = [];
async.eachSeries(
arrUnits,
function(unit, cb2){
readJoint(conn, unit, {
ifNotFound: function(){
throw Error("unit to be archived not found: "+unit);
},
ifFound: function(objJoint){
archiving.generateQueriesToArchiveJoint(conn, objJoint, 'uncovered', arrQueries, cb2);
}
});
},
function(){
conn.addQuery(arrQueries, "DELETE FROM known_bad_joints");
console.log('will execute '+arrQueries.length+' queries to archive');
async.series(arrQueries, function(){
arrUnits.forEach(forgetUnit);
cb();
});
}
);
}
console.log('will archive from unit '+from_unit);
var arrUnits = [from_unit];
addChildren([from_unit]);
},
function onDone(){
console.log('done archiving from unit '+from_unit);
});
}
function readAssetInfo(conn, asset, handleAssetInfo){
var objAsset = assocCachedAssetInfos[asset];
if (objAsset)
return handleAssetInfo(objAsset);
conn.query(
"SELECT assets.*, main_chain_index, sequence, is_stable, address AS definer_address, unit AS asset \n\
FROM assets JOIN units USING(unit) JOIN unit_authors USING(unit) WHERE unit=?",
[asset],
function(rows){
if (rows.length > 1)
throw Error("more than one asset?");
if (rows.length === 0)
return handleAssetInfo(null);
var objAsset = rows[0];
if (objAsset.issue_condition)
objAsset.issue_condition = JSON.parse(objAsset.issue_condition);
if (objAsset.transfer_condition)
objAsset.transfer_condition = JSON.parse(objAsset.transfer_condition);
if (objAsset.is_stable) // cache only if stable
assocCachedAssetInfos[asset] = objAsset;
handleAssetInfo(objAsset);
}
);
}
function readAsset(conn, asset, last_ball_mci, handleAsset){
if (last_ball_mci === null){
if (conf.bLight)
last_ball_mci = MAX_INT32;
else
return readLastStableMcIndex(conn, function(last_stable_mci){
readAsset(conn, asset, last_stable_mci, handleAsset);
});
}
readAssetInfo(conn, asset, function(objAsset){
if (!objAsset)
return handleAsset("asset "+asset+" not found");
if (objAsset.main_chain_index > last_ball_mci)
return handleAsset("asset definition must be before last ball");
if (objAsset.sequence !== "good")
return handleAsset("asset definition is not serial");
if (!objAsset.spender_attested)
return handleAsset(null, objAsset);
// find latest list of attestors
conn.query(
"SELECT MAX(level) AS max_level FROM asset_attestors CROSS JOIN units USING(unit) \n\
WHERE asset=? AND main_chain_index<=? AND is_stable=1 AND sequence='good'",
[asset, last_ball_mci],
function(latest_rows){
var max_level = latest_rows[0].max_level;
if (!max_level)
throw Error("no max level of asset attestors");
// read the list
conn.query(
"SELECT attestor_address FROM asset_attestors CROSS JOIN units USING(unit) \n\
WHERE asset=? AND level=? AND main_chain_index<=? AND is_stable=1 AND sequence='good'",
[asset, max_level, last_ball_mci],
function(att_rows){
if (att_rows.length === 0)
throw Error("no attestors?");
objAsset.arrAttestorAddresses = att_rows.map(function(att_row){ return att_row.attestor_address; });
handleAsset(null, objAsset);
}
);
}
);
});
}
// filter only those addresses that are attested (doesn't work for light clients)
function filterAttestedAddresses(conn, objAsset, last_ball_mci, arrAddresses, handleAttestedAddresses){
conn.query(
"SELECT DISTINCT address FROM attestations CROSS JOIN units USING(unit) \n\
WHERE attestor_address IN(?) AND address IN(?) AND main_chain_index<=? AND is_stable=1 AND sequence='good' \n\
AND main_chain_index>IFNULL( \n\
(SELECT main_chain_index FROM address_definition_changes JOIN units USING(unit) \n\
WHERE address_definition_changes.address=attestations.address ORDER BY main_chain_index DESC LIMIT 1), \n\
0)",
[objAsset.arrAttestorAddresses, arrAddresses, last_ball_mci],
function(addr_rows){
var arrAttestedAddresses = addr_rows.map(function(addr_row){ return addr_row.address; });
handleAttestedAddresses(arrAttestedAddresses);