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.

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