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