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.

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