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.

628 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.Account
  318. const currentNonce = common.Nonce(1)
  319. for i := range accs {
  320. account := accs[i]
  321. account.Nonce = common.Nonce(currentNonce)
  322. updateAccounts = append(updateAccounts, account)
  323. }
  324. // Add txs to DB
  325. var invalidTxIDs []common.TxID
  326. for i := range poolL2Txs {
  327. if poolL2Txs[i].Nonce <= currentNonce {
  328. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  329. }
  330. err := l2DB.AddTxTest(&poolL2Txs[i])
  331. assert.NoError(t, err)
  332. }
  333. err = l2DB.CheckNonces(updateAccounts, fakeBatchNum)
  334. assert.NoError(t, err)
  335. // Fetch txs and check that they've been updated correctly
  336. for _, id := range invalidTxIDs {
  337. fetchedTx, err := l2DB.GetTxAPI(id)
  338. assert.NoError(t, err)
  339. assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
  340. assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
  341. }
  342. }
  343. // TestReorg: first part of the test with reorg
  344. // With invalidated transactions BEFORE reorgBatch
  345. // And forged transactions in reorgBatch
  346. func TestReorg(t *testing.T) {
  347. // Generate txs
  348. const lastValidBatch common.BatchNum = 20
  349. const reorgBatch common.BatchNum = lastValidBatch + 1
  350. err := prepareHistoryDB(historyDB)
  351. if err != nil {
  352. log.Error("Error prepare historyDB", err)
  353. }
  354. poolL2Txs, err := generatePoolL2Txs()
  355. assert.NoError(t, err)
  356. reorgedTxIDs := []common.TxID{}
  357. nonReorgedTxIDs := []common.TxID{}
  358. var startForgingTxIDs []common.TxID
  359. var invalidTxIDs []common.TxID
  360. var allTxRandomize []common.TxID
  361. randomizer := 0
  362. // Add txs to DB
  363. for i := range poolL2Txs {
  364. err := l2DB.AddTxTest(&poolL2Txs[i])
  365. assert.NoError(t, err)
  366. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  367. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  368. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  369. } else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
  370. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  371. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  372. }
  373. randomizer++
  374. }
  375. // Start forging txs
  376. err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
  377. assert.NoError(t, err)
  378. var doneForgingTxIDs []common.TxID
  379. randomizer = 0
  380. for _, txID := range allTxRandomize {
  381. invalidTx := false
  382. for i := range invalidTxIDs {
  383. if invalidTxIDs[i] == txID {
  384. invalidTx = true
  385. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  386. }
  387. }
  388. if !invalidTx {
  389. if randomizer%2 == 0 {
  390. doneForgingTxIDs = append(doneForgingTxIDs, txID)
  391. reorgedTxIDs = append(reorgedTxIDs, txID)
  392. } else {
  393. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  394. }
  395. randomizer++
  396. }
  397. }
  398. // Invalidate txs BEFORE reorgBatch --> nonReorg
  399. err = l2DB.InvalidateTxs(invalidTxIDs, lastValidBatch)
  400. assert.NoError(t, err)
  401. // Done forging txs in reorgBatch --> Reorg
  402. err = l2DB.DoneForging(doneForgingTxIDs, reorgBatch)
  403. assert.NoError(t, err)
  404. err = l2DB.Reorg(lastValidBatch)
  405. assert.NoError(t, err)
  406. for _, id := range reorgedTxIDs {
  407. tx, err := l2DB.GetTxAPI(id)
  408. assert.NoError(t, err)
  409. assert.Nil(t, tx.BatchNum)
  410. assert.Equal(t, common.PoolL2TxStatePending, tx.State)
  411. }
  412. for _, id := range nonReorgedTxIDs {
  413. fetchedTx, err := l2DB.GetTxAPI(id)
  414. assert.NoError(t, err)
  415. assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
  416. }
  417. }
  418. // TestReorg: second part of test with reorg
  419. // With invalidated transactions in reorgBatch
  420. // And forged transactions BEFORE reorgBatch
  421. func TestReorg2(t *testing.T) {
  422. // Generate txs
  423. const lastValidBatch common.BatchNum = 20
  424. const reorgBatch common.BatchNum = lastValidBatch + 1
  425. err := prepareHistoryDB(historyDB)
  426. if err != nil {
  427. log.Error("Error prepare historyDB", err)
  428. }
  429. poolL2Txs, err := generatePoolL2Txs()
  430. assert.NoError(t, err)
  431. reorgedTxIDs := []common.TxID{}
  432. nonReorgedTxIDs := []common.TxID{}
  433. var startForgingTxIDs []common.TxID
  434. var invalidTxIDs []common.TxID
  435. var allTxRandomize []common.TxID
  436. randomizer := 0
  437. // Add txs to DB
  438. for i := range poolL2Txs {
  439. err := l2DB.AddTxTest(&poolL2Txs[i])
  440. assert.NoError(t, err)
  441. if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
  442. startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
  443. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  444. } else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
  445. invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
  446. allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
  447. }
  448. randomizer++
  449. }
  450. // Start forging txs
  451. err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
  452. assert.NoError(t, err)
  453. var doneForgingTxIDs []common.TxID
  454. randomizer = 0
  455. for _, txID := range allTxRandomize {
  456. invalidTx := false
  457. for i := range invalidTxIDs {
  458. if invalidTxIDs[i] == txID {
  459. invalidTx = true
  460. reorgedTxIDs = append(reorgedTxIDs, txID)
  461. }
  462. }
  463. if !invalidTx {
  464. if randomizer%2 == 0 {
  465. doneForgingTxIDs = append(doneForgingTxIDs, txID)
  466. }
  467. nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
  468. randomizer++
  469. }
  470. }
  471. // Done forging txs BEFORE reorgBatch --> nonReorg
  472. err = l2DB.DoneForging(doneForgingTxIDs, lastValidBatch)
  473. assert.NoError(t, err)
  474. // Invalidate txs in reorgBatch --> Reorg
  475. err = l2DB.InvalidateTxs(invalidTxIDs, reorgBatch)
  476. assert.NoError(t, err)
  477. err = l2DB.Reorg(lastValidBatch)
  478. assert.NoError(t, err)
  479. for _, id := range reorgedTxIDs {
  480. tx, err := l2DB.GetTxAPI(id)
  481. assert.NoError(t, err)
  482. assert.Nil(t, tx.BatchNum)
  483. assert.Equal(t, common.PoolL2TxStatePending, tx.State)
  484. }
  485. for _, id := range nonReorgedTxIDs {
  486. fetchedTx, err := l2DB.GetTxAPI(id)
  487. assert.NoError(t, err)
  488. assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
  489. }
  490. }
  491. func TestPurge(t *testing.T) {
  492. // Generate txs
  493. err := prepareHistoryDB(historyDB)
  494. if err != nil {
  495. log.Error("Error prepare historyDB", err)
  496. }
  497. // generatePoolL2Txs generate 10 txs
  498. generateTx := int(l2DB.maxTxs/10 + 1)
  499. var poolL2Tx []common.PoolL2Tx
  500. for i := 0; i < generateTx; i++ {
  501. poolL2TxAux, err := generatePoolL2Txs()
  502. assert.NoError(t, err)
  503. poolL2Tx = append(poolL2Tx, poolL2TxAux...)
  504. }
  505. deletedIDs := []common.TxID{}
  506. keepedIDs := []common.TxID{}
  507. var invalidTxIDs []common.TxID
  508. var doneForgingTxIDs []common.TxID
  509. const toDeleteBatchNum common.BatchNum = 30
  510. safeBatchNum := toDeleteBatchNum + l2DB.safetyPeriod + 1
  511. // Add txs to the DB
  512. for i := 0; i < int(l2DB.maxTxs); i++ {
  513. tx := poolL2Tx[i]
  514. if i%2 == 0 { // keep tx
  515. keepedIDs = append(keepedIDs, tx.TxID)
  516. } else { // delete after safety period
  517. if i%3 == 0 {
  518. doneForgingTxIDs = append(doneForgingTxIDs, tx.TxID)
  519. } else {
  520. invalidTxIDs = append(invalidTxIDs, tx.TxID)
  521. }
  522. deletedIDs = append(deletedIDs, tx.TxID)
  523. }
  524. err := l2DB.AddTxTest(&tx)
  525. assert.NoError(t, err)
  526. }
  527. // Set batchNum keeped txs
  528. for i := range keepedIDs {
  529. _, err = l2DB.db.Exec(
  530. "UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;",
  531. safeBatchNum, keepedIDs[i],
  532. )
  533. assert.NoError(t, err)
  534. }
  535. // Start forging txs and set batchNum
  536. err = l2DB.StartForging(doneForgingTxIDs, toDeleteBatchNum)
  537. assert.NoError(t, err)
  538. // Done forging txs and set batchNum
  539. err = l2DB.DoneForging(doneForgingTxIDs, toDeleteBatchNum)
  540. assert.NoError(t, err)
  541. // Invalidate txs and set batchNum
  542. err = l2DB.InvalidateTxs(invalidTxIDs, toDeleteBatchNum)
  543. assert.NoError(t, err)
  544. for i := int(l2DB.maxTxs); i < len(poolL2Tx); i++ {
  545. // Delete after TTL
  546. deletedIDs = append(deletedIDs, poolL2Tx[i].TxID)
  547. err := l2DB.AddTxTest(&poolL2Tx[i])
  548. assert.NoError(t, err)
  549. // Set timestamp
  550. deleteTimestamp := time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0)
  551. _, err = l2DB.db.Exec(
  552. "UPDATE tx_pool SET timestamp = $1 WHERE tx_id = $2;",
  553. deleteTimestamp, poolL2Tx[i].TxID,
  554. )
  555. assert.NoError(t, err)
  556. }
  557. // Purge txs
  558. err = l2DB.Purge(safeBatchNum)
  559. assert.NoError(t, err)
  560. // Check results
  561. for _, id := range deletedIDs {
  562. tx, err := l2DB.GetTx(id)
  563. if err == nil {
  564. log.Debug(tx)
  565. }
  566. assert.Error(t, err)
  567. }
  568. for _, id := range keepedIDs {
  569. _, err := l2DB.GetTx(id)
  570. assert.NoError(t, err)
  571. }
  572. }
  573. func TestAuth(t *testing.T) {
  574. test.WipeDB(l2DB.DB())
  575. const nAuths = 5
  576. // Generate authorizations
  577. auths := test.GenAuths(nAuths)
  578. for i := 0; i < len(auths); i++ {
  579. // Add to the DB
  580. err := l2DB.AddAccountCreationAuth(auths[i])
  581. assert.NoError(t, err)
  582. // Fetch from DB
  583. auth, err := l2DB.GetAccountCreationAuth(auths[i].EthAddr)
  584. assert.NoError(t, err)
  585. // Check fetched vs generated
  586. assert.Equal(t, auths[i].EthAddr, auth.EthAddr)
  587. assert.Equal(t, auths[i].BJJ, auth.BJJ)
  588. assert.Equal(t, auths[i].Signature, auth.Signature)
  589. assert.Equal(t, auths[i].Timestamp.Unix(), auths[i].Timestamp.Unix())
  590. nameZone, offset := auths[i].Timestamp.Zone()
  591. assert.Equal(t, "UTC", nameZone)
  592. assert.Equal(t, 0, offset)
  593. }
  594. }