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.

124 lines
3.3 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. // AddBlock insert a block into the DB
  37. func (hdb *HistoryDB) AddBlock(blocks *common.Block) error {
  38. return nil
  39. }
  40. // addBlocks insert blocks into the DB. TODO: move method to test
  41. func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
  42. return db.BulkInsert(
  43. hdb.db,
  44. "INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
  45. blocks[:],
  46. )
  47. }
  48. // GetBlocks retrrieve blocks from the DB
  49. func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
  50. var blocks []*common.Block
  51. err := meddler.QueryAll(
  52. hdb.db, &blocks,
  53. "SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2",
  54. from, to,
  55. )
  56. return blocks, err
  57. }
  58. // Reorg deletes all the information that was added into the DB after the lastValidBlock
  59. // WARNING: this is a draaft of the function, useful at the moment for tests
  60. func (hdb *HistoryDB) Reorg(lastValidBlock uint64) error {
  61. _, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
  62. return err
  63. }
  64. // SyncRollup stores all the data that can be changed / added on a block in the Rollup SC
  65. func (hdb *HistoryDB) SyncRollup(
  66. blockNum uint64,
  67. l1txs []common.L1Tx,
  68. l2txs []common.L2Tx,
  69. registeredAccounts []common.Account,
  70. exitTree common.ExitTreeLeaf,
  71. withdrawals common.ExitTreeLeaf,
  72. registeredTokens []common.Token,
  73. batch *common.Batch,
  74. vars *common.RollupVars,
  75. ) error {
  76. return nil
  77. }
  78. // SyncPoD stores all the data that can be changed / added on a block in the PoD SC
  79. func (hdb *HistoryDB) SyncPoD(
  80. blockNum uint64,
  81. bids []common.Bid,
  82. coordinators []common.Coordinator,
  83. vars *common.PoDVars,
  84. ) error {
  85. return nil
  86. }
  87. // addBids insert Bids into the DB
  88. func (hdb *HistoryDB) addBids(bids []common.Bid) error {
  89. // TODO: check the coordinator info
  90. return db.BulkInsert(
  91. hdb.db,
  92. "INSERT INTO bid (slot_num, forger_addr, bid_value, eth_block_num) VALUES %s",
  93. bids[:],
  94. )
  95. }
  96. // GetBidsByBlock return the bids done between the block from and to
  97. func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
  98. var bids []*common.Bid
  99. err := meddler.QueryAll(
  100. hdb.db, &bids,
  101. "SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
  102. from, to,
  103. )
  104. return bids, err
  105. }
  106. // Close frees the resources used by HistoryDB
  107. func (hdb *HistoryDB) Close() error {
  108. return hdb.db.Close()
  109. }