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.

326 lines
9.8 KiB

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