arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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// GetBlock retrieve a block from the DB, given a block number
 58func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) {
 59	return nil, nil
 60}
 61
 62// GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
 63func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
 64	var blocks []*common.Block
 65	err := meddler.QueryAll(
 66		hdb.db, &blocks,
 67		"SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2",
 68		from, to,
 69	)
 70	return blocks, err
 71}
 72
 73// GetLastBlock retrieve the block with the highest block number from the DB
 74func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
 75	return nil, nil
 76}
 77
 78// GetLastBatchNum returns the BatchNum of the latest forged batch
 79func (hdb *HistoryDB) GetLastBatchNum() (*common.BatchNum, error) {
 80	return nil, nil
 81}
 82
 83// Reorg deletes all the information that was added into the DB after the lastValidBlock
 84// WARNING: this is a draaft of the function, useful at the moment for tests
 85func (hdb *HistoryDB) Reorg(lastValidBlock uint64) error {
 86	_, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
 87	return err
 88}
 89
 90// SyncRollup stores all the data that can be changed / added on a block in the Rollup SC
 91func (hdb *HistoryDB) SyncRollup(
 92	blockNum uint64,
 93	l1txs []common.L1Tx,
 94	l2txs []common.L2Tx,
 95	registeredAccounts []common.Account,
 96	exitTree common.ExitTreeLeaf,
 97	withdrawals common.ExitTreeLeaf,
 98	registeredTokens []common.Token,
 99	batch *common.Batch,
100	vars *common.RollupVars,
101) error {
102	return nil
103}
104
105// SyncPoD stores all the data that can be changed / added on a block in the PoD SC
106func (hdb *HistoryDB) SyncPoD(
107	blockNum uint64,
108	bids []common.Bid,
109	coordinators []common.Coordinator,
110	vars *common.PoDVars,
111) error {
112	return nil
113}
114
115// addBids insert Bids into the DB
116func (hdb *HistoryDB) addBids(bids []common.Bid) error {
117	// TODO: check the coordinator info
118	return db.BulkInsert(
119		hdb.db,
120		"INSERT INTO bid (slot_num, forger_addr, bid_value, eth_block_num) VALUES %s",
121		bids[:],
122	)
123}
124
125// GetBidsByBlock return the bids done between the block from and to
126func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
127	var bids []*common.Bid
128	err := meddler.QueryAll(
129		hdb.db, &bids,
130		"SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
131		from, to,
132	)
133	return bids, err
134}
135
136// Close frees the resources used by HistoryDB
137func (hdb *HistoryDB) Close() error {
138	return hdb.db.Close()
139}