-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
58 lines (48 loc) · 1.39 KB
/
db.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
const treeDb = "trees.db"
var db *sql.DB
// The db stores address hex values with 0x prefixes (standard for go-ethereum's common.address).
// Roots and leaf hashes are stored as raw bytes for performance reasons.
func initDb() error {
var err error
db, err = sql.Open("sqlite3", treeDb)
if err != nil {
return err
}
// These are the inbox trees for each sucker. The leaves are read from the peer sucker.
if _, err = db.Exec(`CREATE TABLE IF NOT EXISTS trees (
chain_id TEXT,
contract_address TEXT,
token_address TEXT,
current_root TEXT,
block_claims_last_updated TEXT,
PRIMARY KEY (chain_id, contract_address, token_address)
);`); err != nil {
return err
}
// Leaves are associated with their inbox tree, not outbox trees.
// Claimed is a boolean
if _, err = db.Exec(`CREATE TABLE IF NOT EXISTS leaves (
chain_id TEXT,
contract_address TEXT,
token_address TEXT,
leaf_index TEXT,
leaf_beneficiary TEXT,
leaf_project_token_amount TEXT,
leaf_terminal_token_amount TEXT,
leaf_hash TEXT,
is_claimed INTEGER,
PRIMARY KEY (chain_id, contract_address, token_address, leaf_index)
);`); err != nil {
return err
}
if _, err = db.Exec(`CREATE INDEX IF NOT EXISTS idx_leaves_chain_contract_token
ON leaves (chain_id, contract_address, token_address);`); err != nil {
return err
}
return nil
}