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.

268 lines
9.5 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, false, 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 a selection of the L2Txs for the next batch, from the L2DB pool
  60. func (txsel *TxSelector) GetL2TxSelection(batchNum common.BatchNum) ([]*common.PoolL2Tx, error) {
  61. // get pending l2-tx from tx-pool
  62. l2TxsRaw, err := txsel.l2db.GetPendingTxs() // once l2db ready, maybe use parameter 'batchNum'
  63. if err != nil {
  64. return nil, err
  65. }
  66. // discard the txs that don't have an Account in the AccountDB
  67. var validTxs txs
  68. for _, tx := range l2TxsRaw {
  69. _, err = txsel.localAccountsDB.GetAccount(tx.FromIdx)
  70. if err == nil {
  71. // if FromIdx has an account into the AccountsDB
  72. validTxs = append(validTxs, tx)
  73. }
  74. }
  75. // get most profitable L2-tx
  76. txs := txsel.getL2Profitable(validTxs, txsel.MaxTxs)
  77. // process the txs in the local AccountsDB
  78. _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, nil, nil, txs)
  79. if err != nil {
  80. return nil, err
  81. }
  82. err = txsel.localAccountsDB.MakeCheckpoint()
  83. return txs, err
  84. }
  85. // GetL1L2TxSelection returns the selection of L1 + L2 txs
  86. func (txsel *TxSelector) GetL1L2TxSelection(batchNum common.BatchNum, l1Txs []*common.L1Tx) ([]*common.L1Tx, []*common.L1Tx, []*common.PoolL2Tx, error) {
  87. // apply l1-user-tx to localAccountDB
  88. // create new leaves
  89. // update balances
  90. // update nonces
  91. // get pending l2-tx from tx-pool
  92. l2TxsRaw, err := txsel.l2db.GetPendingTxs() // (batchID)
  93. if err != nil {
  94. return nil, nil, nil, err
  95. }
  96. var validTxs txs
  97. var l1CoordinatorTxs []*common.L1Tx
  98. // if tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB, if so,
  99. // tx is used. if tx.ToIdx==0, check if tx.ToEthAddr/tx.ToBJJ exist in
  100. // localAccountsDB, if yes tx is used; if not, check if tx.ToEthAddr is
  101. // in AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx of
  102. // CreateAccountAndDeposit is created.
  103. for i := 0; i < len(l2TxsRaw); i++ {
  104. if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
  105. _, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
  106. if err != nil {
  107. // tx not valid
  108. log.Debugw("invalid L2Tx: ToIdx not found in StateDB", "ToIdx", l2TxsRaw[i].ToIdx)
  109. continue
  110. }
  111. // Account found in the DB, include the l2Tx in the selection
  112. validTxs = append(validTxs, l2TxsRaw[i])
  113. } else if l2TxsRaw[i].ToIdx == common.Idx(0) {
  114. if checkAlreadyPendingToCreate(l1CoordinatorTxs, l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ) {
  115. // if L2Tx needs a new L1CoordinatorTx of CreateAccount type,
  116. // and a previous L2Tx in the current process already created
  117. // a L1CoordinatorTx of this type, in the DB there still seem
  118. // that needs to create a new L1CoordinatorTx, but as is already
  119. // created, the tx is valid
  120. validTxs = append(validTxs, l2TxsRaw[i])
  121. continue
  122. }
  123. if !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.EmptyAddr.Bytes()) &&
  124. !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) {
  125. // case: ToEthAddr != 0x00 neither 0xff
  126. var accAuth *common.AccountCreationAuth
  127. if l2TxsRaw[i].ToBJJ != nil {
  128. // case: ToBJJ!=0:
  129. // if idx exist for EthAddr&BJJ use it
  130. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  131. if err == nil {
  132. // account for ToEthAddr&ToBJJ already exist,
  133. // there is no need to create a new one.
  134. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  135. validTxs = append(validTxs, l2TxsRaw[i])
  136. continue
  137. }
  138. // if not, check if AccountCreationAuth exist for that ToEthAddr&BJJ
  139. // accAuth, err = txsel.l2db.GetAccountCreationAuthBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  140. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  141. if err != nil {
  142. // not found, l2Tx will not be added in the selection
  143. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  144. continue
  145. }
  146. if accAuth.BJJ != l2TxsRaw[i].ToBJJ {
  147. // if AccountCreationAuth.BJJ is not the same than in the tx, tx is not accepted
  148. 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)
  149. continue
  150. }
  151. validTxs = append(validTxs, l2TxsRaw[i])
  152. } else {
  153. // case: ToBJJ==0:
  154. // if idx exist for EthAddr use it
  155. _, err := txsel.localAccountsDB.GetIdxByEthAddr(l2TxsRaw[i].ToEthAddr)
  156. if err == nil {
  157. // account for ToEthAddr already exist,
  158. // there is no need to create a new one.
  159. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  160. validTxs = append(validTxs, l2TxsRaw[i])
  161. continue
  162. }
  163. // if not, check if AccountCreationAuth exist for that ToEthAddr
  164. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  165. if err != nil {
  166. // not found, l2Tx will not be added in the selection
  167. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  168. continue
  169. }
  170. validTxs = append(validTxs, l2TxsRaw[i])
  171. }
  172. // create L1CoordinatorTx for the accountCreation
  173. l1CoordinatorTx := &common.L1Tx{
  174. UserOrigin: false,
  175. FromEthAddr: accAuth.EthAddr,
  176. FromBJJ: accAuth.BJJ,
  177. TokenID: l2TxsRaw[i].TokenID,
  178. LoadAmount: big.NewInt(0),
  179. Type: common.TxTypeCreateAccountDeposit,
  180. }
  181. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  182. } else if bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) && l2TxsRaw[i].ToBJJ != nil {
  183. // if idx exist for EthAddr&BJJ use it
  184. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  185. if err == nil {
  186. // account for ToEthAddr&ToBJJ already exist, (where ToEthAddr==0xff)
  187. // there is no need to create a new one.
  188. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  189. validTxs = append(validTxs, l2TxsRaw[i])
  190. continue
  191. }
  192. // if idx don't exist for EthAddr&BJJ,
  193. // coordinator can create a new account without
  194. // L1Authorization, as ToEthAddr==0xff
  195. // create L1CoordinatorTx for the accountCreation
  196. l1CoordinatorTx := &common.L1Tx{
  197. UserOrigin: false,
  198. FromEthAddr: l2TxsRaw[i].ToEthAddr,
  199. FromBJJ: l2TxsRaw[i].ToBJJ,
  200. TokenID: l2TxsRaw[i].TokenID,
  201. LoadAmount: big.NewInt(0),
  202. Type: common.TxTypeCreateAccountDeposit,
  203. }
  204. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  205. }
  206. } else if l2TxsRaw[i].ToIdx == common.Idx(1) {
  207. // valid txs (of Exit type)
  208. validTxs = append(validTxs, l2TxsRaw[i])
  209. }
  210. }
  211. // get most profitable L2-tx
  212. maxL2Txs := txsel.MaxTxs - uint64(len(l1CoordinatorTxs)) // - len(l1UserTxs)
  213. l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
  214. // process the txs in the local AccountsDB
  215. _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, l1Txs, l1CoordinatorTxs, l2Txs)
  216. if err != nil {
  217. return nil, nil, nil, err
  218. }
  219. err = txsel.localAccountsDB.MakeCheckpoint()
  220. if err != nil {
  221. return nil, nil, nil, err
  222. }
  223. return l1Txs, l1CoordinatorTxs, l2Txs, nil
  224. }
  225. func checkAlreadyPendingToCreate(l1CoordinatorTxs []*common.L1Tx, addr ethCommon.Address, bjj *babyjub.PublicKey) bool {
  226. for i := 0; i < len(l1CoordinatorTxs); i++ {
  227. if bytes.Equal(l1CoordinatorTxs[i].FromEthAddr.Bytes(), addr.Bytes()) {
  228. if bjj == nil {
  229. return true
  230. }
  231. if l1CoordinatorTxs[i].FromBJJ == bjj {
  232. return true
  233. }
  234. }
  235. }
  236. return false
  237. }
  238. func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs {
  239. sort.Sort(txs)
  240. if len(txs) < int(max) {
  241. return txs
  242. }
  243. return txs[:max]
  244. }