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