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.

168 lines
5.2 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.
4 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/jmoiron/sqlx"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. )
  20. func initTest(t *testing.T, testSet string) *TxSelector {
  21. pass := os.Getenv("POSTGRES_PASS")
  22. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  23. require.NoError(t, err)
  24. l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
  25. dir, err := ioutil.TempDir("", "tmpdb")
  26. require.NoError(t, err)
  27. defer assert.NoError(t, os.RemoveAll(dir))
  28. sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
  29. require.NoError(t, err)
  30. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  31. require.NoError(t, err)
  32. defer assert.NoError(t, os.RemoveAll(dir))
  33. coordAccount := &CoordAccount{ // TODO TMP
  34. Addr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  35. BJJ: nil,
  36. AccountCreationAuth: nil,
  37. }
  38. txsel, err := NewTxSelector(coordAccount, txselDir, sdb, l2DB)
  39. require.NoError(t, err)
  40. return txsel
  41. }
  42. func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
  43. for i := 0; i < len(poolL2Txs); i++ {
  44. err := txsel.l2db.AddTxTest(&poolL2Txs[i])
  45. require.NoError(t, err)
  46. }
  47. }
  48. func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
  49. hdb := historydb.NewHistoryDB(db)
  50. test.WipeDB(hdb.DB())
  51. assert.NoError(t, hdb.AddBlock(&common.Block{
  52. Num: 1,
  53. }))
  54. assert.NoError(t, hdb.AddTokens(tokens))
  55. }
  56. func TestCoordIdxsDB(t *testing.T) {
  57. txsel := initTest(t, til.SetPool0)
  58. test.WipeDB(txsel.l2db.DB())
  59. coordIdxs := make(map[common.TokenID]common.Idx)
  60. coordIdxs[common.TokenID(0)] = common.Idx(256)
  61. coordIdxs[common.TokenID(1)] = common.Idx(257)
  62. coordIdxs[common.TokenID(2)] = common.Idx(258)
  63. err := txsel.AddCoordIdxs(coordIdxs)
  64. assert.NoError(t, err)
  65. r, err := txsel.GetCoordIdxs()
  66. assert.NoError(t, err)
  67. assert.Equal(t, coordIdxs, r)
  68. }
  69. func TestGetL2TxSelection(t *testing.T) {
  70. txsel := initTest(t, til.SetPool0)
  71. test.WipeDB(txsel.l2db.DB())
  72. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  73. // generate test transactions
  74. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  75. assert.NoError(t, err)
  76. // poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPool0)
  77. // assert.NoError(t, err)
  78. coordIdxs := make(map[common.TokenID]common.Idx)
  79. coordIdxs[common.TokenID(0)] = common.Idx(256)
  80. coordIdxs[common.TokenID(1)] = common.Idx(257)
  81. coordIdxs[common.TokenID(2)] = common.Idx(258)
  82. coordIdxs[common.TokenID(3)] = common.Idx(259)
  83. err = txsel.AddCoordIdxs(coordIdxs)
  84. assert.NoError(t, err)
  85. // add tokens to HistoryDB to avoid breaking FK constrains
  86. var tokens []common.Token
  87. for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
  88. tokens = append(tokens, common.Token{
  89. TokenID: common.TokenID(i + 1),
  90. EthBlockNum: 1,
  91. EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
  92. Name: strconv.Itoa(i),
  93. Symbol: strconv.Itoa(i),
  94. Decimals: 18,
  95. })
  96. }
  97. addTokens(t, tokens, txsel.l2db.DB())
  98. ptc := statedb.ProcessTxsConfig{
  99. NLevels: 32,
  100. MaxFeeTx: 64,
  101. MaxTx: 512,
  102. MaxL1Tx: 64,
  103. }
  104. selectionConfig := &SelectionConfig{
  105. MaxL1UserTxs: 32,
  106. MaxL1CoordinatorTxs: 32,
  107. ProcessTxsConfig: ptc,
  108. }
  109. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  110. // to create the Coordinator accounts to receive the fees
  111. _, err = txsel.localAccountsDB.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  112. require.NoError(t, err)
  113. // add the 1st batch of transactions to the TxSelector
  114. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[0].L2Txs))
  115. _, _, l1CoordTxs, l2Txs, err := txsel.GetL2TxSelection(selectionConfig, 0)
  116. assert.NoError(t, err)
  117. assert.Equal(t, 0, len(l2Txs))
  118. assert.Equal(t, 0, len(l1CoordTxs))
  119. _, _, _, _, _, err = txsel.GetL1L2TxSelection(selectionConfig, 0, blocks[0].Rollup.L1UserTxs)
  120. assert.NoError(t, err)
  121. // TODO once L2DB is updated to return error in case that AddTxTest
  122. // fails, and the Til is updated, update this test, checking that the
  123. // selected PoolL2Tx are correctly sorted by Nonce
  124. // TODO once L2DB is updated to store the parameter AbsoluteFee (which
  125. // is used by TxSelector to sort L2Txs), uncomment this next lines of
  126. // test, and put the expected value for
  127. // l2Txs[len(l2Txs)-1].AbsoluteFee, which is the Tx which has the
  128. // Fee==192.
  129. /*
  130. // add the 3rd batch of transactions to the TxSelector
  131. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs))
  132. _, l2Txs, err = txsel.GetL2TxSelection(coordIdxs, 0)
  133. assert.NoError(t, err)
  134. for _, tx := range l2Txs {
  135. fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  136. }
  137. require.Equal(t, 10, len(l2Txs))
  138. assert.Equal(t, float64(0), l2Txs[0].AbsoluteFee)
  139. fmt.Println(l2Txs[len(l2Txs)-1].Amount)
  140. assert.Equal(t, float64(4), l2Txs[len(l2Txs)-1].AbsoluteFee)
  141. */
  142. }