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.

273 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, 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. positionL1 := len(l1Txs)
  99. // if tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB, if so,
  100. // tx is used. if tx.ToIdx==0, check if tx.ToEthAddr/tx.ToBJJ exist in
  101. // localAccountsDB, if yes tx is used; if not, check if tx.ToEthAddr is
  102. // in AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx of
  103. // CreateAccountAndDeposit is created.
  104. for i := 0; i < len(l2TxsRaw); i++ {
  105. if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
  106. _, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
  107. if err != nil {
  108. // tx not valid
  109. log.Debugw("invalid L2Tx: ToIdx not found in StateDB", "ToIdx", l2TxsRaw[i].ToIdx)
  110. continue
  111. }
  112. // Account found in the DB, include the l2Tx in the selection
  113. validTxs = append(validTxs, l2TxsRaw[i])
  114. } else if l2TxsRaw[i].ToIdx == common.Idx(0) {
  115. if checkAlreadyPendingToCreate(l1CoordinatorTxs, l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ) {
  116. // if L2Tx needs a new L1CoordinatorTx of CreateAccount type,
  117. // and a previous L2Tx in the current process already created
  118. // a L1CoordinatorTx of this type, in the DB there still seem
  119. // that needs to create a new L1CoordinatorTx, but as is already
  120. // created, the tx is valid
  121. validTxs = append(validTxs, l2TxsRaw[i])
  122. continue
  123. }
  124. if !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.EmptyAddr.Bytes()) &&
  125. !bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) {
  126. // case: ToEthAddr != 0x00 neither 0xff
  127. var accAuth *common.AccountCreationAuth
  128. if l2TxsRaw[i].ToBJJ != nil {
  129. // case: ToBJJ!=0:
  130. // if idx exist for EthAddr&BJJ use it
  131. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  132. if err == nil {
  133. // account for ToEthAddr&ToBJJ already exist,
  134. // there is no need to create a new one.
  135. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  136. validTxs = append(validTxs, l2TxsRaw[i])
  137. continue
  138. }
  139. // if not, check if AccountCreationAuth exist for that ToEthAddr&BJJ
  140. // accAuth, err = txsel.l2db.GetAccountCreationAuthBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  141. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  142. if err != nil {
  143. // not found, l2Tx will not be added in the selection
  144. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  145. continue
  146. }
  147. if accAuth.BJJ != l2TxsRaw[i].ToBJJ {
  148. // if AccountCreationAuth.BJJ is not the same than in the tx, tx is not accepted
  149. 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)
  150. continue
  151. }
  152. validTxs = append(validTxs, l2TxsRaw[i])
  153. } else {
  154. // case: ToBJJ==0:
  155. // if idx exist for EthAddr use it
  156. _, err := txsel.localAccountsDB.GetIdxByEthAddr(l2TxsRaw[i].ToEthAddr)
  157. if err == nil {
  158. // account for ToEthAddr already exist,
  159. // there is no need to create a new one.
  160. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  161. validTxs = append(validTxs, l2TxsRaw[i])
  162. continue
  163. }
  164. // if not, check if AccountCreationAuth exist for that ToEthAddr
  165. accAuth, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr)
  166. if err != nil {
  167. // not found, l2Tx will not be added in the selection
  168. log.Debugw("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr found in AccountCreationAuths L2DB", "ToIdx", l2TxsRaw[i].ToIdx, "ToEthAddr", l2TxsRaw[i].ToEthAddr)
  169. continue
  170. }
  171. validTxs = append(validTxs, l2TxsRaw[i])
  172. }
  173. // create L1CoordinatorTx for the accountCreation
  174. l1CoordinatorTx := &common.L1Tx{
  175. Position: positionL1,
  176. UserOrigin: false,
  177. FromEthAddr: accAuth.EthAddr,
  178. FromBJJ: accAuth.BJJ,
  179. TokenID: l2TxsRaw[i].TokenID,
  180. LoadAmount: big.NewInt(0),
  181. Type: common.TxTypeCreateAccountDeposit,
  182. }
  183. positionL1++
  184. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  185. } else if bytes.Equal(l2TxsRaw[i].ToEthAddr.Bytes(), common.FFAddr.Bytes()) && l2TxsRaw[i].ToBJJ != nil {
  186. // if idx exist for EthAddr&BJJ use it
  187. _, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
  188. if err == nil {
  189. // account for ToEthAddr&ToBJJ already exist, (where ToEthAddr==0xff)
  190. // there is no need to create a new one.
  191. // tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
  192. validTxs = append(validTxs, l2TxsRaw[i])
  193. continue
  194. }
  195. // if idx don't exist for EthAddr&BJJ,
  196. // coordinator can create a new account without
  197. // L1Authorization, as ToEthAddr==0xff
  198. // create L1CoordinatorTx for the accountCreation
  199. l1CoordinatorTx := &common.L1Tx{
  200. Position: positionL1,
  201. UserOrigin: false,
  202. FromEthAddr: l2TxsRaw[i].ToEthAddr,
  203. FromBJJ: l2TxsRaw[i].ToBJJ,
  204. TokenID: l2TxsRaw[i].TokenID,
  205. LoadAmount: big.NewInt(0),
  206. Type: common.TxTypeCreateAccountDeposit,
  207. }
  208. positionL1++
  209. l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
  210. }
  211. } else if l2TxsRaw[i].ToIdx == common.Idx(1) {
  212. // valid txs (of Exit type)
  213. validTxs = append(validTxs, l2TxsRaw[i])
  214. }
  215. }
  216. // get most profitable L2-tx
  217. maxL2Txs := txsel.MaxTxs - uint64(len(l1CoordinatorTxs)) // - len(l1UserTxs)
  218. l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
  219. // process the txs in the local AccountsDB
  220. _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, l1Txs, l1CoordinatorTxs, l2Txs)
  221. if err != nil {
  222. return nil, nil, nil, err
  223. }
  224. err = txsel.localAccountsDB.MakeCheckpoint()
  225. if err != nil {
  226. return nil, nil, nil, err
  227. }
  228. return l1Txs, l1CoordinatorTxs, l2Txs, nil
  229. }
  230. func checkAlreadyPendingToCreate(l1CoordinatorTxs []*common.L1Tx, addr ethCommon.Address, bjj *babyjub.PublicKey) bool {
  231. for i := 0; i < len(l1CoordinatorTxs); i++ {
  232. if bytes.Equal(l1CoordinatorTxs[i].FromEthAddr.Bytes(), addr.Bytes()) {
  233. if bjj == nil {
  234. return true
  235. }
  236. if l1CoordinatorTxs[i].FromBJJ == bjj {
  237. return true
  238. }
  239. }
  240. }
  241. return false
  242. }
  243. func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs {
  244. sort.Sort(txs)
  245. if len(txs) < int(max) {
  246. return txs
  247. }
  248. return txs[:max]
  249. }