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.

440 lines
14 KiB

Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
  1. package historydb
  2. import (
  3. "math"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. dbUtils "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/log"
  10. "github.com/hermeznetwork/hermez-node/test"
  11. "github.com/hermeznetwork/hermez-node/test/til"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  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. pass := os.Getenv("POSTGRES_PASS")
  26. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  27. if err != nil {
  28. panic(err)
  29. }
  30. historyDB = NewHistoryDB(db)
  31. if err != nil {
  32. panic(err)
  33. }
  34. // Run tests
  35. result := m.Run()
  36. // Close DB
  37. if err := db.Close(); err != nil {
  38. log.Error("Error closing the history DB:", err)
  39. }
  40. os.Exit(result)
  41. }
  42. func TestBlocks(t *testing.T) {
  43. var fromBlock, toBlock int64
  44. fromBlock = 1
  45. toBlock = 5
  46. // Delete peviously created rows (clean previous test execs)
  47. test.WipeDB(historyDB.DB())
  48. // Generate fake blocks
  49. blocks := test.GenBlocks(fromBlock, toBlock)
  50. // Insert blocks into DB
  51. for i := 0; i < len(blocks); i++ {
  52. err := historyDB.AddBlock(&blocks[i])
  53. assert.NoError(t, err)
  54. }
  55. // Get all blocks from DB
  56. fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
  57. assert.Equal(t, len(blocks), len(fetchedBlocks))
  58. // Compare generated vs getted blocks
  59. assert.NoError(t, err)
  60. for i := range fetchedBlocks {
  61. assertEqualBlock(t, &blocks[i], &fetchedBlocks[i])
  62. }
  63. // Get blocks from the DB one by one
  64. for i := fromBlock; i < toBlock; i++ {
  65. fetchedBlock, err := historyDB.GetBlock(i)
  66. assert.NoError(t, err)
  67. assertEqualBlock(t, &blocks[i-1], fetchedBlock)
  68. }
  69. // Get last block
  70. lastBlock, err := historyDB.GetLastBlock()
  71. assert.NoError(t, err)
  72. assertEqualBlock(t, &blocks[len(blocks)-1], lastBlock)
  73. }
  74. func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
  75. assert.Equal(t, expected.EthBlockNum, actual.EthBlockNum)
  76. assert.Equal(t, expected.Hash, actual.Hash)
  77. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  78. }
  79. func TestBatches(t *testing.T) {
  80. const fromBlock int64 = 1
  81. const toBlock int64 = 3
  82. // Prepare blocks in the DB
  83. blocks := setTestBlocks(fromBlock, toBlock)
  84. // Generate fake batches
  85. const nBatches = 9
  86. batches := test.GenBatches(nBatches, blocks)
  87. // Test GetLastL1TxsNum with no batches
  88. fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
  89. assert.NoError(t, err)
  90. assert.Nil(t, fetchedLastL1TxsNum)
  91. // Add batches to the DB
  92. err = historyDB.AddBatches(batches)
  93. assert.NoError(t, err)
  94. // Get batches from the DB
  95. fetchedBatches, err := historyDB.GetBatches(0, common.BatchNum(nBatches))
  96. assert.NoError(t, err)
  97. for i, fetchedBatch := range fetchedBatches {
  98. assert.Equal(t, batches[i], fetchedBatch)
  99. }
  100. // Test GetLastBatchNum
  101. fetchedLastBatchNum, err := historyDB.GetLastBatchNum()
  102. assert.NoError(t, err)
  103. assert.Equal(t, batches[len(batches)-1].BatchNum, fetchedLastBatchNum)
  104. // Test GetLastL1TxsNum
  105. fetchedLastL1TxsNum, err = historyDB.GetLastL1TxsNum()
  106. assert.NoError(t, err)
  107. assert.Equal(t, *batches[nBatches-1].ForgeL1TxsNum, *fetchedLastL1TxsNum)
  108. // Test total fee
  109. // Generate fake tokens
  110. const nTokens = 5
  111. tokens, ethToken := test.GenTokens(nTokens, blocks)
  112. err = historyDB.AddTokens(tokens)
  113. assert.NoError(t, err)
  114. tokens = append([]common.Token{ethToken}, tokens...)
  115. feeBatch := batches[0]
  116. feeBatch.BatchNum = 9999
  117. feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
  118. var total float64
  119. for i, token := range tokens {
  120. value := 3.019237 * float64(i)
  121. assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
  122. bigAmount := big.NewInt(345000000)
  123. feeBatch.CollectedFees[token.TokenID] = bigAmount
  124. f := new(big.Float).SetInt(bigAmount)
  125. amount, _ := f.Float64()
  126. total += value * (amount / math.Pow(10, float64(token.Decimals)))
  127. }
  128. err = historyDB.AddBatch(&feeBatch)
  129. assert.NoError(t, err)
  130. fetchedBatches, err = historyDB.GetBatches(feeBatch.BatchNum-1, feeBatch.BatchNum+1)
  131. assert.NoError(t, err)
  132. for _, fetchedBatch := range fetchedBatches {
  133. if fetchedBatch.BatchNum == feeBatch.BatchNum {
  134. assert.Equal(t, total, *fetchedBatch.TotalFeesUSD)
  135. }
  136. }
  137. }
  138. func TestBids(t *testing.T) {
  139. const fromBlock int64 = 1
  140. const toBlock int64 = 5
  141. // Prepare blocks in the DB
  142. blocks := setTestBlocks(fromBlock, toBlock)
  143. // Generate fake coordinators
  144. const nCoords = 5
  145. coords := test.GenCoordinators(nCoords, blocks)
  146. err := historyDB.AddCoordinators(coords)
  147. assert.NoError(t, err)
  148. // Generate fake bids
  149. const nBids = 20
  150. bids := test.GenBids(nBids, blocks, coords)
  151. err = historyDB.AddBids(bids)
  152. assert.NoError(t, err)
  153. // Fetch bids
  154. fetchedBids, err := historyDB.GetAllBids()
  155. assert.NoError(t, err)
  156. // Compare fetched bids vs generated bids
  157. for i, bid := range fetchedBids {
  158. assert.Equal(t, bids[i], bid)
  159. }
  160. }
  161. func TestTokens(t *testing.T) {
  162. const fromBlock int64 = 1
  163. const toBlock int64 = 5
  164. // Prepare blocks in the DB
  165. blocks := setTestBlocks(fromBlock, toBlock)
  166. // Generate fake tokens
  167. const nTokens = 5
  168. tokens, ethToken := test.GenTokens(nTokens, blocks)
  169. err := historyDB.AddTokens(tokens)
  170. assert.NoError(t, err)
  171. tokens = append([]common.Token{ethToken}, tokens...)
  172. limit := uint(10)
  173. // Fetch tokens6
  174. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  175. assert.NoError(t, err)
  176. // Compare fetched tokens vs generated tokens
  177. // All the tokens should have USDUpdate setted by the DB trigger
  178. for i, token := range fetchedTokens {
  179. assert.Equal(t, tokens[i].TokenID, token.TokenID)
  180. assert.Equal(t, tokens[i].EthBlockNum, token.EthBlockNum)
  181. assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
  182. assert.Equal(t, tokens[i].Name, token.Name)
  183. assert.Equal(t, tokens[i].Symbol, token.Symbol)
  184. assert.Nil(t, token.USD)
  185. assert.Nil(t, token.USDUpdate)
  186. }
  187. }
  188. func TestAccounts(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. tokens, ethToken := test.GenTokens(nTokens, blocks)
  196. err := historyDB.AddTokens(tokens)
  197. assert.NoError(t, err)
  198. tokens = append([]common.Token{ethToken}, tokens...)
  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, nil, batches)
  207. err = historyDB.AddAccounts(accs)
  208. assert.NoError(t, err)
  209. // Fetch accounts
  210. fetchedAccs, err := historyDB.GetAccounts()
  211. assert.NoError(t, err)
  212. // Compare fetched accounts vs generated accounts
  213. for i, acc := range fetchedAccs {
  214. accs[i].Balance = nil
  215. assert.Equal(t, accs[i], acc)
  216. }
  217. }
  218. func TestTxs(t *testing.T) {
  219. const fromBlock int64 = 1
  220. const toBlock int64 = 5
  221. // Prepare blocks in the DB
  222. blocks := setTestBlocks(fromBlock, toBlock)
  223. // Generate fake tokens
  224. const nTokens = 500
  225. tokens, ethToken := test.GenTokens(nTokens, blocks)
  226. err := historyDB.AddTokens(tokens)
  227. assert.NoError(t, err)
  228. tokens = append([]common.Token{ethToken}, tokens...)
  229. // Generate fake batches
  230. const nBatches = 10
  231. batches := test.GenBatches(nBatches, blocks)
  232. err = historyDB.AddBatches(batches)
  233. assert.NoError(t, err)
  234. // Generate fake accounts
  235. const nAccounts = 3
  236. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  237. err = historyDB.AddAccounts(accs)
  238. assert.NoError(t, err)
  239. /*
  240. Uncomment once the transaction generation is fixed
  241. !! test that batches that forge user L1s !!
  242. !! Missing tests to check that historic USD is not set if USDUpdate is too old (24h) !!
  243. // Generate fake L1 txs
  244. const nL1s = 64
  245. _, l1txs := test.GenL1Txs(256, nL1s, 0, nil, accs, tokens, blocks, batches)
  246. err = historyDB.AddL1Txs(l1txs)
  247. assert.NoError(t, err)
  248. // Generate fake L2 txs
  249. const nL2s = 2048 - nL1s
  250. _, l2txs := test.GenL2Txs(256, nL2s, 0, nil, accs, tokens, blocks, batches)
  251. err = historyDB.AddL2Txs(l2txs)
  252. assert.NoError(t, err)
  253. // Compare fetched txs vs generated txs.
  254. fetchAndAssertTxs(t, l1txs, l2txs)
  255. // Test trigger: L1 integrity
  256. // from_eth_addr can't be null
  257. l1txs[0].FromEthAddr = ethCommon.Address{}
  258. err = historyDB.AddL1Txs(l1txs)
  259. assert.Error(t, err)
  260. l1txs[0].FromEthAddr = ethCommon.BigToAddress(big.NewInt(int64(5)))
  261. // from_bjj can't be null
  262. l1txs[0].FromBJJ = nil
  263. err = historyDB.AddL1Txs(l1txs)
  264. assert.Error(t, err)
  265. privK := babyjub.NewRandPrivKey()
  266. l1txs[0].FromBJJ = privK.Public()
  267. // load_amount can't be null
  268. l1txs[0].LoadAmount = nil
  269. err = historyDB.AddL1Txs(l1txs)
  270. assert.Error(t, err)
  271. // Test trigger: L2 integrity
  272. // batch_num can't be null
  273. l2txs[0].BatchNum = 0
  274. err = historyDB.AddL2Txs(l2txs)
  275. assert.Error(t, err)
  276. l2txs[0].BatchNum = 1
  277. // nonce can't be null
  278. l2txs[0].Nonce = 0
  279. err = historyDB.AddL2Txs(l2txs)
  280. assert.Error(t, err)
  281. // Test trigger: forge L1 txs
  282. // add next batch to DB
  283. batchNum, toForgeL1TxsNum := test.GetNextToForgeNumAndBatch(batches)
  284. batch := batches[0]
  285. batch.BatchNum = batchNum
  286. batch.ForgeL1TxsNum = toForgeL1TxsNum
  287. assert.NoError(t, historyDB.AddBatch(&batch)) // This should update nL1s / 2 rows
  288. // Set batch num in txs that should have been marked as forged in the DB
  289. for i := 0; i < len(l1txs); i++ {
  290. fetchedTx, err := historyDB.GetTx(l1txs[i].TxID)
  291. assert.NoError(t, err)
  292. if l1txs[i].ToForgeL1TxsNum == toForgeL1TxsNum {
  293. assert.Equal(t, batchNum, *fetchedTx.BatchNum)
  294. } else {
  295. if fetchedTx.BatchNum != nil {
  296. assert.NotEqual(t, batchNum, *fetchedTx.BatchNum)
  297. }
  298. }
  299. }
  300. // Test helper functions for Synchronizer
  301. // GetLastTxsPosition
  302. expectedPosition := -1
  303. var choosenToForgeL1TxsNum int64 = -1
  304. for _, tx := range l1txs {
  305. if choosenToForgeL1TxsNum == -1 && tx.ToForgeL1TxsNum > 0 {
  306. choosenToForgeL1TxsNum = tx.ToForgeL1TxsNum
  307. expectedPosition = tx.Position
  308. } else if choosenToForgeL1TxsNum == tx.ToForgeL1TxsNum && expectedPosition < tx.Position {
  309. expectedPosition = tx.Position
  310. }
  311. }
  312. position, err := historyDB.GetLastTxsPosition(choosenToForgeL1TxsNum)
  313. assert.NoError(t, err)
  314. assert.Equal(t, expectedPosition, position)
  315. // GetL1UserTxs: not needed? tests were broken
  316. // txs, err := historyDB.GetL1UserTxs(2)
  317. // assert.NoError(t, err)
  318. // assert.NotZero(t, len(txs))
  319. // assert.NoError(t, err)
  320. // assert.Equal(t, 22, position)
  321. // // Test Update L1 TX Batch_num
  322. // assert.Equal(t, common.BatchNum(0), txs[0].BatchNum)
  323. // txs[0].BatchNum = common.BatchNum(1)
  324. // txs, err = historyDB.GetL1UserTxs(2)
  325. // assert.NoError(t, err)
  326. // assert.NotZero(t, len(txs))
  327. // assert.Equal(t, common.BatchNum(1), txs[0].BatchNum)
  328. */
  329. }
  330. /*
  331. func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
  332. for i := 0; i < len(l1txs); i++ {
  333. tx := l1txs[i].Tx()
  334. fmt.Println("ASDF", i, tx.TxID)
  335. fetchedTx, err := historyDB.GetTx(tx.TxID)
  336. require.NoError(t, err)
  337. test.AssertUSD(t, tx.USD, fetchedTx.USD)
  338. test.AssertUSD(t, tx.LoadAmountUSD, fetchedTx.LoadAmountUSD)
  339. assert.Equal(t, tx, fetchedTx)
  340. }
  341. for i := 0; i < len(l2txs); i++ {
  342. tx := l2txs[i].Tx()
  343. fetchedTx, err := historyDB.GetTx(tx.TxID)
  344. tx.TokenID = fetchedTx.TokenID
  345. assert.NoError(t, err)
  346. test.AssertUSD(t, fetchedTx.USD, tx.USD)
  347. test.AssertUSD(t, fetchedTx.FeeUSD, tx.FeeUSD)
  348. assert.Equal(t, tx, fetchedTx)
  349. }
  350. }
  351. */
  352. func TestExitTree(t *testing.T) {
  353. nBatches := 17
  354. blocks := setTestBlocks(1, 10)
  355. batches := test.GenBatches(nBatches, blocks)
  356. err := historyDB.AddBatches(batches)
  357. assert.NoError(t, err)
  358. const nTokens = 50
  359. tokens, ethToken := test.GenTokens(nTokens, blocks)
  360. err = historyDB.AddTokens(tokens)
  361. assert.NoError(t, err)
  362. tokens = append([]common.Token{ethToken}, tokens...)
  363. const nAccounts = 3
  364. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  365. assert.NoError(t, historyDB.AddAccounts(accs))
  366. exitTree := test.GenExitTree(nBatches, batches, accs)
  367. err = historyDB.AddExitTree(exitTree)
  368. assert.NoError(t, err)
  369. }
  370. func TestGetL1UserTxs(t *testing.T) {
  371. test.WipeDB(historyDB.DB())
  372. set := `
  373. Type: Blockchain
  374. AddToken(1)
  375. AddToken(2)
  376. AddToken(3)
  377. CreateAccountDeposit(1) A: 20
  378. CreateAccountDeposit(2) A: 20
  379. CreateAccountDeposit(1) B: 5
  380. CreateAccountDeposit(1) C: 5
  381. CreateAccountDeposit(1) D: 5
  382. > block
  383. `
  384. tc := til.NewContext(128)
  385. blocks, err := tc.GenerateBlocks(set)
  386. require.Nil(t, err)
  387. // Sanity check
  388. require.Equal(t, 1, len(blocks))
  389. require.Equal(t, 5, len(blocks[0].L1UserTxs))
  390. // fmt.Printf("DBG Blocks: %+v\n", blocks)
  391. toForgeL1TxsNum := int64(1)
  392. for i := range blocks {
  393. err = historyDB.AddBlockSCData(&blocks[i])
  394. require.Nil(t, err)
  395. }
  396. l1UserTxs, err := historyDB.GetL1UserTxs(toForgeL1TxsNum)
  397. require.Nil(t, err)
  398. assert.Equal(t, 5, len(l1UserTxs))
  399. assert.Equal(t, blocks[0].L1UserTxs, l1UserTxs)
  400. // No l1UserTxs for this toForgeL1TxsNum
  401. l1UserTxs, err = historyDB.GetL1UserTxs(2)
  402. require.Nil(t, err)
  403. assert.Equal(t, 0, len(l1UserTxs))
  404. }
  405. // setTestBlocks WARNING: this will delete the blocks and recreate them
  406. func setTestBlocks(from, to int64) []common.Block {
  407. test.WipeDB(historyDB.DB())
  408. blocks := test.GenBlocks(from, to)
  409. if err := historyDB.AddBlocks(blocks); err != nil {
  410. panic(err)
  411. }
  412. return blocks
  413. }