@@ -382,10 +382,18 @@ impl Worker {
382382 self . check_time_flush( ) . await ;
383383 }
384384
385- // Handle incoming transactions
385+ // Handle incoming transactions - drain all ready TXs at once
386386 tx = self . tx_receiver. recv( ) => {
387387 if let Some ( tx) = tx {
388- self . handle_transaction( tx) . await ;
388+ // Drain additional ready transactions to amortize select! overhead
389+ let mut txs = vec![ tx] ;
390+ while let Ok ( t) = self . tx_receiver. try_recv( ) {
391+ txs. push( t) ;
392+ if txs. len( ) >= self . config. max_batch_txs {
393+ break ;
394+ }
395+ }
396+ self . handle_transactions_batch( txs) . await ;
389397 } else {
390398 warn!( worker_id = self . config. worker_id, "tx_receiver closed" ) ;
391399 self . shutdown = true ;
@@ -422,7 +430,7 @@ impl Worker {
422430
423431 /// Handle incoming transaction
424432 async fn handle_transaction ( & mut self , tx : Transaction ) {
425- info ! (
433+ trace ! (
426434 worker_id = self . config. worker_id,
427435 tx_size = tx. len( ) ,
428436 "Worker received transaction from channel"
@@ -457,14 +465,60 @@ impl Worker {
457465 ) ;
458466 self . process_batch ( batch) . await ;
459467 } else {
460- info ! (
468+ trace ! (
461469 worker_id = self . config. worker_id,
462470 pending_txs = self . batch_maker. pending_count( ) ,
463471 "Transaction added to batch maker, waiting for more or flush"
464472 ) ;
465473 }
466474 }
467475
476+ /// Handle a batch of incoming transactions drained from the channel.
477+ ///
478+ /// Validates each transaction, adds all valid ones to the batch maker,
479+ /// and processes any resulting batches. This amortizes the per-transaction
480+ /// overhead of the select! loop and logging.
481+ async fn handle_transactions_batch ( & mut self , txs : Vec < Transaction > ) {
482+ let total = txs. len ( ) ;
483+ debug ! (
484+ worker_id = self . config. worker_id,
485+ tx_count = total,
486+ "Processing drained transaction batch"
487+ ) ;
488+
489+ // Validate and collect valid transactions
490+ let mut valid_txs = Vec :: with_capacity ( total) ;
491+ for tx in txs {
492+ if let Some ( ref validator) = self . validator {
493+ match validator. validate_transaction ( & tx) . await {
494+ Ok ( ( ) ) => valid_txs. push ( tx) ,
495+ Err ( e) => {
496+ warn ! (
497+ worker_id = self . config. worker_id,
498+ error = %e,
499+ "Transaction validation failed, rejecting"
500+ ) ;
501+ }
502+ }
503+ } else {
504+ valid_txs. push ( tx) ;
505+ }
506+ }
507+
508+ // Add all valid transactions and collect any completed batches
509+ let batches = self . batch_maker . add_transactions ( valid_txs) ;
510+
511+ // Process all completed batches
512+ for batch in batches {
513+ info ! (
514+ worker_id = self . config. worker_id,
515+ tx_count = batch. transactions. len( ) ,
516+ "Batch ready, processing"
517+ ) ;
518+ self . process_batch ( batch) . await ;
519+ }
520+ }
521+
468522 /// Handle message from Primary
469523 async fn handle_primary_message ( & mut self , msg : PrimaryToWorker ) {
470524 match msg {
@@ -694,9 +748,8 @@ impl Worker {
694748 let has_pending = self . batch_maker . has_pending ( ) ;
695749 let elapsed = self . batch_maker . time_since_batch_start ( ) ;
696750
697- // Log every call so we can see the tick is working
698751 if has_pending {
699- info ! (
752+ trace ! (
700753 worker_id = self . config. worker_id,
701754 should_flush,
702755 has_pending,
@@ -706,7 +759,7 @@ impl Worker {
706759 }
707760
708761 if should_flush && has_pending {
709- info ! (
762+ debug ! (
710763 worker_id = self . config. worker_id,
711764 pending_txs = self . batch_maker. pending_count( ) ,
712765 "Time flush triggered, creating batch"
@@ -783,7 +836,7 @@ impl Worker {
783836 if let Some ( ref storage) = self . storage {
784837 match storage. put_batch ( batch. clone ( ) ) . await {
785838 Ok ( _) => {
786- info ! (
839+ debug ! (
787840 worker_id = self . config. worker_id,
788841 digest = %digest. digest,
789842 tx_count = batch. transactions. len( ) ,
@@ -812,20 +865,20 @@ impl Worker {
812865 self . state . store_batch ( batch. clone ( ) ) ;
813866
814867 // Broadcast to peer Workers
815- info ! (
868+ debug ! (
816869 worker_id = self . config. worker_id,
817870 digest = %digest. digest,
818871 "Broadcasting batch to peer Workers..."
819872 ) ;
820873 self . network . broadcast_batch ( & batch) . await ;
821- info ! (
874+ debug ! (
822875 worker_id = self . config. worker_id,
823876 digest = %digest. digest,
824877 "Broadcast complete"
825878 ) ;
826879
827880 // Report to Primary
828- info ! (
881+ debug ! (
829882 worker_id = self . config. worker_id,
830883 digest = %digest. digest,
831884 "Sending BatchDigest to Primary"
@@ -846,7 +899,7 @@ impl Worker {
846899 "Failed to send BatchDigest to Primary - channel closed"
847900 ) ;
848901 } else {
849- info ! (
902+ debug ! (
850903 worker_id = self . config. worker_id,
851904 "BatchDigest sent to Primary successfully"
852905 ) ;
0 commit comments