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	"math/big"
  7	"sort"
  8
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/hermeznetwork/hermez-node/db/l2db"
 11	"github.com/hermeznetwork/hermez-node/db/statedb"
 12)
 13
 14// txs implements the interface Sort for an array of Tx
 15type txs []*common.PoolL2Tx
 16
 17func (t txs) Len() int {
 18	return len(t)
 19}
 20func (t txs) Swap(i, j int) {
 21	t[i], t[j] = t[j], t[i]
 22}
 23func (t txs) Less(i, j int) bool {
 24	return t[i].AbsoluteFee > t[j].AbsoluteFee
 25}
 26
 27// TxSelector implements all the functionalities to select the txs for the next batch
 28type TxSelector struct {
 29	// MaxL1UserTxs is the maximum L1-user-tx for a batch
 30	MaxL1UserTxs uint64
 31	// MaxL1OperatorTxs is the maximum L1-operator-tx for a batch
 32	MaxL1OperatorTxs uint64
 33	// MaxTxs is the maximum txs for a batch
 34	MaxTxs uint64
 35
 36	l2db            *l2db.L2DB
 37	localAccountsDB *statedb.LocalStateDB
 38}
 39
 40// NewTxSelector returns a *TxSelector
 41func NewTxSelector(dbpath string, synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs uint64) (*TxSelector, error) {
 42	localAccountsDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, false, 0) // without merkletree
 43	if err != nil {
 44		return nil, err
 45	}
 46
 47	return &TxSelector{
 48		MaxL1UserTxs:     maxL1UserTxs,
 49		MaxL1OperatorTxs: maxL1OperatorTxs,
 50		MaxTxs:           maxTxs,
 51		l2db:             l2,
 52		localAccountsDB:  localAccountsDB,
 53	}, nil
 54}
 55
 56// Reset tells the TxSelector to get it's internal AccountsDB
 57// from the required `batchNum`
 58func (txsel *TxSelector) Reset(batchNum common.BatchNum) error {
 59	err := txsel.localAccountsDB.Reset(batchNum, true)
 60	if err != nil {
 61		return err
 62	}
 63	return nil
 64}
 65
 66// GetL2TxSelection returns a selection of the L2Txs for the next batch, from the L2DB pool
 67func (txsel *TxSelector) GetL2TxSelection(batchNum common.BatchNum) ([]*common.PoolL2Tx, error) {
 68	// get pending l2-tx from tx-pool
 69	l2TxsRaw, err := txsel.l2db.GetPendingTxs() // once l2db ready, maybe use parameter 'batchNum'
 70	if err != nil {
 71		return nil, err
 72	}
 73
 74	// discard the txs that don't have an Account in the AccountDB
 75	var validTxs txs
 76	for _, tx := range l2TxsRaw {
 77		_, err = txsel.localAccountsDB.GetAccount(tx.FromIdx)
 78		if err == nil {
 79			// if FromIdx has an account into the AccountsDB
 80			validTxs = append(validTxs, tx)
 81		}
 82	}
 83
 84	// get most profitable L2-tx
 85	txs := txsel.getL2Profitable(validTxs, txsel.MaxTxs)
 86
 87	// process the txs in the local AccountsDB
 88	_, _, err = txsel.localAccountsDB.ProcessTxs(false, false, nil, nil, txs)
 89	if err != nil {
 90		return nil, err
 91	}
 92	err = txsel.localAccountsDB.MakeCheckpoint()
 93	return txs, err
 94}
 95
 96// GetL1L2TxSelection returns the selection of L1 + L2 txs
 97func (txsel *TxSelector) GetL1L2TxSelection(batchNum common.BatchNum, l1Txs []*common.L1Tx) ([]*common.L1Tx, []*common.L1Tx, []*common.PoolL2Tx, error) {
 98	// apply l1-user-tx to localAccountDB
 99	//     create new leaves
100	//     update balances
101	//     update nonces
102
103	// get pending l2-tx from tx-pool
104	l2TxsRaw, err := txsel.l2db.GetPendingTxs() // (batchID)
105	if err != nil {
106		return nil, nil, nil, err
107	}
108
109	var validTxs txs
110	var l1CoordinatorTxs []*common.L1Tx
111
112	// if tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB, if so,
113	// tx is used.  if tx.ToIdx==0, check if tx.ToEthAddr/tx.ToBJJ exist in
114	// localAccountsDB, if yes tx is used; if not, check if tx.ToEthAddr is
115	// in AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx of
116	// CreateAccountAndDeposit is created.
117	for i := 0; i < len(l2TxsRaw); i++ {
118		if l2TxsRaw[i].ToIdx >= common.IdxUserThreshold {
119			_, err = txsel.localAccountsDB.GetAccount(l2TxsRaw[i].ToIdx)
120			if err != nil {
121				// tx not valid
122				continue
123			}
124			// Account found in the DB, include the l2Tx in the selection
125			validTxs = append(validTxs, l2TxsRaw[i])
126		} else if l2TxsRaw[i].ToIdx == common.Idx(0) {
127			_, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2TxsRaw[i].ToEthAddr, l2TxsRaw[i].ToBJJ)
128			if err == nil {
129				// account for ToEthAddr&ToBJJ already exist,
130				// there is no need to create a new one.
131				// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
132				validTxs = append(validTxs, l2TxsRaw[i])
133				continue
134			}
135			// check if ToEthAddr is in AccountCreationAuths
136			_, err = txsel.l2db.GetAccountCreationAuth(l2TxsRaw[i].ToEthAddr) // TODO once l2db.GetAccountCreationAuth is ready, use the value returned as 'accAuth'
137			if err != nil {
138				// not found, l2Tx will not be added in the selection
139				continue
140			}
141			validTxs = append(validTxs, l2TxsRaw[i])
142
143			// create L1CoordinatorTx for the accountCreation
144			l1CoordinatorTx := &common.L1Tx{
145				UserOrigin: false,
146				// FromEthAddr: accAuth.EthAddr, // TODO This 2 lines will panic, as l2db.GetAccountCreationAuth is not implemented yet and returns nil. Uncomment this 2 lines once l2db method is done.
147				// FromBJJ:     accAuth.BJJ,
148				TokenID:    l2TxsRaw[i].TokenID,
149				LoadAmount: big.NewInt(0),
150				Type:       common.TxTypeCreateAccountDeposit,
151			}
152			l1CoordinatorTxs = append(l1CoordinatorTxs, l1CoordinatorTx)
153		} else if l2TxsRaw[i].ToIdx == common.Idx(1) {
154			// valid txs (of Exit type)
155			validTxs = append(validTxs, l2TxsRaw[i])
156		}
157	}
158
159	// get most profitable L2-tx
160	maxL2Txs := txsel.MaxTxs - uint64(len(l1CoordinatorTxs)) // - len(l1UserTxs)
161	l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
162
163	// TODO This 3 lines will panic, as l2db.GetAccountCreationAuth is not implemented yet and returns nil. Uncomment this lines once l2db method is done.
164	// process the txs in the local AccountsDB
165	// _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, l1Txs, l1CoordinatorTxs, l2Txs)
166	// if err != nil {
167	//         return nil, nil, nil, err
168	// }
169	err = txsel.localAccountsDB.MakeCheckpoint()
170	if err != nil {
171		return nil, nil, nil, err
172	}
173
174	return l1Txs, l1CoordinatorTxs, l2Txs, nil
175}
176
177func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs {
178	sort.Sort(txs)
179	if len(txs) < int(max) {
180		return txs
181	}
182	return txs[:max]
183}