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.

170 lines
5.3 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/jmoiron/sqlx"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. )
  20. func initTest(t *testing.T, chainID uint16, 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, chainID)
  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: common.EmptyBJJComp,
  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. chainID := uint16(0)
  58. txsel := initTest(t, chainID, til.SetPool0)
  59. test.WipeDB(txsel.l2db.DB())
  60. coordIdxs := make(map[common.TokenID]common.Idx)
  61. coordIdxs[common.TokenID(0)] = common.Idx(256)
  62. coordIdxs[common.TokenID(1)] = common.Idx(257)
  63. coordIdxs[common.TokenID(2)] = common.Idx(258)
  64. err := txsel.AddCoordIdxs(coordIdxs)
  65. assert.NoError(t, err)
  66. r, err := txsel.GetCoordIdxs()
  67. assert.NoError(t, err)
  68. assert.Equal(t, coordIdxs, r)
  69. }
  70. func TestGetL2TxSelection(t *testing.T) {
  71. chainID := uint16(0)
  72. txsel := initTest(t, chainID, til.SetPool0)
  73. test.WipeDB(txsel.l2db.DB())
  74. tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
  75. // generate test transactions
  76. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  77. assert.NoError(t, err)
  78. // poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPool0)
  79. // assert.NoError(t, err)
  80. coordIdxs := make(map[common.TokenID]common.Idx)
  81. coordIdxs[common.TokenID(0)] = common.Idx(256)
  82. coordIdxs[common.TokenID(1)] = common.Idx(257)
  83. coordIdxs[common.TokenID(2)] = common.Idx(258)
  84. coordIdxs[common.TokenID(3)] = common.Idx(259)
  85. err = txsel.AddCoordIdxs(coordIdxs)
  86. assert.NoError(t, err)
  87. // add tokens to HistoryDB to avoid breaking FK constrains
  88. var tokens []common.Token
  89. for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
  90. tokens = append(tokens, common.Token{
  91. TokenID: common.TokenID(i + 1),
  92. EthBlockNum: 1,
  93. EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
  94. Name: strconv.Itoa(i),
  95. Symbol: strconv.Itoa(i),
  96. Decimals: 18,
  97. })
  98. }
  99. addTokens(t, tokens, txsel.l2db.DB())
  100. ptc := statedb.ProcessTxsConfig{
  101. NLevels: 32,
  102. MaxFeeTx: 64,
  103. MaxTx: 512,
  104. MaxL1Tx: 64,
  105. }
  106. selectionConfig := &SelectionConfig{
  107. MaxL1UserTxs: 32,
  108. MaxL1CoordinatorTxs: 32,
  109. ProcessTxsConfig: ptc,
  110. }
  111. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  112. // to create the Coordinator accounts to receive the fees
  113. _, err = txsel.localAccountsDB.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  114. require.NoError(t, err)
  115. // add the 1st batch of transactions to the TxSelector
  116. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[0].L2Txs))
  117. _, _, l1CoordTxs, l2Txs, err := txsel.GetL2TxSelection(selectionConfig, 0)
  118. assert.NoError(t, err)
  119. assert.Equal(t, 0, len(l2Txs))
  120. assert.Equal(t, 0, len(l1CoordTxs))
  121. _, _, _, _, _, err = txsel.GetL1L2TxSelection(selectionConfig, 0, blocks[0].Rollup.L1UserTxs)
  122. assert.NoError(t, err)
  123. // TODO once L2DB is updated to return error in case that AddTxTest
  124. // fails, and the Til is updated, update this test, checking that the
  125. // selected PoolL2Tx are correctly sorted by Nonce
  126. // TODO once L2DB is updated to store the parameter AbsoluteFee (which
  127. // is used by TxSelector to sort L2Txs), uncomment this next lines of
  128. // test, and put the expected value for
  129. // l2Txs[len(l2Txs)-1].AbsoluteFee, which is the Tx which has the
  130. // Fee==192.
  131. /*
  132. // add the 3rd batch of transactions to the TxSelector
  133. addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs))
  134. _, l2Txs, err = txsel.GetL2TxSelection(coordIdxs, 0)
  135. assert.NoError(t, err)
  136. for _, tx := range l2Txs {
  137. fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  138. }
  139. require.Equal(t, 10, len(l2Txs))
  140. assert.Equal(t, float64(0), l2Txs[0].AbsoluteFee)
  141. fmt.Println(l2Txs[len(l2Txs)-1].Amount)
  142. assert.Equal(t, float64(4), l2Txs[len(l2Txs)-1].AbsoluteFee)
  143. */
  144. }