forked from gcarq/rusty-blockparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
168 lines (135 loc) · 4.98 KB
/
schema.sql
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
## Needed if you want to enable compression
#SET GLOBAL innodb_file_per_table=1;
#SET GLOBAL innodb_file_format=Barracuda;
SET NAMES ascii COLLATE ascii_bin;
SET default_storage_engine=INNODB;
CREATE SCHEMA IF NOT EXISTS `btc_blockchain` CHARACTER SET ascii COLLATE ascii_bin;
USE `btc_blockchain`;
DROP TABLE IF EXISTS `blocks`;
CREATE TABLE `blocks` (
`id` int(4) unsigned AUTO_INCREMENT NOT NULL,
`hash` binary(32) NOT NULL,
`height` int(10) unsigned NOT NULL,
`version` int(11) NOT NULL,
`blocksize` int(10) unsigned NOT NULL,
`hashPrev` binary(32) NOT NULL,
`hashMerkleRoot` binary(32) NOT NULL,
`nTime` int(10) unsigned NOT NULL,
`nBits` int(10) unsigned NOT NULL,
`nNonce` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
# ROW_FORMAT=DYNAMIC;
DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
`id` int(4) unsigned AUTO_INCREMENT NOT NULL,
`txid` binary(32) NOT NULL,
`hashBlock` binary(32) NOT NULL,
`version` int(11) unsigned NOT NULL,
`lockTime` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
# ROW_FORMAT=DYNAMIC;
DROP TABLE IF EXISTS `tx_out`;
CREATE TABLE `tx_out` (
`id` int(4) unsigned AUTO_INCREMENT NOT NULL,
`txid` binary(32) NOT NULL,
`indexOut` int(10) unsigned NOT NULL,
`value` bigint(8) unsigned NOT NULL,
`scriptPubKey` blob NOT NULL,
`address` varchar(36) DEFAULT NULL,
`unspent` bit DEFAULT TRUE NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
# ROW_FORMAT=DYNAMIC;
DROP TABLE IF EXISTS `tx_in`;
CREATE TABLE `tx_in` (
`id` int(10) unsigned AUTO_INCREMENT NOT NULL,
`txid` binary(32) NOT NULL,
`hashPrevOut` binary(32) NOT NULL,
`indexPrevOut` int(10) unsigned NOT NULL,
`scriptSig` blob NOT NULL,
`sequence` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
# ROW_FORMAT=DYNAMIC;
SET @@session.unique_checks = 0;
SET @@session.foreign_key_checks = 0;
SET @@session.sync_binlog = 0;
# Speed up bulk load
SET autocommit = 0;
SET sql_safe_updates = 0;
SET sql_log_bin=0;
## Also consider to set following mysql settings for maximum performance
# innodb_read_io_threads = 3
# innodb_write_io_threads = 3
# innodb_buffer_pool_size = 6G
# innodb_autoinc_lock_mode = 2
# innodb_log_file_size = 128M
# innodb_log_buffer_size = 8M
# innodb_flush_method = O_DIRECT
# innodb_flush_log_at_trx_commit = 0
# skip-innodb_doublewrite
TRUNCATE blocks;
## Load blocks into table
LOAD DATA INFILE '/media/tmp/dump/blocks-0-393489.csv'
INTO TABLE blocks
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(@hash, height, version, blocksize, @hashPrev, @hashMerkleRoot, nTime, nBits, nNonce)
SET hash = unhex(@hash),
hashPrev = unhex(@hashPrev),
hashMerkleRoot = unhex(@hashMerkleRoot);
COMMIT;
TRUNCATE transactions;
## Load transactions into table
LOAD DATA INFILE '/media/tmp/dump/transactions-0-393489.csv'
INTO TABLE transactions
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(@txid, @hashBlock, version, lockTime)
SET txid = unhex(@txid),
hashBlock = unhex(@hashBlock);
COMMIT;
TRUNCATE tx_out;
## Load tx_out into table
LOAD DATA INFILE '/media/tmp/dump/tx_out-0-393489.csv'
INTO TABLE tx_out
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(@txid, indexOut, value, @scriptPubKey, address)
SET txid = unhex(@txid),
scriptPubKey = unhex(@scriptPubKey);
COMMIT;
TRUNCATE tx_in;
## Load tx_in into table
LOAD DATA INFILE '/media/tmp/dump/tx_in-0-393489.csv'
INTO TABLE tx_in
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(@txid, @hashPrevOut, indexPrevOut, scriptSig, sequence)
SET txid = unhex(@txid),
hashPrevOut = unhex(@hashPrevOut);
COMMIT;
SET @@session.unique_checks = 1;
SET @@session.foreign_key_checks = 1;
# Add keys
ALTER TABLE `blocks` ADD UNIQUE KEY (`hash`);
ALTER TABLE `transactions` ADD KEY (`txid`);
ALTER TABLE `tx_in` ADD KEY (`hashPrevOut`, `indexPrevOut`);
ALTER TABLE `tx_out` ADD KEY (`txid`, `indexOut`),
ADD KEY (`address`);
#ALTER TABLE `transactions` ADD FOREIGN KEY (`hashBlock`) REFERENCES blocks(`hash`);
#ALTER TABLE `tx_out` ADD FOREIGN KEY (`txid`) REFERENCES transactions(`txid`);
#ALTER TABLE `tx_id` ADD FOREIGN KEY (`txid`) REFERENCES transactions(`txid`);
COMMIT;
# Flag spent tx outputs
UPDATE `tx_out` o, `tx_in` i
SET o.unspent = FALSE
WHERE o.txid = i.hashPrevOut
AND o.indexOut = i.indexPrevOut;
COMMIT;
SET autocommit = 1;
SET sql_log_bin=1;
SET @@session.sync_binlog=1;
SET sql_safe_updates = 1;