@@ -143,11 +143,6 @@ func (r *Reaper) Stop() error {
143143 return nil
144144}
145145
146- type pendingTx struct {
147- tx []byte
148- hash string
149- }
150-
151146func (r * Reaper ) drainMempool (cleanupCh <- chan time.Time ) (bool , error ) {
152147 var totalSubmitted int
153148
@@ -175,16 +170,32 @@ func (r *Reaper) drainMempool(cleanupCh <-chan time.Time) (bool, error) {
175170 break
176171 }
177172
178- filtered := r .filterNewTxs (txs )
179- if len (filtered ) == 0 {
173+ hashes := hashTxs (txs )
174+ seen := r .cache .AreTxsSeen (hashes )
175+
176+ newTxs := make ([][]byte , 0 , len (txs ))
177+ newHashes := make ([]string , 0 , len (txs ))
178+ for i , tx := range txs {
179+ if ! seen [i ] {
180+ newTxs = append (newTxs , tx )
181+ newHashes = append (newHashes , hashes [i ])
182+ }
183+ }
184+
185+ if len (newTxs ) == 0 {
180186 break
181187 }
182188
183- n , err := r .submitFiltered (filtered )
189+ _ , err = r .sequencer .SubmitBatchTxs (r .ctx , coresequencer.SubmitBatchTxsRequest {
190+ Id : []byte (r .chainID ),
191+ Batch : & coresequencer.Batch {Transactions : newTxs },
192+ })
184193 if err != nil {
185- return totalSubmitted > 0 , err
194+ return totalSubmitted > 0 , fmt . Errorf ( "failed to submit txs to sequencer: %w" , err )
186195 }
187- totalSubmitted += n
196+
197+ r .cache .SetTxsSeen (newHashes )
198+ totalSubmitted += len (newTxs )
188199 }
189200
190201 if totalSubmitted > 0 {
@@ -194,38 +205,16 @@ func (r *Reaper) drainMempool(cleanupCh <-chan time.Time) (bool, error) {
194205 return totalSubmitted > 0 , nil
195206}
196207
197- func (r * Reaper ) filterNewTxs (txs [][]byte ) []pendingTx {
198- pending := make ([]pendingTx , 0 , len (txs ))
199- for _ , tx := range txs {
200- h := hashTx (tx )
201- if ! r .cache .IsTxSeen (h ) {
202- pending = append (pending , pendingTx {tx : tx , hash : h })
203- }
204- }
205- return pending
206- }
207-
208- func (r * Reaper ) submitFiltered (batch []pendingTx ) (int , error ) {
209- txs := make ([][]byte , len (batch ))
210- hashes := make ([]string , len (batch ))
211- for i , p := range batch {
212- txs [i ] = p .tx
213- hashes [i ] = p .hash
208+ func hashTxs (txs [][]byte ) []string {
209+ hashes := make ([]string , len (txs ))
210+ for i , tx := range txs {
211+ h := sha256 .Sum256 (tx )
212+ hashes [i ] = hex .EncodeToString (h [:])
214213 }
215-
216- _ , err := r .sequencer .SubmitBatchTxs (r .ctx , coresequencer.SubmitBatchTxsRequest {
217- Id : []byte (r .chainID ),
218- Batch : & coresequencer.Batch {Transactions : txs },
219- })
220- if err != nil {
221- return 0 , fmt .Errorf ("failed to submit txs to sequencer: %w" , err )
222- }
223-
224- r .cache .SetTxsSeen (hashes )
225- return len (txs ), nil
214+ return hashes
226215}
227216
228217func hashTx (tx []byte ) string {
229- hash := sha256 .Sum256 (tx )
230- return hex .EncodeToString (hash [:])
218+ h := sha256 .Sum256 (tx )
219+ return hex .EncodeToString (h [:])
231220}
0 commit comments