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.

154 lines
4.9 KiB

Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
  1. package txselector
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strconv"
  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/db/l2db"
  13. "github.com/hermeznetwork/hermez-node/db/statedb"
  14. "github.com/hermeznetwork/hermez-node/test"
  15. "github.com/hermeznetwork/hermez-node/test/til"
  16. "github.com/hermeznetwork/hermez-node/txprocessor"
  17. "github.com/jmoiron/sqlx"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. )
  21. func initTest(t *testing.T, chainID uint16, testSet string) *TxSelector {
  22. pass := os.Getenv("POSTGRES_PASS")
  23. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  24. require.NoError(t, err)
  25. l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
  26. dir, err := ioutil.TempDir("", "tmpdb")
  27. require.NoError(t, err)
  28. defer assert.NoError(t, os.RemoveAll(dir))
  29. sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeTxSelector, 0)
  30. require.NoError(t, err)
  31. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  32. require.NoError(t, err)
  33. defer assert.NoError(t, os.RemoveAll(dir))
  34. coordAccount := &CoordAccount{ // TODO TMP
  35. Addr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  36. BJJ: common.EmptyBJJComp,
  37. AccountCreationAuth: nil,
  38. }
  39. txsel, err := NewTxSelector(coordAccount, txselDir, sdb, l2DB)
  40. require.NoError(t, err)
  41. return txsel
  42. }
  43. func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
  44. for i := 0; i < len(poolL2Txs); i++ {
  45. err := txsel.l2db.AddTxTest(&poolL2Txs[i])
  46. require.NoError(t, err)
  47. }
  48. }
  49. func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
  50. hdb := historydb.NewHistoryDB(db)
  51. test.WipeDB(hdb.DB())
  52. assert.NoError(t, hdb.AddBlock(&common.Block{
  53. Num: 1,
  54. }))
  55. assert.NoError(t, hdb.AddTokens(tokens))
  56. }
  57. func TestGetL2TxSelection(t *testing.T) {
  58. chainID := uint16(0)
  59. txsel := initTest(t, chainID, til.SetPool0)
  60. test.WipeDB(txsel.l2db.DB())
  61. tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
  62. // generate test transactions
  63. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  64. assert.NoError(t, err)
  65. // poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPool0)
  66. // assert.NoError(t, err)
  67. coordIdxs := make(map[common.TokenID]common.Idx)
  68. coordIdxs[common.TokenID(0)] = common.Idx(256)
  69. coordIdxs[common.TokenID(1)] = common.Idx(257)
  70. coordIdxs[common.TokenID(2)] = common.Idx(258)
  71. coordIdxs[common.TokenID(3)] = common.Idx(259)
  72. // add tokens to HistoryDB to avoid breaking FK constrains
  73. var tokens []common.Token
  74. for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
  75. tokens = append(tokens, common.Token{
  76. TokenID: common.TokenID(i + 1),
  77. EthBlockNum: 1,
  78. EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
  79. Name: strconv.Itoa(i),
  80. Symbol: strconv.Itoa(i),
  81. Decimals: 18,
  82. })
  83. }
  84. addTokens(t, tokens, txsel.l2db.DB())
  85. tpc := txprocessor.Config{
  86. NLevels: 32,
  87. MaxFeeTx: 64,
  88. MaxTx: 512,
  89. MaxL1Tx: 64,
  90. ChainID: chainID,
  91. }
  92. selectionConfig := &SelectionConfig{
  93. MaxL1UserTxs: 32,
  94. MaxL1CoordinatorTxs: 32,
  95. TxProcessorConfig: tpc,
  96. }
  97. txselStateDB := txsel.localAccountsDB.StateDB
  98. tp := txprocessor.NewTxProcessor(txselStateDB, selectionConfig.TxProcessorConfig)
  99. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  100. // to create the Coordinator accounts to receive the fees
  101. _, err = tp.ProcessTxs(nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  102. require.NoError(t, err)
  103. // add the 1st batch of transactions to the TxSelector
  104. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[0].L2Txs))
  105. _, _, l1CoordTxs, l2Txs, err := txsel.GetL2TxSelection(selectionConfig, 0)
  106. assert.NoError(t, err)
  107. assert.Equal(t, 0, len(l2Txs))
  108. assert.Equal(t, 0, len(l1CoordTxs))
  109. _, _, _, _, _, err = txsel.GetL1L2TxSelection(selectionConfig, 0, blocks[0].Rollup.L1UserTxs)
  110. assert.NoError(t, err)
  111. // TODO once L2DB is updated to return error in case that AddTxTest
  112. // fails, and the Til is updated, update this test, checking that the
  113. // selected PoolL2Tx are correctly sorted by Nonce
  114. // TODO once L2DB is updated to store the parameter AbsoluteFee (which
  115. // is used by TxSelector to sort L2Txs), uncomment this next lines of
  116. // test, and put the expected value for
  117. // l2Txs[len(l2Txs)-1].AbsoluteFee, which is the Tx which has the
  118. // Fee==192.
  119. /*
  120. // add the 3rd batch of transactions to the TxSelector
  121. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs))
  122. _, l2Txs, err = txsel.GetL2TxSelection(coordIdxs, 0)
  123. assert.NoError(t, err)
  124. for _, tx := range l2Txs {
  125. fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  126. }
  127. require.Equal(t, 10, len(l2Txs))
  128. assert.Equal(t, float64(0), l2Txs[0].AbsoluteFee)
  129. fmt.Println(l2Txs[len(l2Txs)-1].Amount)
  130. assert.Equal(t, float64(4), l2Txs[len(l2Txs)-1].AbsoluteFee)
  131. */
  132. }