Skip to main content

sui_core/
authority_server.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5use anyhow::Result;
6use async_trait::async_trait;
7use fastcrypto::traits::KeyPair;
8use futures::{TryFutureExt, future};
9use itertools::Itertools as _;
10use moka::sync::Cache;
11use mysten_common::ZipDebugEqIteratorExt;
12use mysten_common::{assert_reachable, debug_fatal};
13use mysten_metrics::spawn_monitored_task;
14use parking_lot::Mutex;
15use prometheus::{
16    Gauge, Histogram, HistogramVec, IntCounter, IntCounterVec, IntGauge, Registry,
17    register_gauge_with_registry, register_histogram_vec_with_registry,
18    register_histogram_with_registry, register_int_counter_vec_with_registry,
19    register_int_counter_with_registry, register_int_gauge_with_registry,
20};
21use std::{
22    collections::{HashMap, HashSet},
23    io,
24    net::{IpAddr, SocketAddr},
25    sync::Arc,
26    time::{Duration, Instant, SystemTime},
27};
28use sui_network::{
29    api::{Validator, ValidatorServer},
30    tonic,
31    validator::server::SUI_TLS_SERVER_NAME,
32};
33use sui_types::effects::TransactionEffectsAPI;
34use sui_types::message_envelope::Message;
35use sui_types::messages_consensus::{ConsensusTransaction, ConsensusTransactionKey};
36use sui_types::messages_grpc::{
37    ObjectInfoRequest, ObjectInfoResponse, RawSubmitTxResponse, SystemStateRequest,
38    TransactionInfoRequest, TransactionInfoResponse,
39};
40use sui_types::multiaddr::Multiaddr;
41use sui_types::object::Object;
42use sui_types::sui_system_state::SuiSystemState;
43use sui_types::traffic_control::{ClientIdSource, Weight};
44use sui_types::{
45    base_types::ObjectID,
46    digests::{TransactionDigest, TransactionEffectsDigest},
47    error::{SuiErrorKind, UserInputError},
48};
49use sui_types::{
50    effects::TransactionEffects,
51    messages_grpc::{
52        ExecutedData, RawSubmitTxRequest, RawWaitForEffectsRequest, RawWaitForEffectsResponse,
53        SubmitTxResult, WaitForEffectsRequest, WaitForEffectsResponse,
54    },
55};
56use sui_types::{effects::TransactionEvents, messages_grpc::SubmitTxType};
57use sui_types::{error::*, transaction::*};
58use sui_types::{
59    fp_ensure,
60    messages_checkpoint::{
61        CheckpointRequest, CheckpointRequestV2, CheckpointResponse, CheckpointResponseV2,
62    },
63};
64use tokio::time::timeout;
65use tonic::metadata::{Ascii, MetadataValue};
66use tracing::{debug, error, info, instrument};
67
68use crate::admission_queue::{AdmissionQueueContext, AdmissionQueueManager};
69use crate::consensus_transaction_pool::TransactionPoolContext;
70use crate::gasless_rate_limiter::GaslessRateLimiter;
71use crate::{
72    authority::{AuthorityState, consensus_tx_status_cache::ConsensusTxStatus},
73    consensus_adapter::{ConsensusAdapter, ConsensusAdapterMetrics, ConsensusOverloadChecker},
74    consensus_handler::SequencedConsensusTransactionKey,
75    traffic_controller::{TrafficController, parse_ip, policies::TrafficTally},
76};
77use crate::{
78    authority::{
79        authority_per_epoch_store::AuthorityPerEpochStore,
80        consensus_tx_status_cache::NotifyReadConsensusTxStatusResult,
81    },
82    checkpoints::CheckpointStore,
83    mysticeti_adapter::LazyMysticetiClient,
84};
85use sui_config::local_ip_utils::new_local_tcp_address_for_testing;
86
87#[cfg(test)]
88#[path = "unit_tests/server_tests.rs"]
89mod server_tests;
90
91#[cfg(test)]
92#[path = "unit_tests/wait_for_effects_tests.rs"]
93mod wait_for_effects_tests;
94
95#[cfg(test)]
96#[path = "unit_tests/submit_transaction_tests.rs"]
97mod submit_transaction_tests;
98
99pub struct AuthorityServerHandle {
100    server_handle: sui_network::validator::server::Server,
101}
102
103impl AuthorityServerHandle {
104    pub async fn join(self) -> Result<(), io::Error> {
105        self.server_handle.handle().wait_for_shutdown().await;
106        Ok(())
107    }
108
109    pub async fn kill(self) -> Result<(), io::Error> {
110        self.server_handle.handle().shutdown().await;
111        Ok(())
112    }
113
114    pub fn address(&self) -> &Multiaddr {
115        self.server_handle.local_addr()
116    }
117}
118
119pub struct AuthorityServer {
120    address: Multiaddr,
121    pub state: Arc<AuthorityState>,
122    consensus_adapter: Arc<ConsensusAdapter>,
123    pub metrics: Arc<ValidatorServiceMetrics>,
124}
125
126impl AuthorityServer {
127    pub fn new_for_test_with_consensus_adapter(
128        state: Arc<AuthorityState>,
129        consensus_adapter: Arc<ConsensusAdapter>,
130    ) -> Self {
131        let address = new_local_tcp_address_for_testing();
132        let metrics = Arc::new(ValidatorServiceMetrics::new_for_tests());
133
134        Self {
135            address,
136            state,
137            consensus_adapter,
138            metrics,
139        }
140    }
141
142    pub fn new_for_test(state: Arc<AuthorityState>) -> Self {
143        let slot_freed_notify = Arc::new(tokio::sync::Notify::new());
144        let consensus_adapter = Arc::new(ConsensusAdapter::new(
145            Arc::new(LazyMysticetiClient::new()),
146            CheckpointStore::new_for_tests(),
147            state.name,
148            100_000,
149            100_000,
150            ConsensusAdapterMetrics::new_test(),
151            slot_freed_notify,
152        ));
153        Self::new_for_test_with_consensus_adapter(state, consensus_adapter)
154    }
155
156    pub async fn spawn_for_test(self) -> Result<AuthorityServerHandle, io::Error> {
157        let address = self.address.clone();
158        self.spawn_with_bind_address_for_test(address).await
159    }
160
161    pub async fn spawn_with_bind_address_for_test(
162        self,
163        address: Multiaddr,
164    ) -> Result<AuthorityServerHandle, io::Error> {
165        let tls_config = sui_tls::create_rustls_server_config(
166            self.state.config.network_key_pair().copy().private(),
167            SUI_TLS_SERVER_NAME.to_string(),
168        );
169        let config = mysten_network::config::Config::new();
170        let server = sui_network::validator::server::ServerBuilder::from_config(
171            &config,
172            mysten_network::metrics::DefaultMetricsCallbackProvider::default(),
173        )
174        .add_service(ValidatorServer::new(ValidatorService::new_for_tests(
175            self.state,
176            self.consensus_adapter,
177            self.metrics,
178        )))
179        .bind(&address, Some(tls_config))
180        .await
181        .unwrap();
182        let local_addr = server.local_addr().to_owned();
183        info!("Listening to traffic on {local_addr}");
184        let handle = AuthorityServerHandle {
185            server_handle: server,
186        };
187        Ok(handle)
188    }
189}
190
191pub struct ValidatorServiceMetrics {
192    pub signature_errors: IntCounter,
193    pub tx_verification_latency: Histogram,
194    pub handle_transaction_latency: Histogram,
195    pub handle_transaction_consensus_latency: Histogram,
196    pub handle_submit_transaction_consensus_latency: HistogramVec,
197    pub handle_wait_for_effects_ping_latency: HistogramVec,
198
199    handle_submit_transaction_latency: HistogramVec,
200    handle_submit_transaction_bytes: HistogramVec,
201    handle_submit_transaction_batch_size: HistogramVec,
202
203    num_rejected_tx_during_overload: IntCounterVec,
204    submission_rejected_transactions: IntCounterVec,
205    submission_suppressed_already_processed: IntCounterVec,
206    submission_suppressed_recently_submitted: IntCounterVec,
207    recently_submitted_cache_size: IntGauge,
208    recently_submitted_resubmission_interval: Histogram,
209    submission_suppressed_inflight: IntCounterVec,
210    inflight_transactions: IntGauge,
211    connection_ip_not_found: IntCounter,
212    forwarded_header_parse_error: IntCounter,
213    forwarded_header_invalid: IntCounter,
214    forwarded_header_not_included: IntCounter,
215    client_id_source_config_mismatch: IntCounter,
216    x_forwarded_for_num_hops: Gauge,
217    pub gasless_rate_limited_count: IntCounter,
218    pub gasless_submission_outcomes: IntCounterVec,
219}
220
221impl ValidatorServiceMetrics {
222    pub fn new(registry: &Registry) -> Self {
223        Self {
224            signature_errors: register_int_counter_with_registry!(
225                "total_signature_errors",
226                "Number of transaction signature errors",
227                registry,
228            )
229            .unwrap(),
230            tx_verification_latency: register_histogram_with_registry!(
231                "validator_service_tx_verification_latency",
232                "Latency of verifying a transaction",
233                mysten_metrics::SUBSECOND_LATENCY_SEC_BUCKETS.to_vec(),
234                registry,
235            )
236            .unwrap(),
237            handle_transaction_latency: register_histogram_with_registry!(
238                "validator_service_handle_transaction_latency",
239                "Latency of handling a transaction",
240                mysten_metrics::SUBSECOND_LATENCY_SEC_BUCKETS.to_vec(),
241                registry,
242            )
243            .unwrap(),
244            handle_transaction_consensus_latency: register_histogram_with_registry!(
245                "validator_service_handle_transaction_consensus_latency",
246                "Latency of handling a user transaction sent through consensus",
247                mysten_metrics::COARSE_LATENCY_SEC_BUCKETS.to_vec(),
248                registry,
249            )
250            .unwrap(),
251            handle_submit_transaction_consensus_latency: register_histogram_vec_with_registry!(
252                "validator_service_submit_transaction_consensus_latency",
253                "Latency of submitting a user transaction sent through consensus",
254                &["req_type"],
255                mysten_metrics::COARSE_LATENCY_SEC_BUCKETS.to_vec(),
256                registry,
257            )
258            .unwrap(),
259            handle_submit_transaction_latency: register_histogram_vec_with_registry!(
260                "validator_service_submit_transaction_latency",
261                "Latency of submit transaction handler",
262                &["req_type"],
263                mysten_metrics::LATENCY_SEC_BUCKETS.to_vec(),
264                registry,
265            )
266            .unwrap(),
267            handle_wait_for_effects_ping_latency: register_histogram_vec_with_registry!(
268                "validator_service_handle_wait_for_effects_ping_latency",
269                "Latency of handling a ping request for wait_for_effects",
270                &["req_type"],
271                mysten_metrics::SUBSECOND_LATENCY_SEC_BUCKETS.to_vec(),
272                registry,
273            )
274            .unwrap(),
275            handle_submit_transaction_bytes: register_histogram_vec_with_registry!(
276                "validator_service_submit_transaction_bytes",
277                "The size of transactions in the submit transaction request",
278                &["req_type"],
279                mysten_metrics::BYTES_BUCKETS.to_vec(),
280                registry,
281            )
282            .unwrap(),
283            handle_submit_transaction_batch_size: register_histogram_vec_with_registry!(
284                "validator_service_submit_transaction_batch_size",
285                "The number of transactions in the submit transaction request",
286                &["req_type"],
287                mysten_metrics::COUNT_BUCKETS.to_vec(),
288                registry,
289            )
290            .unwrap(),
291            num_rejected_tx_during_overload: register_int_counter_vec_with_registry!(
292                "validator_service_num_rejected_tx_during_overload",
293                "Number of rejected transaction due to system overload",
294                &["error_type"],
295                registry,
296            )
297            .unwrap(),
298            submission_rejected_transactions: register_int_counter_vec_with_registry!(
299                "validator_service_submission_rejected_transactions",
300                "Number of transactions rejected during submission",
301                &["reason"],
302                registry,
303            )
304            .unwrap(),
305            submission_suppressed_already_processed: register_int_counter_vec_with_registry!(
306                "validator_service_submission_suppressed_already_processed",
307                "Number of submitted transactions suppressed because consensus had already \
308                 processed them this epoch (re-submission of already-processed transactions)",
309                &["req_type"],
310                registry,
311            )
312            .unwrap(),
313            submission_suppressed_recently_submitted: register_int_counter_vec_with_registry!(
314                "validator_service_submission_suppressed_recently_submitted",
315                "Number of submitted transactions suppressed because the same transaction was \
316                 submitted within the recent-submission window",
317                &["req_type"],
318                registry,
319            )
320            .unwrap(),
321            recently_submitted_cache_size: register_int_gauge_with_registry!(
322                "validator_service_recently_submitted_cache_size",
323                "Approximate number of transaction digests held in the recent-submission duplicate-suppression cache",
324                registry,
325            )
326            .unwrap(),
327            recently_submitted_resubmission_interval: register_histogram_with_registry!(
328                "validator_service_recently_submitted_resubmission_interval_seconds",
329                "Time between a transaction being recorded and a duplicate resubmission of it being suppressed",
330                mysten_metrics::SUBSECOND_LATENCY_SEC_BUCKETS.to_vec(),
331                registry,
332            )
333            .unwrap(),
334            submission_suppressed_inflight: register_int_counter_vec_with_registry!(
335                "validator_service_submission_suppressed_inflight",
336                "Number of submitted transactions suppressed because the same transaction was \
337                 already being handled by a concurrent in-flight submit request",
338                &["req_type"],
339                registry,
340            )
341            .unwrap(),
342            inflight_transactions: register_int_gauge_with_registry!(
343                "validator_service_inflight_transactions",
344                "Number of transactions from inflight submit requests",
345                registry,
346            )
347            .unwrap(),
348            connection_ip_not_found: register_int_counter_with_registry!(
349                "validator_service_connection_ip_not_found",
350                "Number of times connection IP was not extractable from request",
351                registry,
352            )
353            .unwrap(),
354            forwarded_header_parse_error: register_int_counter_with_registry!(
355                "validator_service_forwarded_header_parse_error",
356                "Number of times x-forwarded-for header could not be parsed",
357                registry,
358            )
359            .unwrap(),
360            forwarded_header_invalid: register_int_counter_with_registry!(
361                "validator_service_forwarded_header_invalid",
362                "Number of times x-forwarded-for header was invalid",
363                registry,
364            )
365            .unwrap(),
366            forwarded_header_not_included: register_int_counter_with_registry!(
367                "validator_service_forwarded_header_not_included",
368                "Number of times x-forwarded-for header was (unexpectedly) not included in request",
369                registry,
370            )
371            .unwrap(),
372            client_id_source_config_mismatch: register_int_counter_with_registry!(
373                "validator_service_client_id_source_config_mismatch",
374                "Number of times detected that client id source config doesn't agree with x-forwarded-for header",
375                registry,
376            )
377            .unwrap(),
378            x_forwarded_for_num_hops: register_gauge_with_registry!(
379                "validator_service_x_forwarded_for_num_hops",
380                "Number of hops in x-forwarded-for header",
381                registry,
382            )
383            .unwrap(),
384            gasless_rate_limited_count: register_int_counter_with_registry!(
385                "validator_service_gasless_rate_limited_count",
386                "Number of gasless transactions rejected by rate limiter",
387                registry,
388            )
389            .unwrap(),
390            gasless_submission_outcomes: register_int_counter_vec_with_registry!(
391                "validator_service_gasless_submission_outcomes",
392                "Number of valid gasless transaction submissions by outcome",
393                &["outcome"],
394                registry,
395            )
396            .unwrap(),
397        }
398    }
399
400    pub fn new_for_tests() -> Self {
401        let registry = Registry::new();
402        Self::new(&registry)
403    }
404}
405
406/// Per-request routing decision: records where `handle_submit_transaction` sends one
407/// particular request.
408#[derive(Clone, Copy)]
409enum UserSubmissionMode {
410    /// Admit via the gas-price priority queue.
411    Queue,
412    /// Admit via the consensus-polled transaction pool.
413    Pool,
414    /// Submit directly to consensus, bypassing the queue — used when the queue
415    /// is turned off by config, temporarily disabled by failover, or for a ping
416    /// request. Individual txs are rejected when consensus is saturated
417    /// (pre-queue behavior).
418    Direct,
419}
420
421/// The user-transaction submission infrastructure this validator runs, fixed at
422/// startup from `NodeConfig`. Contrast with `UserSubmissionMode`, the
423/// per-request routing decision derived from this configuration.
424#[derive(Clone)]
425pub enum UserSubmissionPath {
426    Direct,
427    AdmissionQueue(AdmissionQueueContext),
428    Pool(Arc<TransactionPoolContext>),
429}
430
431#[derive(Clone)]
432pub struct ValidatorService {
433    state: Arc<AuthorityState>,
434    consensus_adapter: Arc<ConsensusAdapter>,
435    metrics: Arc<ValidatorServiceMetrics>,
436    traffic_controller: Option<Arc<TrafficController>>,
437    client_id_source: Option<ClientIdSource>,
438    gasless_limiter: GaslessRateLimiter,
439    user_submission_path: UserSubmissionPath,
440    /// Digests submitted within the last `recent_submission_window` (value: when recorded), to
441    /// drop duplicate resubmissions before they reach consensus.
442    recently_submitted: Cache<TransactionDigest, Instant>,
443    /// How long a transaction is suppressed after submission (from node config).
444    recent_submission_window: Duration,
445    /// Digests currently being handled by an in-flight submit handler. Acquired atomically at
446    /// entry and removed (then demoted into `recently_submitted`) when the handler returns.
447    inflight_transactions: Arc<Mutex<HashSet<TransactionDigest>>>,
448}
449
450/// Assumed peak distinct-submission rate, used to size the dedup cache (per window).
451const RECENT_SUBMISSION_PEAK_TPS: u64 = 50_000;
452
453impl ValidatorService {
454    pub fn new(
455        state: Arc<AuthorityState>,
456        consensus_adapter: Arc<ConsensusAdapter>,
457        validator_metrics: Arc<ValidatorServiceMetrics>,
458        client_id_source: Option<ClientIdSource>,
459        user_submission_path: UserSubmissionPath,
460    ) -> Self {
461        let traffic_controller = state.traffic_controller.clone();
462        let gasless_limiter = GaslessRateLimiter::new(state.consensus_gasless_counter.clone());
463        let recent_submission_window = state.config.recent_submission_dedup_window();
464        Self {
465            state,
466            consensus_adapter,
467            metrics: validator_metrics,
468            traffic_controller,
469            client_id_source,
470            gasless_limiter,
471            user_submission_path,
472            recently_submitted: Self::new_recently_submitted_cache(recent_submission_window),
473            recent_submission_window,
474            inflight_transactions: Arc::new(Mutex::new(HashSet::new())),
475        }
476    }
477
478    fn new_recently_submitted_cache(window: Duration) -> Cache<TransactionDigest, Instant> {
479        // Memory backstop only; the window bounds the cache, and amplified duplicates do not add
480        // entries (they share a digest). Sized for roughly one window at peak throughput.
481        let max_capacity = window.as_secs().max(1) * RECENT_SUBMISSION_PEAK_TPS;
482        Cache::builder()
483            .time_to_live(window)
484            .max_capacity(max_capacity)
485            .build()
486    }
487
488    pub fn new_for_tests(
489        state: Arc<AuthorityState>,
490        consensus_adapter: Arc<ConsensusAdapter>,
491        metrics: Arc<ValidatorServiceMetrics>,
492    ) -> Self {
493        let gasless_limiter = GaslessRateLimiter::new(state.consensus_gasless_counter.clone());
494        let epoch_store = state.epoch_store_for_testing().clone();
495        let slot_freed_notify = Arc::new(tokio::sync::Notify::new());
496        let manager = Arc::new(AdmissionQueueManager::new_for_tests(
497            consensus_adapter.clone(),
498            slot_freed_notify,
499        ));
500        let user_submission_path =
501            UserSubmissionPath::AdmissionQueue(AdmissionQueueContext::spawn(manager, epoch_store));
502        let recent_submission_window = state.config.recent_submission_dedup_window();
503        Self {
504            state,
505            consensus_adapter,
506            metrics,
507            traffic_controller: None,
508            client_id_source: None,
509            gasless_limiter,
510            user_submission_path,
511            recently_submitted: Self::new_recently_submitted_cache(recent_submission_window),
512            recent_submission_window,
513            inflight_transactions: Arc::new(Mutex::new(HashSet::new())),
514        }
515    }
516
517    pub fn validator_state(&self) -> &Arc<AuthorityState> {
518        &self.state
519    }
520
521    /// Test method that performs transaction validation without going through gRPC.
522    pub fn handle_transaction_for_testing(&self, transaction: Transaction) -> SuiResult<()> {
523        let epoch_store = self.state.load_epoch_store_one_call_per_task();
524
525        // Validity check (basic structural validation)
526        transaction.validity_check(&epoch_store.tx_validity_check_context())?;
527
528        // Signature verification
529        let transaction = epoch_store
530            .verify_transaction_require_no_aliases(transaction)?
531            .into_tx();
532
533        // Validate the transaction
534        self.state
535            .handle_vote_transaction(&epoch_store, transaction)?;
536
537        Ok(())
538    }
539
540    /// Test method that performs transaction validation with overload checking.
541    /// Used for testing validator overload behavior.
542    pub fn handle_transaction_for_testing_with_overload_check(
543        &self,
544        transaction: Transaction,
545    ) -> SuiResult<()> {
546        let epoch_store = self.state.load_epoch_store_one_call_per_task();
547
548        // Validity check (basic structural validation)
549        transaction.validity_check(&epoch_store.tx_validity_check_context())?;
550
551        // Check system overload
552        self.state.check_system_overload(
553            transaction.data(),
554            self.state.check_system_overload_at_signing(),
555        )?;
556
557        // Signature verification
558        let transaction = epoch_store
559            .verify_transaction_require_no_aliases(transaction)?
560            .into_tx();
561
562        // Validate the transaction
563        self.state
564            .handle_vote_transaction(&epoch_store, transaction)?;
565
566        Ok(())
567    }
568
569    /// Collect the IDs of input objects that are immutable.
570    /// This is used to create the ImmutableInputObjects claim for consensus messages.
571    async fn collect_immutable_object_ids(
572        &self,
573        tx: &VerifiedTransaction,
574        state: &AuthorityState,
575    ) -> SuiResult<Vec<ObjectID>> {
576        let input_objects = tx.data().transaction_data().input_objects()?;
577
578        // Collect object IDs from ImmOrOwnedMoveObject inputs
579        let object_ids: Vec<ObjectID> = input_objects
580            .iter()
581            .filter_map(|obj| match obj {
582                InputObjectKind::ImmOrOwnedMoveObject((id, _, _)) => Some(*id),
583                _ => None,
584            })
585            .collect();
586        if object_ids.is_empty() {
587            return Ok(vec![]);
588        }
589
590        // Load objects from cache and filter to immutable ones
591        let objects = state.get_object_cache_reader().get_objects(&object_ids);
592
593        // All objects should be found, since owned input objects have been validated to exist.
594        objects
595            .into_iter()
596            .zip_debug_eq(object_ids.iter())
597            .filter_map(|(obj, id)| {
598                let Some(o) = obj else {
599                    return Some(Err::<ObjectID, SuiError>(
600                        SuiErrorKind::UserInputError {
601                            error: UserInputError::ObjectNotFound {
602                                object_id: *id,
603                                version: None,
604                            },
605                        }
606                        .into(),
607                    ));
608                };
609                if o.is_immutable() {
610                    Some(Ok(*id))
611                } else {
612                    None
613                }
614            })
615            .collect::<SuiResult<Vec<ObjectID>>>()
616    }
617
618    #[instrument(
619        name = "ValidatorService::handle_submit_transaction",
620        level = "error",
621        skip_all,
622        err(level = "debug")
623    )]
624    async fn handle_submit_transaction(
625        &self,
626        request: tonic::Request<RawSubmitTxRequest>,
627    ) -> WrappedServiceResponse<RawSubmitTxResponse> {
628        let Self {
629            state,
630            consensus_adapter: _,
631            metrics,
632            traffic_controller: _,
633            client_id_source,
634            gasless_limiter: _,
635            user_submission_path: _,
636            recently_submitted: _,
637            recent_submission_window: _,
638            inflight_transactions: _,
639        } = self.clone();
640
641        let submitter_client_addr = if let Some(client_id_source) = &client_id_source {
642            self.get_client_ip_addr(&request, client_id_source)
643        } else {
644            self.get_client_ip_addr(&request, &ClientIdSource::SocketAddr)
645        };
646
647        let inner = request.into_inner();
648        let start_epoch = state.load_epoch_store_one_call_per_task().epoch();
649
650        let next_epoch = start_epoch + 1;
651        let mut max_retries = 1;
652
653        let mut inflight_guard = InflightTransactionsGuard::new(self);
654        loop {
655            let res = self
656                .handle_submit_transaction_inner(
657                    &state,
658                    &metrics,
659                    &inner,
660                    submitter_client_addr,
661                    &mut inflight_guard,
662                )
663                .await;
664            match res {
665                Ok((response, weight)) => return Ok((tonic::Response::new(response), weight)),
666                Err(err) => {
667                    if max_retries > 0
668                        && let SuiErrorKind::ValidatorHaltedAtEpochEnd = err.as_inner()
669                    {
670                        max_retries -= 1;
671
672                        debug!(
673                            "ValidatorHaltedAtEpochEnd. Will retry after validator reconfigures"
674                        );
675
676                        if let Ok(Ok(new_epoch)) =
677                            timeout(Duration::from_secs(15), state.wait_for_epoch(next_epoch)).await
678                        {
679                            assert_reachable!("retry submission at epoch end");
680                            if new_epoch >= next_epoch {
681                                continue;
682                            }
683                            // wait_for_epoch guarantees >= target; < would indicate a bug there.
684                            debug_fatal!(
685                                "wait_for_epoch returned early: expected >= {}, got {}",
686                                next_epoch,
687                                new_epoch
688                            );
689                        }
690                    }
691                    return Err(err.into());
692                }
693            }
694        }
695    }
696
697    async fn handle_submit_transaction_inner(
698        &self,
699        state: &AuthorityState,
700        metrics: &ValidatorServiceMetrics,
701        request: &RawSubmitTxRequest,
702        submitter_client_addr: Option<IpAddr>,
703        inflight_guard: &mut InflightTransactionsGuard,
704    ) -> SuiResult<(RawSubmitTxResponse, Weight)> {
705        let epoch_store = state.load_epoch_store_one_call_per_task();
706        // A node leaving the committee keeps serving until reconfiguration shuts this server
707        // down, but it can no longer sequence transactions: a submission would wait for a
708        // consensus position until the epoch ends.
709        if !epoch_store.is_validator() {
710            return Err(SuiErrorKind::ValidatorHaltedAtEpochEnd.into());
711        }
712        let submit_type = SubmitTxType::try_from(request.submit_type).map_err(|e| {
713            SuiErrorKind::GrpcMessageDeserializeError {
714                type_info: "RawSubmitTxRequest.submit_type".to_string(),
715                error: e.to_string(),
716            }
717        })?;
718
719        let is_ping_request = submit_type == SubmitTxType::Ping;
720        if is_ping_request {
721            fp_ensure!(
722                request.transactions.is_empty(),
723                SuiErrorKind::InvalidRequest(format!(
724                    "Ping request cannot contain {} transactions",
725                    request.transactions.len()
726                ))
727                .into()
728            );
729        } else {
730            // Ensure default and soft bundle requests contain at least one transaction.
731            fp_ensure!(
732                !request.transactions.is_empty(),
733                SuiErrorKind::InvalidRequest(
734                    "At least one transaction needs to be submitted".to_string(),
735                )
736                .into()
737            );
738        }
739
740        // NOTE: for soft bundle requests, the system tries to sequence the transactions in the same order
741        // if they use the same gas price. But this is only done with best effort.
742        // Transactions in a soft bundle can be individually rejected or deferred, without affecting
743        // other transactions in the same bundle.
744        let is_soft_bundle_request = submit_type == SubmitTxType::SoftBundle;
745
746        let max_num_transactions = if is_soft_bundle_request {
747            // Soft bundle cannot contain too many transactions.
748            // Otherwise it is hard to include all of them in a single block.
749            epoch_store.protocol_config().max_soft_bundle_size()
750        } else {
751            // Still enforce a limit even when transactions do not need to be in the same block.
752            epoch_store
753                .protocol_config()
754                .max_num_transactions_in_block()
755        };
756        fp_ensure!(
757            request.transactions.len() <= max_num_transactions as usize,
758            SuiErrorKind::InvalidRequest(format!(
759                "Too many transactions in request: {} vs {}",
760                request.transactions.len(),
761                max_num_transactions
762            ))
763            .into()
764        );
765
766        // Transaction digests.
767        let mut tx_digests = Vec::with_capacity(request.transactions.len());
768        // Transactions to submit to consensus.
769        let mut consensus_transactions = Vec::with_capacity(request.transactions.len());
770        // Indexes of transactions above in the request transactions.
771        let mut transaction_indexes = Vec::with_capacity(request.transactions.len());
772        // Results corresponding to each transaction in the request.
773        let mut results: Vec<Option<SubmitTxResult>> = vec![None; request.transactions.len()];
774        // Total size of all transactions in the request.
775        let mut total_size_bytes = 0;
776        // Whether the request contains any gasless transaction.
777        let mut has_gasless = false;
778        // Set when a transaction duplicates an in-flight submission at admission. Tracked
779        // separately because it is detected after the per-tx results are finalized (those
780        // remain Submitted), so it cannot be derived from the results alone.
781        let mut duplicate_at_admission = false;
782        // First gas price seen in this soft bundle.
783        let mut expected_soft_bundle_gas_price = None;
784        // First member of this soft bundle, with the proposers it allows.
785        let mut expected_soft_bundle_allowed_proposers = None;
786        // Transaction digests seen in this request attempt, used to reject repeated transactions.
787        let mut request_digests = HashSet::new();
788
789        let req_type = if is_ping_request {
790            "ping"
791        } else if request.transactions.len() == 1 {
792            "single_transaction"
793        } else if is_soft_bundle_request {
794            "soft_bundle"
795        } else {
796            "batch"
797        };
798
799        let _handle_tx_metrics_guard = metrics
800            .handle_submit_transaction_latency
801            .with_label_values(&[req_type])
802            .start_timer();
803
804        let submit_mode = self.classify_submit_mode(is_ping_request);
805
806        for (idx, tx_bytes) in request.transactions.iter().enumerate() {
807            let transaction = match bcs::from_bytes::<Transaction>(tx_bytes) {
808                Ok(txn) => txn,
809                Err(e) => {
810                    // Ok to fail the request when any transaction is invalid.
811                    return Err(SuiErrorKind::TransactionDeserializationError {
812                        error: format!("Failed to deserialize transaction at index {}: {}", idx, e),
813                    }
814                    .into());
815                }
816            };
817
818            // Ok to fail the request when any transaction is invalid.
819            let tx_size = transaction.validity_check(&epoch_store.tx_validity_check_context())?;
820            let tx_digest = *transaction.digest();
821
822            // Reject up front rather than proposing a block that peers would reject: the client
823            // must submit to one of the proposers the transaction allows.
824            epoch_store.check_self_allowed_proposer(transaction.data().transaction_data())?;
825
826            // A request must not repeat a transaction.
827            if !request_digests.insert(tx_digest) {
828                let error: SuiError = SuiErrorKind::UserInputError {
829                    error: UserInputError::RepeatedTransactions { digest: tx_digest },
830                }
831                .into();
832                // Reject individual repeated transactions in batch.
833                if is_soft_bundle_request {
834                    return Err(error);
835                }
836                results[idx] = Some(SubmitTxResult::Rejected { error });
837                continue;
838            }
839
840            // Soft bundles require all transactions to use the same gas price, and to name
841            // the same allowed proposers: the bundle is proposed as one unit, so members
842            // disagreeing on who may propose it cannot all be honoured, and an
843            // unrestricted member would otherwise carry a restricted one past its bound.
844            if is_soft_bundle_request {
845                let gas_price = transaction.data().transaction_data().gas_price();
846                if let Some(expected) = expected_soft_bundle_gas_price {
847                    fp_ensure!(
848                        gas_price == expected,
849                        SuiErrorKind::UserInputError {
850                            error: UserInputError::GasPriceMismatchError {
851                                digest: tx_digest,
852                                expected,
853                                actual: gas_price,
854                            }
855                        }
856                        .into()
857                    );
858                } else {
859                    expected_soft_bundle_gas_price = Some(gas_price);
860                }
861
862                let allowed_proposers = transaction
863                    .data()
864                    .transaction_data()
865                    .expiration()
866                    .allowed_proposers(epoch_store.epoch())
867                    .cloned();
868                if let Some((first_digest, expected)) = &expected_soft_bundle_allowed_proposers {
869                    fp_ensure!(
870                        allowed_proposers == *expected,
871                        SuiErrorKind::UserInputError {
872                            error: UserInputError::InvalidExpiration {
873                                error: format!(
874                                    "soft bundle members must name the same allowed proposers: \
875                                     transaction {tx_digest} differs from {first_digest}"
876                                ),
877                            }
878                        }
879                        .into()
880                    );
881                } else {
882                    expected_soft_bundle_allowed_proposers = Some((tx_digest, allowed_proposers));
883                }
884            }
885
886            let is_gasless = transaction
887                .data()
888                .transaction_data()
889                .is_gasless_transaction();
890
891            if is_gasless {
892                has_gasless = true;
893                metrics
894                    .gasless_submission_outcomes
895                    .with_label_values(&["attempted"])
896                    .inc();
897            }
898
899            let overload_check_res = state.check_system_overload(
900                transaction.data(),
901                state.check_system_overload_at_signing(),
902            );
903            if let Err(error) = overload_check_res {
904                metrics
905                    .num_rejected_tx_during_overload
906                    .with_label_values(&[error.as_ref()])
907                    .inc();
908                if is_gasless {
909                    metrics
910                        .gasless_submission_outcomes
911                        .with_label_values(&["rejected_overload"])
912                        .inc();
913                }
914                results[idx] = Some(SubmitTxResult::Rejected { error });
915                continue;
916            }
917
918            // Use the pre-queue per-tx consensus overload reject on the direct
919            // submission path (queue off, failover, or ping). Skipped in pool
920            // mode: the check reads the ConsensusAdapter's inflight-submission buffers,
921            // which are not relevant when block contents are pulled by consensus.
922            if matches!(submit_mode, UserSubmissionMode::Direct)
923                && !matches!(&self.user_submission_path, UserSubmissionPath::Pool(_))
924                && let Err(error) = self.consensus_adapter.check_consensus_overload()
925            {
926                state.update_overload_metrics("consensus");
927                metrics
928                    .num_rejected_tx_during_overload
929                    .with_label_values(&[error.as_ref()])
930                    .inc();
931                if is_gasless {
932                    metrics
933                        .gasless_submission_outcomes
934                        .with_label_values(&["rejected_overload"])
935                        .inc();
936                }
937                results[idx] = Some(SubmitTxResult::Rejected { error });
938                continue;
939            }
940
941            // The rate-limiter slot is not refunded, so a gasless transaction before its
942            // `min_timestamp` is rejected first.
943            if is_gasless
944                && let Err(error) = epoch_store
945                    .check_timestamp_expiration_at_submission(transaction.data().transaction_data())
946                && matches!(
947                    error.as_inner(),
948                    SuiErrorKind::TransactionNotYetValid { .. }
949                )
950            {
951                metrics
952                    .submission_rejected_transactions
953                    .with_label_values(&[error.to_variant_name()])
954                    .inc();
955                results[idx] = Some(SubmitTxResult::Rejected { error });
956                continue;
957            }
958
959            if is_gasless
960                && !self
961                    .gasless_limiter
962                    .try_acquire(epoch_store.protocol_config())
963            {
964                metrics.gasless_rate_limited_count.inc();
965                metrics
966                    .gasless_submission_outcomes
967                    .with_label_values(&["rejected_rate_limited"])
968                    .inc();
969                results[idx] = Some(SubmitTxResult::Rejected {
970                    error: SuiErrorKind::ValidatorOverloadedRetryAfter {
971                        retry_after_secs: 1,
972                    }
973                    .into(),
974                });
975                continue;
976            }
977
978            // Ok to fail the request when any signature is invalid.
979            let verified_transaction = {
980                let _metrics_guard = metrics.tx_verification_latency.start_timer();
981                if epoch_store.protocol_config().address_aliases() {
982                    match epoch_store.verify_transaction_with_current_aliases(transaction) {
983                        Ok(tx) => tx,
984                        Err(e) => {
985                            metrics.signature_errors.inc();
986                            return Err(e);
987                        }
988                    }
989                } else {
990                    match epoch_store.verify_transaction_require_no_aliases(transaction) {
991                        Ok(tx) => tx,
992                        Err(e) => {
993                            metrics.signature_errors.inc();
994                            return Err(e);
995                        }
996                    }
997                }
998            };
999
1000            debug!(
1001                ?tx_digest,
1002                "handle_submit_transaction: verified transaction"
1003            );
1004
1005            // Check if the transaction has executed, before checking input objects
1006            // which could have been consumed.
1007            if let Some(effects) = state
1008                .get_transaction_cache_reader()
1009                .get_executed_effects(&tx_digest)
1010            {
1011                let effects_digest = effects.digest();
1012                if let Err(error) = state.check_effects_against_previously_signed(
1013                    &epoch_store,
1014                    &tx_digest,
1015                    &effects_digest,
1016                    "submit_transaction",
1017                ) {
1018                    results[idx] = Some(SubmitTxResult::Rejected { error });
1019                    continue;
1020                }
1021                if let Ok(executed_data) = self.complete_executed_data(effects).await {
1022                    let executed_result = SubmitTxResult::Executed {
1023                        effects_digest,
1024                        details: Some(executed_data),
1025                    };
1026                    results[idx] = Some(executed_result);
1027                    debug!(?tx_digest, "handle_submit_transaction: already executed");
1028                    continue;
1029                }
1030            }
1031
1032            if self
1033                .state
1034                .get_transaction_cache_reader()
1035                .transaction_executed_in_last_epoch(&tx_digest, epoch_store.epoch())
1036            {
1037                results[idx] = Some(SubmitTxResult::Rejected {
1038                    error: UserInputError::TransactionAlreadyExecuted { digest: tx_digest }.into(),
1039                });
1040                debug!(
1041                    ?tx_digest,
1042                    "handle_submit_transaction: transaction already executed in previous epoch"
1043                );
1044                continue;
1045            }
1046
1047            // Suppress resubmission of transactions consensus already processed this epoch:
1048            // executed transactions whose effects details could not be reconstructed above (e.g.
1049            // objects pruned), sequenced-but-deferred transactions, and dropped transactions.
1050            let consensus_key = SequencedConsensusTransactionKey::External(
1051                ConsensusTransactionKey::Certificate(tx_digest),
1052            );
1053            if epoch_store.is_consensus_message_processed(&consensus_key)? {
1054                // Prefer a concrete, non-retriable error over the generic, retriable
1055                // TransactionProcessing suppression. A processed-but-unexecuted digest is
1056                // commonly a dropped owned-object conflict loser; surfacing the terminal
1057                // error lets the client stop retrying instead of polling for effects that
1058                // will never come.
1059                //
1060                // First check the epoch owned-object lock table with the same conflict
1061                // logic the consensus handler uses post-consensus. Locks are never
1062                // released within an epoch, so this reports the conflict even before the
1063                // winner executes, while the loser's input versions still validate as
1064                // live.
1065                if let Ok(input_objects) = verified_transaction
1066                    .tx()
1067                    .data()
1068                    .transaction_data()
1069                    .input_objects()
1070                {
1071                    let immutable_object_ids = self
1072                        .collect_immutable_object_ids(verified_transaction.tx(), state)
1073                        .await?;
1074                    let owned_object_refs: Vec<_> = input_objects
1075                        .iter()
1076                        .filter_map(|obj| match obj {
1077                            InputObjectKind::ImmOrOwnedMoveObject(obj_ref)
1078                                if !immutable_object_ids.contains(&obj_ref.0) =>
1079                            {
1080                                Some(*obj_ref)
1081                            }
1082                            _ => None,
1083                        })
1084                        .collect();
1085                    let existing_locks = epoch_store.get_owned_object_locks_map(&owned_object_refs);
1086                    if let Err(error) = epoch_store.try_acquire_owned_object_locks_post_consensus(
1087                        &owned_object_refs,
1088                        tx_digest,
1089                        &HashMap::new(),
1090                        &existing_locks,
1091                    ) {
1092                        debug!(
1093                            ?tx_digest,
1094                            "handle_submit_transaction: processed transaction rejected on lock conflict: {error}"
1095                        );
1096                        metrics
1097                            .submission_rejected_transactions
1098                            .with_label_values(&[error.to_variant_name()])
1099                            .inc();
1100                        results[idx] = Some(SubmitTxResult::Rejected { error });
1101                        continue;
1102                    }
1103                }
1104                // Then revalidate against live state, which surfaces the terminal
1105                // stale-version error once the conflict winner has executed.
1106                if let Err(error) =
1107                    state.handle_vote_transaction(&epoch_store, verified_transaction.tx().clone())
1108                {
1109                    // The transaction may have executed while being validated (e.g. it was
1110                    // deferred rather than dropped).
1111                    if let Some(effects) = state
1112                        .get_transaction_cache_reader()
1113                        .get_executed_effects(&tx_digest)
1114                    {
1115                        let effects_digest = effects.digest();
1116                        if let Err(error) = state.check_effects_against_previously_signed(
1117                            &epoch_store,
1118                            &tx_digest,
1119                            &effects_digest,
1120                            "submit_transaction",
1121                        ) {
1122                            results[idx] = Some(SubmitTxResult::Rejected { error });
1123                            continue;
1124                        }
1125                        if let Ok(executed_data) = self.complete_executed_data(effects).await {
1126                            results[idx] = Some(SubmitTxResult::Executed {
1127                                effects_digest,
1128                                details: Some(executed_data),
1129                            });
1130                            continue;
1131                        }
1132                    }
1133                    debug!(
1134                        ?tx_digest,
1135                        "handle_submit_transaction: processed transaction rejected on revalidation: {error}"
1136                    );
1137                    metrics
1138                        .submission_rejected_transactions
1139                        .with_label_values(&[error.to_variant_name()])
1140                        .inc();
1141                    results[idx] = Some(SubmitTxResult::Rejected { error });
1142                    continue;
1143                }
1144                // Validation passed, so this processed digest may still be executable.
1145                // Return retriable TransactionProcessing rather than resubmitting it to consensus.
1146                // A later client retry can observe effects or a concrete terminal validation error.
1147                metrics
1148                    .submission_suppressed_already_processed
1149                    .with_label_values(&[req_type])
1150                    .inc();
1151                results[idx] = Some(SubmitTxResult::Rejected {
1152                    error: SuiErrorKind::TransactionProcessing {
1153                        digest: tx_digest,
1154                        status: "consensus message processed".to_string(),
1155                    }
1156                    .into(),
1157                });
1158                debug!(
1159                    ?tx_digest,
1160                    "handle_submit_transaction: consensus message already processed"
1161                );
1162                continue;
1163            }
1164
1165            // Checked after the lookups above, so that a retry of a transaction that already
1166            // executed gets its effects rather than an expiry error.
1167            if let Err(error) = epoch_store.check_timestamp_expiration_at_submission(
1168                verified_transaction.tx().data().transaction_data(),
1169            ) {
1170                debug!(
1171                    ?tx_digest,
1172                    "Transaction rejected during submission: {error}"
1173                );
1174                metrics
1175                    .submission_rejected_transactions
1176                    .with_label_values(&[error.to_variant_name()])
1177                    .inc();
1178                results[idx] = Some(SubmitTxResult::Rejected { error });
1179                continue;
1180            }
1181
1182            // Atomically acquire the digest for the duration of this handler. Reject concurrent
1183            // and recent duplicates and record the result per result index.
1184            match inflight_guard.try_acquire(tx_digest) {
1185                AcquireOutcome::Acquired | AcquireOutcome::AlreadyAcquiredByThisRequest => {
1186                    // Continue to process the transaction and submit to consensus.
1187                }
1188                AcquireOutcome::AlreadyAcquiredByAnotherRequest => {
1189                    metrics
1190                        .submission_suppressed_inflight
1191                        .with_label_values(&[req_type])
1192                        .inc();
1193                    results[idx] = Some(SubmitTxResult::Rejected {
1194                        error: SuiErrorKind::TransactionSubmitted { digest: tx_digest }.into(),
1195                    });
1196                    debug!(
1197                        ?tx_digest,
1198                        "handle_submit_transaction: concurrent submission in progress"
1199                    );
1200                    continue;
1201                }
1202                AcquireOutcome::RecentlyProcessed { since } => {
1203                    metrics
1204                        .submission_suppressed_recently_submitted
1205                        .with_label_values(&[req_type])
1206                        .inc();
1207                    metrics
1208                        .recently_submitted_resubmission_interval
1209                        .observe(since.as_secs_f64());
1210                    results[idx] = Some(SubmitTxResult::Rejected {
1211                        error: SuiErrorKind::TransactionSubmitted { digest: tx_digest }.into(),
1212                    });
1213                    debug!(?tx_digest, "handle_submit_transaction: recently processed");
1214                    continue;
1215                }
1216            }
1217
1218            debug!(
1219                ?tx_digest,
1220                "handle_submit_transaction: waiting for fastpath dependency objects"
1221            );
1222            if !state
1223                .wait_for_fastpath_dependency_objects(
1224                    verified_transaction.tx(),
1225                    epoch_store.epoch(),
1226                )
1227                .await?
1228            {
1229                debug!(
1230                    ?tx_digest,
1231                    "fastpath input objects are still unavailable after waiting"
1232                );
1233            }
1234
1235            match state.handle_vote_transaction(&epoch_store, verified_transaction.tx().clone()) {
1236                Ok(_) => { /* continue processing */ }
1237                Err(e) => {
1238                    // Check if transaction has been executed while being validated.
1239                    // This is an edge case so checking executed effects twice is acceptable.
1240                    if let Some(effects) = state
1241                        .get_transaction_cache_reader()
1242                        .get_executed_effects(&tx_digest)
1243                    {
1244                        let effects_digest = effects.digest();
1245                        if let Err(error) = state.check_effects_against_previously_signed(
1246                            &epoch_store,
1247                            &tx_digest,
1248                            &effects_digest,
1249                            "submit_transaction",
1250                        ) {
1251                            results[idx] = Some(SubmitTxResult::Rejected { error });
1252                            continue;
1253                        }
1254                        if let Ok(executed_data) = self.complete_executed_data(effects).await {
1255                            let executed_result = SubmitTxResult::Executed {
1256                                effects_digest,
1257                                details: Some(executed_data),
1258                            };
1259                            results[idx] = Some(executed_result);
1260                            continue;
1261                        }
1262                    }
1263
1264                    // When the transaction has not been executed, record the error for the transaction.
1265                    debug!(?tx_digest, "Transaction rejected during submission: {e}");
1266                    metrics
1267                        .submission_rejected_transactions
1268                        .with_label_values(&[e.to_variant_name()])
1269                        .inc();
1270                    results[idx] = Some(SubmitTxResult::Rejected { error: e });
1271                    continue;
1272                }
1273            }
1274
1275            // Create claims with aliases and / or immutable objects.
1276            let mut claims = vec![];
1277
1278            let immutable_object_ids = self
1279                .collect_immutable_object_ids(verified_transaction.tx(), state)
1280                .await?;
1281            if !immutable_object_ids.is_empty() {
1282                claims.push(TransactionClaim::ImmutableInputObjects(
1283                    immutable_object_ids,
1284                ));
1285            }
1286
1287            let (tx, aliases) = verified_transaction.into_inner();
1288            if epoch_store.protocol_config().address_aliases() {
1289                if epoch_store
1290                    .protocol_config()
1291                    .fix_checkpoint_signature_mapping()
1292                {
1293                    claims.push(TransactionClaim::AddressAliasesV2(aliases));
1294                } else {
1295                    let v1_aliases: Vec<_> = tx
1296                        .data()
1297                        .intent_message()
1298                        .value
1299                        .required_signers()
1300                        .into_iter()
1301                        .zip_eq(aliases.into_iter().map(|(_, seq)| seq))
1302                        .collect();
1303                    #[allow(deprecated)]
1304                    claims.push(TransactionClaim::AddressAliases(
1305                        nonempty::NonEmpty::from_vec(v1_aliases)
1306                            .expect("must have at least one required_signer"),
1307                    ));
1308                }
1309            }
1310
1311            let tx_with_claims = TransactionWithClaims::new(tx.into(), claims);
1312
1313            consensus_transactions.push(ConsensusTransaction::new_user_transaction_v2_message(
1314                &state.name,
1315                tx_with_claims,
1316            ));
1317            if is_gasless {
1318                metrics
1319                    .gasless_submission_outcomes
1320                    .with_label_values(&["submitted"])
1321                    .inc();
1322            }
1323
1324            transaction_indexes.push(idx);
1325            tx_digests.push(tx_digest);
1326            total_size_bytes += tx_size;
1327        }
1328
1329        if consensus_transactions.is_empty() && !is_ping_request {
1330            let spam_weight = Self::request_spam_weight(
1331                &results,
1332                has_gasless,
1333                duplicate_at_admission,
1334                is_ping_request,
1335            );
1336            let response = Self::try_from_submit_tx_response(results)?;
1337            return Ok((response, spam_weight));
1338        }
1339
1340        // Set the max bytes size of the soft bundle to be half of the consensus max transactions in block size.
1341        // We do this to account for serialization overheads and to ensure that the soft bundle is not too large
1342        // when is attempted to be posted via consensus.
1343        let max_transaction_bytes = if is_soft_bundle_request {
1344            epoch_store
1345                .protocol_config()
1346                .consensus_max_transactions_in_block_bytes()
1347                / 2
1348        } else {
1349            epoch_store
1350                .protocol_config()
1351                .consensus_max_transactions_in_block_bytes()
1352        };
1353        fp_ensure!(
1354            total_size_bytes <= max_transaction_bytes as usize,
1355            SuiErrorKind::UserInputError {
1356                error: UserInputError::TotalTransactionSizeTooLargeInBatch {
1357                    size: total_size_bytes,
1358                    limit: max_transaction_bytes,
1359                },
1360            }
1361            .into()
1362        );
1363
1364        metrics
1365            .handle_submit_transaction_bytes
1366            .with_label_values(&[req_type])
1367            .observe(total_size_bytes as f64);
1368        metrics
1369            .handle_submit_transaction_batch_size
1370            .with_label_values(&[req_type])
1371            .observe(consensus_transactions.len() as f64);
1372
1373        let _latency_metric_guard = metrics
1374            .handle_submit_transaction_consensus_latency
1375            .with_label_values(&[req_type])
1376            .start_timer();
1377
1378        if is_soft_bundle_request {
1379            // We only allow the `consensus_transactions` to be empty for ping requests. This is how it should and is be treated from the downstream components.
1380            // For any other case, having an empty `consensus_transactions` vector is an invalid state and we should have never reached at this point.
1381            assert!(
1382                !consensus_transactions.is_empty(),
1383                "A valid soft bundle must have at least one transaction"
1384            );
1385        }
1386
1387        // Soft bundles are inserted as a single queue entry.
1388        // Individual transactions are each inserted separately.
1389        let tx_groups: Vec<Vec<ConsensusTransaction>> = if is_soft_bundle_request || is_ping_request
1390        {
1391            vec![consensus_transactions]
1392        } else {
1393            consensus_transactions
1394                .into_iter()
1395                .map(|t| vec![t])
1396                .collect()
1397        };
1398
1399        // Map each submission group back to the (result index, digest) of the transactions it
1400        // contains, so a per-group outcome — consensus positions, or an "already processing"
1401        // error — can be recorded against each individual transaction. Soft bundles submit as a
1402        // single group; individual transactions submit one group each.
1403        let group_tx_meta = if is_soft_bundle_request {
1404            vec![
1405                transaction_indexes
1406                    .into_iter()
1407                    .zip_eq(tx_digests)
1408                    .collect::<Vec<_>>(),
1409            ]
1410        } else {
1411            transaction_indexes
1412                .into_iter()
1413                .zip_eq(tx_digests)
1414                .map(|pair| vec![pair])
1415                .collect::<Vec<_>>()
1416        };
1417
1418        // Collect one result per submission group WITHOUT short-circuiting. An
1419        // already-processing transaction is reported per-tx as a retriable below;
1420        // any other error fails the whole request, after all groups have settled.
1421        // Soft bundles submit as a single group; individual transactions submit one group each.
1422        let group_results = match submit_mode {
1423            UserSubmissionMode::Direct => {
1424                let futures = tx_groups.into_iter().map(|txns| {
1425                    debug!(
1426                        "handle_submit_transaction: submitting consensus transactions ({}): {}",
1427                        req_type,
1428                        txns.iter().map(|t| t.local_display()).join(", ")
1429                    );
1430                    self.consensus_adapter.submit_and_get_positions(
1431                        txns,
1432                        &epoch_store,
1433                        submitter_client_addr,
1434                    )
1435                });
1436                future::join_all(futures).await
1437            }
1438            UserSubmissionMode::Queue => {
1439                let UserSubmissionPath::AdmissionQueue(context) = &self.user_submission_path else {
1440                    debug_fatal!("queue mode requires an admission queue");
1441                    return Err(SuiErrorKind::GenericAuthorityError {
1442                        error: "queue mode requires an admission queue".to_string(),
1443                    }
1444                    .into());
1445                };
1446                let aq = context.load();
1447                let mut receivers = Vec::with_capacity(tx_groups.len());
1448                for txns in tx_groups {
1449                    let gas_price = Self::extract_gas_price(&txns);
1450                    match aq.try_insert(gas_price, txns, submitter_client_addr).await {
1451                        Ok((rx, newly_inserted)) => {
1452                            if !newly_inserted {
1453                                // Duplicate of an in-flight submission; flag the request as spam.
1454                                // The per-tx result is still Submitted, so this is tracked
1455                                // separately.
1456                                duplicate_at_admission = true;
1457                            }
1458                            receivers.push(Ok(rx));
1459                        }
1460                        Err(error) if Self::is_owned_object_conflict(&error) => {
1461                            receivers.push(Err(error))
1462                        }
1463                        Err(error) => return Err(error),
1464                    }
1465                }
1466                future::join_all(receivers.into_iter().map(|rx| async move {
1467                    match rx?.await {
1468                        Ok(result) => result.map_err(SuiError::from),
1469                        Err(_) => Err(SuiError::from(
1470                            SuiErrorKind::TooManyTransactionsPendingConsensus,
1471                        )),
1472                    }
1473                }))
1474                .await
1475            }
1476            UserSubmissionMode::Pool => {
1477                let UserSubmissionPath::Pool(context) = &self.user_submission_path else {
1478                    debug_fatal!("pool mode requires a transaction pool");
1479                    return Err(SuiErrorKind::GenericAuthorityError {
1480                        error: "pool mode requires a transaction pool".to_string(),
1481                    }
1482                    .into());
1483                };
1484                {
1485                    let reconfiguration_lock = epoch_store.get_reconfig_state_read_lock_guard();
1486                    if !reconfiguration_lock.should_accept_user_certs() {
1487                        context
1488                            .adapter_metrics()
1489                            .num_rejected_cert_in_epoch_boundary
1490                            .inc();
1491                        return Err(SuiErrorKind::ValidatorHaltedAtEpochEnd.into());
1492                    }
1493                }
1494
1495                let mut receivers = Vec::with_capacity(tx_groups.len());
1496                for txns in tx_groups {
1497                    // Gas-price-based DoS accounting; pull-mode user transactions bypass
1498                    // the recording in ConsensusAdapter::submit_and_wait_inner, so record
1499                    // here instead.
1500                    epoch_store.record_submitted_user_transactions(&txns, submitter_client_addr);
1501                    let gas_price = Self::extract_gas_price(&txns);
1502                    let result = context
1503                        .try_insert(epoch_store.epoch(), gas_price, txns)
1504                        .await;
1505                    if let Ok((_, false)) = &result {
1506                        // Duplicate of an in-flight submission; flag the request as spam. The
1507                        // per-tx result is still Submitted, so this is tracked separately.
1508                        duplicate_at_admission = true;
1509                    }
1510                    receivers.push(result.map(|(receiver, _)| receiver));
1511                }
1512                let halted_rejections = context
1513                    .adapter_metrics()
1514                    .num_rejected_cert_in_epoch_boundary
1515                    .clone();
1516                future::join_all(receivers.into_iter().map(|receiver| {
1517                    let halted_rejections = halted_rejections.clone();
1518                    async move {
1519                        let result = match receiver {
1520                            Ok(receiver) => receiver.await.unwrap_or_else(|_| {
1521                                Err(SuiErrorKind::TooManyTransactionsPendingConsensus.into())
1522                            }),
1523                            Err(error) => Err(error),
1524                        };
1525                        if let Err(error) = &result
1526                            && matches!(error.as_inner(), SuiErrorKind::ValidatorHaltedAtEpochEnd)
1527                        {
1528                            halted_rejections.inc();
1529                        }
1530                        result
1531                    }
1532                }))
1533                .await
1534            }
1535        };
1536
1537        if is_ping_request {
1538            // For ping requests there is a single group returning the special consensus position.
1539            let consensus_positions = group_results
1540                .into_iter()
1541                .next()
1542                .expect("Ping request must have exactly one submission group")?;
1543            assert_eq!(consensus_positions.len(), 1);
1544            results.push(Some(SubmitTxResult::Submitted {
1545                consensus_position: consensus_positions[0],
1546            }));
1547        } else {
1548            // A group refused at the held quota or for an owned object conflict is not queued.
1549            // Its digests are released, so that a resubmission is handled afresh instead of
1550            // being suppressed as recently submitted. The release runs for every such group
1551            // before the loop below, because the loop returns at the first group that fails the
1552            // whole request.
1553            for (group_result, txns_meta) in group_results.iter().zip_debug_eq(&group_tx_meta) {
1554                if matches!(
1555                    group_result,
1556                    Err(err) if Self::is_hold_quota_full(err) || Self::is_owned_object_conflict(err)
1557                ) {
1558                    for (_, tx_digest) in txns_meta {
1559                        inflight_guard.release(*tx_digest);
1560                    }
1561                }
1562            }
1563            for (group_result, txns_meta) in group_results.into_iter().zip_debug_eq(group_tx_meta) {
1564                match group_result {
1565                    Ok(consensus_positions) => {
1566                        for ((idx, tx_digest), consensus_position) in
1567                            txns_meta.into_iter().zip_debug_eq(consensus_positions)
1568                        {
1569                            debug!(
1570                                ?tx_digest,
1571                                "handle_submit_transaction: submitted consensus transaction at {}",
1572                                consensus_position,
1573                            );
1574                            results[idx] = Some(SubmitTxResult::Submitted { consensus_position });
1575                        }
1576                    }
1577                    // Another queued transaction holds an owned gas object of this group. Report
1578                    // it per-tx, like a duplicate, so it counts toward the request's spam weight
1579                    // and does not fail the other groups. Its digests were released above.
1580                    Err(err) if Self::is_owned_object_conflict(&err) => {
1581                        for (idx, _) in txns_meta {
1582                            results[idx] = Some(SubmitTxResult::Rejected { error: err.clone() });
1583                        }
1584                    }
1585                    // The transaction(s) in this group are already being processed by consensus.
1586                    // Report per-tx as a retriable rejection rather than failing the whole request.
1587                    // Every other error, including the staggered-submission errors, fails the
1588                    // whole request. See `Self::without_undecodable_rejection`.
1589                    Err(err) => {
1590                        let SuiErrorKind::TransactionProcessing { status, .. } =
1591                            err.as_inner().clone()
1592                        else {
1593                            return Err(err);
1594                        };
1595                        // For TransactionProcessing error, ensure the per txn result has the correct digest.
1596                        for (idx, tx_digest) in txns_meta {
1597                            debug!(
1598                                ?tx_digest,
1599                                "handle_submit_transaction: transaction already processing: {err}"
1600                            );
1601                            // Same suppression the upfront `is_consensus_message_processed` check
1602                            // records, just detected during submission instead of before it. The
1603                            // two paths are mutually exclusive, so this does not double-count.
1604                            metrics
1605                                .submission_suppressed_already_processed
1606                                .with_label_values(&[req_type])
1607                                .inc();
1608                            results[idx] = Some(SubmitTxResult::Rejected {
1609                                error: SuiErrorKind::TransactionProcessing {
1610                                    digest: tx_digest,
1611                                    status: status.clone(),
1612                                }
1613                                .into(),
1614                            });
1615                        }
1616                    }
1617                }
1618            }
1619        }
1620
1621        let spam_weight = Self::request_spam_weight(
1622            &results,
1623            has_gasless,
1624            duplicate_at_admission,
1625            is_ping_request,
1626        );
1627        let response = Self::try_from_submit_tx_response(results)?;
1628        Ok((response, spam_weight))
1629    }
1630
1631    fn is_owned_object_conflict(error: &SuiError) -> bool {
1632        matches!(error.as_inner(), SuiErrorKind::OwnedObjectConflict { .. })
1633    }
1634
1635    fn is_hold_quota_full(error: &SuiError) -> bool {
1636        matches!(
1637            error.as_inner(),
1638            SuiErrorKind::StaggeredSubmissionHoldQuotaFull
1639        )
1640    }
1641
1642    /// Traffic-control spam weight for a whole submit request. The request is spam unless it is
1643    /// entirely accepted gas-chargable work.
1644    fn request_spam_weight(
1645        results: &[Option<SubmitTxResult>],
1646        has_gasless: bool,
1647        duplicate_at_admission: bool,
1648        is_ping: bool,
1649    ) -> Weight {
1650        if is_ping || has_gasless || duplicate_at_admission {
1651            return Weight::one();
1652        }
1653        for result in results {
1654            let Some(result) = result else {
1655                // `results` is expected to be fully populated (every entry `Some`) for the
1656                // request's transactions; a missing entry is a bug and is conservatively
1657                // treated as spam.
1658                debug_fatal!("transaction outcome unset when computing spam weight");
1659                return Weight::one();
1660            };
1661            if Self::submission_spam_weight(result) == Weight::one() {
1662                return Weight::one();
1663            }
1664        }
1665        Weight::zero()
1666    }
1667
1668    fn submission_spam_weight(result: &SubmitTxResult) -> Weight {
1669        match result {
1670            SubmitTxResult::Submitted { .. } => Weight::zero(),
1671            // Non-submitted results can't be charged.
1672            SubmitTxResult::Executed { .. } | SubmitTxResult::Rejected { .. } => Weight::one(),
1673        }
1674    }
1675
1676    fn try_from_submit_tx_response(
1677        results: Vec<Option<SubmitTxResult>>,
1678    ) -> Result<RawSubmitTxResponse, SuiError> {
1679        let mut raw_results = Vec::new();
1680        for (i, result) in results.into_iter().enumerate() {
1681            let result = result.ok_or_else(|| SuiErrorKind::GenericAuthorityError {
1682                error: format!("Missing transaction result at {}", i),
1683            })?;
1684            let result = Self::without_undecodable_rejection(result);
1685            let raw_result = result.try_into()?;
1686            raw_results.push(raw_result);
1687        }
1688        Ok(RawSubmitTxResponse {
1689            results: raw_results,
1690        })
1691    }
1692
1693    /// Replaces a per-transaction `StaggeredSubmissionHoldQuotaFull` rejection with the
1694    /// overload error that released clients decode. A released client decodes an unknown
1695    /// variant in a whole-request error as a retriable RPC error, but it fails to decode the
1696    /// whole response when the unknown variant is inside a per-transaction result, and it
1697    /// then treats the request as failed with a non-retriable error. The submit handler
1698    /// returns this error only for the whole request, so the replacement runs only after a
1699    /// bug.
1700    fn without_undecodable_rejection(result: SubmitTxResult) -> SubmitTxResult {
1701        match result {
1702            SubmitTxResult::Rejected { error } if Self::is_hold_quota_full(&error) => {
1703                debug_fatal!("StaggeredSubmissionHoldQuotaFull in a per-transaction rejection");
1704                SubmitTxResult::Rejected {
1705                    error: SuiErrorKind::ValidatorOverloadedRetryAfter {
1706                        retry_after_secs: 1,
1707                    }
1708                    .into(),
1709                }
1710            }
1711            result => result,
1712        }
1713    }
1714
1715    /// Extract the gas price from a batch of consensus transactions.
1716    /// Returns the minimum gas price in the batch, or 0 if no user transactions.
1717    fn extract_gas_price(transactions: &[ConsensusTransaction]) -> u64 {
1718        use sui_types::messages_consensus::ConsensusTransactionKind;
1719        transactions
1720            .iter()
1721            .filter_map(|tx| match &tx.kind {
1722                ConsensusTransactionKind::CertifiedTransaction(cert) => Some(cert.gas_price()),
1723                ConsensusTransactionKind::UserTransaction(t) => {
1724                    Some(t.data().transaction_data().gas_price())
1725                }
1726                ConsensusTransactionKind::UserTransactionV2(t) => {
1727                    Some(t.tx().data().transaction_data().gas_price())
1728                }
1729                _ => None,
1730            })
1731            .min()
1732            .unwrap_or(0)
1733    }
1734
1735    fn classify_submit_mode(&self, is_ping_request: bool) -> UserSubmissionMode {
1736        // Ping requests carry no transactions and must not wait behind queued
1737        // work; submit them directly to consensus.
1738        if is_ping_request {
1739            return UserSubmissionMode::Direct;
1740        }
1741
1742        match &self.user_submission_path {
1743            UserSubmissionPath::Direct => UserSubmissionMode::Direct,
1744            UserSubmissionPath::Pool(_) => UserSubmissionMode::Pool,
1745            UserSubmissionPath::AdmissionQueue(context) => {
1746                // If the queue actor is stuck, fall back to direct submission with the
1747                // pre-queue saturation reject until it resumes making progress.
1748                if context.load().failover_tripped() {
1749                    UserSubmissionMode::Direct
1750                } else {
1751                    UserSubmissionMode::Queue
1752                }
1753            }
1754        }
1755    }
1756
1757    async fn collect_effects_data(
1758        &self,
1759        effects: &TransactionEffects,
1760        include_events: bool,
1761        include_input_objects: bool,
1762        include_output_objects: bool,
1763    ) -> SuiResult<(Option<TransactionEvents>, Vec<Object>, Vec<Object>)> {
1764        let events = if include_events && effects.events_digest().is_some() {
1765            Some(
1766                self.state
1767                    .get_transaction_events(effects.transaction_digest())?,
1768            )
1769        } else {
1770            None
1771        };
1772
1773        let input_objects = if include_input_objects {
1774            self.state.get_transaction_input_objects(effects)?
1775        } else {
1776            vec![]
1777        };
1778
1779        let output_objects = if include_output_objects {
1780            self.state.get_transaction_output_objects(effects)?
1781        } else {
1782            vec![]
1783        };
1784
1785        Ok((events, input_objects, output_objects))
1786    }
1787}
1788
1789type WrappedServiceResponse<T> = Result<(tonic::Response<T>, Weight), tonic::Status>;
1790
1791/// RAII guard tracking the transaction digests a single submit request is actively handling, so
1792/// concurrent duplicates can be rejected. On drop, each acquired digest is removed from
1793/// the in-flight set and demoted into `recently_submitted` cache, so resubmissions
1794/// arriving shortly after the handler returns are still suppressed.
1795struct InflightTransactionsGuard {
1796    // Handle to inflight map and recently submitted cache.
1797    inflight: Arc<Mutex<HashSet<TransactionDigest>>>,
1798    recently_submitted: Cache<TransactionDigest, Instant>,
1799    window: Duration,
1800    metrics: Arc<ValidatorServiceMetrics>,
1801    /// Digests this request successfully acquired.
1802    acquired: HashSet<TransactionDigest>,
1803}
1804
1805enum AcquireOutcome {
1806    /// Transaction digest newly acquired by this request.
1807    Acquired,
1808    /// Transaction digest already acquired by this request before this internal retry attempt.
1809    AlreadyAcquiredByThisRequest,
1810    /// Transaction digest being handled by another concurrent request — reject this index.
1811    AlreadyAcquiredByAnotherRequest,
1812    /// Transaction digest recently processed — reject this index.
1813    RecentlyProcessed { since: Duration },
1814}
1815
1816impl InflightTransactionsGuard {
1817    fn new(service: &ValidatorService) -> Self {
1818        Self {
1819            inflight: service.inflight_transactions.clone(),
1820            recently_submitted: service.recently_submitted.clone(),
1821            window: service.recent_submission_window,
1822            metrics: service.metrics.clone(),
1823            acquired: HashSet::new(),
1824        }
1825    }
1826
1827    fn try_acquire(&mut self, digest: TransactionDigest) -> AcquireOutcome {
1828        // A retry of this own request re-acquires the same digests.
1829        if self.acquired.contains(&digest) {
1830            return AcquireOutcome::AlreadyAcquiredByThisRequest;
1831        }
1832
1833        // Suppress resubmissions of recently processed transactions.
1834        if let Some(outcome) = self.recently_processed_outcome(digest) {
1835            return outcome;
1836        }
1837
1838        // Atomic check-and-acquire against concurrent in-flight transactions.
1839        {
1840            let mut set = self.inflight.lock();
1841            // Only continue processing the transaction if it is not already inflight.
1842            if !set.insert(digest) {
1843                return AcquireOutcome::AlreadyAcquiredByAnotherRequest;
1844            }
1845            self.metrics.inflight_transactions.set(set.len() as i64);
1846        }
1847
1848        // Without this re-check, a duplicated transaction arriving between the first check and
1849        // the digest acquisition could slip through.
1850        if let Some(outcome) = self.recently_processed_outcome(digest) {
1851            let mut set = self.inflight.lock();
1852            set.remove(&digest);
1853            self.metrics.inflight_transactions.set(set.len() as i64);
1854            return outcome;
1855        }
1856
1857        self.acquired.insert(digest);
1858        AcquireOutcome::Acquired
1859    }
1860
1861    /// Releases a digest that was not queued, so a resubmission is handled afresh instead of
1862    /// being suppressed as recently submitted.
1863    fn release(&mut self, digest: TransactionDigest) {
1864        if !self.acquired.remove(&digest) {
1865            return;
1866        }
1867        let mut set = self.inflight.lock();
1868        set.remove(&digest);
1869        self.metrics.inflight_transactions.set(set.len() as i64);
1870    }
1871
1872    fn recently_processed_outcome(&self, digest: TransactionDigest) -> Option<AcquireOutcome> {
1873        let recorded_at = self.recently_submitted.get(&digest)?;
1874        let since = recorded_at.elapsed();
1875        (since < self.window).then_some(AcquireOutcome::RecentlyProcessed { since })
1876    }
1877}
1878
1879impl Drop for InflightTransactionsGuard {
1880    fn drop(&mut self) {
1881        if self.acquired.is_empty() {
1882            return;
1883        }
1884        // Demote inflight transactions to recently submitted cache before taking the in-flight lock,
1885        // to avoid cleaning up the cache with the lock.
1886        let now = Instant::now();
1887        for digest in &self.acquired {
1888            self.recently_submitted.insert(*digest, now);
1889        }
1890        {
1891            let mut set = self.inflight.lock();
1892            for digest in &self.acquired {
1893                set.remove(digest);
1894            }
1895            self.metrics.inflight_transactions.set(set.len() as i64);
1896        }
1897        self.metrics
1898            .recently_submitted_cache_size
1899            .set(self.recently_submitted.entry_count() as i64);
1900    }
1901}
1902
1903impl ValidatorService {
1904    async fn handle_submit_transaction_impl(
1905        &self,
1906        request: tonic::Request<RawSubmitTxRequest>,
1907    ) -> WrappedServiceResponse<RawSubmitTxResponse> {
1908        self.handle_submit_transaction(request).await
1909    }
1910
1911    async fn wait_for_effects_impl(
1912        &self,
1913        request: tonic::Request<RawWaitForEffectsRequest>,
1914    ) -> WrappedServiceResponse<RawWaitForEffectsResponse> {
1915        let request: WaitForEffectsRequest = request.into_inner().try_into()?;
1916        let epoch_store = self.state.load_epoch_store_one_call_per_task();
1917        let response = timeout(
1918            // TODO(fastpath): Tune this once we have a good estimate of the typical delay.
1919            Duration::from_secs(20),
1920            epoch_store
1921                .within_alive_epoch(self.wait_for_effects_response(request, &epoch_store))
1922                .map_err(|_| SuiErrorKind::EpochEnded(epoch_store.epoch())),
1923        )
1924        .await
1925        .map_err(|_| tonic::Status::internal("Timeout waiting for effects"))???
1926        .try_into()?;
1927        Ok((tonic::Response::new(response), Weight::zero()))
1928    }
1929
1930    #[instrument(name= "ValidatorService::wait_for_effects_response", level = "debug", skip_all, fields(consensus_position = ?request.consensus_position))]
1931    async fn wait_for_effects_response(
1932        &self,
1933        request: WaitForEffectsRequest,
1934        epoch_store: &Arc<AuthorityPerEpochStore>,
1935    ) -> SuiResult<WaitForEffectsResponse> {
1936        if request.ping_type.is_some() {
1937            return timeout(
1938                Duration::from_secs(10),
1939                self.ping_response(request, epoch_store),
1940            )
1941            .await
1942            .map_err(|_| SuiErrorKind::TimeoutError)?;
1943        }
1944
1945        let Some(tx_digest) = request.transaction_digest else {
1946            return Err(SuiErrorKind::InvalidRequest(
1947                "Transaction digest is required for wait for effects requests".to_string(),
1948            )
1949            .into());
1950        };
1951        let tx_digests = [tx_digest];
1952
1953        // When consensus_position is provided, also watch the consensus status cache
1954        // so rejected/dropped transactions get a timely response instead of waiting
1955        // forever for effects that will never be produced.
1956        let consensus_status_future = async {
1957            let consensus_position = match request.consensus_position {
1958                Some(pos) => pos,
1959                None => return futures::future::pending().await,
1960            };
1961            let consensus_tx_status_cache = &epoch_store.consensus_tx_status_cache;
1962            consensus_tx_status_cache.check_position_too_ahead(&consensus_position)?;
1963            match consensus_tx_status_cache
1964                .notify_read_transaction_status(consensus_position)
1965                .await
1966            {
1967                NotifyReadConsensusTxStatusResult::Status(
1968                    ConsensusTxStatus::Rejected | ConsensusTxStatus::Dropped,
1969                ) => Ok(WaitForEffectsResponse::Rejected {
1970                    error: epoch_store.get_rejection_vote_reason(consensus_position),
1971                }),
1972                NotifyReadConsensusTxStatusResult::Status(ConsensusTxStatus::Finalized) => {
1973                    // Effects will be produced — yield to let the effects future win.
1974                    futures::future::pending().await
1975                }
1976                NotifyReadConsensusTxStatusResult::Expired(round) => {
1977                    Ok(WaitForEffectsResponse::Expired {
1978                        epoch: epoch_store.epoch(),
1979                        round: Some(round),
1980                    })
1981                }
1982            }
1983        };
1984
1985        tokio::select! {
1986            effects_result = self.state
1987                .get_transaction_cache_reader()
1988                .notify_read_executed_effects_may_fail(
1989                    "AuthorityServer::wait_for_effects::notify_read_executed_effects_finalized",
1990                    &tx_digests,
1991                ) => {
1992                let effects = effects_result?.pop().unwrap();
1993                let effects_digest = effects.digest();
1994                self.state.check_effects_against_previously_signed(
1995                    epoch_store,
1996                    &tx_digest,
1997                    &effects_digest,
1998                    "wait_for_effects",
1999                )?;
2000                let details = if request.include_details {
2001                    Some(self.complete_executed_data(effects).await?)
2002                } else {
2003                    None
2004                };
2005                Ok(WaitForEffectsResponse::Executed {
2006                    effects_digest,
2007                    details,
2008                })
2009            }
2010            status_response = consensus_status_future => {
2011                status_response
2012            }
2013        }
2014    }
2015
2016    #[instrument(level = "error", skip_all, err(level = "debug"))]
2017    async fn ping_response(
2018        &self,
2019        request: WaitForEffectsRequest,
2020        epoch_store: &Arc<AuthorityPerEpochStore>,
2021    ) -> SuiResult<WaitForEffectsResponse> {
2022        let consensus_tx_status_cache = &epoch_store.consensus_tx_status_cache;
2023
2024        let Some(consensus_position) = request.consensus_position else {
2025            return Err(SuiErrorKind::InvalidRequest(
2026                "Consensus position is required for Ping requests".to_string(),
2027            )
2028            .into());
2029        };
2030
2031        // We assume that the caller has already checked for the existence of the `ping` field, but handling it gracefully here.
2032        let Some(ping) = request.ping_type else {
2033            return Err(SuiErrorKind::InvalidRequest(
2034                "Ping type is required for ping requests".to_string(),
2035            )
2036            .into());
2037        };
2038
2039        let _metrics_guard = self
2040            .metrics
2041            .handle_wait_for_effects_ping_latency
2042            .with_label_values(&[ping.as_str()])
2043            .start_timer();
2044
2045        consensus_tx_status_cache.check_position_too_ahead(&consensus_position)?;
2046
2047        let details = if request.include_details {
2048            Some(Box::new(ExecutedData::default()))
2049        } else {
2050            None
2051        };
2052
2053        let status = consensus_tx_status_cache
2054            .notify_read_transaction_status(consensus_position)
2055            .await;
2056        match status {
2057            NotifyReadConsensusTxStatusResult::Status(status) => match status {
2058                ConsensusTxStatus::Rejected | ConsensusTxStatus::Dropped => {
2059                    Ok(WaitForEffectsResponse::Rejected {
2060                        error: epoch_store.get_rejection_vote_reason(consensus_position),
2061                    })
2062                }
2063                ConsensusTxStatus::Finalized => Ok(WaitForEffectsResponse::Executed {
2064                    effects_digest: TransactionEffectsDigest::ZERO,
2065                    details,
2066                }),
2067            },
2068            NotifyReadConsensusTxStatusResult::Expired(round) => {
2069                Ok(WaitForEffectsResponse::Expired {
2070                    epoch: epoch_store.epoch(),
2071                    round: Some(round),
2072                })
2073            }
2074        }
2075    }
2076
2077    async fn complete_executed_data(
2078        &self,
2079        effects: TransactionEffects,
2080    ) -> SuiResult<Box<ExecutedData>> {
2081        let (events, input_objects, output_objects) = self
2082            .collect_effects_data(
2083                &effects, /* include_events */ true, /* include_input_objects */ true,
2084                /* include_output_objects */ true,
2085            )
2086            .await?;
2087        Ok(Box::new(ExecutedData {
2088            effects,
2089            events,
2090            input_objects,
2091            output_objects,
2092        }))
2093    }
2094
2095    async fn object_info_impl(
2096        &self,
2097        request: tonic::Request<ObjectInfoRequest>,
2098    ) -> WrappedServiceResponse<ObjectInfoResponse> {
2099        let request = request.into_inner();
2100        let response = self.state.handle_object_info_request(request).await?;
2101        Ok((tonic::Response::new(response), Weight::one()))
2102    }
2103
2104    async fn transaction_info_impl(
2105        &self,
2106        request: tonic::Request<TransactionInfoRequest>,
2107    ) -> WrappedServiceResponse<TransactionInfoResponse> {
2108        let request = request.into_inner();
2109        let response = self.state.handle_transaction_info_request(request).await?;
2110        Ok((tonic::Response::new(response), Weight::one()))
2111    }
2112
2113    async fn checkpoint_impl(
2114        &self,
2115        request: tonic::Request<CheckpointRequest>,
2116    ) -> WrappedServiceResponse<CheckpointResponse> {
2117        let request = request.into_inner();
2118        let response = self.state.handle_checkpoint_request(&request)?;
2119        Ok((tonic::Response::new(response), Weight::one()))
2120    }
2121
2122    async fn checkpoint_v2_impl(
2123        &self,
2124        request: tonic::Request<CheckpointRequestV2>,
2125    ) -> WrappedServiceResponse<CheckpointResponseV2> {
2126        let request = request.into_inner();
2127        let response = self.state.handle_checkpoint_request_v2(&request)?;
2128        Ok((tonic::Response::new(response), Weight::one()))
2129    }
2130
2131    async fn get_system_state_object_impl(
2132        &self,
2133        _request: tonic::Request<SystemStateRequest>,
2134    ) -> WrappedServiceResponse<SuiSystemState> {
2135        let response = self
2136            .state
2137            .get_object_cache_reader()
2138            .get_sui_system_state_object_unsafe()?;
2139        Ok((tonic::Response::new(response), Weight::one()))
2140    }
2141
2142    async fn validator_health_impl(
2143        &self,
2144        _request: tonic::Request<sui_types::messages_grpc::RawValidatorHealthRequest>,
2145    ) -> WrappedServiceResponse<sui_types::messages_grpc::RawValidatorHealthResponse> {
2146        let state = &self.state;
2147
2148        // Get epoch store once for both metrics
2149        let epoch_store = state.load_epoch_store_one_call_per_task();
2150
2151        // Get in-flight execution transactions from execution scheduler
2152        let num_inflight_execution_transactions =
2153            state.execution_scheduler().num_pending_certificates() as u64;
2154
2155        // Get in-flight consensus transactions from consensus adapter
2156        let num_inflight_consensus_transactions =
2157            self.consensus_adapter.num_inflight_transactions();
2158
2159        // Get last committed leader round from epoch store
2160        let last_committed_leader_round = epoch_store
2161            .consensus_tx_status_cache
2162            .get_last_committed_leader_round()
2163            .unwrap_or(0);
2164
2165        // Get last locally built checkpoint sequence
2166        let last_locally_built_checkpoint = epoch_store
2167            .last_built_checkpoint_summary()
2168            .ok()
2169            .flatten()
2170            .map(|(_, summary)| summary.sequence_number)
2171            .unwrap_or(0);
2172
2173        let typed_response = sui_types::messages_grpc::ValidatorHealthResponse {
2174            num_inflight_consensus_transactions,
2175            num_inflight_execution_transactions,
2176            last_locally_built_checkpoint,
2177            last_committed_leader_round,
2178            staggering_active: Some(epoch_store.staggered_submission().is_active()),
2179        };
2180
2181        let raw_response = typed_response
2182            .try_into()
2183            .map_err(|e: sui_types::error::SuiError| {
2184                tonic::Status::internal(format!("Failed to serialize health response: {}", e))
2185            })?;
2186
2187        Ok((tonic::Response::new(raw_response), Weight::one()))
2188    }
2189
2190    fn get_client_ip_addr<T>(
2191        &self,
2192        request: &tonic::Request<T>,
2193        source: &ClientIdSource,
2194    ) -> Option<IpAddr> {
2195        let forwarded_header = request.metadata().get_all("x-forwarded-for").iter().next();
2196
2197        if let Some(header) = forwarded_header {
2198            let num_hops = header
2199                .to_str()
2200                .map(|h| h.split(',').count().saturating_sub(1))
2201                .unwrap_or(0);
2202
2203            self.metrics.x_forwarded_for_num_hops.set(num_hops as f64);
2204        }
2205
2206        match source {
2207            ClientIdSource::SocketAddr => {
2208                let socket_addr: Option<SocketAddr> = request.remote_addr();
2209
2210                // We will hit this case if the IO type used does not
2211                // implement Connected or when using a unix domain socket.
2212                // TODO: once we have confirmed that no legitimate traffic
2213                // is hitting this case, we should reject such requests that
2214                // hit this case.
2215                if let Some(socket_addr) = socket_addr {
2216                    Some(socket_addr.ip())
2217                } else {
2218                    if cfg!(msim) {
2219                        // Ignore the error from simtests.
2220                    } else if cfg!(test) {
2221                        panic!("Failed to get remote address from request");
2222                    } else {
2223                        self.metrics.connection_ip_not_found.inc();
2224                        error!("Failed to get remote address from request");
2225                    }
2226                    None
2227                }
2228            }
2229            ClientIdSource::XForwardedFor(num_hops) => {
2230                let do_header_parse = |op: &MetadataValue<Ascii>| {
2231                    match op.to_str() {
2232                        Ok(header_val) => {
2233                            let header_contents =
2234                                header_val.split(',').map(str::trim).collect::<Vec<_>>();
2235                            if *num_hops == 0 {
2236                                error!(
2237                                    "x-forwarded-for: 0 specified. x-forwarded-for contents: {:?}. Please assign nonzero value for \
2238                                    number of hops here, or use `socket-addr` client-id-source type if requests are not being proxied \
2239                                    to this node. Skipping traffic controller request handling.",
2240                                    header_contents,
2241                                );
2242                                return None;
2243                            }
2244                            let contents_len = header_contents.len();
2245                            if contents_len < *num_hops {
2246                                error!(
2247                                    "x-forwarded-for header value of {:?} contains {} values, but {} hops were specified. \
2248                                    Expected at least {} values. Please correctly set the `x-forwarded-for` value under \
2249                                    `client-id-source` in the node config.",
2250                                    header_contents, contents_len, num_hops, contents_len,
2251                                );
2252                                self.metrics.client_id_source_config_mismatch.inc();
2253                                return None;
2254                            }
2255                            let Some(client_ip) = header_contents.get(contents_len - num_hops)
2256                            else {
2257                                error!(
2258                                    "x-forwarded-for header value of {:?} contains {} values, but {} hops were specified. \
2259                                    Expected at least {} values. Skipping traffic controller request handling.",
2260                                    header_contents, contents_len, num_hops, contents_len,
2261                                );
2262                                return None;
2263                            };
2264                            parse_ip(client_ip).or_else(|| {
2265                                self.metrics.forwarded_header_parse_error.inc();
2266                                None
2267                            })
2268                        }
2269                        Err(e) => {
2270                            // TODO: once we have confirmed that no legitimate traffic
2271                            // is hitting this case, we should reject such requests that
2272                            // hit this case.
2273                            self.metrics.forwarded_header_invalid.inc();
2274                            error!("Invalid UTF-8 in x-forwarded-for header: {:?}", e);
2275                            None
2276                        }
2277                    }
2278                };
2279                if let Some(op) = request.metadata().get("x-forwarded-for") {
2280                    do_header_parse(op)
2281                } else if let Some(op) = request.metadata().get("X-Forwarded-For") {
2282                    do_header_parse(op)
2283                } else {
2284                    self.metrics.forwarded_header_not_included.inc();
2285                    error!(
2286                        "x-forwarded-for header not present for request despite node configuring x-forwarded-for tracking type"
2287                    );
2288                    None
2289                }
2290            }
2291        }
2292    }
2293
2294    async fn handle_traffic_req(&self, client: Option<IpAddr>) -> Result<(), tonic::Status> {
2295        if let Some(traffic_controller) = &self.traffic_controller {
2296            if !traffic_controller.check(&client, &None).await {
2297                // Entity in blocklist
2298                Err(tonic::Status::from_error(
2299                    SuiErrorKind::TooManyRequests.into(),
2300                ))
2301            } else {
2302                Ok(())
2303            }
2304        } else {
2305            Ok(())
2306        }
2307    }
2308
2309    fn handle_traffic_resp<T>(
2310        &self,
2311        client: Option<IpAddr>,
2312        wrapped_response: WrappedServiceResponse<T>,
2313        method_name: &str,
2314    ) -> Result<tonic::Response<T>, tonic::Status> {
2315        let (error, spam_weight, unwrapped_response) = match wrapped_response {
2316            Ok((result, spam_weight)) => (None, spam_weight.clone(), Ok(result)),
2317            Err(status) => (
2318                Some(SuiError::from(status.clone())),
2319                Weight::zero(),
2320                Err(status.clone()),
2321            ),
2322        };
2323
2324        if let Some(traffic_controller) = self.traffic_controller.clone() {
2325            traffic_controller.tally(TrafficTally {
2326                direct: client,
2327                through_fullnode: None,
2328                error_info: error.map(|e| {
2329                    let error_type = String::from(e.clone().as_ref());
2330                    let error_weight = normalize(e);
2331                    (error_weight, error_type)
2332                }),
2333                spam_weight,
2334                timestamp: SystemTime::now(),
2335                method: Some(method_name.to_string()),
2336            })
2337        }
2338        unwrapped_response
2339    }
2340}
2341
2342// TODO: refine error matching here
2343fn normalize(err: SuiError) -> Weight {
2344    match err.as_inner() {
2345        SuiErrorKind::UserInputError {
2346            error: UserInputError::IncorrectUserSignature { .. },
2347        } => Weight::one(),
2348        SuiErrorKind::InvalidSignature { .. }
2349        | SuiErrorKind::SignerSignatureAbsent { .. }
2350        | SuiErrorKind::SignerSignatureNumberMismatch { .. }
2351        | SuiErrorKind::IncorrectSigner { .. }
2352        | SuiErrorKind::UnknownSigner { .. }
2353        | SuiErrorKind::WrongEpoch { .. } => Weight::one(),
2354        _ => Weight::zero(),
2355    }
2356}
2357
2358/// Implements generic pre- and post-processing. Since this is on the critical
2359/// path, any heavy lifting should be done in a separate non-blocking task
2360/// unless it is necessary to override the return value.
2361#[macro_export]
2362macro_rules! handle_with_decoration {
2363    ($self:ident, $func_name:ident, $request:ident, $method_name:expr) => {{
2364        if $self.client_id_source.is_none() {
2365            return $self.$func_name($request).await.map(|(result, _)| result);
2366        }
2367
2368        let client = $self.get_client_ip_addr(&$request, $self.client_id_source.as_ref().unwrap());
2369
2370        // check if either IP is blocked, in which case return early
2371        $self.handle_traffic_req(client.clone()).await?;
2372
2373        // handle traffic tallying
2374        let wrapped_response = $self.$func_name($request).await;
2375        $self.handle_traffic_resp(client, wrapped_response, $method_name)
2376    }};
2377}
2378
2379#[async_trait]
2380impl Validator for ValidatorService {
2381    async fn submit_transaction(
2382        &self,
2383        request: tonic::Request<RawSubmitTxRequest>,
2384    ) -> Result<tonic::Response<RawSubmitTxResponse>, tonic::Status> {
2385        let validator_service = self.clone();
2386
2387        // Spawns a task which handles the transaction. The task will unconditionally continue
2388        // processing in the event that the client connection is dropped.
2389        spawn_monitored_task!(async move {
2390            // NB: traffic tally wrapping handled within the task rather than on task exit
2391            // to prevent an attacker from subverting traffic control by severing the connection
2392            handle_with_decoration!(
2393                validator_service,
2394                handle_submit_transaction_impl,
2395                request,
2396                "submit_transaction"
2397            )
2398        })
2399        .await
2400        .unwrap()
2401    }
2402
2403    async fn wait_for_effects(
2404        &self,
2405        request: tonic::Request<RawWaitForEffectsRequest>,
2406    ) -> Result<tonic::Response<RawWaitForEffectsResponse>, tonic::Status> {
2407        handle_with_decoration!(self, wait_for_effects_impl, request, "wait_for_effects")
2408    }
2409
2410    async fn object_info(
2411        &self,
2412        request: tonic::Request<ObjectInfoRequest>,
2413    ) -> Result<tonic::Response<ObjectInfoResponse>, tonic::Status> {
2414        handle_with_decoration!(self, object_info_impl, request, "object_info")
2415    }
2416
2417    async fn transaction_info(
2418        &self,
2419        request: tonic::Request<TransactionInfoRequest>,
2420    ) -> Result<tonic::Response<TransactionInfoResponse>, tonic::Status> {
2421        handle_with_decoration!(self, transaction_info_impl, request, "transaction_info")
2422    }
2423
2424    async fn checkpoint(
2425        &self,
2426        request: tonic::Request<CheckpointRequest>,
2427    ) -> Result<tonic::Response<CheckpointResponse>, tonic::Status> {
2428        handle_with_decoration!(self, checkpoint_impl, request, "checkpoint")
2429    }
2430
2431    async fn checkpoint_v2(
2432        &self,
2433        request: tonic::Request<CheckpointRequestV2>,
2434    ) -> Result<tonic::Response<CheckpointResponseV2>, tonic::Status> {
2435        handle_with_decoration!(self, checkpoint_v2_impl, request, "checkpoint_v2")
2436    }
2437
2438    async fn get_system_state_object(
2439        &self,
2440        request: tonic::Request<SystemStateRequest>,
2441    ) -> Result<tonic::Response<SuiSystemState>, tonic::Status> {
2442        handle_with_decoration!(
2443            self,
2444            get_system_state_object_impl,
2445            request,
2446            "get_system_state_object"
2447        )
2448    }
2449
2450    async fn validator_health(
2451        &self,
2452        request: tonic::Request<sui_types::messages_grpc::RawValidatorHealthRequest>,
2453    ) -> Result<tonic::Response<sui_types::messages_grpc::RawValidatorHealthResponse>, tonic::Status>
2454    {
2455        handle_with_decoration!(self, validator_health_impl, request, "validator_health")
2456    }
2457}
2458
2459#[cfg(test)]
2460mod inflight_guard_tests {
2461    use super::*;
2462    use prometheus::Registry;
2463
2464    fn make_guard(
2465        inflight: Arc<Mutex<HashSet<TransactionDigest>>>,
2466        cache: Cache<TransactionDigest, Instant>,
2467        metrics: Arc<ValidatorServiceMetrics>,
2468    ) -> InflightTransactionsGuard {
2469        InflightTransactionsGuard {
2470            inflight,
2471            recently_submitted: cache,
2472            window: Duration::from_secs(10),
2473            metrics,
2474            acquired: HashSet::new(),
2475        }
2476    }
2477
2478    #[test]
2479    fn concurrent_acquire_rejects_other_request_and_is_idempotent_for_owner() {
2480        let inflight = Arc::new(Mutex::new(HashSet::new()));
2481        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2482        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2483        let digest = TransactionDigest::random();
2484
2485        let mut g1 = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2486        let mut g2 = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2487
2488        // First handler acquires it.
2489        assert!(matches!(g1.try_acquire(digest), AcquireOutcome::Acquired));
2490        assert_eq!(metrics.inflight_transactions.get(), 1);
2491
2492        // A concurrent handler sees another in-flight owner and is rejected.
2493        assert!(matches!(
2494            g2.try_acquire(digest),
2495            AcquireOutcome::AlreadyAcquiredByAnotherRequest
2496        ));
2497
2498        // The owning handler re-acquiring (epoch-end retry) is NOT a duplicate.
2499        assert!(matches!(
2500            g1.try_acquire(digest),
2501            AcquireOutcome::AlreadyAcquiredByThisRequest
2502        ));
2503    }
2504
2505    #[test]
2506    fn released_digest_is_not_recorded_as_recently_submitted() {
2507        let inflight = Arc::new(Mutex::new(HashSet::new()));
2508        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2509        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2510        let digest = TransactionDigest::random();
2511
2512        {
2513            let mut g = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2514            assert!(matches!(g.try_acquire(digest), AcquireOutcome::Acquired));
2515            g.release(digest);
2516            assert_eq!(inflight.lock().len(), 0);
2517            assert_eq!(metrics.inflight_transactions.get(), 0);
2518        }
2519        cache.run_pending_tasks();
2520
2521        let mut g_after = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2522        assert!(matches!(
2523            g_after.try_acquire(digest),
2524            AcquireOutcome::Acquired
2525        ));
2526    }
2527
2528    #[test]
2529    fn drop_demotes_into_recently_processed_outcome() {
2530        let inflight = Arc::new(Mutex::new(HashSet::new()));
2531        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2532        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2533        let digest = TransactionDigest::random();
2534
2535        {
2536            let mut g = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2537            assert!(matches!(g.try_acquire(digest), AcquireOutcome::Acquired));
2538            assert_eq!(inflight.lock().len(), 1);
2539        } // guard dropped here -> remove from set + demote to TTL cache
2540
2541        assert_eq!(
2542            inflight.lock().len(),
2543            0,
2544            "acquired digest must be removed from the in-flight set on drop"
2545        );
2546        assert_eq!(metrics.inflight_transactions.get(), 0);
2547
2548        // Make moka's read view deterministic before asserting the demoted entry.
2549        cache.run_pending_tasks();
2550
2551        // A fresh request now sees the digest as recently processed (tail window).
2552        let mut g_after = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2553        assert!(matches!(
2554            g_after.try_acquire(digest),
2555            AcquireOutcome::RecentlyProcessed { .. }
2556        ));
2557    }
2558}