-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.pm
387 lines (291 loc) · 7.19 KB
/
data.pm
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
package data;
use warnings;
use strict;
use DBI;
use event;
my $DBI_DS = 'dbi:SQLite:dbname=var/db';
my $DBI_USER = '';
my $DBI_PASS = '';
my $COMMIT_PERIOD = 10 * 60;
my $SCRIPT = <<SQL;
CREATE TABLE IF NOT EXISTS key (
pub BLOB PRIMARY KEY,
priv BLOB NOT NULL,
addr STRING(50) NOT NULL,
remark STRING NOT NULL
);
CREATE TABLE IF NOT EXISTS tx (
hash BLOB(32) PRIMARY KEY,
nLockTime INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS tx_in (
tx_hash BLOB(32) NOT NULL,
tx_n INTEGER NOT NULL,
prev_hash BLOB(32) NOT NULL,
prev_n INTEGER NOT NULL,
scriptSig BLOB NOT NULL,
nSequence INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS tx_in_idx
ON tx_in (tx_hash, tx_n);
CREATE TABLE IF NOT EXISTS tx_out (
tx_hash BLOB(32) NOT NULL,
tx_n INTEGER NOT NULL,
nValue INTEGER NOT NULL,
scriptPubKey BLOB NOT NULL,
addr STRING(50) NOT NULL,
spentHeight INTEGER NOT NULL -- not in chain -1, not spent 0
);
CREATE INDEX IF NOT EXISTS tx_out_idx
ON tx_out (tx_hash, tx_n);
CREATE INDEX IF NOT EXISTS tx_out_idx2
ON tx_out (addr);
CREATE INDEX IF NOT EXISTS tx_out_idx3
ON tx_out (spentHeight);
CREATE TABLE IF NOT EXISTS blk (
hash BLOB(32) PRIMARY KEY,
hashPrevBlock BLOB(32) NOT NULL,
nTime INTEGER NOT NULL,
nBits INTEGER NOT NULL,
nNonce INTEGER NOT NULL,
nHeight INTEGER NOT NULL, -- orphan is -1
mainBranch INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS blk_idx
ON blk (nHeight, mainBranch);
CREATE TABLE IF NOT EXISTS blk_tx (
blk_hash BLOB(32) NOT NULL,
blk_n INTEGER NOT NULL, -- new tx is -1
tx_hash BLOB(32) NOT NULL
);
CREATE INDEX IF NOT EXISTS blk_tx_idx
ON blk_tx (blk_hash, blk_n);
SQL
my %STH = (
blk_best => <<SQL,
SELECT MAX(nHeight), hash FROM blk WHERE mainBranch = 1
SQL
blk_connect => <<SQL,
UPDATE blk SET nHeight = ?, mainBranch = ? WHERE hash = ?
SQL
blk_orphan => <<SQL,
SELECT hash FROM blk WHERE nHeight = -1 AND hashPrevBlock = ?
SQL
blk_orphans => <<SQL,
SELECT hash, hashPrevBlock FROM blk WHERE nHeight = -1
SQL
blk_tx_del => <<SQL,
DELETE FROM blk_tx WHERE blk_hash = ? AND blk_n = ? AND tx_hash = ?
SQL
tx_out_spent => <<SQL,
UPDATE tx_out SET spentHeight = ? WHERE tx_hash = ? AND tx_n = ?
SQL
tx_out_unspent => <<SQL,
UPDATE tx_out SET spentHeight = 0 WHERE spentHeight >= ?
SQL
tx_out_inchain => <<SQL,
UPDATE tx_out SET spentHeight = 0 WHERE tx_hash = ? AND spentHeight = -1
SQL
key_all => <<SQL,
SELECT addr FROM key
SQL
key_ammo => <<SQL,
SELECT SUM(nValue) AS ammo
FROM tx_out
WHERE addr = ? AND spentHeight = 0
GROUP BY addr
SQL
);
my $dbh;
my %sth;
sub commit {
warn "commit";
$dbh->commit if $dbh;
}
sub init {
$dbh = DBI->connect ($DBI_DS, $DBI_USER, $DBI_PASS, {
RaiseError => 1,
AutoCommit => 0,
});
if ($dbh->{Driver}->{Name} =~ /sqlite/i) {
$dbh->do ('PRAGMA synchronous = OFF');
$dbh->do ('PRAGMA cache_size = 20000');
}
while ($SCRIPT =~ /([^;]+)/g) {
my $str = $1;
s/--.*//g, s/^\s+//, s/\s+\z// for $str;
next if !$str;
$dbh->do ($str);
my ($table) = $str =~ /^\s*create\s+table\s.*?\b(\w+)\s*\(/i
or next;
my (@row) = $str =~ /[(,]\s*([a-z]\w+)/ig;
local $" = ',';
$sth{"$table\_ins"} = $dbh->prepare (<<SQL);
INSERT INTO $table(@row) VALUES (@{[ map '?', @row ]})
SQL
$sth{"$table\_del"} = $dbh->prepare (<<SQL);
DELETE FROM $table WHERE $row[0] = ?
SQL
$row[1] ||= 1;
$sth{"$table\_sel"} = $dbh->prepare (<<SQL);
SELECT @row[1 .. $#row] FROM $table WHERE $row[0] = ?
SQL
no strict 'refs';
*{"$table\_exists"} = sub {
$sth{"$table\_sel"}->execute ($_[0]);
return $sth{"$table\_sel"}->fetchrow_hashref;
};
*{"$table\_cnt"} = sub { $dbh->selectrow_array (<<SQL) };
SELECT COUNT(*) FROM $table
SQL
}
$sth{$_} = $dbh->prepare ($STH{$_}) for keys %STH;
event::timer_new (
period => $COMMIT_PERIOD,
cb => \&commit,
);
}
sub tx_save {
my ($tx_h, $tx) = @_;
$sth{tx_ins}->execute ($tx_h, $tx->{nLockTime});
for (0 .. $#{ $tx->{vin} }) {
my $i = $tx->{vin}[$_];
$sth{tx_in_ins}->execute ($tx_h, $_, $i->{prevout}{hash},
$i->{prevout}{n}, $i->{scriptSig}, $i->{nSequence});
}
for (0 .. $#{ $tx->{vout} }) {
my $i = $tx->{vout}[$_];
$sth{tx_out_ins}->execute ($tx_h, $_, @$i{qw (
nValue scriptPubKey addr spentHeight
)});
}
}
sub tx_load {
my ($tx_h) = @_;
$sth{tx_sel}->execute ($tx_h);
my $h = $sth{tx_sel}->fetchrow_hashref or return;
my $tx = $h;
$sth{tx_in_sel}->execute ($tx_h);
while ($h = $sth{tx_in_sel}->fetchrow_hashref) {
$tx->{vin}[ $h->{tx_n} ] = {
prevout => {
hash => $h->{prev_hash},
n => $h->{prev_n},
},
scriptSig => $h->{scriptSig},
nSequence => $h->{nSequence},
};
}
$sth{tx_out_sel}->execute ($tx_h);
while ($h = $sth{tx_out_sel}->fetchrow_hashref) {
$tx->{vout}[ $h->{tx_n} ] = $h;
}
$tx->{h} = $tx_h;
return $tx;
}
sub tx_out_spent {
my ($tx_h, $tx_n, $height) = @_;
$sth{tx_out_spent}->execute ($height, $tx_h, $tx_n);
}
sub tx_out_unspent {
my ($height) = @_;
$sth{tx_out_unspent}->execute ($height);
}
sub tx_out_inchain {
my ($tx_h) = @_;
$sth{tx_out_inchain}->execute ($tx_h);
}
sub blk_save {
my ($blk_h, $blk) = @_;
$sth{blk_ins}->execute ($blk_h, @$blk{qw(
hashPrevBlock nTime nBits nNonce nHeight mainBranch
)});
for (0 .. $#{ $blk->{vtx_h} }) {
$sth{blk_tx_ins}->execute ($blk_h, $_, $blk->{vtx_h}[$_]);
}
}
sub blk_load {
my ($blk_h) = @_;
$sth{blk_sel}->execute ($blk_h);
my $h = $sth{blk_sel}->fetchrow_hashref or return;
my $blk = $h;
$sth{blk_tx_sel}->execute ($blk_h);
while ($h = $sth{blk_tx_sel}->fetchrow_hashref) {
$blk->{vtx_h}[ $h->{blk_n} ] = $h->{tx_hash};
}
$blk->{h} = $blk_h;
return $blk;
}
sub blk_best {
$sth{blk_best}->execute ();
my $h = $sth{blk_best}->fetchrow_hashref;
return $h && blk_load ($h->{hash});
}
sub blk_connect {
my ($blk) = @_;
$sth{blk_connect}->execute (@$blk{qw( nHeight mainBranch h )});
}
sub blk_orphan {
my ($blk_h) = @_;
$sth{blk_orphan}->execute ($blk_h);
return map $_->[0], @{ $sth{blk_orphan}->fetchall_arrayref };
}
sub blk_missed {
$sth{blk_orphans}->execute ();
my %prev = ();
while (my $h = $sth{blk_orphans}->fetchrow_hashref) {
$prev{ $h->{hash} } = $h->{hashPrevBlock};
}
return grep !exists $prev{$_}, values %prev;
}
sub blk_tx_add {
my ($blk_h, $blk_n, $tx_h) = @_;
$sth{blk_tx_ins}->execute ($blk_h, $blk_n, $tx_h);
}
sub blk_tx_del {
my ($blk_h, $blk_n, $tx_h) = @_;
$sth{blk_tx_del}->execute ($blk_h, $blk_n, $tx_h);
}
sub key_load {
my ($pub) = @_;
$sth{key_sel}->execute ($pub);
return $sth{key_sel}->fetchrow_hashref;
}
sub key_save {
my ($key) = @_;
$key->{remark} ||= '';
$sth{key_ins}->execute (@$key{qw( pub priv addr remark )});
}
sub key_ammo {
my ($addr) = @_;
$sth{key_ammo}->execute ($addr);
my $h = $sth{key_ammo}->fetchrow_hashref;
return $h ? $h->{ammo} : 0;
}
sub key_all {
$sth{key_all}->execute ();
my @res;
while (my $h = $sth{key_all}->fetchrow_hashref) {
push @res, {
addr => $h->{addr},
ammo => key_ammo ($h->{addr}),
};
}
return @res;
}
sub sql {
my ($query, $max) = @_;
my $sth = $dbh->prepare ($query);
$sth->execute ();
my (@res, $h);
while (@res < $max && ($h = $sth->fetchrow_hashref)) {
push @res, $h;
}
$sth->finish;
return \@res;
}
sub version {
return "DBI $DBI::VERSION " .
$dbh->get_info (17) . " " . $dbh->get_info (18);
}
1;