Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintervalue-sqlite.sql
More file actions
Latest commit
779 lines (666 loc) · 27.5 KB
/
Copy pathintervalue-sqlite.sql
File metadata and controls
779 lines (666 loc) · 27.5 KB
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
CREATETABLEunits (
unit CHAR(44) NOT NULLPRIMARY KEY, -- sha256 in base64
creation_date timestampNOT NULL DEFAULT CURRENT_TIMESTAMP,
version VARCHAR(3) NOT NULL DEFAULT '1.0',
alt VARCHAR(3) NOT NULL DEFAULT '1',
witness_list_unit CHAR(44) NULL,
last_ball_unit CHAR(44) NULL,
content_hash CHAR(44) NULL,
headers_commission INTNOT NULL,
payload_commission INTNOT NULL,
is_free TINYINT NOT NULL DEFAULT 1,
is_on_main_chain TINYINT NOT NULL DEFAULT 0,
main_chain_index INTNULL, -- when it first appears
latest_included_mc_index INTNULL, -- latest MC ball that is included in this ball (excluding itself)
level INTNULL,
witnessed_level INTNULL,
is_stable TINYINT NOT NULL DEFAULT 0,
sequence TEXTCHECK (sequence IN('good','temp-bad','final-bad')) NOT NULL DEFAULT 'good',
best_parent_unit CHAR(44) NULL,
CONSTRAINT unitsByLastBallUnit FOREIGN KEY (last_ball_unit) REFERENCES units(unit),
FOREIGN KEY (best_parent_unit) REFERENCES units(unit),
CONSTRAINT unitsByWitnessListUnit FOREIGN KEY (witness_list_unit) REFERENCES units(unit)
);
CREATEINDEXbyLBON units(last_ball_unit);
CREATEINDEXbyBestParentON units(best_parent_unit);
CREATEINDEXbyWLON units(witness_list_unit);
CREATEINDEXbyMainChainON units(is_on_main_chain);
CREATEINDEXbyMcIndexON units(main_chain_index);
CREATEINDEXbyLimciON units(latest_included_mc_index);
CREATEINDEXbyLevelON units(level);
CREATEINDEXbyFreeON units(is_free);
CREATEINDEXbyStableMciON units(is_stable, main_chain_index);
CREATETABLEballs (
ball CHAR(44) NOT NULLPRIMARY KEY, -- sha256 in base64
creation_date timestampNOT NULL DEFAULT CURRENT_TIMESTAMP,
unit CHAR(44) NOT NULL UNIQUE, -- sha256 in base64
-- count_witnesses TINYINT NOT NULL DEFAULT 0,
count_paid_witnesses TINYINT NULL,
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXbyCountPaidWitnessesON balls(count_paid_witnesses);
CREATETABLEskiplist_units (
unit CHAR(44) NOT NULL,
skiplist_unit CHAR(44) NOT NULL, -- only for MC units with MCI divisible by 10: previous MC units divisible by 10
PRIMARY KEY (unit, skiplist_unit),
FOREIGN KEY (unit) REFERENCES units(unit),
FOREIGN KEY (skiplist_unit) REFERENCES units(unit)
);
CREATEINDEXbySkiplistUnitON skiplist_units(skiplist_unit);
-- must be sorted by parent_unit
CREATETABLEparenthoods (
child_unit CHAR(44) NOT NULL,
parent_unit CHAR(44) NOT NULL,
PRIMARY KEY (parent_unit, child_unit),
CONSTRAINT parenthoodsByChild FOREIGN KEY (child_unit) REFERENCES units(unit),
CONSTRAINT parenthoodsByParent FOREIGN KEY (parent_unit) REFERENCES units(unit)
);
CREATEINDEXbyChildUnitON parenthoods(child_unit);
CREATETABLEdefinitions (
definition_chash CHAR(32) NOT NULLPRIMARY KEY,
definition TEXTNOT NULL,
has_references TINYINT NOT NULL
);
-- current list of all known from-addresses
CREATETABLEaddresses (
address CHAR(32) NOT NULLPRIMARY KEY,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- must be sorted by address
CREATETABLEunit_authors (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
definition_chash CHAR(32) NULL, -- only with 1st ball from this address, and with next ball after definition change
_mci INTNULL,
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT unitAuthorsByAddress FOREIGN KEY (address) REFERENCES addresses(address),
FOREIGN KEY (definition_chash) REFERENCES definitions(definition_chash)
);
CREATEINDEXbyDefinitionChashON unit_authors(definition_chash);
CREATEINDEXunitAuthorsIndexByAddressON unit_authors(address);
CREATEINDEXunitAuthorsIndexByAddressDefinitionChashON unit_authors(address, definition_chash);
CREATEINDEXunitAuthorsIndexByAddressMciON unit_authors(address, _mci);
CREATETABLEauthentifiers (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
pathVARCHAR(40) NOT NULL,
authentifier VARCHAR(4096) NOT NULL,
PRIMARY KEY (unit, address, path),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT authentifiersByAddress FOREIGN KEY (address) REFERENCES addresses(address)
);
CREATEINDEXauthentifiersIndexByAddressON authentifiers(address);
-- must be sorted by address
CREATETABLEunit_witnesses (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXbyAddressON unit_witnesses(address);
CREATETABLEwitness_list_hashes (
witness_list_unit CHAR(44) NOT NULLPRIMARY KEY,
witness_list_hash CHAR(44) NOT NULL UNIQUE,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (witness_list_unit) REFERENCES units(unit)
);
-- if this ball wins headers commission from at least one of the included balls, how it is distributed
-- required if more than one author
-- if one author, all commission goes to the author by default
CREATETABLEearned_headers_commission_recipients (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
earned_headers_commission_share INTNOT NULL, -- percentage
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXearnedbyAddressON earned_headers_commission_recipients(address);
CREATETABLEmessages (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
app VARCHAR(30) NOT NULL,
payload_location TEXTCHECK (payload_location IN ('inline','uri','none')) NOT NULL,
payload_hash CHAR(44) NOT NULL,
payload TEXTNULL,
payload_uri_hash CHAR(44) NULL,
payload_uri VARCHAR(500) NULL,
PRIMARY KEY (unit, message_index),
FOREIGN KEY (unit) REFERENCES units(unit)
);
-- must be sorted by spend_proof
CREATETABLEspend_proofs (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
spend_proof_index TINYINT NOT NULL,
spend_proof CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
PRIMARY KEY (unit, message_index, spend_proof_index),
UNIQUE (spend_proof, unit),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT spendProofsByAddress FOREIGN KEY (address) REFERENCES addresses(address)
);
CREATEINDEXspendProofsIndexByAddressON spend_proofs(address);
-- -------------------------
-- Specific message types
CREATETABLEaddress_definition_changes (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
address CHAR(32) NOT NULL,
definition_chash CHAR(32) NOT NULL, -- might not be defined in definitions yet (almost always, it is not defined)
PRIMARY KEY (unit, message_index),
UNIQUE (address, unit),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT addressDefinitionChangesByAddress FOREIGN KEY (address) REFERENCES addresses(address)
);
CREATETABLEdata_feeds (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
feed_name VARCHAR(64) NOT NULL,
-- type ENUM('string', 'number') NOT NULL,
`value`VARCHAR(64) NULL,
`int_value`BIGINTNULL,
PRIMARY KEY (unit, feed_name),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXbyNameStringValueON data_feeds(feed_name, `value`);
CREATEINDEXbyNameIntValueON data_feeds(feed_name, `int_value`);
CREATETABLEpolls (
unit CHAR(44) NOT NULLPRIMARY KEY,
message_index TINYINT NOT NULL,
question VARCHAR(4096) NOT NULL,
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATETABLEpoll_choices (
unit CHAR(44) NOT NULL,
choice_index TINYINT NOT NULL,
choice VARCHAR(32) NOT NULL,
PRIMARY KEY (unit, choice_index),
UNIQUE (unit, choice),
FOREIGN KEY (unit) REFERENCES polls(unit)
);
CREATETABLEvotes (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
poll_unit CHAR(44) NOT NULL,
choice VARCHAR(32) NOT NULL,
PRIMARY KEY (unit, message_index),
UNIQUE (unit, choice),
CONSTRAINT votesByChoice FOREIGN KEY (poll_unit, choice) REFERENCES poll_choices(unit, choice),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXvotesIndexByPollUnitChoiceON votes(poll_unit, choice);
CREATETABLEattestations (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
attestor_address CHAR(32) NOT NULL,
address CHAR(32) NOT NULL,
-- name VARCHAR(44) NOT NULL,
PRIMARY KEY (unit, message_index),
CONSTRAINT attestationsByAttestorAddress FOREIGN KEY (attestor_address) REFERENCES addresses(address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXattestationsByAddressON attestations(address);
CREATEINDEXattestationsIndexByAttestorAddressON attestations(attestor_address);
CREATETABLEassets (
unit CHAR(44) NOT NULLPRIMARY KEY,
message_index TINYINT NOT NULL,
cap BIGINTNULL,
is_private TINYINT NOT NULL,
is_transferrable TINYINT NOT NULL,
auto_destroy TINYINT NOT NULL,
fixed_denominations TINYINT NOT NULL,
issued_by_definer_only TINYINT NOT NULL,
cosigned_by_definer TINYINT NOT NULL,
spender_attested TINYINT NOT NULL, -- must subsequently publish and update the list of trusted attestors
issue_condition TEXTNULL,
transfer_condition TEXTNULL,
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATETABLEasset_denominations (
asset CHAR(44) NOT NULL,
denomination INTNOT NULL,
count_coins BIGINTNULL,
max_issued_serial_number BIGINTNOT NULL DEFAULT 0,
PRIMARY KEY (asset, denomination),
FOREIGN KEY (asset) REFERENCES assets(unit)
);
CREATETABLEasset_attestors (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
asset CHAR(44) NOT NULL, -- in the initial attestor list: same as unit
attestor_address CHAR(32) NOT NULL,
PRIMARY KEY (unit, message_index),
UNIQUE (asset, attestor_address, unit),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT assetAttestorsByAsset FOREIGN KEY (asset) REFERENCES assets(unit)
);
-- -------------------------
-- Payments
CREATETABLEinputs (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
input_index TINYINT NOT NULL,
asset CHAR(44) NULL,
denomination INTNOT NULL DEFAULT 1,
is_unique TINYINT NULL DEFAULT 1,
type TEXTCHECK (type IN('transfer','headers_commission','witnessing','issue')) NOT NULL,
src_unit CHAR(44) NULL, -- transfer
src_message_index TINYINT NULL, -- transfer
src_output_index TINYINT NULL, -- transfer
from_main_chain_index INTNULL, -- witnessing/hc
to_main_chain_index INTNULL, -- witnessing/hc
serial_number BIGINTNULL, -- issue
amount BIGINTNULL, -- issue
address CHAR(32) NOT NULL,
PRIMARY KEY (unit, message_index, input_index),
UNIQUE (src_unit, src_message_index, src_output_index, is_unique), -- UNIQUE guarantees there'll be no double spend for type=transfer
UNIQUE (type, from_main_chain_index, address, is_unique), -- UNIQUE guarantees there'll be no double spend for type=hc/witnessing
UNIQUE (asset, denomination, serial_number, address, is_unique), -- UNIQUE guarantees there'll be no double issue
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT inputsBySrcUnit FOREIGN KEY (src_unit) REFERENCES units(unit),
CONSTRAINT inputsByAddress FOREIGN KEY (address) REFERENCES addresses(address),
CONSTRAINT inputsByAsset FOREIGN KEY (asset) REFERENCES assets(unit)
);
CREATEINDEXinputsIndexByAddressON inputs(address);
CREATEINDEXinputsIndexByAddressTypeToMciON inputs(address, type, to_main_chain_index);
CREATEINDEXinputsIndexByAssetTypeON inputs(asset, type);
CREATETABLEoutputs (
output_id INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT,
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
output_index TINYINT NOT NULL,
asset CHAR(44) NULL,
denomination INTNOT NULL DEFAULT 1,
address CHAR(32) NULL, -- NULL if hidden by output_hash
amount BIGINTNOT NULL,
blinding CHAR(16) NULL,
output_hash CHAR(44) NULL,
is_serial TINYINT NULL, -- NULL if not stable yet
is_spent TINYINT NOT NULL DEFAULT 0,
UNIQUE (unit, message_index, output_index),
FOREIGN KEY (unit) REFERENCES units(unit),
CONSTRAINT outputsByAsset FOREIGN KEY (asset) REFERENCES assets(unit)
);
CREATEINDEXoutputsByAddressSpentON outputs(address, is_spent);
CREATEINDEXoutputsIndexByAssetON outputs(asset);
CREATEINDEXoutputsIsSerialON outputs(is_serial);
-- ------------
-- Commissions
-- updated immediately after main chain is updated
CREATETABLEheaders_commission_contributions (
unit CHAR(44) NOT NULL, -- parent unit that pays commission
address CHAR(32) NOT NULL, -- address of the commission receiver: author of child unit or address named in earned_headers_commission_recipients
amount BIGINTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXhccbyAddressON headers_commission_contributions(address);
CREATETABLEheaders_commission_outputs (
main_chain_index INTNOT NULL, -- mci of the sponsoring (paying) unit
address CHAR(32) NOT NULL, -- address of the commission receiver
amount BIGINTNOT NULL,
is_spent TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (main_chain_index, address)
);
-- CREATE INDEX hcobyAddressSpent ON headers_commission_outputs(address, is_spent);
CREATEUNIQUE INDEXhcobyAddressMciON headers_commission_outputs(address, main_chain_index);
CREATEUNIQUE INDEXhcobyAddressSpentMciON headers_commission_outputs(address, is_spent, main_chain_index);
CREATETABLEpaid_witness_events (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL, -- witness address
-- witnessed_in_ball CHAR(44) NOT NULL, -- if expired, MC ball next after expiry. Or NULL?
delay TINYINT NULL, -- NULL if expired
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit),
FOREIGN KEY (address) REFERENCES addresses(address)
);
CREATEINDEXpweIndexByAddressON paid_witness_events(address);
CREATETABLEwitnessing_outputs (
main_chain_index INTNOT NULL,
address CHAR(32) NOT NULL,
amount BIGINTNOT NULL,
is_spent TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (main_chain_index, address),
FOREIGN KEY (address) REFERENCES addresses(address)
);
-- CREATE INDEX byWitnessAddressSpent ON witnessing_outputs(address, is_spent);
CREATEUNIQUE INDEXbyWitnessAddressMciON witnessing_outputs(address, main_chain_index);
CREATEUNIQUE INDEXbyWitnessAddressSpentMciON witnessing_outputs(address, is_spent, main_chain_index);
-- ---------------------------------------
-- Networking
CREATETABLEdependencies (
unit CHAR(44) NOT NULL,
depends_on_unit CHAR(44) NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (depends_on_unit, unit)
);
CREATEINDEXdepbyUnitON dependencies(unit);
CREATETABLEunhandled_joints (
unit CHAR(44) NOT NULLPRIMARY KEY,
peer VARCHAR(100) NOT NULL,
json TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEarchived_joints (
unit CHAR(44) NOT NULLPRIMARY KEY,
reason TEXTCHECK (reason IN('uncovered', 'voided')) NOT NULL,
-- is_retrievable TINYINT NOT NULL,
json TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEknown_bad_joints (
joint CHAR(44) NULL UNIQUE,
unit CHAR(44) NULL UNIQUE,
json TEXTNOT NULL,
error TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEjoints (
unit CHAR(44) NOT NULLPRIMARY KEY,
json TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEunhandled_private_payments (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
output_index TINYINT NOT NULL,
json TEXTNOT NULL,
peer VARCHAR(100) NOT NULL,
linked TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (unit, message_index, output_index)
);
-- ------------------
-- Catching up
CREATETABLEhash_tree_balls (
ball_index INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT, -- in increasing level order
ball CHAR(44) NOT NULL UNIQUE,
unit CHAR(44) NOT NULL UNIQUE
);
CREATETABLEcatchup_chain_balls (
member_index INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT, -- in increasing level order
ball CHAR(44) NOT NULL UNIQUE
);
-- ------------------------
-- Peers
CREATETABLEpeer_hosts (
peer_host VARCHAR(100) NOT NULLPRIMARY KEY, -- domain or IP
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
count_new_good_joints INTNOT NULL DEFAULT 0,
count_invalid_joints INTNOT NULL DEFAULT 0,
count_nonserial_joints INTNOT NULL DEFAULT 0
);
CREATETABLEpeers (
peer VARCHAR(100) NOT NULLPRIMARY KEY, -- wss:// address
peer_host VARCHAR(100) NOT NULL, -- domain or IP
learnt_from_peer_host VARCHAR(100) NULL, -- domain or IP
is_self TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (learnt_from_peer_host) REFERENCES peer_hosts(peer_host),
FOREIGN KEY (peer_host) REFERENCES peer_hosts(peer_host)
);
CREATEINDEXpeersIndexByPeerHostON peers(peer_host);
CREATEINDEXpeersIndexByLearntHostON peers(learnt_from_peer_host);
-- INSERT INTO peer_hosts (peer_host) VALUES('127.0.0.1');
-- INSERT INTO peers (peer_host, peer) VALUES('127.0.0.1', 'ws://127.0.0.1:8081');
CREATETABLEpeer_events (
peer_host VARCHAR(100) NOT NULL, -- domain or IP
event_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
event TEXTCHECK (event IN('new_good', 'invalid', 'nonserial', 'known_good', 'known_bad')) NOT NULL,
FOREIGN KEY (peer_host) REFERENCES peer_hosts(peer_host)
);
CREATEINDEXpeerEventsIndexByPeerHostON peer_events(peer_host);
-- self advertised urls
-- only inbound peers can advertise their urls
CREATETABLEpeer_host_urls (
peer_host VARCHAR(100) NOT NULL, -- IP
url VARCHAR(100) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
is_active TINYINT NULL DEFAULT 1,
revocation_date TIMESTAMPNULL,
UNIQUE (peer_host, is_active),
FOREIGN KEY (peer_host) REFERENCES peer_hosts(peer_host)
);
-- -----------------------
-- wallet tables
-- wallets composed of BIP44 keys, the keys live on different devices, each device knows each other's extended public key
CREATETABLEwallets (
wallet CHAR(44) NOT NULLPRIMARY KEY,
account INTNOT NULL,
definition_template TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
full_approval_date TIMESTAMPNULL,
ready_date TIMESTAMPNULL-- when all members notified me that they saw the wallet fully approved
);
-- BIP44 addresses. Coin type and account are fixed and stored in credentials in localstorage.
-- derivation path is m/44'/0'/account'/is_change/address_index
CREATETABLEmy_addresses (
address CHAR(32) NOT NULLPRIMARY KEY,
wallet CHAR(44) NOT NULL,
is_change TINYINT NOT NULL,
address_index INTNOT NULL,
definition TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (wallet, is_change, address_index),
FOREIGN KEY (wallet) REFERENCES wallets(wallet)
);
CREATETABLEmy_witnesses (
address CHAR(32) NOT NULLPRIMARY KEY
);
-- --------------------
-- hub tables
CREATETABLEdevices (
device_address CHAR(33) NOT NULLPRIMARY KEY,
pubkey CHAR(44) NOT NULL,
temp_pubkey_package TEXTNULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEdevice_messages (
message_hash CHAR(44) NOT NULLPRIMARY KEY,
device_address CHAR(33) NOT NULL, -- the device this message is addressed to
message TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (device_address) REFERENCES devices(device_address)
);
CREATEINDEXdeviceMessagesIndexByDeviceAddressON device_messages(device_address);
-- --------------------
-- hub client tables
CREATETABLEcorrespondent_devices (
device_address CHAR(33) NOT NULLPRIMARY KEY,
name VARCHAR(100) NOT NULL,
pubkey CHAR(44) NOT NULL,
hub VARCHAR(100) NOT NULL, -- domain name of the hub this address is subscribed to
is_confirmed TINYINT NOT NULL DEFAULT 0,
is_indirect TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEpairing_secrets (
pairing_secret VARCHAR(40) NOT NULLPRIMARY KEY,
is_permanent TINYINT NOT NULL DEFAULT 0,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
expiry_date TIMESTAMPNOT NULL
);
CREATETABLEextended_pubkeys (
wallet CHAR(44) NOT NULL, -- no FK because xpubkey may arrive earlier than the wallet is approved by the user and written to the db
extended_pubkey CHAR(112) NULL, -- base58 encoded, see bip32, NULL while pending
device_address CHAR(33) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
approval_date TIMESTAMPNULL,
member_ready_date TIMESTAMPNULL, -- when this member notified us that he has collected all member xpubkeys
PRIMARY KEY (wallet, device_address)
-- own address is not present in correspondents
-- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address)
);
CREATETABLEwallet_signing_paths (
wallet CHAR(44) NOT NULL, -- no FK because xpubkey may arrive earlier than the wallet is approved by the user and written to the db
signing_path VARCHAR(255) NULL, -- NULL if xpubkey arrived earlier than the wallet was approved by the user
device_address CHAR(33) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (wallet, signing_path),
FOREIGN KEY (wallet) REFERENCES wallets(wallet)
-- own address is not present in correspondents
-- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address)
);
-- addresses composed of several other addresses (such as ["and", [["address", "ADDRESS1"], ["address", "ADDRESS2"]]]),
-- member addresses live on different devices, member addresses themselves may be composed of several keys
CREATETABLEshared_addresses (
shared_address CHAR(32) NOT NULLPRIMARY KEY,
definition TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEpending_shared_addresses (
definition_template_chash CHAR(32) NOT NULLPRIMARY KEY,
definition_template TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATETABLEpending_shared_address_signing_paths (
definition_template_chash CHAR(32) NOT NULL,
device_address CHAR(33) NOT NULL,
signing_path TEXTNOT NULL, -- path from root to member address
address CHAR(32) NULL, -- member address
device_addresses_by_relative_signing_paths TEXTNULL, -- json
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
approval_date TIMESTAMPNULL,
PRIMARY KEY (definition_template_chash, signing_path),
-- own address is not present in correspondents
-- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address),
FOREIGN KEY (definition_template_chash) REFERENCES pending_shared_addresses(definition_template_chash)
);
CREATETABLEshared_address_signing_paths (
shared_address CHAR(32) NOT NULL,
signing_path VARCHAR(255) NULL, -- full path to signing key which is a member of the member address
address CHAR(32) NOT NULL, -- member address
member_signing_path VARCHAR(255) NULL, -- path to signing key from root of the member address
device_address CHAR(33) NOT NULL, -- where this signing key lives or is reachable through
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (shared_address, signing_path),
FOREIGN KEY (shared_address) REFERENCES shared_addresses(shared_address)
-- own address is not present in correspondents
-- FOREIGN KEY byDeviceAddress(device_address) REFERENCES correspondent_devices(device_address)
);
CREATETABLEoutbox (
message_hash CHAR(44) NOT NULLPRIMARY KEY,
`to`CHAR(33) NOT NULL, -- the device this message is addressed to, no FK because of pairing case
message TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
last_error TEXTNULL
);
-- light clients
CREATETABLEwatched_light_addresses (
peer VARCHAR(100) NOT NULL,
address CHAR(32) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (peer, address)
);
CREATEINDEXwlabyAddressON watched_light_addresses(address);
CREATEINDEX "bySequence" ON"units" ("sequence");
DROPTABLE IF EXISTS paid_witness_events;
CREATETABLEIF NOT EXISTS push_registrations (
registrationId TEXT,
device_address TEXTNOT NULL,
platform TEXTNOT NULL,
PRIMARY KEY (device_address)
);
CREATETABLEchat_messages (
id INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT,
correspondent_address CHAR(33) NOT NULL,
message LONGTEXT NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
is_incoming INTEGER(1) NOT NULL,
type CHAR(15) NOT NULL DEFAULT 'text',
FOREIGN KEY (correspondent_address) REFERENCES correspondent_devices(device_address) ON DELETE CASCADE
);
CREATEINDEXchatMessagesIndexByDeviceAddressON chat_messages(correspondent_address, id);
ALTERTABLE correspondent_devices ADD COLUMN my_record_pref INTEGER DEFAULT 1;
ALTERTABLE correspondent_devices ADD COLUMN peer_record_pref INTEGER DEFAULT 1;
CREATETABLEwatched_light_units (
peer VARCHAR(100) NOT NULL,
unit CHAR(44) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (peer, unit)
);
CREATEINDEXwlabyUnitON watched_light_units(unit);
CREATETABLEbots (
id INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT,
rank INTEGERNOT NULL DEFAULT 0,
name VARCHAR(100) NOT NULL UNIQUE,
pairing_code VARCHAR(200) NOT NULL,
description LONGTEXT NOT NULL
);
CREATETABLEasset_metadata (
asset CHAR(44) NOT NULLPRIMARY KEY,
metadata_unit CHAR(44) NOT NULL,
registry_address CHAR(32) NULL, -- filled only on the hub
suffix VARCHAR(20) NULL, -- added only if the same name is registered by different registries for different assets, equal to registry name
name VARCHAR(20) NULL,
decimals TINYINT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (name, registry_address),
FOREIGN KEY (asset) REFERENCES assets(unit),
FOREIGN KEY (metadata_unit) REFERENCES units(unit)
-- FOREIGN KEY (registry_address) REFERENCES addresses(address) -- addresses is not always filled on light
);
CREATETABLEsent_mnemonics (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
mnemonic VARCHAR(107) NOT NULL,
textAddress VARCHAR(120) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXsentByAddressON sent_mnemonics(address);
CREATEINDEXsentByUnitON sent_mnemonics(unit);
CREATETABLEprivate_profiles (
private_profile_id INTEGERNOT NULLPRIMARY KEY AUTOINCREMENT,
unit CHAR(44) NOT NULL,
payload_hash CHAR(44) NOT NULL,
attestor_address CHAR(32) NOT NULL,
address CHAR(32) NOT NULL,
src_profile TEXTNOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATETABLEprivate_profile_fields (
private_profile_id INTEGERNOT NULL ,
`field`VARCHAR(50) NOT NULL,
`value`VARCHAR(50) NOT NULL,
blinding CHAR(16) NOT NULL,
creation_date TIMESTAMPNOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (private_profile_id, `field`),
FOREIGN KEY (private_profile_id) REFERENCES private_profiles(private_profile_id)
);
CREATEINDEXppfByFieldON private_profile_fields(`field`);
CREATETABLEattested_fields (
unit CHAR(44) NOT NULL,
message_index TINYINT NOT NULL,
attestor_address CHAR(32) NOT NULL,
address CHAR(32) NOT NULL,
`field`VARCHAR(50) NOT NULL,
`value`VARCHAR(100) NOT NULL,
PRIMARY KEY (unit, message_index, `field`),
CONSTRAINT attestationsByAttestorAddress FOREIGN KEY (attestor_address) REFERENCES addresses(address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATEINDEXattestedFieldsByAttestorFieldValueON attested_fields(attestor_address, `field`, `value`);
CREATEINDEXattestedFieldsByAddressFieldON attested_fields(address, `field`);
-- user enters an email address (it is original address) and it is translated to BB address
CREATETABLEoriginal_addresses (
unit CHAR(44) NOT NULL,
address CHAR(32) NOT NULL,
original_address VARCHAR(100) NOT NULL, -- email
PRIMARY KEY (unit, address),
FOREIGN KEY (unit) REFERENCES units(unit)
);
--设别地址与交易关联关系表
CREATETABLE "transactions_device_address" (
"id"CHARNOT NULL,
"device"CHARNOT NULL,
"result"TEXT,
"status"TEXT,
PRIMARY KEY ("id", "device")
);
PRAGMA user_version=21;