@@ -627,6 +627,7 @@ func (pool *TxPool) Locals() []common.Address {
627627
628628// PendingPreconfTxs retrieves the preconf transactions and the pending transactions.
629629func (pool * TxPool ) PendingPreconfTxs (enforceTips bool ) ([]* types.Transaction , map [common.Address ]types.Transactions ) {
630+ defer preconf .MetricsPreconfTxPoolFilterCost (time .Now ())
630631 pool .mu .Lock ()
631632 defer pool .mu .Unlock ()
632633
@@ -676,36 +677,36 @@ func (pool *TxPool) extractPreconfTxsFromPending(pending map[common.Address]type
676677
677678 // removes the preconf transaction from the pending map, maintaining the order.
678679 preconfTxs := make ([]* types.Transaction , 0 )
679- for _ , preconfTx := range pool .preconfTxs .Transactions () {
680- from , _ := types . Sender ( pool . signer , preconfTx )
680+ for _ , preconfTx := range pool .preconfTxs .TxEntries () {
681+ preconfTxHash := preconfTx . Tx . Hash ( )
681682
682683 // Get the slice of transactions for the target address
683- txs , exists := pending [from ]
684+ txs , exists := pending [preconfTx . From ]
684685
685686 // If the transaction isn't in pending map but it's expected to be there,
686687 // show the error log.
687688 if ! exists || len (txs ) == 0 {
688- log .Error ("Missing transaction in pending map, please report the issue" , "hash" , preconfTx . Hash () )
689- pool .preconfTxs .Remove (preconfTx . Hash () ) // remove it prevent log always print
689+ log .Error ("Missing transaction in pending map, please report the issue" , "hash" , preconfTxHash )
690+ pool .preconfTxs .Remove (preconfTxHash ) // remove it prevent log always print
690691 continue
691692 }
692693
693694 // Create a new slice to hold the transactions that are not deleted
694695 var newTxs []* types.Transaction
695696 for _ , tx := range txs {
696- if tx .Hash () != preconfTx . Hash () {
697+ if tx .Hash () != preconfTxHash {
697698 newTxs = append (newTxs , tx ) // Only keep the transactions that are not to be deleted
698699 }
699700 }
700701
701702 // Update the slice in the map
702703 if len (newTxs ) == 0 {
703- delete (pending , from ) // If the slice is empty, delete the entry for that address
704+ delete (pending , preconfTx . From ) // If the slice is empty, delete the entry for that address
704705 } else {
705- pending [from ] = newTxs // Replace with the new slice
706+ pending [preconfTx . From ] = newTxs // Replace with the new slice
706707 }
707708
708- preconfTxs = append (preconfTxs , preconfTx )
709+ preconfTxs = append (preconfTxs , preconfTx . Tx )
709710 }
710711 return preconfTxs
711712}
@@ -1279,15 +1280,13 @@ func (pool *TxPool) addPreconfTx(tx *types.Transaction) {
12791280 return
12801281 }
12811282
1282- pool .preconfTxs .Add (tx )
1283+ pool .preconfTxs .Add (from , tx )
12831284
12841285 // handle preconf txs
12851286 pool .handlePreconfTxs ([]* types.Transaction {tx })
12861287}
12871288
12881289func (pool * TxPool ) handlePreconfTxs (news []* types.Transaction ) {
1289- defer preconf .MetricsPreconfTxPoolHandleCost (time .Now ())
1290-
12911290 for _ , tx := range news {
12921291 txHash := tx .Hash ()
12931292 if ! pool .preconfTxs .Contains (txHash ) {
@@ -1296,25 +1295,28 @@ func (pool *TxPool) handlePreconfTxs(news []*types.Transaction) {
12961295 }
12971296
12981297 // send preconf request event
1299- result := make (chan * core.PreconfResponse )
1298+ result := make (chan * core.PreconfResponse , 1 ) // buffer 1 to avoid worker blocking
13001299 pool .preconfTxRequestFeed .Send (core.NewPreconfTxRequest {
13011300 Tx : tx ,
13021301 PreconfResult : result ,
1302+ ClosePreconfResultFn : func () {
1303+ close (result )
1304+ },
13031305 })
1304- log .Trace ("txpool sent preconf tx request" , "tx" , txHash )
1306+ log .Debug ("txpool sent preconf tx request" , "tx" , txHash )
13051307
13061308 // avoid race condition
13071309 tx := tx
13081310 // goroutine to avoid blocking
13091311 go func () {
1310- defer close ( result )
1312+ defer preconf . MetricsPreconfTxPoolHandleCost ( time . Now () )
13111313
1312- // wait for preconf ready, no need to wait again after it's ready, as it only needs to wait once when the service restarts
1313- // wait for 10s, if not ready, skip
1314+ // If preconfReadyCh is not closed, it means this is a preconf tx restored from journal after system restart.
1315+ // In this case, we don't need to execute preconfirmation again to avoid resource contention with worker.
13141316 select {
13151317 case <- pool .preconfReadyCh :
1316- case <- time . After ( time . Minute ) :
1317- log .Error ("preconf txs not ready, skip handle" , "tx" , txHash )
1318+ default :
1319+ log .Info ("preconf txs not ready, skip handle" , "tx" , txHash )
13181320 return
13191321 }
13201322
@@ -1326,10 +1328,11 @@ func (pool *TxPool) handlePreconfTxs(news []*types.Transaction) {
13261328 // timeout
13271329 timeout := time .NewTimer (pool .config .Preconf .PreconfTimeout )
13281330 defer timeout .Stop ()
1331+ now := time .Now ()
13291332 // wait for miner.worker preconf response
13301333 select {
13311334 case response := <- result :
1332- log .Trace ("txpool received preconf tx response" , "tx" , txHash )
1335+ log .Trace ("txpool received preconf tx response" , "tx" , txHash , "duration" , time . Since ( now ) )
13331336 if response .Err == nil {
13341337 event .Status = core .PreconfStatusSuccess
13351338 } else {
@@ -1347,7 +1350,7 @@ func (pool *TxPool) handlePreconfTxs(news []*types.Transaction) {
13471350 }
13481351 case <- timeout .C :
13491352 event .Status = core .PreconfStatusFailed
1350- event .Reason = "preconf timeout"
1353+ event .Reason = fmt . Sprintf ( "preconf timeout, over %s timeout" , time . Since ( now ))
13511354 }
13521355
13531356 // send preconf event
@@ -1359,7 +1362,7 @@ func (pool *TxPool) handlePreconfTxs(news []*types.Transaction) {
13591362 log .Trace ("preconf success" , "tx" , txHash )
13601363 if pool .journal != nil {
13611364 if err := pool .journal .insert (tx ); err != nil {
1362- log .Warn ("Failed to journal preconf success transaction" , "tx" , txHash , "err" , err )
1365+ log .Error ("Failed to journal preconf success transaction" , "tx" , txHash , "err" , err )
13631366 } else {
13641367 log .Trace ("preconf success transaction journaled" , "tx" , txHash )
13651368 }
0 commit comments