hermez-node/db/historydb/historydb.go
db/historydb/historydb.goRaw
1package historydb
2
3import (
4 "fmt"
5
6 "github.com/gobuffalo/packr/v2"
7 "github.com/hermeznetwork/hermez-node/common"
8 "github.com/hermeznetwork/hermez-node/db"
9 "github.com/jmoiron/sqlx"
10 _ "github.com/lib/pq" // driver for postgres DB
11 migrate "github.com/rubenv/sql-migrate"
12 "github.com/russross/meddler"
13)
14
15// HistoryDB persist the historic of the rollup
16type HistoryDB struct {
17 db *sqlx.DB
18}
19
20// NewHistoryDB initialize the DB
21func NewHistoryDB(port int, host, user, password, dbname string) (*HistoryDB, error) {
22 // Connect to DB
23 psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
24 hdb, err := sqlx.Connect("postgres", psqlconn)
25 if err != nil {
26 return nil, err
27 }
28 // Init meddler
29 db.InitMeddler()
30 meddler.Default = meddler.PostgreSQL
31
32 // Run DB migrations
33 migrations := &migrate.PackrMigrationSource{
34 Box: packr.New("history-migrations", "./migrations"),
35 }
36 if _, err := migrate.Exec(hdb.DB, "postgres", migrations, migrate.Up); err != nil {
37 return nil, err
38 }
39
40 return &HistoryDB{hdb}, nil
41}
42
43// AddBlock insert a block into the DB
44func (hdb *HistoryDB) AddBlock(blocks *common.Block) error {
45 return nil
46}
47
48// addBlocks insert blocks into the DB. TODO: move method to test
49func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
50 return db.BulkInsert(
51 hdb.db,
52 "INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
53 blocks[:],
54 )
55}
56
57// GetBlocks retrrieve blocks from the DB
58func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
59 var blocks []*common.Block
60 err := meddler.QueryAll(
61 hdb.db, &blocks,
62 "SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2",
63 from, to,
64 )
65 return blocks, err
66}
67
68// Reorg deletes all the information that was added into the DB after the lastValidBlock
69// WARNING: this is a draaft of the function, useful at the moment for tests
70func (hdb *HistoryDB) Reorg(lastValidBlock uint64) error {
71 _, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
72 return err
73}
74
75// SyncRollup stores all the data that can be changed / added on a block in the Rollup SC
76func (hdb *HistoryDB) SyncRollup(
77 blockNum uint64,
78 l1txs []common.L1Tx,
79 l2txs []common.L2Tx,
80 registeredAccounts []common.Account,
81 exitTree common.ExitTreeLeaf,
82 withdrawals common.ExitTreeLeaf,
83 registeredTokens []common.Token,
84 batch *common.Batch,
85 vars *common.RollupVars,
86) error {
87 return nil
88}
89
90// SyncPoD stores all the data that can be changed / added on a block in the PoD SC
91func (hdb *HistoryDB) SyncPoD(
92 blockNum uint64,
93 bids []common.Bid,
94 coordinators []common.Coordinator,
95 vars *common.PoDVars,
96) error {
97 return nil
98}
99
100// addBids insert Bids into the DB
101func (hdb *HistoryDB) addBids(bids []common.Bid) error {
102 // TODO: check the coordinator info
103 return db.BulkInsert(
104 hdb.db,
105 "INSERT INTO bid (slot_num, forger_addr, bid_value, eth_block_num) VALUES %s",
106 bids[:],
107 )
108}
109
110// GetBidsByBlock return the bids done between the block from and to
111func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
112 var bids []*common.Bid
113 err := meddler.QueryAll(
114 hdb.db, &bids,
115 "SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
116 from, to,
117 )
118 return bids, err
119}
120
121// Close frees the resources used by HistoryDB
122func (hdb *HistoryDB) Close() error {
123 return hdb.db.Close()
124}