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.

321 lines
9.7 KiB

4 years ago
  1. package historydb
  2. import (
  3. "fmt"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "time"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/hermeznetwork/hermez-node/test"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. var historyDB *HistoryDB
  15. // In order to run the test you need to run a Posgres DB with
  16. // a database named "history" that is accessible by
  17. // user: "hermez"
  18. // pass: set it using the env var POSTGRES_PASS
  19. // 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;"
  20. // After running the test you can stop the container by running: sudo docker kill hermez-db-test
  21. // If you already did that for the L2DB you don't have to do it again
  22. func TestMain(m *testing.M) {
  23. // init DB
  24. var err error
  25. pass := os.Getenv("POSTGRES_PASS")
  26. historyDB, err = NewHistoryDB(5432, "localhost", "hermez", pass, "history")
  27. if err != nil {
  28. panic(err)
  29. }
  30. // Run tests
  31. result := m.Run()
  32. // Close DB
  33. if err := historyDB.Close(); err != nil {
  34. fmt.Println("Error closing the history DB:", err)
  35. }
  36. os.Exit(result)
  37. }
  38. func TestBlocks(t *testing.T) {
  39. var fromBlock, toBlock int64
  40. fromBlock = 1
  41. toBlock = 5
  42. // Delete peviously created rows (clean previous test execs)
  43. assert.NoError(t, historyDB.Reorg(fromBlock-1))
  44. // Generate fake blocks
  45. blocks := test.GenBlocks(fromBlock, toBlock)
  46. // Insert blocks into DB
  47. for i := 0; i < len(blocks); i++ {
  48. err := historyDB.AddBlock(&blocks[i])
  49. assert.NoError(t, err)
  50. }
  51. // Get all blocks from DB
  52. fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
  53. assert.Equal(t, len(blocks), len(fetchedBlocks))
  54. // Compare generated vs getted blocks
  55. assert.NoError(t, err)
  56. for i, fetchedBlock := range fetchedBlocks {
  57. assertEqualBlock(t, &blocks[i], fetchedBlock)
  58. }
  59. // Get blocks from the DB one by one
  60. for i := fromBlock; i < toBlock; i++ {
  61. fetchedBlock, err := historyDB.GetBlock(i)
  62. assert.NoError(t, err)
  63. assertEqualBlock(t, &blocks[i-1], fetchedBlock)
  64. }
  65. // Get last block
  66. lastBlock, err := historyDB.GetLastBlock()
  67. assert.NoError(t, err)
  68. assertEqualBlock(t, &blocks[len(blocks)-1], lastBlock)
  69. }
  70. func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
  71. assert.Equal(t, expected.EthBlockNum, actual.EthBlockNum)
  72. assert.Equal(t, expected.Hash, actual.Hash)
  73. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  74. }
  75. func TestBatches(t *testing.T) {
  76. const fromBlock int64 = 1
  77. const toBlock int64 = 3
  78. // Prepare blocks in the DB
  79. blocks := setTestBlocks(fromBlock, toBlock)
  80. // Generate fake batches
  81. const nBatches = 9
  82. batches := test.GenBatches(nBatches, blocks)
  83. // Test GetLastL1TxsNum with no batches
  84. fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
  85. assert.NoError(t, err)
  86. assert.Nil(t, fetchedLastL1TxsNum)
  87. // Add batches to the DB
  88. err = historyDB.AddBatches(batches)
  89. assert.NoError(t, err)
  90. // Get batches from the DB
  91. fetchedBatches, err := historyDB.GetBatches(0, common.BatchNum(nBatches))
  92. assert.NoError(t, err)
  93. for i, fetchedBatch := range fetchedBatches {
  94. assert.Equal(t, batches[i], *fetchedBatch)
  95. }
  96. // Test GetLastBatchNum
  97. fetchedLastBatchNum, err := historyDB.GetLastBatchNum()
  98. assert.NoError(t, err)
  99. assert.Equal(t, batches[len(batches)-1].BatchNum, fetchedLastBatchNum)
  100. // Test GetLastL1TxsNum
  101. fetchedLastL1TxsNum, err = historyDB.GetLastL1TxsNum()
  102. assert.NoError(t, err)
  103. assert.Equal(t, batches[nBatches-1].ForgeL1TxsNum, *fetchedLastL1TxsNum)
  104. }
  105. func TestBids(t *testing.T) {
  106. const fromBlock int64 = 1
  107. const toBlock int64 = 5
  108. // Prepare blocks in the DB
  109. blocks := setTestBlocks(fromBlock, toBlock)
  110. // Generate fake coordinators
  111. const nCoords = 5
  112. coords := test.GenCoordinators(nCoords, blocks)
  113. // Generate fake bids
  114. const nBids = 20
  115. bids := test.GenBids(nBids, blocks, coords)
  116. err := historyDB.addBids(bids)
  117. assert.NoError(t, err)
  118. // Fetch bids
  119. fetchedBids, err := historyDB.GetBids()
  120. assert.NoError(t, err)
  121. // Compare fetched bids vs generated bids
  122. for i, bid := range fetchedBids {
  123. assert.Equal(t, bids[i], *bid)
  124. }
  125. }
  126. func TestTokens(t *testing.T) {
  127. const fromBlock int64 = 1
  128. const toBlock int64 = 5
  129. // Prepare blocks in the DB
  130. blocks := setTestBlocks(fromBlock, toBlock)
  131. // Generate fake tokens
  132. const nTokens = 5
  133. tokens := test.GenTokens(nTokens, blocks)
  134. err := historyDB.AddTokens(tokens)
  135. assert.NoError(t, err)
  136. // Update price of generated tokens without price
  137. for i := 0; i < len(tokens); i++ {
  138. if tokens[i].USD == 0 {
  139. value := 3.33 + float64(i)
  140. tokens[i].USD = value
  141. err := historyDB.UpdateTokenValue(tokens[i].TokenID, value)
  142. assert.NoError(t, err)
  143. }
  144. }
  145. // Fetch tokens
  146. fetchedTokens, err := historyDB.GetTokens()
  147. assert.NoError(t, err)
  148. // Compare fetched tokens vs generated tokens
  149. // All the tokens should have USDUpdate setted by the DB trigger
  150. for i, token := range fetchedTokens {
  151. assert.Equal(t, tokens[i].TokenID, token.TokenID)
  152. assert.Equal(t, tokens[i].EthBlockNum, token.EthBlockNum)
  153. assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
  154. assert.Equal(t, tokens[i].Name, token.Name)
  155. assert.Equal(t, tokens[i].Symbol, token.Symbol)
  156. assert.Equal(t, tokens[i].USD, token.USD)
  157. assert.Greater(t, int64(1*time.Second), int64(time.Since(token.USDUpdate)))
  158. }
  159. }
  160. func TestAccounts(t *testing.T) {
  161. const fromBlock int64 = 1
  162. const toBlock int64 = 5
  163. // Prepare blocks in the DB
  164. blocks := setTestBlocks(fromBlock, toBlock)
  165. // Generate fake tokens
  166. const nTokens = 5
  167. tokens := test.GenTokens(nTokens, blocks)
  168. err := historyDB.AddTokens(tokens)
  169. assert.NoError(t, err)
  170. // Generate fake batches
  171. const nBatches = 10
  172. batches := test.GenBatches(nBatches, blocks)
  173. err = historyDB.AddBatches(batches)
  174. assert.NoError(t, err)
  175. // Generate fake accounts
  176. const nAccounts = 3
  177. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  178. err = historyDB.AddAccounts(accs)
  179. assert.NoError(t, err)
  180. // Fetch accounts
  181. fetchedAccs, err := historyDB.GetAccounts()
  182. assert.NoError(t, err)
  183. // Compare fetched accounts vs generated accounts
  184. for i, acc := range fetchedAccs {
  185. assert.Equal(t, accs[i], *acc)
  186. }
  187. }
  188. func TestTxs(t *testing.T) {
  189. const fromBlock int64 = 1
  190. const toBlock int64 = 5
  191. // Prepare blocks in the DB
  192. blocks := setTestBlocks(fromBlock, toBlock)
  193. // Generate fake tokens
  194. const nTokens = 5
  195. const tokenValue = 1.23456
  196. tokens := test.GenTokens(nTokens, blocks)
  197. for i := 0; i < len(tokens); i++ {
  198. tokens[i].USD = tokenValue
  199. }
  200. err := historyDB.AddTokens(tokens)
  201. assert.NoError(t, err)
  202. // Generate fake batches
  203. const nBatches = 10
  204. batches := test.GenBatches(nBatches, blocks)
  205. err = historyDB.AddBatches(batches)
  206. assert.NoError(t, err)
  207. // Generate fake accounts
  208. const nAccounts = 3
  209. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  210. err = historyDB.AddAccounts(accs)
  211. assert.NoError(t, err)
  212. // Generate fake L1 txs
  213. const nL1s = 30
  214. _, l1txs := test.GenL1Txs(0, nL1s, 0, nil, accs, tokens, blocks, batches)
  215. err = historyDB.AddL1Txs(l1txs)
  216. assert.NoError(t, err)
  217. // Generate fake L2 txs
  218. const nL2s = 20
  219. _, l2txs := test.GenL2Txs(0, nL2s, 0, nil, accs, tokens, blocks, batches)
  220. err = historyDB.AddL2Txs(l2txs)
  221. assert.NoError(t, err)
  222. // Compare fetched txs vs generated txs.
  223. for i := 0; i < len(l1txs); i++ {
  224. tx := l1txs[i].Tx()
  225. fetchedTx, err := historyDB.GetTx(tx.TxID)
  226. assert.NoError(t, err)
  227. tx.USD = tokenValue * tx.AmountFloat
  228. if fetchedTx.USD > tx.USD {
  229. assert.Less(t, 0.999, tx.USD/fetchedTx.USD)
  230. } else {
  231. assert.Less(t, 0.999, fetchedTx.USD/tx.USD)
  232. }
  233. tx.LoadAmountUSD = tokenValue * tx.LoadAmountFloat
  234. if fetchedTx.LoadAmountUSD > tx.LoadAmountUSD {
  235. assert.Less(t, 0.999, tx.LoadAmountUSD/fetchedTx.LoadAmountUSD)
  236. } else {
  237. assert.Less(t, 0.999, fetchedTx.LoadAmountUSD/tx.LoadAmountUSD)
  238. }
  239. tx.LoadAmountUSD = 0
  240. tx.USD = 0
  241. fetchedTx.LoadAmountUSD = 0
  242. fetchedTx.USD = 0
  243. assert.Equal(t, tx, fetchedTx)
  244. }
  245. for i := 0; i < len(l2txs); i++ {
  246. tx := l2txs[i].Tx()
  247. fetchedTx, err := historyDB.GetTx(tx.TxID)
  248. assert.NoError(t, err)
  249. tx.USD = tokenValue * tx.AmountFloat
  250. if fetchedTx.USD > tx.USD {
  251. assert.Less(t, 0.999, tx.USD/fetchedTx.USD)
  252. } else {
  253. assert.Less(t, 0.999, fetchedTx.USD/tx.USD)
  254. }
  255. tx.FeeUSD = tx.USD * tx.Fee.Percentage()
  256. if fetchedTx.FeeUSD > tx.FeeUSD {
  257. assert.Less(t, 0.999, tx.FeeUSD/fetchedTx.FeeUSD)
  258. } else if fetchedTx.FeeUSD < tx.FeeUSD {
  259. assert.Less(t, 0.999, fetchedTx.FeeUSD/tx.FeeUSD)
  260. }
  261. tx.FeeUSD = 0
  262. tx.USD = 0
  263. fetchedTx.FeeUSD = 0
  264. fetchedTx.USD = 0
  265. assert.Equal(t, tx, fetchedTx)
  266. }
  267. // Test trigger: L1 integrity
  268. // from_eth_addr can't be null
  269. l1txs[0].FromEthAddr = ethCommon.Address{}
  270. err = historyDB.AddL1Txs(l1txs)
  271. assert.Error(t, err)
  272. l1txs[0].FromEthAddr = ethCommon.BigToAddress(big.NewInt(int64(5)))
  273. // from_bjj can't be null
  274. l1txs[0].FromBJJ = nil
  275. err = historyDB.AddL1Txs(l1txs)
  276. assert.Error(t, err)
  277. privK := babyjub.NewRandPrivKey()
  278. l1txs[0].FromBJJ = privK.Public()
  279. // load_amount can't be null
  280. l1txs[0].LoadAmount = nil
  281. err = historyDB.AddL1Txs(l1txs)
  282. assert.Error(t, err)
  283. // Test trigger: L2 integrity
  284. // batch_num can't be null
  285. l2txs[0].BatchNum = 0
  286. err = historyDB.AddL2Txs(l2txs)
  287. assert.Error(t, err)
  288. l2txs[0].BatchNum = 1
  289. // nonce can't be null
  290. l2txs[0].Nonce = 0
  291. err = historyDB.AddL2Txs(l2txs)
  292. assert.Error(t, err)
  293. }
  294. // setTestBlocks WARNING: this will delete the blocks and recreate them
  295. func setTestBlocks(from, to int64) []common.Block {
  296. if err := cleanHistoryDB(); err != nil {
  297. panic(err)
  298. }
  299. blocks := test.GenBlocks(from, to)
  300. if err := historyDB.AddBlocks(blocks); err != nil {
  301. panic(err)
  302. }
  303. return blocks
  304. }
  305. func cleanHistoryDB() error {
  306. return historyDB.Reorg(0)
  307. }