forked from hermeznetwork/hermez-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txselector.go
1257 lines (1188 loc) · 50.3 KB
/
txselector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Package txselector is responsible to choose the transactions from the pool that will be forged in the next batch.
The main goal is to come with the most profitable selection, always respecting the constrains of the protocol.
This constrains can be splitted in two categories:
Batch constrains (this information is passed to the txselector as `selectionConfig txprocessor.Config`):
- NLevels: limit the amount of accounts that can be created by NLevels^2 -1.
Note that this constraint is not properly checked, if this situation is reached the entire selection will fail
- MaxFeeTx: maximum amount of different coordinator accounts (idx) that can be used to collect fees.
Note that this constraint is not checked right now, if this situation is reached the `txprocessor` will fail later on preparing the `zki`
- MaxTx: maximum amount of transactions that can fit in a batch, in other words: len(l1UserTxs) + len(l1CoordinatorTxs) + len(l2Txs) <= MaxTx
- MaxL1Tx: maximum amount of L1 transactions that can fit in a batch, in other words: len(l1UserTxs) + len(l1CoordinatorTxs) <= MaxL1Tx
Transaction constrains (this takes into consideration the txs fetched from the pool and the current state stored in StateDB):
- Sender account exists: `FromIdx` must exist in the `StateDB`, and `tx.TokenID == account.TokenID` has to be respected
- Sender account has enough balance: tx.Amount + fee <= account.Balance
- Sender account has correct nonce: `tx.Nonce == account.Nonce`
- Recipient account exists or can be created:
- In case of transfer to Idx: account MUST already exist on StateDB, and match the `tx.TokenID`
- In case of transfer to Ethereum address: if the account doesn't exists, it can be created through a `l1CoordinatorTx` IF there is a valid `AccountCreationAuthorization`
- In case of transfer to BJJ: if the account doesn't exists, it can be created through a `l1CoordinatorTx` (no need for `AccountCreationAuthorization`)
- Atomic transactions: requested transaction exist and can be linked,
according to the `RqOffset` spec: https://docs.hermez.io/#/developers/protocol/hermez-protocol/circuits/circuits?id=rq-tx-verifier
Important considerations:
- It's assumed that signatures are correct, since they're checked before inserting txs to the pool
- The state is processed sequentially meaning that each tx that is selected affects the state, in other words:
the order in which txs are selected can make other txs became valid or invalid.
This specially relevant for the constrains `Sender account has enough balance` and `Sender account has correct nonce`
- The creation of accounts using `l1CoordinatorTxs` increments the amount of used L1 transactions.
This has to be taken into consideration for the constrain `MaxL1Tx`
Current implementation:
The current approach is simple but effective, specially in a scenario of not having a lot of transactions in the pool most of the time:
0. Process L1UserTxs (this transactions come from the Blockchain and it's mandatory by protocol to forge them)
1. Get transactions from the pool
2. Order transactions by (nonce, fee in USD)
3. Selection loop: iterate over the sorted transactions and split in selected and non selected.
Repeat this process with the non-selected of each iteration until one iteration doesn't return any selected txs
Note that this step is the one that ensures that the constrains are respected.
4. Choose coordinator idxs to collect the fees. Note that `MaxFeeTx` constrain is ignored in this step.
5. Return the selected L2 txs as well as the necessary `l1CoordinatorTxs` the `l1UserTxs`
(this is redundant as the return will always be the same as the input) and the coordinator idxs used to collect fees
The previous flow description doesn't take in consideration the constrain `Atomic transactions`.
This constrain alters the previous step as follow:
- Atomic transactions are grouped into `AtomicGroups`, and each group has an average fee that is used to sort the transactions together with the non atomic transactions,
in a way that all the atomic transactions from the same group preserve the relative order as found in the pool.
This is done in this way because it's assumed that transactions from the same `AtomicGroup`
have the same `AtomicGroupID`, and are ordered with valid `RqOffset` in the pool.
- If one atomic transaction fails to be processed in the `Selection loop`,
the group will be marked as invalid and the entire process will reboot from the beginning with the only difference being that
txs belonging to failed atomic groups will be discarded before reaching the `Selection loop`.
This is done this way because the state is altered sequentially, so if a transaction belonging to an atomic group is selected,
but later on a transaction from the same group can't be selected, the selection will be invalid since there will be a selected tx that depends on a tx that
doesn't exist in the selection. Right now the mechanism that the StateDB has to revert changes is to go back to a previous checkpoint (checkpoints are created per batch).
This limitation forces the txselector to restart from the beginning of the batch selection.
This should be improved once the StateDB has more granular mechanisms to revert the effects of processed txs.
*/
package txselector
// current: very simple version of TxSelector
import (
"fmt"
"math/big"
"sort"
"github.com/arnaubennassar/hermez-node/v2/api"
"github.com/arnaubennassar/hermez-node/v2/common"
"github.com/arnaubennassar/hermez-node/v2/db/kvdb"
"github.com/arnaubennassar/hermez-node/v2/db/l2db"
"github.com/arnaubennassar/hermez-node/v2/db/statedb"
"github.com/arnaubennassar/hermez-node/v2/log"
"github.com/arnaubennassar/hermez-node/v2/metric"
"github.com/arnaubennassar/hermez-node/v2/txprocessor"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
)
// CoordAccount contains the data of the Coordinator account, that will be used
// to create new transactions of CreateAccountDeposit type to add new TokenID
// accounts for the Coordinator to receive the fees.
type CoordAccount struct {
Addr ethCommon.Address
BJJ babyjub.PublicKeyComp
AccountCreationAuth []byte // signature in byte array format
}
// TxSelector implements all the functionalities to select the txs for the next
// batch
type TxSelector struct {
l2db *l2db.L2DB
localAccountsDB *statedb.LocalStateDB
coordAccount *CoordAccount
}
// NewTxSelector returns a *TxSelector
func NewTxSelector(coordAccount *CoordAccount, dbpath string,
synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB) (*TxSelector, error) {
localAccountsDB, err := statedb.NewLocalStateDB(
statedb.Config{
Path: dbpath,
Keep: kvdb.DefaultKeep,
Type: statedb.TypeTxSelector,
NLevels: 0,
},
synchronizerStateDB) // without merkletree
if err != nil {
return nil, tracerr.Wrap(err)
}
return &TxSelector{
l2db: l2,
localAccountsDB: localAccountsDB,
coordAccount: coordAccount,
}, nil
}
// LocalAccountsDB returns the LocalStateDB of the TxSelector
func (txsel *TxSelector) LocalAccountsDB() *statedb.LocalStateDB {
return txsel.localAccountsDB
}
// Reset tells the TxSelector to get it's internal AccountsDB
// from the required `batchNum`
func (txsel *TxSelector) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
return tracerr.Wrap(txsel.localAccountsDB.Reset(batchNum, fromSynchronizer))
}
func (txsel *TxSelector) getCoordIdx(tokenID common.TokenID) (common.Idx, error) {
return txsel.localAccountsDB.GetIdxByEthAddrBJJ(txsel.coordAccount.Addr,
txsel.coordAccount.BJJ, tokenID)
}
// coordAccountForTokenID creates a new L1CoordinatorTx to create a new
// Coordinator account for the given TokenID in the case that the account does
// not exist yet in the db, and does not exist a L1CoordinatorTx to creat that
// account in the given array of L1CoordinatorTxs. If a new Coordinator account
// needs to be created, a new L1CoordinatorTx will be returned from this
// function. After calling this method, if the l1CoordinatorTx is added to the
// selection, positionL1 must be increased 1.
func (txsel *TxSelector) coordAccountForTokenID(tokenID common.TokenID, positionL1 int) (*common.L1Tx, int, error) {
_, err := txsel.getCoordIdx(tokenID)
if tracerr.Unwrap(err) == statedb.ErrIdxNotFound {
// create L1CoordinatorTx to create new CoordAccount for
// TokenID
l1CoordinatorTx := common.L1Tx{
Position: positionL1,
UserOrigin: false,
FromEthAddr: txsel.coordAccount.Addr,
FromBJJ: txsel.coordAccount.BJJ,
TokenID: tokenID,
Amount: big.NewInt(0),
DepositAmount: big.NewInt(0),
Type: common.TxTypeCreateAccountDeposit,
}
return &l1CoordinatorTx, positionL1, nil
}
if err != nil {
return nil, positionL1, tracerr.Wrap(err)
}
// CoordAccount for TokenID already exists
return nil, positionL1, nil
}
// GetL2TxSelection returns the L1CoordinatorTxs and a selection of the L2Txs
// for the next batch, from the L2DB pool.
// It returns: the CoordinatorIdxs used to receive the fees of the selected
// L2Txs. An array of bytearrays with the signatures of the
// AccountCreationAuthorization of the accounts of the users created by the
// Coordinator with L1CoordinatorTxs of those accounts that does not exist yet
// but there is a transactions to them and the authorization of account
// creation exists. The L1UserTxs, L1CoordinatorTxs, PoolL2Txs that will be
// included in the next batch.
func (txsel *TxSelector) GetL2TxSelection(selectionConfig txprocessor.Config, l1UserFutureTxs []common.L1Tx) ([]common.Idx,
[][]byte, []common.L1Tx, []common.PoolL2Tx, []common.PoolL2Tx, error) {
metric.GetL2TxSelection.Inc()
coordIdxs, accCreationAuths, _, l1CoordinatorTxs, l2Txs,
discardedL2Txs, err := txsel.getL1L2TxSelection(selectionConfig,
[]common.L1Tx{}, l1UserFutureTxs)
return coordIdxs, accCreationAuths, l1CoordinatorTxs, l2Txs,
discardedL2Txs, tracerr.Wrap(err)
}
// GetL1L2TxSelection returns the selection of L1 + L2 txs.
// It returns: the CoordinatorIdxs used to receive the fees of the selected
// L2Txs. An array of bytearrays with the signatures of the
// AccountCreationAuthorization of the accounts of the users created by the
// Coordinator with L1CoordinatorTxs of those accounts that does not exist yet
// but there is a transactions to them and the authorization of account
// creation exists. The L1UserTxs, L1CoordinatorTxs, PoolL2Txs that will be
// included in the next batch.
func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig txprocessor.Config,
l1UserTxs, l1UserFutureTxs []common.L1Tx) ([]common.Idx, [][]byte, []common.L1Tx,
[]common.L1Tx, []common.PoolL2Tx, []common.PoolL2Tx, error) {
metric.GetL1L2TxSelection.Inc()
coordIdxs, accCreationAuths, l1UserTxs, l1CoordinatorTxs, l2Txs,
discardedL2Txs, err := txsel.getL1L2TxSelection(selectionConfig, l1UserTxs, l1UserFutureTxs)
return coordIdxs, accCreationAuths, l1UserTxs, l1CoordinatorTxs, l2Txs,
discardedL2Txs, tracerr.Wrap(err)
}
type failedAtomicGroup struct {
id common.AtomicGroupID
failedTxID common.TxID // ID of the tx that made the entire atomic group fail
reason common.TxSelectorError
}
// getL1L2TxSelection returns the selection of L1 + L2 txs.
// It returns: the CoordinatorIdxs used to receive the fees of the selected
// L2Txs. An array of bytearrays with the signatures of the
// AccountCreationAuthorization of the accounts of the users created by the
// Coordinator with L1CoordinatorTxs of those accounts that does not exist yet
// but there is a transactions to them and the authorization of account
// creation exists. The L1UserTxs, L1CoordinatorTxs, PoolL2Txs that will be
// included in the next batch.
func (txsel *TxSelector) getL1L2TxSelection(selectionConfig txprocessor.Config,
l1UserTxs, l1UserFutureTxs []common.L1Tx) ([]common.Idx, [][]byte, []common.L1Tx,
[]common.L1Tx, []common.PoolL2Tx, []common.PoolL2Tx, error) {
// WIP.0: the TxSelector is not optimized and will need a redesign. The
// current version is implemented in order to have a functional
// implementation that can be used ASAP.
// Steps of this method:
// - ProcessL1Txs (User txs)
// - getPendingTxs (forgable directly with current state & not forgable
// yet)
// - split between l2TxsForgable & l2TxsNonForgable, where:
// - l2TxsForgable are the txs that are directly forgable with the
// current state
// - l2TxsNonForgable are the txs that are not directly forgable
// with the current state, but that may be forgable once the
// l2TxsForgable ones are processed
// - for l2TxsForgable, and if needed, for l2TxsNonForgable:
// - sort by Fee & Nonce
// - loop over l2Txs (txsel.processL2Txs)
// - Fill tx.TokenID tx.Nonce
// - Check enough Balance on sender
// - Check Nonce
// - Create CoordAccount L1CoordTx for TokenID if needed
// - & ProcessL1Tx of L1CoordTx
// - Check validity of receiver Account for ToEthAddr / ToBJJ
// - Create UserAccount L1CoordTx if needed (and possible)
// - If everything is fine, store l2Tx to selectedTxs & update NoncesMap
// - Prepare coordIdxsMap & AccumulatedFees
// - Distribute AccumulatedFees to CoordIdxs
// - MakeCheckpoint
failedAtomicGroups := []failedAtomicGroup{}
START_SELECTION:
txselStateDB := txsel.localAccountsDB.StateDB
tp := txprocessor.NewTxProcessor(txselStateDB, selectionConfig)
tp.AccumulatedFees = make(map[common.Idx]*big.Int)
// Process L1UserTxs
for i := 0; i < len(l1UserTxs); i++ {
// assumption: l1usertx are sorted by L1Tx.Position
_, _, _, _, err := tp.ProcessL1Tx(nil, &l1UserTxs[i])
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
}
// Get pending txs from the pool
l2TxsFromDB, err := txsel.l2db.GetPendingTxs()
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
// Filter transactions belonging to failed atomic groups
selectableTxsTmp, discardedTxs := filterFailedAtomicGroups(l2TxsFromDB, failedAtomicGroups)
// Filter invalid atomic groups
selectableTxs, discardedTxsTmp := filterInvalidAtomicGroups(selectableTxsTmp)
discardedTxs = append(discardedTxs, discardedTxsTmp...)
// in case that length of l2TxsForgable is 0, no need to continue, there
// is no L2Txs to forge at all
if len(selectableTxs) == 0 {
err = tp.StateDB().MakeCheckpoint()
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
metric.SelectedL1UserTxs.Set(float64(len(l1UserTxs)))
metric.SelectedL1CoordinatorTxs.Set(0)
metric.SelectedL2Txs.Set(0)
metric.DiscardedL2Txs.Set(float64(len(discardedTxs)))
return nil, nil, l1UserTxs, nil, nil, discardedTxs, nil
}
// Calculate average fee for atomic groups
atomicFeeMap := calculateAtomicGroupsAverageFee(selectableTxs)
// Initialize selection arrays
// Used authorizations in the l1CoordinatorTxs
var accAuths [][]byte
// Processed txs for necessary account creation
// (fees for coordinator or missing destinatary accounts)
var l1CoordinatorTxs []common.L1Tx
// Processed txs
var selectedTxs []common.PoolL2Tx
// Start selection process
shouldKeepSelectionProcess := true
// Order L2 txs. This has to be done just once,
// as the array will get smaller over iterations, but the order won't be affected
selectableTxs = sortL2Txs(selectableTxs, atomicFeeMap)
for shouldKeepSelectionProcess {
// Process txs and get selection
iteAccAuths, iteL1CoordinatorTxs, iteSelectedTxs,
nonSelectedTxs, invalidTxs, failedAtomicGroup, err := txsel.processL2Txs(
tp,
selectionConfig,
len(l1UserTxs)+len(l1CoordinatorTxs), // Already added L1 Txs
len(selectedTxs), // Already added L2 Txs
l1UserFutureTxs, // Used to prevent the creation of unnecessary accounts
selectableTxs, // Txs that can be selected
)
if failedAtomicGroup.id != common.EmptyAtomicGroupID {
// An atomic group failed to be processed
// after at least one tx from the group already altered the state.
// Revert state to current batch and start the selection process again,
// ignoring the txs from the group that failed
log.Info(err)
failedAtomicGroups = append(failedAtomicGroups, failedAtomicGroup)
if err := txsel.localAccountsDB.Reset(
txsel.localAccountsDB.CurrentBatch(), false,
); err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
goto START_SELECTION
}
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
// Add iteration results to selection arrays
accAuths = append(accAuths, iteAccAuths...)
l1CoordinatorTxs = append(l1CoordinatorTxs, iteL1CoordinatorTxs...)
selectedTxs = append(selectedTxs, iteSelectedTxs...)
discardedTxs = append(discardedTxs, invalidTxs...)
// Prepare for next iteration
if len(iteSelectedTxs) == 0 { // Stop iterating
// If in this iteration no txs got selected, stop selection process
shouldKeepSelectionProcess = false
// Add non selected txs to the discarded array as at this point they won't get selected
for i := 0; i < len(nonSelectedTxs); i++ {
discardedTxs = append(discardedTxs, nonSelectedTxs[i])
}
} else { // Keep iterating
// Try to select nonSelected txs in next iteration
selectableTxs = nonSelectedTxs
}
}
// get CoordIdxsMap for the TokenIDs
coordIdxsMap := make(map[common.TokenID]common.Idx)
for i := 0; i < len(selectedTxs); i++ {
// get TokenID from tx.Sender
accSender, err := tp.StateDB().GetAccount(selectedTxs[i].FromIdx)
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
tokenID := accSender.TokenID
coordIdx, err := txsel.getCoordIdx(tokenID)
if err != nil {
// if err is db.ErrNotFound, should not happen, as all
// the selectedTxs.TokenID should have a CoordinatorIdx
// created in the DB at this point
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
coordIdxsMap[tokenID] = coordIdx
}
var coordIdxs []common.Idx
for _, idx := range coordIdxsMap {
coordIdxs = append(coordIdxs, idx)
}
// sort CoordIdxs
sort.SliceStable(coordIdxs, func(i, j int) bool {
return coordIdxs[i] < coordIdxs[j]
})
// distribute the AccumulatedFees from the processed L2Txs into the
// Coordinator Idxs
for idx, accumulatedFee := range tp.AccumulatedFees {
cmp := accumulatedFee.Cmp(big.NewInt(0))
if cmp == 1 { // accumulatedFee>0
// send the fee to the Idx of the Coordinator for the TokenID
accCoord, err := txsel.localAccountsDB.GetAccount(idx)
if err != nil {
log.Errorw("Can not distribute accumulated fees to coordinator "+
"account: No coord Idx to receive fee", "idx", idx)
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee)
_, err = txsel.localAccountsDB.UpdateAccount(idx, accCoord)
if err != nil {
log.Error(err)
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
}
}
err = tp.StateDB().MakeCheckpoint()
if err != nil {
return nil, nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
metric.SelectedL1CoordinatorTxs.Set(float64(len(l1CoordinatorTxs)))
metric.SelectedL1UserTxs.Set(float64(len(l1UserTxs)))
metric.SelectedL2Txs.Set(float64(len(selectedTxs)))
metric.DiscardedL2Txs.Set(float64(len(discardedTxs)))
return coordIdxs, accAuths, l1UserTxs, l1CoordinatorTxs, selectedTxs, discardedTxs, nil
}
func (txsel *TxSelector) processL2Txs(
tp *txprocessor.TxProcessor,
selectionConfig txprocessor.Config,
nAlreadyProcessedL1Txs, nAlreadyProcessedL2Txs int,
l1UserFutureTxs []common.L1Tx,
l2Txs []common.PoolL2Tx,
) (
accAuths [][]byte,
// Processed txs for creating accounts (for coordinator fees or destinatary accounts)
l1CoordinatorTxs []common.L1Tx,
// Processed L2Txs
selectedL2Txs []common.PoolL2Tx,
// L2Txs that are not selected but could get selected in future iterations
nonSelectedL2Txs []common.PoolL2Tx,
// Discarded txs that are impossible to forge
// (nonce too small or impossible to create destinatary account)
unforjableL2Txs []common.PoolL2Tx,
// Info for the atomic group that failed (potentially)
failedAG failedAtomicGroup,
err error,
) {
const failedGroupErrMsg = "Failed forging atomic tx from Group %s." +
" Restarting selection process without txs from this group"
// TODO: differentiate between nonSelectedL2Txs and unforjableL2Txs
// (right now all fall into nonSelectedL2Txs, which is safe but non optimal)
positionL1 := nAlreadyProcessedL1Txs
nextBatchNum := uint32(txsel.localAccountsDB.CurrentBatch()) + 1
// Iterate over l2Txs
// - check Nonces
// - check enough Balance for the Amount+Fee
// - if needed, create new L1CoordinatorTxs for unexisting ToIdx
// - keep used accAuths
// - put the valid txs into selectedTxs array
for i := 0; i < len(l2Txs); i++ {
// Check if there is space for more L2Txs in the selection
if !canAddL2Tx(nAlreadyProcessedL1Txs, nAlreadyProcessedL2Txs, selectionConfig) {
// If tx is atomic, restart process without txs from the atomic group
obj := common.TxSelectorError{
Message: ErrNoAvailableSlots,
Code: ErrNoAvailableSlotsCode,
Type: ErrNoAvailableSlotsType,
}
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// no more available slots for L2Txs, so mark this tx
// but also the rest of remaining txs as discarded
for j := i; j < len(l2Txs); j++ {
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[j])
}
break
}
// Reject tx if the batch that is being selected is greater than MaxNumBatch
if l2Txs[i].MaxNumBatch != 0 && nextBatchNum > l2Txs[i].MaxNumBatch {
obj := common.TxSelectorError{
Message: ErrUnsupportedMaxNumBatch,
Code: ErrUnsupportedMaxNumBatchCode,
Type: ErrUnsupportedMaxNumBatchType,
}
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
// Tx won't be forjable since the current batch num won't go backwards
unforjableL2Txs = append(unforjableL2Txs, l2Txs[i])
continue
}
// Discard exits with amount 0
if l2Txs[i].Type == common.TxTypeExit && l2Txs[i].Amount.Cmp(big.NewInt(0)) <= 0 {
// If tx is atomic, restart process without txs from the atomic group
obj := common.TxSelectorError{
Message: ErrExitAmount,
Code: ErrExitAmountCode,
Type: ErrExitAmountType,
}
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
// Although tecnicaly forjable, it won't never get forged with current code
unforjableL2Txs = append(unforjableL2Txs, l2Txs[i])
continue
}
// get Nonce & TokenID from the Account by l2Tx.FromIdx
accSender, err := tp.StateDB().GetAccount(l2Txs[i].FromIdx)
if err != nil {
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(err)
}
l2Txs[i].TokenID = accSender.TokenID
// Check enough Balance on sender
enoughBalance, balance, feeAndAmount := tp.CheckEnoughBalance(l2Txs[i])
if !enoughBalance {
obj := common.TxSelectorError{
Message: fmt.Sprintf(ErrSenderNotEnoughBalance+"Current sender account Balance: %s, Amount+Fee: %s",
balance.String(), feeAndAmount.String()),
Code: ErrSenderNotEnoughBalanceCode,
Type: ErrSenderNotEnoughBalanceType,
}
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// not valid Amount with current Balance. Discard L2Tx,
// and update Info parameter of the tx, and add it to
// the discardedTxs array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
// Check if Nonce is correct
if l2Txs[i].Nonce != accSender.Nonce {
obj := common.TxSelectorError{
Message: fmt.Sprintf(ErrNoCurrentNonce+"Tx.Nonce: %d, Account.Nonce: %d", l2Txs[i].Nonce, accSender.Nonce),
Code: ErrNoCurrentNonceCode,
Type: ErrNoCurrentNonceType,
}
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// not valid Nonce at tx. Discard L2Tx, and update Info
// parameter of the tx, and add it to the discardedTxs
// array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
// if TokenID does not exist yet, create new L1CoordinatorTx to
// create the CoordinatorAccount for that TokenID, to receive
// the fees. Only in the case that there does not exist yet a
// pending L1CoordinatorTx to create the account for the
// Coordinator for that TokenID
var newL1CoordTx *common.L1Tx
newL1CoordTx, positionL1, err = txsel.coordAccountForTokenID(accSender.TokenID, positionL1)
if err != nil {
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(err)
}
if newL1CoordTx != nil {
obj := common.TxSelectorError{
Message: ErrNotEnoughSpaceL1Coordinator,
Code: ErrNotEnoughSpaceL1CoordinatorCode,
Type: ErrNotEnoughSpaceL1CoordinatorType,
}
// if there is no space for the L1CoordinatorTx as MaxL1Tx, or no space
// for L1CoordinatorTx + L2Tx as MaxTx, discard the L2Tx
if !canAddL2TxThatNeedsNewCoordL1Tx(
nAlreadyProcessedL1Txs,
nAlreadyProcessedL2Txs,
selectionConfig,
) {
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// discard L2Tx, and update Info parameter of
// the tx, and add it to the discardedTxs array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
// increase positionL1
positionL1++
l1CoordinatorTxs = append(l1CoordinatorTxs, *newL1CoordTx)
accAuths = append(accAuths, txsel.coordAccount.AccountCreationAuth)
// process the L1CoordTx
_, _, _, _, err := tp.ProcessL1Tx(nil, newL1CoordTx)
if err != nil {
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(err)
}
nAlreadyProcessedL1Txs++
}
// If tx.ToIdx>=256, tx.ToIdx should exist to localAccountsDB,
// if so, tx is used. If tx.ToIdx==0, for an L2Tx will be the
// case of TxToEthAddr or TxToBJJ, check if
// tx.ToEthAddr/tx.ToBJJ exist in localAccountsDB, if yes tx is
// used; if not, check if tx.ToEthAddr is in
// AccountCreationAuthDB, if so, tx is used and L1CoordinatorTx
// of CreateAccountAndDeposit is created. If tx.ToIdx==1, is a
// Exit type and is used.
if l2Txs[i].ToIdx == 0 { // ToEthAddr/ToBJJ case
validL2Tx, l1CoordinatorTx, accAuth, err := txsel.processTxToEthAddrBJJ(
selectionConfig,
l1UserFutureTxs,
nAlreadyProcessedL1Txs,
nAlreadyProcessedL2Txs,
positionL1,
l2Txs[i],
)
if err != nil {
obj := common.TxSelectorError{
Message: fmt.Sprintf(ErrTxDiscartedInProcessTxToEthAddrBJJ+" due to %s", err.Error()),
Code: ErrTxDiscartedInProcessTxToEthAddrBJJCode,
Type: ErrTxDiscartedInProcessTxToEthAddrBJJType,
}
log.Debugw("txsel.processTxToEthAddrBJJ", "err", err)
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// Discard L2Tx, and update Info parameter of
// the tx, and add it to the discardedTxs array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
if l1CoordinatorTx != nil && validL2Tx != nil {
// If ToEthAddr == 0xff.. this means that we
// are handling a TransferToBJJ, which doesn't
// require an authorization because it doesn't
// contain a valid ethereum address.
// Otherwise only create the account if we have
// the corresponding authorization
if validL2Tx.ToEthAddr == common.FFAddr {
accAuths = append(accAuths, common.EmptyEthSignature)
l1CoordinatorTxs = append(l1CoordinatorTxs, *l1CoordinatorTx)
positionL1++
} else if accAuth != nil {
accAuths = append(accAuths, accAuth.Signature)
l1CoordinatorTxs = append(l1CoordinatorTxs, *l1CoordinatorTx)
positionL1++
}
// process the L1CoordTx
_, _, _, _, err := tp.ProcessL1Tx(nil, l1CoordinatorTx)
if err != nil {
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(err)
}
nAlreadyProcessedL1Txs++
}
if validL2Tx == nil {
// TODO: Missing info on why this tx is not selected? Check l2Txs.Info at this point!
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
obj := common.TxSelectorError{
Message: l2Txs[i].Info,
Code: l2Txs[i].ErrorCode,
Type: l2Txs[i].ErrorType,
}
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
} else if l2Txs[i].ToIdx >= common.IdxUserThreshold {
_, err := txsel.localAccountsDB.GetAccount(l2Txs[i].ToIdx)
if err != nil {
obj := common.TxSelectorError{
Message: fmt.Sprintf(ErrToIdxNotFound+"ToIdx: %d", l2Txs[i].ToIdx),
Code: ErrToIdxNotFoundCode,
Type: ErrToIdxNotFoundType,
}
// tx not valid
log.Debugw("invalid L2Tx: ToIdx not found in StateDB",
"ToIdx", l2Txs[i].ToIdx)
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// Discard L2Tx, and update Info parameter of
// the tx, and add it to the discardedTxs array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
}
// get CoordIdxsMap for the TokenID of the current l2Txs[i]
// get TokenID from tx.Sender account
tokenID := accSender.TokenID
coordIdx, err := txsel.getCoordIdx(tokenID)
if err != nil {
// if err is db.ErrNotFound, should not happen, as all
// the selectedTxs.TokenID should have a CoordinatorIdx
// created in the DB at this point
return nil, nil, nil, nil, nil, failedAG,
tracerr.Wrap(fmt.Errorf("Could not get CoordIdx for TokenID=%d, "+
"due: %s", tokenID, err))
}
// prepare temp coordIdxsMap & AccumulatedFees for the call to
// ProcessL2Tx
coordIdxsMap := map[common.TokenID]common.Idx{tokenID: coordIdx}
// tp.AccumulatedFees = make(map[common.Idx]*big.Int)
if _, ok := tp.AccumulatedFees[coordIdx]; !ok {
tp.AccumulatedFees[coordIdx] = big.NewInt(0)
}
_, _, _, err = tp.ProcessL2Tx(coordIdxsMap, nil, nil, &l2Txs[i])
if err != nil {
obj := common.TxSelectorError{
Message: fmt.Sprintf(ErrTxDiscartedInProcessL2Tx+" due to %s", err.Error()),
Code: ErrTxDiscartedInProcessL2TxCode,
Type: ErrTxDiscartedInProcessL2TxType,
}
log.Debugw("txselector.getL1L2TxSelection at ProcessL2Tx", "err", err)
// If tx is atomic, restart process without txs from the atomic group
if l2Txs[i].AtomicGroupID != common.EmptyAtomicGroupID {
failedAG = failedAtomicGroup{
id: l2Txs[i].AtomicGroupID,
failedTxID: l2Txs[i].TxID,
reason: obj,
}
return nil, nil, nil, nil, nil, failedAG, tracerr.Wrap(fmt.Errorf(
failedGroupErrMsg,
l2Txs[i].AtomicGroupID.String(),
))
}
// Discard L2Tx, and update Info parameter of the tx,
// and add it to the discardedTxs array
l2Txs[i].Info = obj.Message
l2Txs[i].ErrorCode = obj.Code
l2Txs[i].ErrorType = obj.Type
nonSelectedL2Txs = append(nonSelectedL2Txs, l2Txs[i])
continue
}
nAlreadyProcessedL2Txs++
selectedL2Txs = append(selectedL2Txs, l2Txs[i])
} // after this loop, no checks to discard txs should be done
return accAuths, l1CoordinatorTxs, selectedL2Txs, nonSelectedL2Txs, unforjableL2Txs, failedAG, nil
}
// processTxsToEthAddrBJJ process the common.PoolL2Tx in the case where
// ToIdx==0, which can be the tx type of ToEthAddr or ToBJJ. If the receiver
// does not have an account yet, a new L1CoordinatorTx of type
// CreateAccountDeposit (with 0 as DepositAmount) is created
func (txsel *TxSelector) processTxToEthAddrBJJ(
selectionConfig txprocessor.Config, l1UserFutureTxs []common.L1Tx,
nAlreadyProcessedL1Txs, nAlreadyProcessedL2Txs, positionL1 int, l2Tx common.PoolL2Tx) (
*common.PoolL2Tx, *common.L1Tx, *common.AccountCreationAuth, error) {
// check if L2Tx receiver account will be created by a L1UserFutureTxs
// (in the next batch, the current frozen queue). In that case, the L2Tx
// will be discarded at the current batch, even if there is an
// AccountCreationAuth for the account, as there is a L1UserTx in the
// frozen queue that will create the receiver Account. The L2Tx is
// discarded to avoid the Coordinator creating a new L1CoordinatorTx to
// create the receiver account, which will be also created in the next
// batch from the L1UserFutureTx, ending with the user having 2
// different accounts for the same TokenID. The double account creation
// is supported by the Hermez zkRollup specification, but it was decided
// to mitigate it at the TxSelector level for the explained cases.
if checkPendingToCreateFutureTxs(l1UserFutureTxs, l2Tx.TokenID, l2Tx.ToEthAddr, l2Tx.ToBJJ) {
return nil, nil, nil, fmt.Errorf("L2Tx discarded at the current batch, as the" +
" receiver account does not exist yet, and there is a L1UserTx that" +
" will create that account in a future batch.")
}
var l1CoordinatorTx *common.L1Tx
var accAuth *common.AccountCreationAuth
if l2Tx.ToEthAddr != common.EmptyAddr && l2Tx.ToEthAddr != common.FFAddr {
// case: ToEthAddr != 0x00 neither 0xff
if l2Tx.ToBJJ != common.EmptyBJJComp {
// case: ToBJJ!=0:
// if idx exist for EthAddr&BJJ use it
_, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2Tx.ToEthAddr,
l2Tx.ToBJJ, l2Tx.TokenID)
if err == nil {
// account for ToEthAddr&ToBJJ already exist,
// there is no need to create a new one.
// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
return &l2Tx, nil, nil, nil
}
// if not, check if AccountCreationAuth exist for that
// ToEthAddr
accAuth, err = txsel.l2db.GetAccountCreationAuth(l2Tx.ToEthAddr)
if err != nil {
// not found, l2Tx will not be added in the selection
return nil, nil, nil,
tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in StateDB, neither "+
"ToEthAddr found in AccountCreationAuths L2DB. ToIdx: %d, ToEthAddr: %s",
l2Tx.ToIdx, l2Tx.ToEthAddr.Hex()))
}
if accAuth.BJJ != l2Tx.ToBJJ {
// if AccountCreationAuth.BJJ is not the same
// than in the tx, tx is not accepted
return nil, nil, nil,
tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in StateDB, "+
"neither ToEthAddr & ToBJJ found in AccountCreationAuths L2DB. "+
"ToIdx: %d, ToEthAddr: %s, ToBJJ: %s",
l2Tx.ToIdx, l2Tx.ToEthAddr.Hex(), l2Tx.ToBJJ.String()))
}
} else {
// case: ToBJJ==0:
// if idx exist for EthAddr use it
_, err := txsel.localAccountsDB.GetIdxByEthAddr(l2Tx.ToEthAddr, l2Tx.TokenID)
if err == nil {
// account for ToEthAddr already exist,
// there is no need to create a new one.
// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
return &l2Tx, nil, nil, nil
}
// if not, check if AccountCreationAuth exist for that ToEthAddr
accAuth, err = txsel.l2db.GetAccountCreationAuth(l2Tx.ToEthAddr)
if err != nil {
// not found, l2Tx will not be added in the selection
return nil, nil, nil,
tracerr.Wrap(fmt.Errorf("invalid L2Tx: ToIdx not found in "+
"StateDB, neither ToEthAddr found in "+
"AccountCreationAuths L2DB. ToIdx: %d, ToEthAddr: %s",
l2Tx.ToIdx, l2Tx.ToEthAddr))
}
}
// create L1CoordinatorTx for the accountCreation
l1CoordinatorTx = &common.L1Tx{
Position: positionL1,
UserOrigin: false,
FromEthAddr: accAuth.EthAddr,
FromBJJ: accAuth.BJJ,
TokenID: l2Tx.TokenID,
Amount: big.NewInt(0),
DepositAmount: big.NewInt(0),
Type: common.TxTypeCreateAccountDeposit,
}
} else if l2Tx.ToEthAddr == common.FFAddr && l2Tx.ToBJJ != common.EmptyBJJComp {
// if idx exist for EthAddr&BJJ use it
_, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(l2Tx.ToEthAddr, l2Tx.ToBJJ,
l2Tx.TokenID)
if err == nil {
// account for ToEthAddr&ToBJJ already exist, (where ToEthAddr==0xff)
// there is no need to create a new one.
// tx valid, StateDB will use the ToIdx==0 to define the AuxToIdx
return &l2Tx, nil, nil, nil
}
// if idx don't exist for EthAddr&BJJ, coordinator can create a
// new account without L1Authorization, as ToEthAddr==0xff
// create L1CoordinatorTx for the accountCreation
l1CoordinatorTx = &common.L1Tx{
Position: positionL1,
UserOrigin: false,
FromEthAddr: l2Tx.ToEthAddr,
FromBJJ: l2Tx.ToBJJ,
TokenID: l2Tx.TokenID,
Amount: big.NewInt(0),
DepositAmount: big.NewInt(0),
Type: common.TxTypeCreateAccountDeposit,
}
}
// if there is no space for the L1CoordinatorTx as MaxL1Tx, or no space
// for L1CoordinatorTx + L2Tx as MaxTx, discard the L2Tx
if !canAddL2TxThatNeedsNewCoordL1Tx(
nAlreadyProcessedL1Txs,
nAlreadyProcessedL2Txs,
selectionConfig,
) {
// L2Tx discarded
return nil, nil, nil, tracerr.Wrap(fmt.Errorf("L2Tx discarded due to no available slots " +
"for L1CoordinatorTx to create a new account for receiver of L2Tx"))
}
return &l2Tx, l1CoordinatorTx, accAuth, nil
}
func checkPendingToCreateFutureTxs(l1UserFutureTxs []common.L1Tx, tokenID common.TokenID,
addr ethCommon.Address, bjj babyjub.PublicKeyComp) bool {
for i := 0; i < len(l1UserFutureTxs); i++ {
if l1UserFutureTxs[i].FromEthAddr == addr &&
l1UserFutureTxs[i].TokenID == tokenID &&
l1UserFutureTxs[i].FromBJJ == bjj {
return true
}
if l1UserFutureTxs[i].FromEthAddr == addr &&
l1UserFutureTxs[i].TokenID == tokenID &&
common.EmptyBJJComp == bjj {
return true
}
}
return false
}
// sortL2Txs sorts the PoolL2Txs by AverageFee if they are atomic and AbsoluteFee
// and then by Nonce if they aren't atomic txs that are within the same atomic group
// are guaranteed to manatin order and consecutiveness.
// Assumption: the order within the atomic groups is correct for l2Txs
func sortL2Txs(
l2Txs []common.PoolL2Tx,
atomicGroupsFee map[common.AtomicGroupID]float64,
) []common.PoolL2Tx {
// Separate atomic txs
atomicGroupsMap := make(map[common.AtomicGroupID][]common.PoolL2Tx)
nonAtomicTxs := []common.PoolL2Tx{}