@@ -114,13 +114,14 @@ func (tracker *TxTracker) TrackAll(txs []*types.Transaction) {
114114}
115115
116116// recheck checks and returns any transactions that needs to be resubmitted.
117- func (tracker * TxTracker ) recheck (journalCheck bool ) ( resubmits []* types.Transaction , rejournal map [common. Address ]types. Transactions ) {
117+ func (tracker * TxTracker ) recheck (journalCheck bool ) []* types.Transaction {
118118 tracker .mu .Lock ()
119119 defer tracker .mu .Unlock ()
120120
121121 var (
122122 numStales = 0
123123 numOk = 0
124+ resubmits []* types.Transaction
124125 )
125126 for sender , txs := range tracker .byAddr {
126127 // Wipe the stales
@@ -141,7 +142,7 @@ func (tracker *TxTracker) recheck(journalCheck bool) (resubmits []*types.Transac
141142 }
142143
143144 if journalCheck { // rejournal
144- rejournal = make (map [common.Address ]types.Transactions )
145+ rejournal : = make (map [common.Address ]types.Transactions )
145146 for _ , tx := range tracker .all {
146147 addr , _ := types .Sender (tracker .signer , tx )
147148 rejournal [addr ] = append (rejournal [addr ], tx )
@@ -153,10 +154,18 @@ func (tracker *TxTracker) recheck(journalCheck bool) (resubmits []*types.Transac
153154 return int (a .Nonce () - b .Nonce ())
154155 })
155156 }
157+ // Rejournal the tracker while holding the lock. No new transactions will
158+ // be added to the old journal during this period, preventing any potential
159+ // transaction loss.
160+ if tracker .journal != nil {
161+ if err := tracker .journal .rotate (rejournal ); err != nil {
162+ log .Warn ("Transaction journal rotation failed" , "err" , err )
163+ }
164+ }
156165 }
157166 localGauge .Update (int64 (len (tracker .all )))
158167 log .Debug ("Tx tracker status" , "need-resubmit" , len (resubmits ), "stale" , numStales , "ok" , numOk )
159- return resubmits , rejournal
168+ return resubmits
160169}
161170
162171// Start implements node.Lifecycle interface
@@ -185,6 +194,12 @@ func (tracker *TxTracker) loop() {
185194 tracker .TrackAll (transactions )
186195 return nil
187196 })
197+
198+ // Setup the writer for the upcoming transactions
199+ if err := tracker .journal .setupWriter (); err != nil {
200+ log .Error ("Failed to setup the journal writer" , "err" , err )
201+ return
202+ }
188203 defer tracker .journal .close ()
189204 }
190205 var (
@@ -196,20 +211,15 @@ func (tracker *TxTracker) loop() {
196211 case <- tracker .shutdownCh :
197212 return
198213 case <- timer .C :
199- checkJournal := tracker .journal != nil && time .Since (lastJournal ) > tracker .rejournal
200- resubmits , rejournal := tracker .recheck (checkJournal )
214+ var rejournal bool
215+ if tracker .journal != nil && time .Since (lastJournal ) > tracker .rejournal {
216+ rejournal , lastJournal = true , time .Now ()
217+ log .Debug ("Rejournal the transaction tracker" )
218+ }
219+ resubmits := tracker .recheck (rejournal )
201220 if len (resubmits ) > 0 {
202221 tracker .pool .Add (resubmits , false )
203222 }
204- if checkJournal {
205- // Lock to prevent journal.rotate <-> journal.insert (via TrackAll) conflicts
206- tracker .mu .Lock ()
207- lastJournal = time .Now ()
208- if err := tracker .journal .rotate (rejournal ); err != nil {
209- log .Warn ("Transaction journal rotation failed" , "err" , err )
210- }
211- tracker .mu .Unlock ()
212- }
213223 timer .Reset (recheckInterval )
214224 }
215225 }
0 commit comments