diff --git a/ethergo/submitter/export_test.go b/ethergo/submitter/export_test.go index e04dd69fbc..3855e123b0 100644 --- a/ethergo/submitter/export_test.go +++ b/ethergo/submitter/export_test.go @@ -39,8 +39,8 @@ func TxToAttributes(transaction *types.Transaction, UUID string) []attribute.Key } // SortTxes exports sortTxesByChainID for testing. -func SortTxes(txs []db.TX) map[uint64][]db.TX { - return sortTxesByChainID(txs) +func SortTxes(txs []db.TX, maxPerChain int) map[uint64][]db.TX { + return sortTxesByChainID(txs, maxPerChain) } // GroupTxesByNonce exports groupTxesByNonce for testing. diff --git a/ethergo/submitter/queue.go b/ethergo/submitter/queue.go index 7cfc46f243..a7d8ba5c90 100644 --- a/ethergo/submitter/queue.go +++ b/ethergo/submitter/queue.go @@ -99,6 +99,8 @@ func (t *txSubmitterImpl) processQueue(parentCtx context.Context) (err error) { return nil } +const maxTxesPerChain = 100 + func (t *txSubmitterImpl) processConfirmedQueue(parentCtx context.Context) (err error) { ctx, span := t.metrics.Tracer().Start(parentCtx, "submitter.processConfirmedQueue") defer func() { @@ -110,7 +112,7 @@ func (t *txSubmitterImpl) processConfirmedQueue(parentCtx context.Context) (err return fmt.Errorf("could not get txs: %w", err) } - sortedTXsByChainID := sortTxesByChainID(txs) + sortedTXsByChainID := sortTxesByChainID(txs, maxTxesPerChain) var wg sync.WaitGroup wg.Add(len(sortedTXsByChainID)) diff --git a/ethergo/submitter/util.go b/ethergo/submitter/util.go index 83c97ffb3e..68f73a1f97 100644 --- a/ethergo/submitter/util.go +++ b/ethergo/submitter/util.go @@ -103,7 +103,7 @@ func bigPtrToString(num *big.Int) string { } // sortTxesByChainID sorts a slice of transactions by nonce. -func sortTxesByChainID(txs []db.TX) map[uint64][]db.TX { +func sortTxesByChainID(txs []db.TX, maxPerChain int) map[uint64][]db.TX { txesByChainID := make(map[uint64][]db.TX) // put the transactions in a map by chain id for _, t := range txs { @@ -128,6 +128,13 @@ func sortTxesByChainID(txs []db.TX) map[uint64][]db.TX { }) } + // cap the number of txes per chain + for chainID, txes := range txesByChainID { + if len(txes) > maxPerChain { + txesByChainID[chainID] = txes[:maxPerChain] + } + } + return txesByChainID } diff --git a/ethergo/submitter/util_test.go b/ethergo/submitter/util_test.go index 5614760382..d39e3452a0 100644 --- a/ethergo/submitter/util_test.go +++ b/ethergo/submitter/util_test.go @@ -238,13 +238,24 @@ func (s *SubmitterSuite) TestSortTxes() { } wg.Wait() - sorted := submitter.SortTxes(allTxes) + sorted := submitter.SortTxes(allTxes, 50) assert.Equal(s.T(), len(sorted), len(expected)) for chainID, txes := range expected { for i := range txes { assert.Equal(s.T(), sorted[chainID][i].Hash(), txes[i].Hash()) } } + + // check tx cap + numTxes := 10 + sorted = submitter.SortTxes(allTxes, numTxes) + assert.Equal(s.T(), len(sorted), len(expected)) + for chainID, txes := range expected { + chainTxes := txes[:numTxes] + for i := range chainTxes { + assert.Equal(s.T(), sorted[chainID][i].Hash(), txes[i].Hash()) + } + } } func (s *SubmitterSuite) TestGroupTxesByNonce() {