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.

629 lines
18 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package l2db
  2. import (
  3. "math"
  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. dbUtils "github.com/hermeznetwork/hermez-node/db"
  11. "github.com/hermeznetwork/hermez-node/db/historydb"
  12. "github.com/hermeznetwork/hermez-node/log"
  13. "github.com/hermeznetwork/hermez-node/test"
  14. "github.com/hermeznetwork/hermez-node/test/til"
  15. "github.com/hermeznetwork/tracerr"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. var l2DB *L2DB
  19. var historyDB *historydb.HistoryDB
  20. var tc *til.Context
  21. var tokens map[common.TokenID]historydb.TokenWithUSD
  22. var tokensValue map[common.TokenID]float64
  23. var accs map[common.Idx]common.Account
  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. l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
  32. test.WipeDB(l2DB.DB())
  33. historyDB = historydb.NewHistoryDB(db)
  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 prepareHistoryDB(historyDB *historydb.HistoryDB) error {
  43. // Reset DB
  44. test.WipeDB(l2DB.DB())
  45. // Generate pool txs using til
  46. setBlockchain := `
  47. Type: Blockchain
  48. AddToken(1)
  49. AddToken(2)
  50. CreateAccountDeposit(1) A: 2000
  51. CreateAccountDeposit(2) A: 2000
  52. CreateAccountDeposit(1) B: 1000
  53. CreateAccountDeposit(2) B: 1000
  54. > batchL1
  55. > batchL1
  56. > block
  57. > block
  58. `
  59. tc = til.NewContext(common.RollupConstMaxL1UserTx)
  60. tilCfgExtra := til.ConfigExtra{
  61. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  62. CoordUser: "A",
  63. }
  64. blocks, err := tc.GenerateBlocks(setBlockchain)
  65. if err != nil {
  66. return tracerr.Wrap(err)
  67. }
  68. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  69. if err != nil {
  70. return tracerr.Wrap(err)
  71. }
  72. tokens = make(map[common.TokenID]historydb.TokenWithUSD)
  73. tokensValue = make(map[common.TokenID]float64)
  74. accs = make(map[common.Idx]common.Account)
  75. value := 5 * 5.389329
  76. now := time.Now().UTC()
  77. // Add all blocks except for the last one
  78. for i := range blocks[:len(blocks)-1] {
  79. err = historyDB.AddBlockSCData(&blocks[i])
  80. if err != nil {
  81. return tracerr.Wrap(err)
  82. }
  83. for _, batch := range blocks[i].Rollup.Batches {
  84. for _, account := range batch.CreatedAccounts {
  85. accs[account.Idx] = account
  86. }
  87. }
  88. for _, token := range blocks[i].Rollup.AddedTokens {
  89. readToken := historydb.TokenWithUSD{
  90. TokenID: token.TokenID,
  91. EthBlockNum: token.EthBlockNum,
  92. EthAddr: token.EthAddr,
  93. Name: token.Name,
  94. Symbol: token.Symbol,
  95. Decimals: token.Decimals,
  96. }
  97. tokensValue[token.TokenID] = value / math.Pow(10, float64(token.Decimals))
  98. readToken.USDUpdate = &now
  99. readToken.USD = &value
  100. tokens[token.TokenID] = readToken
  101. }
  102. // Set value to the tokens (tokens have no symbol)
  103. tokenSymbol := ""
  104. err := historyDB.UpdateTokenValue(tokenSymbol, value)
  105. if err != nil {
  106. return tracerr.Wrap(err)
  107. }
  108. }
  109. return nil
  110. }
  111. func generatePoolL2Txs() ([]common.PoolL2Tx, error) {
  112. setPool := `
  113. Type: PoolL2
  114. PoolTransfer(1) A-B: 6 (4)
  115. PoolTransfer(2) A-B: 3 (1)
  116. PoolTransfer(1) B-A: 5 (2)
  117. PoolTransfer(2) B-A: 10 (3)
  118. PoolTransfer(1) A-B: 7 (2)
  119. PoolTransfer(2) A-B: 2 (1)
  120. PoolTransfer(1) B-A: 8 (2)
  121. PoolTransfer(2) B-A: 1 (1)
  122. PoolTransfer(1) A-B: 3 (1)
  123. PoolTransfer(2) B-A: 5 (2)
  124. PoolExit(1) A: 5 (2)
  125. PoolExit(2) B: 3 (1)
  126. `
  127. poolL2Txs, err := tc.GeneratePoolL2Txs(setPool)
  128. if err != nil {
  129. return nil, tracerr.Wrap(err)
  130. }
  131. return poolL2Txs, nil
  132. }
  133. func TestAddTxTest(t *testing.T) {
  134. err := prepareHistoryDB(historyDB)
  135. if err != nil {
  136. log.Error("Error prepare historyDB", err)
  137. }
  138. poolL2Txs, err := generatePoolL2Txs()
  139. assert.NoError(t, err)
  140. for i := range poolL2Txs {
  141. err := l2DB.AddTxTest(&poolL2Txs[i])
  142. assert.NoError(t, err)
  143. fetchedTx, err := l2DB.GetTx(poolL2Txs[i].TxID)
  144. assert.NoError(t, err)
  145. assertTx(t, &poolL2Txs[i], fetchedTx)
  146. nameZone, offset := fetchedTx.Timestamp.Zone()
  147. assert.Equal(t, "UTC", nameZone)
  148. assert.Equal(t, 0, offset)
  149. }
  150. }
  151. func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
  152. // Check that timestamp has been set within the last 3 seconds
  153. assert.Less(t, time.Now().UTC().Unix()-3, actual.Timestamp.Unix())
  154. assert.GreaterOrEqual(t, time.Now().UTC().Unix(), actual.Timestamp.Unix())
  155. expected.Timestamp = actual.Timestamp
  156. // Check absolute fee
  157. // find token
  158. token := tokens[expected.TokenID]
  159. // If the token has value in USD setted
  160. if token.USDUpdate != nil {
  161. assert.Equal(t, token.USDUpdate.Unix(), actual.AbsoluteFeeUpdate.Unix())
  162. expected.AbsoluteFeeUpdate = actual.AbsoluteFeeUpdate
  163. // Set expected fee
  164. f := new(big.Float).SetInt(expected.Amount)
  165. amountF, _ := f.Float64()
  166. expected.AbsoluteFee = *token.USD * amountF * expected.Fee.Percentage()
  167. test.AssertUSD(t, &expected.AbsoluteFee, &actual.AbsoluteFee)
  168. }
  169. assert.Equal(t, expected, actual)
  170. }
  171. // NO UPDATE: benchmarks will be done after impl is finished
  172. // func BenchmarkAddTxTest(b *testing.B) {
  173. // const nInserts = 20
  174. // test.WipeDB(l2DB.DB())
  175. // txs := test.GenPoolTxs(nInserts, tokens)
  176. // now := time.Now()
  177. // for _, tx := range txs {
  178. // _ = l2DB.AddTxTest(tx)
  179. // }
  180. // elapsedTime := time.Since(now)
  181. // log.Info("Time to insert 2048 txs:", elapsedTime)
  182. // }
  183. func TestGetPending(t *testing.T) {
  184. err := prepareHistoryDB(historyDB)
  185. if err != nil {
  186. log.Error("Error prepare historyDB", err)
  187. }
  188. poolL2Txs, err := generatePoolL2Txs()
  189. assert.NoError(t, err)
  190. var pendingTxs []*common.PoolL2Tx
  191. for i := range poolL2Txs {
  192. err := l2DB.AddTxTest(&poolL2Txs[i])
  193. assert.NoError(t, err)
  194. pendingTxs = append(pendingTxs, &poolL2Txs[i])
  195. }
  196. fetchedTxs, err := l2DB.GetPendingTxs()
  197. assert.NoError(t, err)
  198. assert.Equal(t, len(pendingTxs), len(fetchedTxs))
  199. for i := range fetchedTxs {
  200. assertTx(t, pendingTxs[i], &fetchedTxs[i])
  201. }
  202. }
  203. func TestStartForging(t *testing.T) {
  204. // Generate txs
  205. const fakeBatchNum common.BatchNum = 33
  206. err := prepareHistoryDB(historyDB)
  207. if err != nil {
  208. log.Error("Error prepare historyDB", err)
  209. }
  210. poolL2Txs, err := generatePoolL2Txs()
  211. assert.NoError(t, err)
  212. var startForgingTxIDs []common.TxID
  213. randomizer := 0
  214. // Add txs to DB
  215. for i := range poolL2Txs {
  216. err := l2DB.AddTxTest(&poolL2Txs[i])
  217. assert.NoError(t, err)
  218. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  219. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  220. }
  221. randomizer++
  222. }
  223. // Start forging txs
  224. err = l2DB.StartForging(startForgingTxIDs, fakeBatchNum)
  225. assert.NoError(t, err)
  226. // Fetch txs and check that they've been updated correctly
  227. for _, id := range startForgingTxIDs {
  228. fetchedTx, err := l2DB.GetTxAPI(id)
  229. assert.NoError(t, err)
  230. assert.Equal(t, common.PoolL2TxStateForging, fetchedTx.State)
  231. assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
  232. }
  233. }
  234. func TestDoneForging(t *testing.T) {
  235. // Generate txs
  236. const fakeBatchNum common.BatchNum = 33
  237. err := prepareHistoryDB(historyDB)
  238. if err != nil {
  239. log.Error("Error prepare historyDB", err)
  240. }
  241. poolL2Txs, err := generatePoolL2Txs()
  242. assert.NoError(t, err)
  243. var startForgingTxIDs []common.TxID
  244. randomizer := 0
  245. // Add txs to DB
  246. for i := range poolL2Txs {
  247. err := l2DB.AddTxTest(&poolL2Txs[i])
  248. assert.NoError(t, err)
  249. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  250. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  251. }
  252. randomizer++
  253. }
  254. // Start forging txs
  255. err = l2DB.StartForging(startForgingTxIDs, fakeBatchNum)
  256. assert.NoError(t, err)
  257. var doneForgingTxIDs []common.TxID
  258. randomizer = 0
  259. for _, txID := range startForgingTxIDs {
  260. if randomizer%2 == 0 {
  261. doneForgingTxIDs = append(doneForgingTxIDs, txID)
  262. }
  263. randomizer++
  264. }
  265. // Done forging txs
  266. err = l2DB.DoneForging(doneForgingTxIDs, fakeBatchNum)
  267. assert.NoError(t, err)
  268. // Fetch txs and check that they've been updated correctly
  269. for _, id := range doneForgingTxIDs {
  270. fetchedTx, err := l2DB.GetTxAPI(id)
  271. assert.NoError(t, err)
  272. assert.Equal(t, common.PoolL2TxStateForged, fetchedTx.State)
  273. assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
  274. }
  275. }
  276. func TestInvalidate(t *testing.T) {
  277. // Generate txs
  278. const fakeBatchNum common.BatchNum = 33
  279. err := prepareHistoryDB(historyDB)
  280. if err != nil {
  281. log.Error("Error prepare historyDB", err)
  282. }
  283. poolL2Txs, err := generatePoolL2Txs()
  284. assert.NoError(t, err)
  285. var invalidTxIDs []common.TxID
  286. randomizer := 0
  287. // Add txs to DB
  288. for i := range poolL2Txs {
  289. err := l2DB.AddTxTest(&poolL2Txs[i])
  290. assert.NoError(t, err)
  291. if poolL2Txs[i].State != common.PoolL2TxStateInvalid && randomizer%2 == 0 {
  292. randomizer++
  293. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  294. }
  295. }
  296. // Invalidate txs
  297. err = l2DB.InvalidateTxs(invalidTxIDs, fakeBatchNum)
  298. assert.NoError(t, err)
  299. // Fetch txs and check that they've been updated correctly
  300. for _, id := range invalidTxIDs {
  301. fetchedTx, err := l2DB.GetTxAPI(id)
  302. assert.NoError(t, err)
  303. assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
  304. assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
  305. }
  306. }
  307. func TestCheckNonces(t *testing.T) {
  308. // Generate txs
  309. const fakeBatchNum common.BatchNum = 33
  310. err := prepareHistoryDB(historyDB)
  311. if err != nil {
  312. log.Error("Error prepare historyDB", err)
  313. }
  314. poolL2Txs, err := generatePoolL2Txs()
  315. assert.NoError(t, err)
  316. // Update Accounts currentNonce
  317. var updateAccounts []common.IdxNonce
  318. const currentNonce = common.Nonce(1)
  319. for i := range accs {
  320. updateAccounts = append(updateAccounts, common.IdxNonce{
  321. Idx: accs[i].Idx,
  322. Nonce: common.Nonce(currentNonce),
  323. })
  324. }
  325. // Add txs to DB
  326. var invalidTxIDs []common.TxID
  327. for i := range poolL2Txs {
  328. if poolL2Txs[i].Nonce <= currentNonce {
  329. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  330. }
  331. err := l2DB.AddTxTest(&poolL2Txs[i])
  332. assert.NoError(t, err)
  333. }
  334. err = l2DB.CheckNonces(updateAccounts, fakeBatchNum)
  335. assert.NoError(t, err)
  336. // Fetch txs and check that they've been updated correctly
  337. for _, id := range invalidTxIDs {
  338. fetchedTx, err := l2DB.GetTxAPI(id)
  339. assert.NoError(t, err)
  340. assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
  341. assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
  342. }
  343. }
  344. // TestReorg: first part of the test with reorg
  345. // With invalidated transactions BEFORE reorgBatch
  346. // And forged transactions in reorgBatch
  347. func TestReorg(t *testing.T) {
  348. // Generate txs
  349. const lastValidBatch common.BatchNum = 20
  350. const reorgBatch common.BatchNum = lastValidBatch + 1
  351. err := prepareHistoryDB(historyDB)
  352. if err != nil {
  353. log.Error("Error prepare historyDB", err)
  354. }
  355. poolL2Txs, err := generatePoolL2Txs()
  356. assert.NoError(t, err)
  357. reorgedTxIDs := []common.TxID{}
  358. nonReorgedTxIDs := []common.TxID{}
  359. var startForgingTxIDs []common.TxID
  360. var invalidTxIDs []common.TxID
  361. var allTxRandomize []common.TxID
  362. randomizer := 0
  363. // Add txs to DB
  364. for i := range poolL2Txs {
  365. err := l2DB.AddTxTest(&poolL2Txs[i])
  366. assert.NoError(t, err)
  367. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  368. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  369. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  370. } else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
  371. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  372. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  373. }
  374. randomizer++
  375. }
  376. // Start forging txs
  377. err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
  378. assert.NoError(t, err)
  379. var doneForgingTxIDs []common.TxID
  380. randomizer = 0
  381. for _, txID := range allTxRandomize {
  382. invalidTx := false
  383. for i := range invalidTxIDs {
  384. if invalidTxIDs[i] == txID {
  385. invalidTx = true
  386. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  387. }
  388. }
  389. if !invalidTx {
  390. if randomizer%2 == 0 {
  391. doneForgingTxIDs = append(doneForgingTxIDs, txID)
  392. reorgedTxIDs = append(reorgedTxIDs, txID)
  393. } else {
  394. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  395. }
  396. randomizer++
  397. }
  398. }
  399. // Invalidate txs BEFORE reorgBatch --> nonReorg
  400. err = l2DB.InvalidateTxs(invalidTxIDs, lastValidBatch)
  401. assert.NoError(t, err)
  402. // Done forging txs in reorgBatch --> Reorg
  403. err = l2DB.DoneForging(doneForgingTxIDs, reorgBatch)
  404. assert.NoError(t, err)
  405. err = l2DB.Reorg(lastValidBatch)
  406. assert.NoError(t, err)
  407. for _, id := range reorgedTxIDs {
  408. tx, err := l2DB.GetTxAPI(id)
  409. assert.NoError(t, err)
  410. assert.Nil(t, tx.BatchNum)
  411. assert.Equal(t, common.PoolL2TxStatePending, tx.State)
  412. }
  413. for _, id := range nonReorgedTxIDs {
  414. fetchedTx, err := l2DB.GetTxAPI(id)
  415. assert.NoError(t, err)
  416. assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
  417. }
  418. }
  419. // TestReorg: second part of test with reorg
  420. // With invalidated transactions in reorgBatch
  421. // And forged transactions BEFORE reorgBatch
  422. func TestReorg2(t *testing.T) {
  423. // Generate txs
  424. const lastValidBatch common.BatchNum = 20
  425. const reorgBatch common.BatchNum = lastValidBatch + 1
  426. err := prepareHistoryDB(historyDB)
  427. if err != nil {
  428. log.Error("Error prepare historyDB", err)
  429. }
  430. poolL2Txs, err := generatePoolL2Txs()
  431. assert.NoError(t, err)
  432. reorgedTxIDs := []common.TxID{}
  433. nonReorgedTxIDs := []common.TxID{}
  434. var startForgingTxIDs []common.TxID
  435. var invalidTxIDs []common.TxID
  436. var allTxRandomize []common.TxID
  437. randomizer := 0
  438. // Add txs to DB
  439. for i := range poolL2Txs {
  440. err := l2DB.AddTxTest(&poolL2Txs[i])
  441. assert.NoError(t, err)
  442. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  443. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  444. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  445. } else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
  446. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  447. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  448. }
  449. randomizer++
  450. }
  451. // Start forging txs
  452. err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
  453. assert.NoError(t, err)
  454. var doneForgingTxIDs []common.TxID
  455. randomizer = 0
  456. for _, txID := range allTxRandomize {
  457. invalidTx := false
  458. for i := range invalidTxIDs {
  459. if invalidTxIDs[i] == txID {
  460. invalidTx = true
  461. reorgedTxIDs = append(reorgedTxIDs, txID)
  462. }
  463. }
  464. if !invalidTx {
  465. if randomizer%2 == 0 {
  466. doneForgingTxIDs = append(doneForgingTxIDs, txID)
  467. }
  468. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  469. randomizer++
  470. }
  471. }
  472. // Done forging txs BEFORE reorgBatch --> nonReorg
  473. err = l2DB.DoneForging(doneForgingTxIDs, lastValidBatch)
  474. assert.NoError(t, err)
  475. // Invalidate txs in reorgBatch --> Reorg
  476. err = l2DB.InvalidateTxs(invalidTxIDs, reorgBatch)
  477. assert.NoError(t, err)
  478. err = l2DB.Reorg(lastValidBatch)
  479. assert.NoError(t, err)
  480. for _, id := range reorgedTxIDs {
  481. tx, err := l2DB.GetTxAPI(id)
  482. assert.NoError(t, err)
  483. assert.Nil(t, tx.BatchNum)
  484. assert.Equal(t, common.PoolL2TxStatePending, tx.State)
  485. }
  486. for _, id := range nonReorgedTxIDs {
  487. fetchedTx, err := l2DB.GetTxAPI(id)
  488. assert.NoError(t, err)
  489. assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
  490. }
  491. }
  492. func TestPurge(t *testing.T) {
  493. // Generate txs
  494. err := prepareHistoryDB(historyDB)
  495. if err != nil {
  496. log.Error("Error prepare historyDB", err)
  497. }
  498. // generatePoolL2Txs generate 10 txs
  499. generateTx := int(l2DB.maxTxs/10 + 1)
  500. var poolL2Tx []common.PoolL2Tx
  501. for i := 0; i < generateTx; i++ {
  502. poolL2TxAux, err := generatePoolL2Txs()
  503. assert.NoError(t, err)
  504. poolL2Tx = append(poolL2Tx, poolL2TxAux...)
  505. }
  506. deletedIDs := []common.TxID{}
  507. keepedIDs := []common.TxID{}
  508. var invalidTxIDs []common.TxID
  509. var doneForgingTxIDs []common.TxID
  510. const toDeleteBatchNum common.BatchNum = 30
  511. safeBatchNum := toDeleteBatchNum + l2DB.safetyPeriod + 1
  512. // Add txs to the DB
  513. for i := 0; i < int(l2DB.maxTxs); i++ {
  514. tx := poolL2Tx[i]
  515. if i%2 == 0 { // keep tx
  516. keepedIDs = append(keepedIDs, tx.TxID)
  517. } else { // delete after safety period
  518. if i%3 == 0 {
  519. doneForgingTxIDs = append(doneForgingTxIDs, tx.TxID)
  520. } else {
  521. invalidTxIDs = append(invalidTxIDs, tx.TxID)
  522. }
  523. deletedIDs = append(deletedIDs, tx.TxID)
  524. }
  525. err := l2DB.AddTxTest(&tx)
  526. assert.NoError(t, err)
  527. }
  528. // Set batchNum keeped txs
  529. for i := range keepedIDs {
  530. _, err = l2DB.db.Exec(
  531. "UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;",
  532. safeBatchNum, keepedIDs[i],
  533. )
  534. assert.NoError(t, err)
  535. }
  536. // Start forging txs and set batchNum
  537. err = l2DB.StartForging(doneForgingTxIDs, toDeleteBatchNum)
  538. assert.NoError(t, err)
  539. // Done forging txs and set batchNum
  540. err = l2DB.DoneForging(doneForgingTxIDs, toDeleteBatchNum)
  541. assert.NoError(t, err)
  542. // Invalidate txs and set batchNum
  543. err = l2DB.InvalidateTxs(invalidTxIDs, toDeleteBatchNum)
  544. assert.NoError(t, err)
  545. for i := int(l2DB.maxTxs); i < len(poolL2Tx); i++ {
  546. // Delete after TTL
  547. deletedIDs = append(deletedIDs, poolL2Tx[i].TxID)
  548. err := l2DB.AddTxTest(&poolL2Tx[i])
  549. assert.NoError(t, err)
  550. // Set timestamp
  551. deleteTimestamp := time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0)
  552. _, err = l2DB.db.Exec(
  553. "UPDATE tx_pool SET timestamp = $1 WHERE tx_id = $2;",
  554. deleteTimestamp, poolL2Tx[i].TxID,
  555. )
  556. assert.NoError(t, err)
  557. }
  558. // Purge txs
  559. err = l2DB.Purge(safeBatchNum)
  560. assert.NoError(t, err)
  561. // Check results
  562. for _, id := range deletedIDs {
  563. tx, err := l2DB.GetTx(id)
  564. if err == nil {
  565. log.Debug(tx)
  566. }
  567. assert.Error(t, err)
  568. }
  569. for _, id := range keepedIDs {
  570. _, err := l2DB.GetTx(id)
  571. assert.NoError(t, err)
  572. }
  573. }
  574. func TestAuth(t *testing.T) {
  575. test.WipeDB(l2DB.DB())
  576. const nAuths = 5
  577. // Generate authorizations
  578. auths := test.GenAuths(nAuths)
  579. for i := 0; i < len(auths); i++ {
  580. // Add to the DB
  581. err := l2DB.AddAccountCreationAuth(auths[i])
  582. assert.NoError(t, err)
  583. // Fetch from DB
  584. auth, err := l2DB.GetAccountCreationAuth(auths[i].EthAddr)
  585. assert.NoError(t, err)
  586. // Check fetched vs generated
  587. assert.Equal(t, auths[i].EthAddr, auth.EthAddr)
  588. assert.Equal(t, auths[i].BJJ, auth.BJJ)
  589. assert.Equal(t, auths[i].Signature, auth.Signature)
  590. assert.Equal(t, auths[i].Timestamp.Unix(), auths[i].Timestamp.Unix())
  591. nameZone, offset := auths[i].Timestamp.Zone()
  592. assert.Equal(t, "UTC", nameZone)
  593. assert.Equal(t, 0, offset)
  594. }
  595. }