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.

183 lines
5.9 KiB

  1. package txselector
  2. // current: very simple version of TxSelector
  3. import (
  4. "math/big"
  5. "sort"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/db/l2db"
  8. "github.com/hermeznetwork/hermez-node/db/statedb"
  9. )
  10. // txs implements the interface Sort for an array of Tx
  11. type txs []*common.PoolL2Tx
  12. func (t txs) Len() int {
  13. return len(t)
  14. }
  15. func (t txs) Swap(i, j int) {
  16. t[i], t[j] = t[j], t[i]
  17. }
  18. func (t txs) Less(i, j int) bool {
  19. return t[i].AbsoluteFee > t[j].AbsoluteFee
  20. }
  21. // TxSelector implements all the functionalities to select the txs for the next batch
  22. type TxSelector struct {
  23. // MaxL1UserTxs is the maximum L1-user-tx for a batch
  24. MaxL1UserTxs uint64
  25. // MaxL1OperatorTxs is the maximum L1-operator-tx for a batch
  26. MaxL1OperatorTxs uint64
  27. // MaxTxs is the maximum txs for a batch
  28. MaxTxs uint64
  29. l2db *l2db.L2DB
  30. localAccountsDB *statedb.LocalStateDB
  31. }
  32. // NewTxSelector returns a *TxSelector
  33. func NewTxSelector(dbpath string, synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs uint64) (*TxSelector, error) {
  34. localAccountsDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, false, 0) // without merkletree
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &TxSelector{
  39. MaxL1UserTxs: maxL1UserTxs,
  40. MaxL1OperatorTxs: maxL1OperatorTxs,
  41. MaxTxs: maxTxs,
  42. l2db: l2,
  43. localAccountsDB: localAccountsDB,
  44. }, nil
  45. }
  46. // Reset tells the TxSelector to get it's internal AccountsDB
  47. // from the required `batchNum`
  48. func (txsel *TxSelector) Reset(batchNum common.BatchNum) error {
  49. err := txsel.localAccountsDB.Reset(batchNum, true)
  50. if err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. // GetL2TxSelection returns a selection of the L2Txs for the next batch, from the L2DB pool
  56. func (txsel *TxSelector) GetL2TxSelection(batchNum common.BatchNum) ([]*common.PoolL2Tx, error) {
  57. // get pending l2-tx from tx-pool
  58. l2TxsRaw, err := txsel.l2db.GetPendingTxs() // once l2db ready, maybe use parameter 'batchNum'
  59. if err != nil {
  60. return nil, err
  61. }
  62. // discard the txs that don't have an Account in the AccountDB
  63. var validTxs txs
  64. for _, tx := range l2TxsRaw {
  65. _, err = txsel.localAccountsDB.GetAccount(tx.FromIdx)
  66. if err == nil {
  67. // if FromIdx has an account into the AccountsDB
  68. validTxs = append(validTxs, tx)
  69. }
  70. }
  71. // get most profitable L2-tx
  72. txs := txsel.getL2Profitable(validTxs, txsel.MaxTxs)
  73. // process the txs in the local AccountsDB
  74. _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, nil, nil, txs)
  75. if err != nil {
  76. return nil, err
  77. }
  78. err = txsel.localAccountsDB.MakeCheckpoint()
  79. return txs, err
  80. }
  81. // GetL1L2TxSelection returns the selection of L1 + L2 txs
  82. func (txsel *TxSelector) GetL1L2TxSelection(batchNum common.BatchNum, l1Txs []*common.L1Tx) ([]*common.L1Tx, []*common.L1Tx, []*common.PoolL2Tx, error) {
  83. // apply l1-user-tx to localAccountDB
  84. // create new leaves
  85. // update balances
  86. // update nonces
  87. // get pending l2-tx from tx-pool
  88. l2TxsRaw, err := txsel.l2db.GetPendingTxs() // (batchID)
  89. if err != nil {
  90. return nil, nil, nil, err
  91. }
  92. var validTxs txs
  93. var l1CoordinatorTxs []*common.L1Tx
  94. // if tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB, if so,
  95. // tx is used. if tx.ToIdx==0, check if tx.ToEthAddr/tx.ToBJJ exist in
  96. // localAccountsDB, if yes tx is used; if not, check if tx.ToEthAddr is
  97. // in AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx of
  98. // CreateAccountAndDeposit is created.
  99. for i := 0; i < len(l2TxsRaw); i++ {
  100. if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
  101. _, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
  102. if err != nil {
  103. // tx not valid
  104. continue
  105. }
  106. // Account found in the DB, include the l2Tx in the selection
  107. validTxs = append(validTxs, l2TxsRaw[i])
  108. } else if l2TxsRaw[i].ToIdx == common.Idx(0) {
  109. idx := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  110. if idx != common.Idx(0) {
  111. // account for ToEthAddr/ToBJJ already exist,
  112. // there is no need to create a new one.
  113. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  114. validTxs = append(validTxs, l2TxsRaw[i])
  115. continue
  116. }
  117. // check if ToEthAddr is in AccountCreationAuths
  118. _, err := txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr) // TODO once l2db.GetAccountCreationAuth is ready, use the value returned as 'accAuth'
  119. if err != nil {
  120. // not found, l2Tx will not be added in the selection
  121. continue
  122. }
  123. validTxs = append(validTxs, l2TxsRaw[i])
  124. // create L1CoordinatorTx for the accountCreation
  125. l1CoordinatorTx := &common.L1Tx{
  126. UserOrigin: false,
  127. // FromEthAddr: accAuth.EthAddr, // TODO This 2 lines will panic, as l2db.GetAccountCreationAuth is not implemented yet and returns nil. Uncomment this 2 lines once l2db method is done.
  128. // FromBJJ: accAuth.BJJ,
  129. TokenID: l2TxsRaw[i].TokenID,
  130. LoadAmount: big.NewInt(0),
  131. Type: common.TxTypeCreateAccountDeposit,
  132. }
  133. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  134. } else if l2TxsRaw[i].ToIdx == common.Idx(1) {
  135. // valid txs (of Exit type)
  136. validTxs = append(validTxs, l2TxsRaw[i])
  137. }
  138. }
  139. // get most profitable L2-tx
  140. maxL2Txs := txsel.MaxTxs - uint64(len(l1CoordinatorTxs)) // - len(l1UserTxs)
  141. l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
  142. // TODO This 3 lines will panic, as l2db.GetAccountCreationAuth is not implemented yet and returns nil. Uncomment this lines once l2db method is done.
  143. // process the txs in the local AccountsDB
  144. // _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, l1Txs, l1CoordinatorTxs, l2Txs)
  145. // if err != nil {
  146. // return nil, nil, nil, err
  147. // }
  148. err = txsel.localAccountsDB.MakeCheckpoint()
  149. if err != nil {
  150. return nil, nil, nil, err
  151. }
  152. return l1Txs, l1CoordinatorTxs, l2Txs, nil
  153. }
  154. func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs {
  155. sort.Sort(txs)
  156. if len(txs) < int(max) {
  157. return txs
  158. }
  159. return txs[:max]
  160. }