-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
528 lines (488 loc) · 14.7 KB
/
database.go
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
package main
import (
"compress/gzip"
"crypto/sha512"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sort"
_ "github.com/mattn/go-sqlite3"
)
const sqliteDatabaseName = "wot.sqlite3"
var dbTables = map[string]string{
"block": `
CREATE TABLE IF NOT EXISTS block (
height INTEGER PRIMARY KEY,
hash TEXT NOT NULL UNIQUE,
ts INTEGER
)`,
"publisher": `
CREATE TABLE IF NOT EXISTS publisher (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
data TEXT
)`,
"publisher_pubkey": `
CREATE TABLE IF NOT EXISTS publisher_pubkey (
id INTEGER PRIMARY KEY,
publisher_id INTEGER NOT NULL REFERENCES publisher(id),
pubkey TEXT NOT NULL,
since_block INTEGER NOT NULL REFERENCES block(height),
to_block INTEGER REFERENCES block(height)
)`,
"fact": `
CREATE TABLE IF NOT EXISTS fact (
publisher_id INTEGER NOT NULL REFERENCES publisher(id),
key TEXT NOT NULL,
value TEXT NOT NULL
)`,
"document": `
CREATE TABLE IF NOT EXISTS document (
publisher_id INTEGER PRIMARY KEY REFERENCES publisher(id),
id VARCHAR NOT NULL,
block INTEGER NOT NULL REFERENCES block(id)
)`,
"state": `
CREATE TABLE IF NOT EXISTS state (
id INTEGER PRIMARY KEY,
pubkey TEXT NOT NULL UNIQUE,
balance INTEGER NOT NULL DEFAULT 0,
nonce INTEGER NOT NULL DEFAULT 0,
data TEXT NOT NULL DEFAULT '',
flags INTEGER NOT NULL DEFAULT 0
)
`,
"utx": `
CREATE TABLE IF NOT EXISTS utx (
id INTEGER PRIMARY KEY,
ts INTEGER NOT NULL,
hash TEXT NOT NULL UNIQUE,
tx TEXT NOT NULL
)`,
}
var dbTableIndexes = map[string]string{
"publisher_pubkey_idx": `CREATE INDEX IF NOT EXISTS publisher_pubkey_idx ON publisher_pubkey(pubkey)`,
"publisher_pubkey_id_idx": `CREATE INDEX IF NOT EXISTS publisher_pubkey_id_idx ON publisher_pubkey(publisher_id)`,
"fact_publisher_idx": `CREATE UNIQUE INDEX IF NOT EXISTS fact_publisher_idx ON fact(publisher_id, key)`,
"document_idx": `CREATE UNIQUE INDEX IF NOT EXISTS document_idx ON document(publisher_id, id)`,
}
type Publisher struct {
ID int
Name string
CurrentPubKey string
CurrentPubKeyID int
SinceBlock int
ToBlock int
}
var db *sql.DB
func dbFilePresent() bool {
dbName := path.Join(*dataDir, sqliteDatabaseName)
_, err := os.Stat(dbName)
return err == nil
}
func initDatabase() {
var err error
exists := true
dbName := path.Join(*dataDir, sqliteDatabaseName)
if _, err = os.Stat(dbName); err != nil {
exists = false
}
db, err = sql.Open("sqlite3", dbName)
if err != nil {
log.Fatal(err)
}
if !exists {
_, err = db.Exec("PRAGMA journal_mode=WAL")
if err != nil {
log.Fatal(err)
}
}
for tname, tdef := range dbTables {
_, err := db.Exec(tdef)
if err != nil {
log.Println("Error creating table", tname)
log.Fatal(err)
}
}
for iname, idef := range dbTableIndexes {
_, err := db.Exec(idef)
if err != nil {
log.Println("Error creating index", iname)
log.Fatal(err)
}
}
blocks, err := filepath.Glob(path.Join(blocksDir, blockFileGlob))
if err != nil {
log.Fatal(err)
}
sort.Strings(blocks)
for _, bFile := range blocks {
height, hash := getBlockDataFromFilename(bFile)
if !dbExistsBlockByHeight(height) {
err := dbImportBlockFile(bFile, height, hash)
if err != nil {
log.Fatal(err)
}
}
}
_, err = db.Exec("PRAGMA foreign_keys")
if err != nil {
log.Fatal(err)
}
}
func shutdownDatabase() {
db.Exec("PRAGMA optimize")
db.Close()
}
func dbExistsBlockByHeight(height int) bool {
count := 0
err := db.QueryRow("SELECT COUNT(*) FROM block WHERE height=?", height).Scan(&count)
if err != nil {
log.Panic(err)
}
return count != 0
}
func dbImportBlockFile(fn string, height int, hash string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
zf, err := gzip.NewReader(f)
if err != nil {
return err
}
defer zf.Close()
bData, err := ioutil.ReadAll(zf)
if err != nil {
return err
}
return dbImportBlock(bData, height, hash)
}
func dbImportBlock(bData []byte, height int, hash string) error {
log.Println("Importing block", hash, "at", height)
// Check if hash matches
h := sha512.New512_256()
h.Write(bData)
bHash := h.Sum(nil)
if mustEncodeBase64URL(bHash) != hash {
return fmt.Errorf("Block hash doesn't match block data: Expecting %s got %s", mustEncodeBase64URL(bHash), hash)
}
b := BlockWithHeader{BlockHeader: BlockHeader{Hash: hash}}
err := json.Unmarshal(bData, &b.Block)
if err != nil {
return fmt.Errorf("Cannot unmarshall block: %s", err.Error())
}
if height == 0 {
if b.BlockHeader.Hash != GenesisBlock.BlockHeader.Hash {
return fmt.Errorf("Genesis block not on this chain: got %s expecting %s", b.BlockHeader.Hash, GenesisBlock.BlockHeader.Hash)
}
}
dbtx, err := db.Begin()
if err != nil {
return err
}
err = dbImportCheckedBlock(dbtx, b, height, hash)
if err != nil {
dbtx.Rollback()
log.Println(err)
} else {
dbtx.Commit()
}
return nil
}
func dbImportCheckedBlock(dbtx *sql.Tx, b BlockWithHeader, height int, hash string) error {
_, err := dbtx.Exec("INSERT INTO block (height, hash) VALUES (?, ?)", height, hash)
if err != nil {
return err
}
touchedPubKeys := []string{}
totalFees := uint64(0)
coinbaseAmount := uint64(0)
coinbaseCount := 0
for _, btx := range b.Transactions {
// Verify tx signature
tx, err := btx.VerifyBasics()
if err != nil {
return err
}
senderBalance := uint64(0)
senderNonce := uint64(0)
isCoinbase := inStringSlice("coinbase", tx.Flags)
if isCoinbase {
coinbaseCount++
} else {
err = dbtx.QueryRow("SELECT balance, nonce FROM state WHERE pubkey=?", tx.SigningPubKey).Scan(&senderBalance, &senderNonce)
if err != nil && err != sql.ErrNoRows {
return err
}
if tx.PubKeyNonce != senderNonce+1 {
return fmt.Errorf("nonce out of sync for %s: expecting %d, got %d", tx.SigningPubKey, senderNonce+1, tx.PubKeyNonce)
}
// Check if outputs are possible, i.e. within current balances, and
// deduce them from the sender's balance.
for _, out := range tx.Outputs {
if out.Amount > senderBalance {
return fmt.Errorf("Transaction amount exeeds balance for %s. Balance is %v, got %v", tx.SigningPubKey, senderBalance, out.Amount)
}
senderBalance -= out.Amount
}
senderNonce++
touchedPubKeys = append(touchedPubKeys, tx.SigningPubKey)
}
totalFees += uint64(tx.MinerFeeAmount)
// Import tx payload document data
if len(tx.Data) > 0 {
// fmt.Println(jsonifyWhatever(tx.Data))
if tx.Data["_key"] != "" && tx.Data["_key"] != tx.SigningPubKey {
return fmt.Errorf("_key in tx %s doesn't match signing key. Expecting %s, got %s", btx.TxHash, tx.SigningPubKey, tx.Data["_key"])
}
publisher, err := dbGetPublisherbyKey(dbtx, tx.SigningPubKey, height)
if err != nil {
if height > 0 {
return fmt.Errorf("Publisher not found for key %s: %s", tx.SigningPubKey, err.Error())
}
// Special handling for the genesis block
publisher, err = dbIntroducePublisher(dbtx, &btx, &tx, 0)
if err != nil {
return err
}
}
for key, value := range tx.Data {
if key[0] == '_' {
continue
}
_, err := dbtx.Exec("INSERT OR REPLACE INTO fact(publisher_id, key, value) VALUES (?, ?, ?)", publisher.ID, key, value)
if err != nil {
return err
}
}
err = dbSaveDocument(dbtx, publisher, &tx, height)
if err != nil {
return err
}
}
// Update recipient states, collect receipts
for _, out := range tx.Outputs {
balance := uint64(0)
err := dbtx.QueryRow("SELECT balance FROM state WHERE pubkey=?", out.PubKey).Scan(&balance)
if err != nil {
if err != sql.ErrNoRows {
return err
}
// Output to a brand new address / state
_, err = dbtx.Exec("INSERT INTO state(pubkey, balance, nonce) VALUES (?, ?, ?)", out.PubKey, out.Amount, 1)
if err != nil {
return err
}
//log.Println("Inserted new balance", out.PubKey, out.Amount)
} else {
newBalance := balance + out.Amount
_, err = dbtx.Exec("UPDATE state SET balance = ? WHERE pubkey = ?", newBalance, out.PubKey)
if err != nil {
return err
}
}
touchedPubKeys = append(touchedPubKeys, out.PubKey)
if isCoinbase {
coinbaseAmount += out.Amount
}
}
// Update sender balance state
if !isCoinbase {
_, err = dbtx.Exec("UPDATE state SET balance=?, nonce=? WHERE pubkey=?", senderBalance, senderNonce, tx.SigningPubKey)
if err != nil {
return err
}
}
}
if coinbaseCount != 1 {
return fmt.Errorf("Exactly 1 coinbase expected in every block. Got %d", coinbaseCount)
}
if coinbaseAmount != getCoinbaseAtHeight(height)+totalFees {
return fmt.Errorf("The sum of coinbase and fees is invalid. Expecting %v, got %v", coinbaseAmount, getCoinbaseAtHeight(height)+totalFees)
}
states, err := dbGetStates(dbtx, touchedPubKeys)
if err != nil {
return err
}
stateHash := states.getStrHash()
if stateHash != b.StateHash {
log.Println("ERROR stateHash:", jsonifyWhatever(states))
return fmt.Errorf("StateHash doesn't match. Expecting %s, got %s", stateHash, b.StateHash)
}
return nil
}
func dbGetPublisherbyKey(dbtx *sql.Tx, pubKey string, atBlock int) (*Publisher, error) {
p := Publisher{}
nullToBlock := sql.NullInt64{}
err := dbtx.QueryRow("SELECT id, publisher_id, since_block, to_block FROM publisher_pubkey WHERE pubkey=? ORDER BY since_block DESC", pubKey).Scan(&p.CurrentPubKeyID, &p.ID, &p.SinceBlock, &nullToBlock)
if err != nil {
return nil, fmt.Errorf("Error getting publisher for pubkey %s: %s", pubKey, err.Error())
}
p.ToBlock = int(nullToBlock.Int64)
err = dbtx.QueryRow("SELECT name FROM publisher WHERE id=?", p.ID).Scan(&p.Name)
if err != nil {
return nil, fmt.Errorf("Error getting publisher by ID %d", p.ID)
}
if nullToBlock.Valid && nullToBlock.Int64 > 0 {
if p.SinceBlock >= atBlock && atBlock <= p.ToBlock {
return &p, nil
}
} else {
if p.SinceBlock >= atBlock {
return &p, nil
}
}
return nil, fmt.Errorf("Publisher's key has expired: %s at block %d", pubKey, atBlock)
}
func dbSaveDocument(dbtx *sql.Tx, publisher *Publisher, tx *Tx, height int) error {
_, err := dbtx.Exec("INSERT OR REPLACE INTO document (publisher_id, id, block) VALUES (?, ?, ?)", publisher.ID, tx.Data["_id"], height)
return err
}
func dbIntroducePublisher(dbtx *sql.Tx, btx *BlockTransaction, tx *Tx, height int) (*Publisher, error) {
id, ok := tx.Data["_id"]
if !ok {
return nil, fmt.Errorf("Missing _id in %s", btx.TxHash)
}
if id != "_intro" {
return nil, fmt.Errorf("Trying to introduce a publisher without _id in %s", btx.TxHash)
}
name, ok := tx.Data["_name"]
if !ok {
return nil, fmt.Errorf("Trying to introduce a publisher without _name in %s", btx.TxHash)
}
pubKey, ok := tx.Data["_key"]
if !ok {
return nil, fmt.Errorf("Trying to introduce a publisher without _key in %s", btx.TxHash)
}
publisherID := 0
publisherPubKeyID := 0
publisherSinceBlock := 0
err := dbtx.QueryRow("SELECT publisher_id, id, since_block FROM publisher_pubkey WHERE pubkey=?", pubKey).Scan(&publisherID, &publisherPubKeyID, &publisherSinceBlock)
if err == nil {
// The publisher already exists, so this operation can only replace its key
newKey, ok := tx.Data["_newkey"]
if !ok {
return nil, fmt.Errorf("Trying to re-introduce (replace key) a publisher without _newkey in %s", btx.TxHash)
}
res, err := dbtx.Exec("INSERT INTO publisher_pubkey (publisher_id, pubkey, since_block) VALUES (?, ?, ?)", publisherID, newKey, height)
if err != nil {
dbtx.Rollback()
return nil, err
}
lastPubKeyID, err := res.LastInsertId()
if err != nil {
dbtx.Rollback()
return nil, err
}
publisherPubKeyID = int(lastPubKeyID)
_, err = dbtx.Exec("UPDATE publisher SET name=? WHERE id=?", name, publisherID)
if err != nil {
dbtx.Rollback()
return nil, err
}
} else {
// Brand new publisher
res, err := dbtx.Exec("INSERT INTO publisher (name) VALUES (?)", name)
if err != nil {
return nil, err
}
lastID, err := res.LastInsertId()
if err != nil {
return nil, err
}
publisherID := int(lastID)
res, err = dbtx.Exec("INSERT INTO publisher_pubkey (publisher_id, pubkey, since_block) VALUES (?,?,?)", publisherID, pubKey, height)
if err != nil {
return nil, err
}
lastKeyID, err := res.LastInsertId()
if err != nil {
return nil, err
}
publisherPubKeyID = int(lastKeyID)
publisherSinceBlock = height
}
p := Publisher{ID: publisherID, CurrentPubKeyID: publisherPubKeyID, CurrentPubKey: pubKey, Name: name, SinceBlock: publisherSinceBlock}
return &p, nil
}
func dbGetStates(dbtx *sql.Tx, pubkeys []string) (AccountStates, error) {
result := AccountStates{}
for _, k := range pubkeys {
state := RawAccountState{}
err := dbtx.QueryRow("SELECT balance, nonce, data FROM state WHERE pubkey=?", k).Scan(&state.Balance, &state.Nonce, &state.Data)
if err != nil && err != sql.ErrNoRows {
return result, err
}
if err == nil {
result[k] = &state
}
}
return result, nil
}
func dbSimStateHashStr(dbtx *sql.Tx, block BlockWithHeader) (string, error) {
touchedAddressesMap := map[string]bool{}
for _, btx := range block.Transactions {
tx := Tx{}
err := json.Unmarshal([]byte(btx.TxData), &tx)
if err != nil {
return "", err
}
if !inStringSlice("coinbase", tx.Flags) {
touchedAddressesMap[tx.SigningPubKey] = true
}
for _, out := range tx.Outputs {
touchedAddressesMap[out.PubKey] = true
}
}
touchedAddresses := []string{}
for a := range touchedAddressesMap {
touchedAddresses = append(touchedAddresses, a)
}
states, err := dbGetStates(dbtx, touchedAddresses)
if err != nil {
return "", err
}
//fmt.Println("before:", jsonifyWhatever(states))
coinbaseCount := 0
for _, btx := range block.Transactions {
tx := Tx{}
json.Unmarshal([]byte(btx.TxData), &tx)
isCoinbase := inStringSlice("coinbase", tx.Flags)
if isCoinbase {
coinbaseCount++
} else {
if states[tx.SigningPubKey] == nil {
return "", fmt.Errorf("No state to send from for tx %s", btx.TxHash)
}
}
// XXX: fees accounting?!
// XXX: mining needs a better way to be notified of an invalid tx; custom error type?
for _, out := range tx.Outputs {
if !isCoinbase {
if out.Amount < states[tx.SigningPubKey].Balance {
states[tx.SigningPubKey].Balance -= out.Amount
states[tx.SigningPubKey].Nonce++
if states[tx.SigningPubKey].Nonce != tx.PubKeyNonce {
return btx.TxHash, fmt.Errorf("[sim] nonce out of sync for tx %s: expecting %d, got %d", btx.TxHash, states[tx.SigningPubKey].Nonce, tx.PubKeyNonce)
}
} else {
return "", fmt.Errorf("Not enough balance for tx %s", btx.TxHash)
}
}
if states[out.PubKey] == nil {
states[out.PubKey] = &RawAccountState{Balance: out.Amount, Nonce: 1}
} else {
states[out.PubKey].Balance += out.Amount
}
}
}
//fmt.Println("after:", jsonifyWhatever(states))
return states.getStrHash(), nil
}