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.

446 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. "fmt"
  4. "math"
  5. "math/big"
  6. "os"
  7. "testing"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. dbUtils "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. "github.com/hermeznetwork/hermez-node/test"
  12. "github.com/hermeznetwork/hermez-node/test/til"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. var historyDB *HistoryDB
  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. func TestMain(m *testing.M) {
  25. // init DB
  26. pass := os.Getenv("POSTGRES_PASS")
  27. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  28. if err != nil {
  29. panic(err)
  30. }
  31. historyDB = NewHistoryDB(db)
  32. if err != nil {
  33. panic(err)
  34. }
  35. // Run tests
  36. result := m.Run()
  37. // Close DB
  38. if err := db.Close(); err != nil {
  39. log.Error("Error closing the history DB:", err)
  40. }
  41. os.Exit(result)
  42. }
  43. func TestBlocks(t *testing.T) {
  44. var fromBlock, toBlock int64
  45. fromBlock = 1
  46. toBlock = 5
  47. // Delete peviously created rows (clean previous test execs)
  48. assert.NoError(t, historyDB.Reorg(fromBlock-1))
  49. // Generate fake blocks
  50. blocks := test.GenBlocks(fromBlock, toBlock)
  51. // Insert blocks into DB
  52. for i := 0; i < len(blocks); i++ {
  53. err := historyDB.AddBlock(&blocks[i])
  54. assert.NoError(t, err)
  55. }
  56. // Get all blocks from DB
  57. fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
  58. assert.Equal(t, len(blocks), len(fetchedBlocks))
  59. // Compare generated vs getted blocks
  60. assert.NoError(t, err)
  61. for i := range fetchedBlocks {
  62. assertEqualBlock(t, &blocks[i], &fetchedBlocks[i])
  63. }
  64. // Get blocks from the DB one by one
  65. for i := fromBlock; i < toBlock; i++ {
  66. fetchedBlock, err := historyDB.GetBlock(i)
  67. assert.NoError(t, err)
  68. assertEqualBlock(t, &blocks[i-1], fetchedBlock)
  69. }
  70. // Get last block
  71. lastBlock, err := historyDB.GetLastBlock()
  72. assert.NoError(t, err)
  73. assertEqualBlock(t, &blocks[len(blocks)-1], lastBlock)
  74. }
  75. func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
  76. assert.Equal(t, expected.EthBlockNum, actual.EthBlockNum)
  77. assert.Equal(t, expected.Hash, actual.Hash)
  78. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  79. }
  80. func TestBatches(t *testing.T) {
  81. const fromBlock int64 = 1
  82. const toBlock int64 = 3
  83. // Prepare blocks in the DB
  84. blocks := setTestBlocks(fromBlock, toBlock)
  85. // Generate fake batches
  86. const nBatches = 9
  87. batches := test.GenBatches(nBatches, blocks)
  88. // Test GetLastL1TxsNum with no batches
  89. fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
  90. assert.NoError(t, err)
  91. assert.Nil(t, fetchedLastL1TxsNum)
  92. // Add batches to the DB
  93. err = historyDB.AddBatches(batches)
  94. assert.NoError(t, err)
  95. // Get batches from the DB
  96. fetchedBatches, err := historyDB.GetBatches(0, common.BatchNum(nBatches))
  97. assert.NoError(t, err)
  98. for i, fetchedBatch := range fetchedBatches {
  99. assert.Equal(t, batches[i], fetchedBatch)
  100. }
  101. // Test GetLastBatchNum
  102. fetchedLastBatchNum, err := historyDB.GetLastBatchNum()
  103. assert.NoError(t, err)
  104. assert.Equal(t, batches[len(batches)-1].BatchNum, fetchedLastBatchNum)
  105. // Test GetLastL1TxsNum
  106. fetchedLastL1TxsNum, err = historyDB.GetLastL1TxsNum()
  107. assert.NoError(t, err)
  108. assert.Equal(t, *batches[nBatches-1].ForgeL1TxsNum, *fetchedLastL1TxsNum)
  109. // Test total fee
  110. // Generate fake tokens
  111. const nTokens = 5
  112. tokens := test.GenTokens(nTokens, blocks)
  113. err = historyDB.AddTokens(tokens)
  114. assert.NoError(t, err)
  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.GetBids()
  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 := test.GenTokens(nTokens, blocks)
  169. err := historyDB.AddTokens(tokens)
  170. assert.NoError(t, err)
  171. limit := uint(10)
  172. // Fetch tokens6
  173. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  174. assert.NoError(t, err)
  175. // Compare fetched tokens vs generated tokens
  176. // All the tokens should have USDUpdate setted by the DB trigger
  177. for i, token := range fetchedTokens {
  178. assert.Equal(t, tokens[i].TokenID, token.TokenID)
  179. assert.Equal(t, tokens[i].EthBlockNum, token.EthBlockNum)
  180. assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
  181. assert.Equal(t, tokens[i].Name, token.Name)
  182. assert.Equal(t, tokens[i].Symbol, token.Symbol)
  183. assert.Nil(t, token.USD)
  184. assert.Nil(t, token.USDUpdate)
  185. }
  186. }
  187. func TestAccounts(t *testing.T) {
  188. const fromBlock int64 = 1
  189. const toBlock int64 = 5
  190. // Prepare blocks in the DB
  191. blocks := setTestBlocks(fromBlock, toBlock)
  192. // Generate fake tokens
  193. const nTokens = 5
  194. tokens := test.GenTokens(nTokens, blocks)
  195. err := historyDB.AddTokens(tokens)
  196. assert.NoError(t, err)
  197. // Generate fake batches
  198. const nBatches = 10
  199. batches := test.GenBatches(nBatches, blocks)
  200. err = historyDB.AddBatches(batches)
  201. assert.NoError(t, err)
  202. // Generate fake accounts
  203. const nAccounts = 3
  204. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  205. err = historyDB.AddAccounts(accs)
  206. assert.NoError(t, err)
  207. // Fetch accounts
  208. fetchedAccs, err := historyDB.GetAccounts()
  209. assert.NoError(t, err)
  210. // Compare fetched accounts vs generated accounts
  211. for i, acc := range fetchedAccs {
  212. accs[i].Balance = nil
  213. assert.Equal(t, accs[i], acc)
  214. }
  215. }
  216. func TestTxs(t *testing.T) {
  217. const fromBlock int64 = 1
  218. const toBlock int64 = 5
  219. // Prepare blocks in the DB
  220. blocks := setTestBlocks(fromBlock, toBlock)
  221. // Generate fake tokens
  222. const nTokens = 500
  223. tokens := test.GenTokens(nTokens, blocks)
  224. err := historyDB.AddTokens(tokens)
  225. assert.NoError(t, err)
  226. // Generate fake batches
  227. const nBatches = 10
  228. batches := test.GenBatches(nBatches, blocks)
  229. err = historyDB.AddBatches(batches)
  230. assert.NoError(t, err)
  231. // Generate fake accounts
  232. const nAccounts = 3
  233. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  234. err = historyDB.AddAccounts(accs)
  235. assert.NoError(t, err)
  236. /*
  237. Uncomment once the transaction generation is fixed
  238. !! test that batches that forge user L1s !!
  239. !! Missing tests to check that historic USD is not set if USDUpdate is too old (24h) !!
  240. // Generate fake L1 txs
  241. const nL1s = 64
  242. _, l1txs := test.GenL1Txs(256, nL1s, 0, nil, accs, tokens, blocks, batches)
  243. err = historyDB.AddL1Txs(l1txs)
  244. assert.NoError(t, err)
  245. // Generate fake L2 txs
  246. const nL2s = 2048 - nL1s
  247. _, l2txs := test.GenL2Txs(256, nL2s, 0, nil, accs, tokens, blocks, batches)
  248. err = historyDB.AddL2Txs(l2txs)
  249. assert.NoError(t, err)
  250. // Compare fetched txs vs generated txs.
  251. fetchAndAssertTxs(t, l1txs, l2txs)
  252. // Test trigger: L1 integrity
  253. // from_eth_addr can't be null
  254. l1txs[0].FromEthAddr = ethCommon.Address{}
  255. err = historyDB.AddL1Txs(l1txs)
  256. assert.Error(t, err)
  257. l1txs[0].FromEthAddr = ethCommon.BigToAddress(big.NewInt(int64(5)))
  258. // from_bjj can't be null
  259. l1txs[0].FromBJJ = nil
  260. err = historyDB.AddL1Txs(l1txs)
  261. assert.Error(t, err)
  262. privK := babyjub.NewRandPrivKey()
  263. l1txs[0].FromBJJ = privK.Public()
  264. // load_amount can't be null
  265. l1txs[0].LoadAmount = nil
  266. err = historyDB.AddL1Txs(l1txs)
  267. assert.Error(t, err)
  268. // Test trigger: L2 integrity
  269. // batch_num can't be null
  270. l2txs[0].BatchNum = 0
  271. err = historyDB.AddL2Txs(l2txs)
  272. assert.Error(t, err)
  273. l2txs[0].BatchNum = 1
  274. // nonce can't be null
  275. l2txs[0].Nonce = 0
  276. err = historyDB.AddL2Txs(l2txs)
  277. assert.Error(t, err)
  278. // Test trigger: forge L1 txs
  279. // add next batch to DB
  280. batchNum, toForgeL1TxsNum := test.GetNextToForgeNumAndBatch(batches)
  281. batch := batches[0]
  282. batch.BatchNum = batchNum
  283. batch.ForgeL1TxsNum = toForgeL1TxsNum
  284. assert.NoError(t, historyDB.AddBatch(&batch)) // This should update nL1s / 2 rows
  285. // Set batch num in txs that should have been marked as forged in the DB
  286. for i := 0; i < len(l1txs); i++ {
  287. fetchedTx, err := historyDB.GetTx(l1txs[i].TxID)
  288. assert.NoError(t, err)
  289. if l1txs[i].ToForgeL1TxsNum == toForgeL1TxsNum {
  290. assert.Equal(t, batchNum, *fetchedTx.BatchNum)
  291. } else {
  292. if fetchedTx.BatchNum != nil {
  293. assert.NotEqual(t, batchNum, *fetchedTx.BatchNum)
  294. }
  295. }
  296. }
  297. // Test helper functions for Synchronizer
  298. // GetLastTxsPosition
  299. expectedPosition := -1
  300. var choosenToForgeL1TxsNum int64 = -1
  301. for _, tx := range l1txs {
  302. if choosenToForgeL1TxsNum == -1 && tx.ToForgeL1TxsNum > 0 {
  303. choosenToForgeL1TxsNum = tx.ToForgeL1TxsNum
  304. expectedPosition = tx.Position
  305. } else if choosenToForgeL1TxsNum == tx.ToForgeL1TxsNum && expectedPosition < tx.Position {
  306. expectedPosition = tx.Position
  307. }
  308. }
  309. position, err := historyDB.GetLastTxsPosition(choosenToForgeL1TxsNum)
  310. assert.NoError(t, err)
  311. assert.Equal(t, expectedPosition, position)
  312. // GetL1UserTxs: not needed? tests were broken
  313. // txs, err := historyDB.GetL1UserTxs(2)
  314. // assert.NoError(t, err)
  315. // assert.NotZero(t, len(txs))
  316. // assert.NoError(t, err)
  317. // assert.Equal(t, 22, position)
  318. // // Test Update L1 TX Batch_num
  319. // assert.Equal(t, common.BatchNum(0), txs[0].BatchNum)
  320. // txs[0].BatchNum = common.BatchNum(1)
  321. // txs, err = historyDB.GetL1UserTxs(2)
  322. // assert.NoError(t, err)
  323. // assert.NotZero(t, len(txs))
  324. // assert.Equal(t, common.BatchNum(1), txs[0].BatchNum)
  325. */
  326. }
  327. /*
  328. func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
  329. for i := 0; i < len(l1txs); i++ {
  330. tx := l1txs[i].Tx()
  331. fmt.Println("ASDF", i, tx.TxID)
  332. fetchedTx, err := historyDB.GetTx(tx.TxID)
  333. require.NoError(t, err)
  334. test.AssertUSD(t, tx.USD, fetchedTx.USD)
  335. test.AssertUSD(t, tx.LoadAmountUSD, fetchedTx.LoadAmountUSD)
  336. assert.Equal(t, tx, fetchedTx)
  337. }
  338. for i := 0; i < len(l2txs); i++ {
  339. tx := l2txs[i].Tx()
  340. fetchedTx, err := historyDB.GetTx(tx.TxID)
  341. tx.TokenID = fetchedTx.TokenID
  342. assert.NoError(t, err)
  343. test.AssertUSD(t, fetchedTx.USD, tx.USD)
  344. test.AssertUSD(t, fetchedTx.FeeUSD, tx.FeeUSD)
  345. assert.Equal(t, tx, fetchedTx)
  346. }
  347. }
  348. */
  349. func TestExitTree(t *testing.T) {
  350. nBatches := 17
  351. blocks := setTestBlocks(0, 10)
  352. batches := test.GenBatches(nBatches, blocks)
  353. err := historyDB.AddBatches(batches)
  354. const nTokens = 50
  355. tokens := test.GenTokens(nTokens, blocks)
  356. assert.NoError(t, historyDB.AddTokens(tokens))
  357. assert.NoError(t, err)
  358. const nAccounts = 3
  359. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  360. assert.NoError(t, historyDB.AddAccounts(accs))
  361. exitTree := test.GenExitTree(nBatches, batches, accs)
  362. err = historyDB.AddExitTree(exitTree)
  363. assert.NoError(t, err)
  364. }
  365. func TestGetL1UserTxs(t *testing.T) {
  366. require.NoError(t, cleanHistoryDB())
  367. set := `
  368. Type: Blockchain
  369. AddToken(1)
  370. AddToken(2)
  371. AddToken(3)
  372. CreateAccountDeposit(1) A: 20
  373. CreateAccountDeposit(2) A: 20
  374. CreateAccountDeposit(1) B: 5
  375. CreateAccountDeposit(1) C: 5
  376. CreateAccountDeposit(1) D: 5
  377. > block
  378. `
  379. tc := til.NewContext(128)
  380. blocks, err := tc.GenerateBlocks(set)
  381. require.Nil(t, err)
  382. // Sanity check
  383. require.Equal(t, 1, len(blocks))
  384. require.Equal(t, 5, len(blocks[0].L1UserTxs))
  385. // fmt.Printf("DBG Blocks: %+v\n", blocks)
  386. toForgeL1TxsNum := int64(1)
  387. for i := range blocks {
  388. fmt.Printf("DBG %+v\n", blocks[i])
  389. // fmt.Printf("DBG Batches %+v\n", blocks[i].Batches)
  390. // for _, l1Tx := range blocks[i].L1UserTxs {
  391. // fmt.Printf("DBG l1UserTx %+v\n", l1Tx)
  392. // }
  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. if err := cleanHistoryDB(); err != nil {
  408. panic(err)
  409. }
  410. blocks := test.GenBlocks(from, to)
  411. if err := historyDB.AddBlocks(blocks); err != nil {
  412. panic(err)
  413. }
  414. return blocks
  415. }
  416. func cleanHistoryDB() error {
  417. return historyDB.Reorg(-1)
  418. }