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.

122 lines
3.6 KiB

  1. package historydb
  2. import (
  3. "fmt"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "time"
  8. eth "github.com/ethereum/go-ethereum/common"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. var historyDB *HistoryDB
  13. // In order to run the test you need to run a Posgres DB with
  14. // a database named "history" that is accessible by
  15. // user: "hermez"
  16. // pass: set it using the env var POSTGRES_PASS
  17. // This can be achieved by running: POSTGRES_PASS=your_strong_pass && sudo docker run --rm --name hermez-db-test -p 5432:5432 -e POSTGRES_DB=history -e POSTGRES_USER=hermez -e POSTGRES_PASSWORD=$POSTGRES_PASS -d postgres && sleep 2s && sudo docker exec -it hermez-db-test psql -a history -U hermez -c "CREATE DATABASE l2;"
  18. // After running the test you can stop the container by running: sudo docker kill hermez-db-test
  19. // If you already did that for the L2DB you don't have to do it again
  20. func TestMain(m *testing.M) {
  21. // init DB
  22. var err error
  23. pass := os.Getenv("POSTGRES_PASS")
  24. historyDB, err = NewHistoryDB(5432, "localhost", "hermez", pass, "history")
  25. if err != nil {
  26. panic(err)
  27. }
  28. // Run tests
  29. result := m.Run()
  30. // Close DB
  31. if err := historyDB.Close(); err != nil {
  32. fmt.Println("Error closing the history DB:", err)
  33. }
  34. os.Exit(result)
  35. }
  36. func TestAddBlock(t *testing.T) {
  37. var fromBlock, toBlock uint64
  38. fromBlock = 1
  39. toBlock = 5
  40. // Delete peviously created rows (clean previous test execs)
  41. assert.NoError(t, historyDB.reorg(fromBlock-1))
  42. // Generate fake blocks
  43. blocks := genBlocks(fromBlock, toBlock)
  44. // Insert blocks into DB
  45. err := historyDB.addBlocks(blocks)
  46. assert.NoError(t, err)
  47. // Get blocks from DB
  48. fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
  49. // Compare generated vs getted blocks
  50. assert.NoError(t, err)
  51. for i, fetchedBlock := range fetchedBlocks {
  52. assert.Equal(t, blocks[i].EthBlockNum, fetchedBlock.EthBlockNum)
  53. assert.Equal(t, blocks[i].Hash, fetchedBlock.Hash)
  54. assert.Equal(t, blocks[i].Timestamp.Unix(), fetchedBlock.Timestamp.Unix())
  55. }
  56. }
  57. func TestBids(t *testing.T) {
  58. const fromBlock uint64 = 1
  59. const toBlock uint64 = 5
  60. const bidsPerSlot = 5
  61. // Prepare blocks in the DB
  62. setTestBlocks(fromBlock, toBlock)
  63. // Generate fake bids
  64. bids := make([]common.Bid, 0, (toBlock-fromBlock)*bidsPerSlot)
  65. for i := fromBlock; i < toBlock; i++ {
  66. for j := 0; j < bidsPerSlot; j++ {
  67. bids = append(bids, common.Bid{
  68. SlotNum: common.SlotNum(i),
  69. BidValue: big.NewInt(int64(j)),
  70. EthBlockNum: i,
  71. ForgerAddr: eth.BigToAddress(big.NewInt(int64(j))),
  72. })
  73. }
  74. }
  75. err := historyDB.addBids(bids)
  76. assert.NoError(t, err)
  77. // Fetch bids
  78. fetchedBidsPtr, err := historyDB.GetBidsByBlock(fromBlock, toBlock)
  79. assert.NoError(t, err)
  80. // Compare fetched bids vs generated bids
  81. fetchedBids := make([]common.Bid, 0, (toBlock-fromBlock)*bidsPerSlot)
  82. for _, bid := range fetchedBidsPtr {
  83. fetchedBids = append(fetchedBids, *bid)
  84. }
  85. assert.Equal(t, bids, fetchedBids)
  86. }
  87. // setTestBlocks WARNING: this will delete the blocks and recreate them
  88. func setTestBlocks(from, to uint64) {
  89. if from == 0 {
  90. if err := historyDB.reorg(from); err != nil {
  91. panic(err)
  92. }
  93. } else {
  94. if err := historyDB.reorg(from - 1); err != nil {
  95. panic(err)
  96. }
  97. }
  98. blocks := genBlocks(from, to)
  99. if err := historyDB.addBlocks(blocks); err != nil {
  100. panic(err)
  101. }
  102. }
  103. func genBlocks(from, to uint64) []common.Block {
  104. var blocks []common.Block
  105. for i := from; i < to; i++ {
  106. blocks = append(blocks, common.Block{
  107. EthBlockNum: i,
  108. Timestamp: time.Now().Add(time.Second * 13).UTC(),
  109. Hash: eth.BigToHash(big.NewInt(int64(i))),
  110. })
  111. }
  112. return blocks
  113. }