You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  1. package historydb
  2. import (
  3. "fmt"
  4. "github.com/gobuffalo/packr/v2"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db"
  7. "github.com/jmoiron/sqlx"
  8. _ "github.com/lib/pq" // driver for postgres DB
  9. migrate "github.com/rubenv/sql-migrate"
  10. "github.com/russross/meddler"
  11. )
  12. // HistoryDB persist the historic of the rollup
  13. type HistoryDB struct {
  14. db *sqlx.DB
  15. }
  16. // NewHistoryDB initialize the DB
  17. func NewHistoryDB(port int, host, user, password, dbname string) (*HistoryDB, error) {
  18. // Connect to DB
  19. psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  20. hdb, err := sqlx.Connect("postgres", psqlconn)
  21. if err != nil {
  22. return nil, err
  23. }
  24. // Init meddler
  25. db.InitMeddler()
  26. meddler.Default = meddler.PostgreSQL
  27. // Run DB migrations
  28. migrations := &migrate.PackrMigrationSource{
  29. Box: packr.New("history-migrations", "./migrations"),
  30. }
  31. if _, err := migrate.Exec(hdb.DB, "postgres", migrations, migrate.Up); err != nil {
  32. return nil, err
  33. }
  34. return &HistoryDB{hdb}, nil
  35. }
  36. // addBlocks insert blocks into the DB
  37. func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
  38. return db.BulkInsert(
  39. hdb.db,
  40. "INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
  41. blocks[:],
  42. )
  43. }
  44. // GetBlocks retrrieve blocks from the DB
  45. func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
  46. var blocks []*common.Block
  47. err := meddler.QueryAll(
  48. hdb.db, &blocks,
  49. "SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2",
  50. from, to,
  51. )
  52. return blocks, err
  53. }
  54. // reorg deletes all the information that was added into the DB after the lastValidBlock
  55. // WARNING: this is a draaft of the function, useful at the moment for tests
  56. func (hdb *HistoryDB) reorg(lastValidBlock uint64) error {
  57. _, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
  58. return err
  59. }
  60. // addBids insert Bids into the DB
  61. func (hdb *HistoryDB) addBids(bids []common.Bid) error {
  62. // TODO: check the coordinator info
  63. return db.BulkInsert(
  64. hdb.db,
  65. "INSERT INTO bid (slot_num, forger_addr, bid_value, eth_block_num) VALUES %s",
  66. bids[:],
  67. )
  68. }
  69. // GetBidsByBlock return the bids done between the block from and to
  70. func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
  71. var bids []*common.Bid
  72. err := meddler.QueryAll(
  73. hdb.db, &bids,
  74. "SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
  75. from, to,
  76. )
  77. return bids, err
  78. }
  79. // Close frees the resources used by HistoryDB
  80. func (hdb *HistoryDB) Close() error {
  81. return hdb.db.Close()
  82. }