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
  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. assert.NoError(t, historyDB.Reorg(fromBlock-1))
  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 := test.GenTokens(nTokens, blocks)
  112. err = historyDB.AddTokens(tokens)
  113. assert.NoError(t, err)
  114. feeBatch := batches[0]
  115. feeBatch.BatchNum = 9999
  116. feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
  117. var total float64
  118. for i, token := range tokens {
  119. value := 3.019237 * float64(i)
  120. assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
  121. bigAmount := big.NewInt(345000000)
  122. feeBatch.CollectedFees[token.TokenID] = bigAmount
  123. f := new(big.Float).SetInt(bigAmount)
  124. amount, _ := f.Float64()
  125. total += value * (amount / math.Pow(10, float64(token.Decimals)))
  126. }
  127. err = historyDB.AddBatch(&feeBatch)
  128. assert.NoError(t, err)
  129. fetchedBatches, err = historyDB.GetBatches(feeBatch.BatchNum-1, feeBatch.BatchNum+1)
  130. assert.NoError(t, err)
  131. for _, fetchedBatch := range fetchedBatches {
  132. if fetchedBatch.BatchNum == feeBatch.BatchNum {
  133. assert.Equal(t, total, *fetchedBatch.TotalFeesUSD)
  134. }
  135. }
  136. }
  137. func TestBids(t *testing.T) {
  138. const fromBlock int64 = 1
  139. const toBlock int64 = 5
  140. // Prepare blocks in the DB
  141. blocks := setTestBlocks(fromBlock, toBlock)
  142. // Generate fake coordinators
  143. const nCoords = 5
  144. coords := test.GenCoordinators(nCoords, blocks)
  145. err := historyDB.AddCoordinators(coords)
  146. assert.NoError(t, err)
  147. // Generate fake bids
  148. const nBids = 20
  149. bids := test.GenBids(nBids, blocks, coords)
  150. err = historyDB.AddBids(bids)
  151. assert.NoError(t, err)
  152. // Fetch bids
  153. fetchedBids, err := historyDB.GetBids()
  154. assert.NoError(t, err)
  155. // Compare fetched bids vs generated bids
  156. for i, bid := range fetchedBids {
  157. assert.Equal(t, bids[i], bid)
  158. }
  159. }
  160. func TestTokens(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. limit := uint(10)
  171. // Fetch tokens6
  172. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  173. assert.NoError(t, err)
  174. // Compare fetched tokens vs generated tokens
  175. // All the tokens should have USDUpdate setted by the DB trigger
  176. for i, token := range fetchedTokens {
  177. assert.Equal(t, tokens[i].TokenID, token.TokenID)
  178. assert.Equal(t, tokens[i].EthBlockNum, token.EthBlockNum)
  179. assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
  180. assert.Equal(t, tokens[i].Name, token.Name)
  181. assert.Equal(t, tokens[i].Symbol, token.Symbol)
  182. assert.Nil(t, token.USD)
  183. assert.Nil(t, token.USDUpdate)
  184. }
  185. }
  186. func TestAccounts(t *testing.T) {
  187. const fromBlock int64 = 1
  188. const toBlock int64 = 5
  189. // Prepare blocks in the DB
  190. blocks := setTestBlocks(fromBlock, toBlock)
  191. // Generate fake tokens
  192. const nTokens = 5
  193. tokens := test.GenTokens(nTokens, blocks)
  194. err := historyDB.AddTokens(tokens)
  195. assert.NoError(t, err)
  196. // Generate fake batches
  197. const nBatches = 10
  198. batches := test.GenBatches(nBatches, blocks)
  199. err = historyDB.AddBatches(batches)
  200. assert.NoError(t, err)
  201. // Generate fake accounts
  202. const nAccounts = 3
  203. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  204. err = historyDB.AddAccounts(accs)
  205. assert.NoError(t, err)
  206. // Fetch accounts
  207. fetchedAccs, err := historyDB.GetAccounts()
  208. assert.NoError(t, err)
  209. // Compare fetched accounts vs generated accounts
  210. for i, acc := range fetchedAccs {
  211. accs[i].Balance = nil
  212. assert.Equal(t, accs[i], acc)
  213. }
  214. }
  215. func TestTxs(t *testing.T) {
  216. const fromBlock int64 = 1
  217. const toBlock int64 = 5
  218. // Prepare blocks in the DB
  219. blocks := setTestBlocks(fromBlock, toBlock)
  220. // Generate fake tokens
  221. const nTokens = 500
  222. tokens := test.GenTokens(nTokens, blocks)
  223. err := historyDB.AddTokens(tokens)
  224. assert.NoError(t, err)
  225. // Generate fake batches
  226. const nBatches = 10
  227. batches := test.GenBatches(nBatches, blocks)
  228. err = historyDB.AddBatches(batches)
  229. assert.NoError(t, err)
  230. // Generate fake accounts
  231. const nAccounts = 3
  232. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  233. err = historyDB.AddAccounts(accs)
  234. assert.NoError(t, err)
  235. /*
  236. Uncomment once the transaction generation is fixed
  237. !! test that batches that forge user L1s !!
  238. !! Missing tests to check that historic USD is not set if USDUpdate is too old (24h) !!
  239. // Generate fake L1 txs
  240. const nL1s = 64
  241. _, l1txs := test.GenL1Txs(256, nL1s, 0, nil, accs, tokens, blocks, batches)
  242. err = historyDB.AddL1Txs(l1txs)
  243. assert.NoError(t, err)
  244. // Generate fake L2 txs
  245. const nL2s = 2048 - nL1s
  246. _, l2txs := test.GenL2Txs(256, nL2s, 0, nil, accs, tokens, blocks, batches)
  247. err = historyDB.AddL2Txs(l2txs)
  248. assert.NoError(t, err)
  249. // Compare fetched txs vs generated txs.
  250. fetchAndAssertTxs(t, l1txs, l2txs)
  251. // Test trigger: L1 integrity
  252. // from_eth_addr can't be null
  253. l1txs[0].FromEthAddr = ethCommon.Address{}
  254. err = historyDB.AddL1Txs(l1txs)
  255. assert.Error(t, err)
  256. l1txs[0].FromEthAddr = ethCommon.BigToAddress(big.NewInt(int64(5)))
  257. // from_bjj can't be null
  258. l1txs[0].FromBJJ = nil
  259. err = historyDB.AddL1Txs(l1txs)
  260. assert.Error(t, err)
  261. privK := babyjub.NewRandPrivKey()
  262. l1txs[0].FromBJJ = privK.Public()
  263. // load_amount can't be null
  264. l1txs[0].LoadAmount = nil
  265. err = historyDB.AddL1Txs(l1txs)
  266. assert.Error(t, err)
  267. // Test trigger: L2 integrity
  268. // batch_num can't be null
  269. l2txs[0].BatchNum = 0
  270. err = historyDB.AddL2Txs(l2txs)
  271. assert.Error(t, err)
  272. l2txs[0].BatchNum = 1
  273. // nonce can't be null
  274. l2txs[0].Nonce = 0
  275. err = historyDB.AddL2Txs(l2txs)
  276. assert.Error(t, err)
  277. // Test trigger: forge L1 txs
  278. // add next batch to DB
  279. batchNum, toForgeL1TxsNum := test.GetNextToForgeNumAndBatch(batches)
  280. batch := batches[0]
  281. batch.BatchNum = batchNum
  282. batch.ForgeL1TxsNum = toForgeL1TxsNum
  283. assert.NoError(t, historyDB.AddBatch(&batch)) // This should update nL1s / 2 rows
  284. // Set batch num in txs that should have been marked as forged in the DB
  285. for i := 0; i < len(l1txs); i++ {
  286. fetchedTx, err := historyDB.GetTx(l1txs[i].TxID)
  287. assert.NoError(t, err)
  288. if l1txs[i].ToForgeL1TxsNum == toForgeL1TxsNum {
  289. assert.Equal(t, batchNum, *fetchedTx.BatchNum)
  290. } else {
  291. if fetchedTx.BatchNum != nil {
  292. assert.NotEqual(t, batchNum, *fetchedTx.BatchNum)
  293. }
  294. }
  295. }
  296. // Test helper functions for Synchronizer
  297. // GetLastTxsPosition
  298. expectedPosition := -1
  299. var choosenToForgeL1TxsNum int64 = -1
  300. for _, tx := range l1txs {
  301. if choosenToForgeL1TxsNum == -1 && tx.ToForgeL1TxsNum > 0 {
  302. choosenToForgeL1TxsNum = tx.ToForgeL1TxsNum
  303. expectedPosition = tx.Position
  304. } else if choosenToForgeL1TxsNum == tx.ToForgeL1TxsNum && expectedPosition < tx.Position {
  305. expectedPosition = tx.Position
  306. }
  307. }
  308. position, err := historyDB.GetLastTxsPosition(choosenToForgeL1TxsNum)
  309. assert.NoError(t, err)
  310. assert.Equal(t, expectedPosition, position)
  311. // GetL1UserTxs: not needed? tests were broken
  312. // txs, err := historyDB.GetL1UserTxs(2)
  313. // assert.NoError(t, err)
  314. // assert.NotZero(t, len(txs))
  315. // assert.NoError(t, err)
  316. // assert.Equal(t, 22, position)
  317. // // Test Update L1 TX Batch_num
  318. // assert.Equal(t, common.BatchNum(0), txs[0].BatchNum)
  319. // txs[0].BatchNum = common.BatchNum(1)
  320. // txs, err = historyDB.GetL1UserTxs(2)
  321. // assert.NoError(t, err)
  322. // assert.NotZero(t, len(txs))
  323. // assert.Equal(t, common.BatchNum(1), txs[0].BatchNum)
  324. */
  325. }
  326. /*
  327. func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
  328. for i := 0; i < len(l1txs); i++ {
  329. tx := l1txs[i].Tx()
  330. fmt.Println("ASDF", i, tx.TxID)
  331. fetchedTx, err := historyDB.GetTx(tx.TxID)
  332. require.NoError(t, err)
  333. test.AssertUSD(t, tx.USD, fetchedTx.USD)
  334. test.AssertUSD(t, tx.LoadAmountUSD, fetchedTx.LoadAmountUSD)
  335. assert.Equal(t, tx, fetchedTx)
  336. }
  337. for i := 0; i < len(l2txs); i++ {
  338. tx := l2txs[i].Tx()
  339. fetchedTx, err := historyDB.GetTx(tx.TxID)
  340. tx.TokenID = fetchedTx.TokenID
  341. assert.NoError(t, err)
  342. test.AssertUSD(t, fetchedTx.USD, tx.USD)
  343. test.AssertUSD(t, fetchedTx.FeeUSD, tx.FeeUSD)
  344. assert.Equal(t, tx, fetchedTx)
  345. }
  346. }
  347. */
  348. func TestExitTree(t *testing.T) {
  349. nBatches := 17
  350. blocks := setTestBlocks(0, 10)
  351. batches := test.GenBatches(nBatches, blocks)
  352. err := historyDB.AddBatches(batches)
  353. const nTokens = 50
  354. tokens := test.GenTokens(nTokens, blocks)
  355. assert.NoError(t, historyDB.AddTokens(tokens))
  356. assert.NoError(t, err)
  357. const nAccounts = 3
  358. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  359. assert.NoError(t, historyDB.AddAccounts(accs))
  360. exitTree := test.GenExitTree(nBatches, batches, accs)
  361. err = historyDB.AddExitTree(exitTree)
  362. assert.NoError(t, err)
  363. }
  364. func TestGetL1UserTxs(t *testing.T) {
  365. require.NoError(t, cleanHistoryDB())
  366. set := `
  367. Type: Blockchain
  368. AddToken(1)
  369. AddToken(2)
  370. AddToken(3)
  371. CreateAccountDeposit(1) A: 20
  372. CreateAccountDeposit(2) A: 20
  373. CreateAccountDeposit(1) B: 5
  374. CreateAccountDeposit(1) C: 5
  375. CreateAccountDeposit(1) D: 5
  376. > block
  377. `
  378. tc := til.NewContext(128)
  379. blocks, err := tc.GenerateBlocks(set)
  380. require.Nil(t, err)
  381. // Sanity check
  382. require.Equal(t, 1, len(blocks))
  383. require.Equal(t, 5, len(blocks[0].L1UserTxs))
  384. // fmt.Printf("DBG Blocks: %+v\n", blocks)
  385. toForgeL1TxsNum := int64(1)
  386. for i := range blocks {
  387. err = historyDB.AddBlockSCData(&blocks[i])
  388. require.Nil(t, err)
  389. }
  390. l1UserTxs, err := historyDB.GetL1UserTxs(toForgeL1TxsNum)
  391. require.Nil(t, err)
  392. assert.Equal(t, 5, len(l1UserTxs))
  393. assert.Equal(t, blocks[0].L1UserTxs, l1UserTxs)
  394. // No l1UserTxs for this toForgeL1TxsNum
  395. l1UserTxs, err = historyDB.GetL1UserTxs(2)
  396. require.Nil(t, err)
  397. assert.Equal(t, 0, len(l1UserTxs))
  398. }
  399. // setTestBlocks WARNING: this will delete the blocks and recreate them
  400. func setTestBlocks(from, to int64) []common.Block {
  401. if err := cleanHistoryDB(); err != nil {
  402. panic(err)
  403. }
  404. blocks := test.GenBlocks(from, to)
  405. if err := historyDB.AddBlocks(blocks); err != nil {
  406. panic(err)
  407. }
  408. return blocks
  409. }
  410. func cleanHistoryDB() error {
  411. return historyDB.Reorg(-1)
  412. }