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.

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