arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/txselector/txselector.go
txselector/txselector.goRaw
  1package txselector
  2
  3// current: very simple version of TxSelector
  4
  5import (
  6	"bytes"
  7	"fmt"
  8	"math/big"
  9	"sort"
 10
 11	ethCommon "github.com/ethereum/go-ethereum/common"
 12	"github.com/hermeznetwork/hermez-node/common"
 13	"github.com/hermeznetwork/hermez-node/db/l2db"
 14	"github.com/hermeznetwork/hermez-node/db/statedb"
 15	"github.com/hermeznetwork/hermez-node/log"
 16	"github.com/hermeznetwork/tracerr"
 17	"github.com/iden3/go-iden3-crypto/babyjub"
 18	"github.com/iden3/go-merkletree/db"
 19	"github.com/iden3/go-merkletree/db/pebble"
 20)
 21
 22const (
 23	// PathCoordIdxsDB defines the path of the key-value db where the
 24	// CoordIdxs will be stored
 25	PathCoordIdxsDB = "/coordidxs"
 26)
 27
 28// txs implements the interface Sort for an array of Tx
 29type txs []common.PoolL2Tx
 30
 31func (t txs) Len() int {
 32	return len(t)
 33}
 34func (t txs) Swap(i, j int) {
 35	t[i], t[j] = t[j], t[i]
 36}
 37func (t txs) Less(i, j int) bool {
 38	return t[i].AbsoluteFee > t[j].AbsoluteFee
 39}
 40
 41// CoordAccount contains the data of the Coordinator account, that will be used
 42// to create new transactions of CreateAccountDeposit type to add new TokenID
 43// accounts for the Coordinator to receive the fees.
 44type CoordAccount struct {
 45	Addr                ethCommon.Address
 46	BJJ                 babyjub.PublicKeyComp
 47	AccountCreationAuth []byte
 48}
 49
 50// SelectionConfig contains the parameters of configuration of the selection of
 51// transactions for the next batch
 52type SelectionConfig struct {
 53	// MaxL1UserTxs is the maximum L1-user-tx for a batch
 54	MaxL1UserTxs uint64
 55	// MaxL1CoordinatorTxs is the maximum L1-coordinator-tx for a batch
 56	MaxL1CoordinatorTxs uint64
 57
 58	// ProcessTxsConfig contains the config for ProcessTxs
 59	ProcessTxsConfig statedb.ProcessTxsConfig
 60}
 61
 62// TxSelector implements all the functionalities to select the txs for the next
 63// batch
 64type TxSelector struct {
 65	l2db            *l2db.L2DB
 66	localAccountsDB *statedb.LocalStateDB
 67
 68	coordAccount *CoordAccount
 69	coordIdxsDB  *pebble.PebbleStorage
 70}
 71
 72// NewTxSelector returns a *TxSelector
 73func NewTxSelector(coordAccount *CoordAccount, dbpath string,
 74	synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB) (*TxSelector, error) {
 75	localAccountsDB, err := statedb.NewLocalStateDB(dbpath, 128,
 76		synchronizerStateDB, statedb.TypeTxSelector, 0) // without merkletree
 77	if err != nil {
 78		return nil, tracerr.Wrap(err)
 79	}
 80
 81	coordIdxsDB, err := pebble.NewPebbleStorage(dbpath+PathCoordIdxsDB, false)
 82	if err != nil {
 83		return nil, tracerr.Wrap(err)
 84	}
 85
 86	return &TxSelector{
 87		l2db:            l2,
 88		localAccountsDB: localAccountsDB,
 89		coordAccount:    coordAccount,
 90		coordIdxsDB:     coordIdxsDB,
 91	}, nil
 92}
 93
 94// LocalAccountsDB returns the LocalStateDB of the TxSelector
 95func (txsel *TxSelector) LocalAccountsDB() *statedb.LocalStateDB {
 96	return txsel.localAccountsDB
 97}
 98
 99// Reset tells the TxSelector to get it's internal AccountsDB
100// from the required `batchNum`
101func (txsel *TxSelector) Reset(batchNum common.BatchNum) error {
102	err := txsel.localAccountsDB.Reset(batchNum, true)
103	if err != nil {
104		return tracerr.Wrap(err)
105	}
106	return nil
107}
108
109// AddCoordIdxs stores the given TokenID with the correspondent Idx to the
110// CoordIdxsDB
111func (txsel *TxSelector) AddCoordIdxs(idxs map[common.TokenID]common.Idx) error {
112	tx, err := txsel.coordIdxsDB.NewTx()
113	if err != nil {
114		return tracerr.Wrap(err)
115	}
116	for tokenID, idx := range idxs {
117		idxBytes, err := idx.Bytes()
118		if err != nil {
119			return tracerr.Wrap(err)
120		}
121		err = tx.Put(tokenID.Bytes(), idxBytes[:])
122		if err != nil {
123			return tracerr.Wrap(err)
124		}
125	}
126	if err := tx.Commit(); err != nil {
127		return tracerr.Wrap(err)
128	}
129	return nil
130}
131
132// GetCoordIdxs returns a map with the stored TokenID with the correspondent
133// Coordinator Idx
134func (txsel *TxSelector) GetCoordIdxs() (map[common.TokenID]common.Idx, error) {
135	r := make(map[common.TokenID]common.Idx)
136	err := txsel.coordIdxsDB.Iterate(func(tokenIDBytes []byte, idxBytes []byte) (bool, error) {
137		idx, err := common.IdxFromBytes(idxBytes)
138		if err != nil {
139			return false, tracerr.Wrap(err)
140		}
141		tokenID, err := common.TokenIDFromBytes(tokenIDBytes)
142		if err != nil {
143			return false, tracerr.Wrap(err)
144		}
145		r[tokenID] = idx
146		return true, nil
147	})
148
149	return r, tracerr.Wrap(err)
150}
151
152//nolint:unused
153func (txsel *TxSelector) coordAccountForTokenID(l1CoordinatorTxs []common.L1Tx, tokenID common.TokenID, positionL1 int) (*common.L1Tx, int, error) {
154	// check if CoordinatorAccount for TokenID is already pending to create
155	if checkAlreadyPendingToCreate(l1CoordinatorTxs, tokenID, txsel.coordAccount.Addr, txsel.coordAccount.BJJ) {
156		return nil, positionL1, nil
157	}
158
159	_, err := txsel.coordIdxsDB.Get(tokenID.Bytes())
160	if tracerr.Unwrap(err) == db.ErrNotFound {
161		// create L1CoordinatorTx to create new CoordAccount for TokenID
162		l1CoordinatorTx := common.L1Tx{
163			Position:      positionL1,
164			UserOrigin:    false,
165			FromEthAddr:   txsel.coordAccount.Addr,
166			FromBJJ:       txsel.coordAccount.BJJ,
167			TokenID:       tokenID,
168			DepositAmount: big.NewInt(0),
169			Type:          common.TxTypeCreateAccountDeposit,
170		}
171		positionL1++
172
173		return &l1CoordinatorTx, positionL1, nil
174	}
175	if err != nil {
176		return nil, positionL1, tracerr.Wrap(err)
177	}
178	// CoordAccount for TokenID already exists
179	return nil, positionL1, nil
180}
181
182// GetL2TxSelection returns the L1CoordinatorTxs and a selection of the L2Txs
183// for the next batch, from the L2DB pool
184func (txsel *TxSelector) GetL2TxSelection(selectionConfig *SelectionConfig,
185	batchNum common.BatchNum) ([]common.Idx, [][]byte, []common.L1Tx, []common.PoolL2Tx, error) {
186	coordIdxs, accCreationAuths, _, l1CoordinatorTxs, l2Txs, err := txsel.GetL1L2TxSelection(selectionConfig, batchNum,
187		[]common.L1Tx{})
188	return coordIdxs, accCreationAuths, l1CoordinatorTxs, l2Txs, tracerr.Wrap(err)
189}
190
191// GetL1L2TxSelection returns the selection of L1 + L2 txs
192func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig *SelectionConfig,
193	batchNum common.BatchNum, l1Txs []common.L1Tx) ([]common.Idx, [][]byte, []common.L1Tx, []common.L1Tx,
194	[]common.PoolL2Tx, error) {
195	// TODO WIP this method uses a 'cherry-pick' of internal calls of the
196	// StateDB, a refactor of the StateDB to reorganize it internally is
197	// planned once the main functionallities are covered, with that
198	// refactor the TxSelector will be updated also
199
200	// apply l1-user-tx to localAccountDB
201	//     create new leaves
202	//     update balances
203	//     update nonces
204
205	// get existing CoordIdxs
206	coordIdxsMap, err := txsel.GetCoordIdxs()
207	if err != nil {
208		return nil, nil, nil, nil, nil, tracerr.Wrap(err)
209	}
210	var coordIdxs []common.Idx
211	for tokenID := range coordIdxsMap {
212		coordIdxs = append(coordIdxs, coordIdxsMap[tokenID])
213	}
214
215	// get pending l2-tx from tx-pool
216	l2TxsRaw, err := txsel.l2db.GetPendingTxs() // (batchID)
217	if err != nil {
218		return nil, nil, nil, nil, nil, tracerr.Wrap(err)
219	}
220
221	var validTxs txs
222	var l1CoordinatorTxs []common.L1Tx
223	positionL1 := len(l1Txs)
224
225	// Process L1UserTxs
226	for i := 0; i < len(l1Txs); i++ {
227		// assumption: l1usertx are sorted by L1Tx.Position
228		_, _, _, _, err := txsel.localAccountsDB.ProcessL1Tx(nil, &l1Txs[i])
229		if err != nil {
230			return nil, nil, nil, nil, nil, tracerr.Wrap(err)
231		}
232	}
233
234	// get last idx from LocalStateDB
235	// lastIdx := txsel.localStateDB.idx
236	// update lastIdx with the L1UserTxs (of account creation)
237
238	for i := 0; i < len(l2TxsRaw); i++ {
239		// If tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB,
240		// if so, tx is used.  If tx.ToIdx==0, for an L2Tx will be the
241		// case of TxToEthAddr or TxToBJJ, check if
242		// tx.ToEthAddr/tx.ToBJJ exist in localAccountsDB, if yes tx is
243		// used; if not, check if tx.ToEthAddr is in
244		// AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx
245		// of CreateAccountAndDeposit is created. If tx.ToIdx==1, is a
246		// Exit type and is used.
247		if l2TxsRaw[i].ToIdx == 0 { // ToEthAddr/ToBJJ case
248			validTxs, l1CoordinatorTxs, positionL1, err =
249				txsel.processTxToEthAddrBJJ(validTxs, l1CoordinatorTxs,
250					positionL1, l2TxsRaw[i])
251			if err != nil {
252				log.Debug(err)
253				continue
254			}
255		} else if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
256			_, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
257			if err != nil {
258				// tx not valid
259				log.Debugw("invalid L2Tx: ToIdx not found in StateDB",
260					"ToIdx", l2TxsRaw[i].ToIdx)
261				continue
262			}
263
264			// TODO if EthAddr!=0 or BJJ!=0, check that ToIdxAccount.EthAddr or BJJ
265
266			// Account found in the DB, include the l2Tx in the selection
267			validTxs = append(validTxs, l2TxsRaw[i])
268		} else if l2TxsRaw[i].ToIdx == common.Idx(1) {
269			// valid txs (of Exit type)
270			validTxs = append(validTxs, l2TxsRaw[i])
271		}
272		// TODO if needed add L1CoordinatorTx to create a Coordinator
273		// account for the new TokenID
274		// var newL1CoordTx *common.L1Tx
275		// newL1CoordTx, positionL1, err = txsel.coordAccountForTokenID(l1CoordinatorTxs, l2TxsRaw[i].TokenID, positionL1)
276		// if err != nil {
277		//         return nil, nil, nil, nil, nil, tracerr.Wrap(err)
278		// }
279		// if newL1CoordTx != nil {
280		//         l1CoordinatorTxs = append(l1CoordinatorTxs, *newL1CoordTx)
281		// }
282	}
283
284	// Process L1CoordinatorTxs
285	for i := 0; i < len(l1CoordinatorTxs); i++ {
286		fmt.Println("PRINT", i, &l1CoordinatorTxs[i])
287		_, _, _, _, err := txsel.localAccountsDB.ProcessL1Tx(nil, &l1CoordinatorTxs[i])
288		if err != nil {
289			return nil, nil, nil, nil, nil, tracerr.Wrap(err)
290		}
291	}
292	txsel.localAccountsDB.AccumulatedFees = make(map[common.Idx]*big.Int)
293	for _, idx := range coordIdxs {
294		txsel.localAccountsDB.AccumulatedFees[idx] = big.NewInt(0)
295	}
296
297	// once L1UserTxs & L1CoordinatorTxs are processed, get TokenIDs of
298	// coordIdxs. In this way, if a coordIdx uses an Idx that is being
299	// created in the current batch, at this point the Idx will be created
300	coordIdxsMap, err = txsel.localAccountsDB.GetTokenIDsFromIdxs(coordIdxs)
301	if err != nil {
302		return nil, nil, nil, nil, nil, tracerr.Wrap(err)
303	}
304
305	// get most profitable L2-tx
306	maxL2Txs := selectionConfig.ProcessTxsConfig.MaxTx - uint32(len(l1CoordinatorTxs)) // - len(l1UserTxs) // TODO if there are L1UserTxs take them in to account
307	l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
308
309	// Process L2Txs
310	for i := 0; i < len(l2Txs); i++ {
311		_, _, _, err = txsel.localAccountsDB.ProcessL2Tx(coordIdxsMap, nil, nil, &l2Txs[i])
312		if err != nil {
313			return nil, nil, nil, nil, nil, tracerr.Wrap(err)
314		}
315	}
316	err = txsel.AddCoordIdxs(coordIdxsMap)
317	if err != nil {
318		return nil, nil, nil, nil, nil, tracerr.Wrap(err)
319	}
320
321	err = txsel.localAccountsDB.MakeCheckpoint()
322	if err != nil {
323		return nil, nil, nil, nil, nil, tracerr.Wrap(err)
324	}
325
326	// TODO
327	auths := make([][]byte, len(l1CoordinatorTxs))
328	for i := range auths {
329		auths[i] = make([]byte, 65)
330	}
331	return nil, auths, l1Txs, l1CoordinatorTxs, l2Txs, nil
332}
333
334// processTxsToEthAddrBJJ process the common.PoolL2Tx in the case where
335// ToIdx==0, which can be the tx type of ToEthAddr or ToBJJ. If the receiver
336// does not have an account yet, a new L1CoordinatorTx of type
337// CreateAccountDeposit (with 0 as DepositAmount) is created and added to the
338// l1CoordinatorTxs array, and then the PoolL2Tx is added into the validTxs
339// array.
340func (txsel *TxSelector) processTxToEthAddrBJJ(validTxs txs, l1CoordinatorTxs []common.L1Tx,
341	positionL1 int, l2Tx common.PoolL2Tx) (txs, []common.L1Tx, int, error) {
342	// if L2Tx needs a new L1CoordinatorTx of CreateAccount type, and a
343	// previous L2Tx in the current process already created a
344	// L1CoordinatorTx of this type, in the DB there still seem that needs
345	// to create a new L1CoordinatorTx, but as is already created, the tx
346	// is valid
347	if checkAlreadyPendingToCreate(l1CoordinatorTxs, l2Tx.TokenID, l2Tx.ToEthAddr, l2Tx.ToBJJ) {
348		validTxs = append(validTxs, l2Tx)
349		return validTxs, l1CoordinatorTxs, positionL1, nil
350	}
351
352	if !bytes.Equal(l2Tx.ToEthAddr.Bytes(), common.EmptyAddr.Bytes()) &&
353		!bytes.Equal(l2Tx.ToEthAddr.Bytes(), common.FFAddr.Bytes()) {
354		// case: ToEthAddr != 0x00 neither 0xff
355		var accAuth *common.AccountCreationAuth
356		if l2Tx.ToBJJ != common.EmptyBJJComp {
357			// case: ToBJJ!=0:
358			// if idx exist for EthAddr&BJJ use it
359			_, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2Tx.ToEthAddr,
360				l2Tx.ToBJJ, l2Tx.TokenID)
361			if err == nil {
362				// account for ToEthAddr&ToBJJ already exist,
363				// there is no need to create a new one.
364				// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
365				validTxs = append(validTxs, l2Tx)
366				return validTxs, l1CoordinatorTxs, positionL1, nil
367			}
368			// if not, check if AccountCreationAuth exist for that
369			// ToEthAddr
370			accAuth, err = txsel.l2db.GetAccountCreationAuth(l2Tx.ToEthAddr)
371			if err != nil {
372				// not found, l2Tx will not be added in the selection
373				return validTxs, l1CoordinatorTxs, positionL1, tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr found in AccountCreationAuths L2DB. ToIdx: %d, ToEthAddr: %s",
374					l2Tx.ToIdx, l2Tx.ToEthAddr.Hex()))
375			}
376			if accAuth.BJJ != l2Tx.ToBJJ {
377				// if AccountCreationAuth.BJJ is not the same
378				// than in the tx, tx is not accepted
379				return validTxs, l1CoordinatorTxs, positionL1, tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB. ToIdx: %d, ToEthAddr: %s, ToBJJ: %s",
380					l2Tx.ToIdx, l2Tx.ToEthAddr.Hex(), l2Tx.ToBJJ.String()))
381			}
382			validTxs = append(validTxs, l2Tx)
383		} else {
384			// case: ToBJJ==0:
385			// if idx exist for EthAddr use it
386			_, err := txsel.localAccountsDB.GetIdxByEthAddr(l2Tx.ToEthAddr, l2Tx.TokenID)
387			if err == nil {
388				// account for ToEthAddr already exist,
389				// there is no need to create a new one.
390				// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
391				validTxs = append(validTxs, l2Tx)
392				return validTxs, l1CoordinatorTxs, positionL1, nil
393			}
394			// if not, check if AccountCreationAuth exist for that ToEthAddr
395			accAuth, err = txsel.l2db.GetAccountCreationAuth(l2Tx.ToEthAddr)
396			if err != nil {
397				// not found, l2Tx will not be added in the selection
398				return validTxs, l1CoordinatorTxs, positionL1, tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in StateDB, neither ToEthAddr found in AccountCreationAuths L2DB. ToIdx: %d, ToEthAddr: %s",
399					l2Tx.ToIdx, l2Tx.ToEthAddr))
400			}
401			validTxs = append(validTxs, l2Tx)
402		}
403		// create L1CoordinatorTx for the accountCreation
404		l1CoordinatorTx := common.L1Tx{
405			Position:      positionL1,
406			UserOrigin:    false,
407			FromEthAddr:   accAuth.EthAddr,
408			FromBJJ:       accAuth.BJJ,
409			TokenID:       l2Tx.TokenID,
410			DepositAmount: big.NewInt(0),
411			Type:          common.TxTypeCreateAccountDeposit,
412		}
413		positionL1++
414		l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
415	} else if bytes.Equal(l2Tx.ToEthAddr.Bytes(), common.FFAddr.Bytes()) && l2Tx.ToBJJ != common.EmptyBJJComp {
416		// if idx exist for EthAddr&BJJ use it
417		_, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2Tx.ToEthAddr, l2Tx.ToBJJ,
418			l2Tx.TokenID)
419		if err == nil {
420			// account for ToEthAddr&ToBJJ already exist, (where ToEthAddr==0xff)
421			// there is no need to create a new one.
422			// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
423			validTxs = append(validTxs, l2Tx)
424			return validTxs, l1CoordinatorTxs, positionL1, nil
425		}
426		// if idx don't exist for EthAddr&BJJ,
427		// coordinator can create a new account without
428		// L1Authorization, as ToEthAddr==0xff
429		// create L1CoordinatorTx for the accountCreation
430		l1CoordinatorTx := common.L1Tx{
431			Position:      positionL1,
432			UserOrigin:    false,
433			FromEthAddr:   l2Tx.ToEthAddr,
434			FromBJJ:       l2Tx.ToBJJ,
435			TokenID:       l2Tx.TokenID,
436			DepositAmount: big.NewInt(0),
437			Type:          common.TxTypeCreateAccountDeposit,
438		}
439		positionL1++
440		l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
441	}
442
443	return validTxs, l1CoordinatorTxs, positionL1, nil
444}
445
446func checkAlreadyPendingToCreate(l1CoordinatorTxs []common.L1Tx, tokenID common.TokenID,
447	addr ethCommon.Address, bjj babyjub.PublicKeyComp) bool {
448	for i := 0; i < len(l1CoordinatorTxs); i++ {
449		if bytes.Equal(l1CoordinatorTxs[i].FromEthAddr.Bytes(), addr.Bytes()) &&
450			l1CoordinatorTxs[i].TokenID == tokenID &&
451			l1CoordinatorTxs[i].FromBJJ == bjj {
452			return true
453		}
454	}
455	return false
456}
457
458// getL2Profitable returns the profitable selection of L2Txssorted by Nonce
459func (txsel *TxSelector) getL2Profitable(txs txs, max uint32) txs {
460	sort.Sort(txs)
461	if len(txs) < int(max) {
462		return txs
463	}
464	txs = txs[:max]
465
466	// sort l2Txs by Nonce. This can be done in many different ways, what
467	// is needed is to output the txs where the Nonce of txs for each
468	// Account is sorted, but the txs can not be grouped by sender Account
469	// neither by Fee. This is because later on the Nonces will need to be
470	// sequential for the zkproof generation.
471	sort.SliceStable(txs, func(i, j int) bool {
472		return txs[i].Nonce < txs[j].Nonce
473	})
474
475	return txs
476}