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.

262 lines
9.6 KiB

  1. package txselector
  2. // current: very simple version of TxSelector
  3. import (
  4. "bytes"
  5. "math/big"
  6. "sort"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/db/l2db"
  10. "github.com/hermeznetwork/hermez-node/db/statedb"
  11. "github.com/hermeznetwork/hermez-node/log"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. )
  14. // txs implements the interface Sort for an array of Tx
  15. type txs []common.PoolL2Tx
  16. func (t txs) Len() int {
  17. return len(t)
  18. }
  19. func (t txs) Swap(i, j int) {
  20. t[i], t[j] = t[j], t[i]
  21. }
  22. func (t txs) Less(i, j int) bool {
  23. return t[i].AbsoluteFee > t[j].AbsoluteFee
  24. }
  25. // TxSelector implements all the functionalities to select the txs for the next batch
  26. type TxSelector struct {
  27. // MaxL1UserTxs is the maximum L1-user-tx for a batch
  28. MaxL1UserTxs uint64
  29. // MaxL1OperatorTxs is the maximum L1-operator-tx for a batch
  30. MaxL1OperatorTxs uint64
  31. // MaxTxs is the maximum txs for a batch
  32. MaxTxs uint64
  33. l2db *l2db.L2DB
  34. localAccountsDB *statedb.LocalStateDB
  35. }
  36. // NewTxSelector returns a *TxSelector
  37. func NewTxSelector(dbpath string, synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs uint64) (*TxSelector, error) {
  38. localAccountsDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, statedb.TypeTxSelector, 0) // without merkletree
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &TxSelector{
  43. MaxL1UserTxs: maxL1UserTxs,
  44. MaxL1OperatorTxs: maxL1OperatorTxs,
  45. MaxTxs: maxTxs,
  46. l2db: l2,
  47. localAccountsDB: localAccountsDB,
  48. }, nil
  49. }
  50. // Reset tells the TxSelector to get it's internal AccountsDB
  51. // from the required `batchNum`
  52. func (txsel *TxSelector) Reset(batchNum common.BatchNum) error {
  53. err := txsel.localAccountsDB.Reset(batchNum, true)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. // GetL2TxSelection returns the L1CoordinatorTxs and a selection of the L2Txs
  60. // for the next batch, from the L2DB pool
  61. func (txsel *TxSelector) GetL2TxSelection(coordIdxs []common.Idx, batchNum common.BatchNum) ([]common.L1Tx, []common.PoolL2Tx, error) {
  62. _, l1CoordinatorTxs, l2Txs, err := txsel.GetL1L2TxSelection(coordIdxs, batchNum, []common.L1Tx{})
  63. return l1CoordinatorTxs, l2Txs, err
  64. }
  65. // GetL1L2TxSelection returns the selection of L1 + L2 txs
  66. func (txsel *TxSelector) GetL1L2TxSelection(coordIdxs []common.Idx, batchNum common.BatchNum, l1Txs []common.L1Tx) ([]common.L1Tx, []common.L1Tx, []common.PoolL2Tx, error) {
  67. // apply l1-user-tx to localAccountDB
  68. // create new leaves
  69. // update balances
  70. // update nonces
  71. // get pending l2-tx from tx-pool
  72. l2TxsRaw, err := txsel.l2db.GetPendingTxs() // (batchID)
  73. if err != nil {
  74. return nil, nil, nil, err
  75. }
  76. var validTxs txs
  77. var l1CoordinatorTxs []common.L1Tx
  78. positionL1 := len(l1Txs)
  79. // if tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB, if so,
  80. // tx is used. if tx.ToIdx==0, check if tx.ToEthAddr/tx.ToBJJ exist in
  81. // localAccountsDB, if yes tx is used; if not, check if tx.ToEthAddr is
  82. // in AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx of
  83. // CreateAccountAndDeposit is created.
  84. for i := 0; i < len(l2TxsRaw); i++ {
  85. if l2TxsRaw[i].ToIdx == 0 {
  86. if checkAlreadyPendingToCreate(l1CoordinatorTxs, l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ) {
  87. // if L2Tx needs a new L1CoordinatorTx of CreateAccount type,
  88. // and a previous L2Tx in the current process already created
  89. // a L1CoordinatorTx of this type, in the DB there still seem
  90. // that needs to create a new L1CoordinatorTx, but as is already
  91. // created, the tx is valid
  92. validTxs = append(validTxs, l2TxsRaw[i])
  93. continue
  94. }
  95. if !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.EmptyAddr.Bytes()) &&
  96. !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) {
  97. // case: ToEthAddr != 0x00 neither 0xff
  98. var accAuth *common.AccountCreationAuth
  99. if l2TxsRaw[i].ToBJJ != nil {
  100. // case: ToBJJ!=0:
  101. // if idx exist for EthAddr&BJJ use it
  102. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ, l2TxsRaw[i].TokenID)
  103. if err == nil {
  104. // account for ToEthAddr&ToBJJ already exist,
  105. // there is no need to create a new one.
  106. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  107. validTxs = append(validTxs, l2TxsRaw[i])
  108. continue
  109. }
  110. // if not, check if AccountCreationAuth exist for that ToEthAddr&BJJ
  111. // accAuth, err = txsel.l2db.GetAccountCreationAuthBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  112. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  113. if err != nil {
  114. // not found, l2Tx will not be added in the selection
  115. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  116. continue
  117. }
  118. if accAuth.BJJ != l2TxsRaw[i].ToBJJ {
  119. // if AccountCreationAuth.BJJ is not the same than in the tx, tx is not accepted
  120. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr, "ToBJJ", l2TxsRaw[i].ToBJJ)
  121. continue
  122. }
  123. validTxs = append(validTxs, l2TxsRaw[i])
  124. } else {
  125. // case: ToBJJ==0:
  126. // if idx exist for EthAddr use it
  127. _, err := txsel.localAccountsDB.GetIdxByEthAddr(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].TokenID)
  128. if err == nil {
  129. // account for ToEthAddr already exist,
  130. // there is no need to create a new one.
  131. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  132. validTxs = append(validTxs, l2TxsRaw[i])
  133. continue
  134. }
  135. // if not, check if AccountCreationAuth exist for that ToEthAddr
  136. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  137. if err != nil {
  138. // not found, l2Tx will not be added in the selection
  139. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  140. continue
  141. }
  142. validTxs = append(validTxs, l2TxsRaw[i])
  143. }
  144. // create L1CoordinatorTx for the accountCreation
  145. l1CoordinatorTx := common.L1Tx{
  146. Position: positionL1,
  147. UserOrigin: false,
  148. FromEthAddr: accAuth.EthAddr,
  149. FromBJJ: accAuth.BJJ,
  150. TokenID: l2TxsRaw[i].TokenID,
  151. LoadAmount: big.NewInt(0),
  152. Type: common.TxTypeCreateAccountDeposit,
  153. }
  154. positionL1++
  155. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  156. } else if bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) && l2TxsRaw[i].ToBJJ != nil {
  157. // if idx exist for EthAddr&BJJ use it
  158. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ, l2TxsRaw[i].TokenID)
  159. if err == nil {
  160. // account for ToEthAddr&ToBJJ already exist, (where ToEthAddr==0xff)
  161. // there is no need to create a new one.
  162. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  163. validTxs = append(validTxs, l2TxsRaw[i])
  164. continue
  165. }
  166. // if idx don't exist for EthAddr&BJJ,
  167. // coordinator can create a new account without
  168. // L1Authorization, as ToEthAddr==0xff
  169. // create L1CoordinatorTx for the accountCreation
  170. l1CoordinatorTx := common.L1Tx{
  171. Position: positionL1,
  172. UserOrigin: false,
  173. FromEthAddr: l2TxsRaw[i].ToEthAddr,
  174. FromBJJ: l2TxsRaw[i].ToBJJ,
  175. TokenID: l2TxsRaw[i].TokenID,
  176. LoadAmount: big.NewInt(0),
  177. Type: common.TxTypeCreateAccountDeposit,
  178. }
  179. positionL1++
  180. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  181. }
  182. } else if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
  183. _, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
  184. if err != nil {
  185. // tx not valid
  186. log.Debugw("invalid L2Tx: ToIdx not found in StateDB", "ToIdx", l2TxsRaw[i].ToIdx)
  187. continue
  188. }
  189. // Account found in the DB, include the l2Tx in the selection
  190. validTxs = append(validTxs, l2TxsRaw[i])
  191. } else if l2TxsRaw[i].ToIdx == common.Idx(1) {
  192. // valid txs (of Exit type)
  193. validTxs = append(validTxs, l2TxsRaw[i])
  194. }
  195. }
  196. // get most profitable L2-tx
  197. maxL2Txs := txsel.MaxTxs - uint64(len(l1CoordinatorTxs)) // - len(l1UserTxs)
  198. l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
  199. // process the txs in the local AccountsDB
  200. _, err = txsel.localAccountsDB.ProcessTxs(coordIdxs, l1Txs, l1CoordinatorTxs, l2Txs)
  201. if err != nil {
  202. return nil, nil, nil, err
  203. }
  204. err = txsel.localAccountsDB.MakeCheckpoint()
  205. if err != nil {
  206. return nil, nil, nil, err
  207. }
  208. return l1Txs, l1CoordinatorTxs, l2Txs, nil
  209. }
  210. func checkAlreadyPendingToCreate(l1CoordinatorTxs []common.L1Tx, addr ethCommon.Address, bjj *babyjub.PublicKey) bool {
  211. for i := 0; i < len(l1CoordinatorTxs); i++ {
  212. if bytes.Equal(l1CoordinatorTxs[i].FromEthAddr.Bytes(), addr.Bytes()) {
  213. if bjj == nil {
  214. return true
  215. }
  216. if l1CoordinatorTxs[i].FromBJJ == bjj {
  217. return true
  218. }
  219. }
  220. }
  221. return false
  222. }
  223. // getL2Profitable returns the profitable selection of L2Txssorted by Nonce
  224. func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs {
  225. sort.Sort(txs)
  226. if len(txs) < int(max) {
  227. return txs
  228. }
  229. txs = txs[:max]
  230. // sort l2Txs by Nonce. This can be done in many different ways, what
  231. // is needed is to output the txs where the Nonce of txs for each
  232. // Account is sorted, but the txs can not be grouped by sender Account
  233. // neither by Fee. This is because later on the Nonces will need to be
  234. // sequential for the zkproof generation.
  235. sort.SliceStable(txs, func(i, j int) bool {
  236. return txs[i].Nonce < txs[j].Nonce
  237. })
  238. return txs
  239. }