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            for (group_result, txns_meta) in group_results.into_iter().zip_debug_eq(group_tx_meta) {
1549                match group_result {
1550                    Ok(consensus_positions) => {
1551                        for ((idx, tx_digest), consensus_position) in
1552                            txns_meta.into_iter().zip_debug_eq(consensus_positions)
1553                        {
1554                            debug!(
1555                                ?tx_digest,
1556                                "handle_submit_transaction: submitted consensus transaction at {}",
1557                                consensus_position,
1558                            );
1559                            results[idx] = Some(SubmitTxResult::Submitted { consensus_position });
1560                        }
1561                    }
1562                    // Another queued transaction holds an owned gas object of this group. Report
1563                    // it per-tx, like a duplicate, so it counts toward the request's spam weight
1564                    // and does not fail the other groups. The group is not queued, so its digests
1565                    // are released rather than recorded as recently submitted.
1566                    Err(err) if Self::is_owned_object_conflict(&err) => {
1567                        for (idx, tx_digest) in txns_meta {
1568                            inflight_guard.release(tx_digest);
1569                            results[idx] = Some(SubmitTxResult::Rejected { error: err.clone() });
1570                        }
1571                    }
1572                    // The transaction(s) in this group are already being processed by consensus.
1573                    // Report per-tx as a retriable rejection rather than failing the whole request.
1574                    Err(err) => {
1575                        let SuiErrorKind::TransactionProcessing { status, .. } =
1576                            err.as_inner().clone()
1577                        else {
1578                            return Err(err);
1579                        };
1580                        // For TransactionProcessing error, ensure the per txn result has the correct digest.
1581                        for (idx, tx_digest) in txns_meta {
1582                            debug!(
1583                                ?tx_digest,
1584                                "handle_submit_transaction: transaction already processing: {err}"
1585                            );
1586                            // Same suppression the upfront `is_consensus_message_processed` check
1587                            // records, just detected during submission instead of before it. The
1588                            // two paths are mutually exclusive, so this does not double-count.
1589                            metrics
1590                                .submission_suppressed_already_processed
1591                                .with_label_values(&[req_type])
1592                                .inc();
1593                            results[idx] = Some(SubmitTxResult::Rejected {
1594                                error: SuiErrorKind::TransactionProcessing {
1595                                    digest: tx_digest,
1596                                    status: status.clone(),
1597                                }
1598                                .into(),
1599                            });
1600                        }
1601                    }
1602                }
1603            }
1604        }
1605
1606        let spam_weight = Self::request_spam_weight(
1607            &results,
1608            has_gasless,
1609            duplicate_at_admission,
1610            is_ping_request,
1611        );
1612        let response = Self::try_from_submit_tx_response(results)?;
1613        Ok((response, spam_weight))
1614    }
1615
1616    fn is_owned_object_conflict(error: &SuiError) -> bool {
1617        matches!(error.as_inner(), SuiErrorKind::OwnedObjectConflict { .. })
1618    }
1619
1620    /// Traffic-control spam weight for a whole submit request. The request is spam unless it is
1621    /// entirely accepted gas-chargable work.
1622    fn request_spam_weight(
1623        results: &[Option<SubmitTxResult>],
1624        has_gasless: bool,
1625        duplicate_at_admission: bool,
1626        is_ping: bool,
1627    ) -> Weight {
1628        if is_ping || has_gasless || duplicate_at_admission {
1629            return Weight::one();
1630        }
1631        for result in results {
1632            let Some(result) = result else {
1633                // `results` is expected to be fully populated (every entry `Some`) for the
1634                // request's transactions; a missing entry is a bug and is conservatively
1635                // treated as spam.
1636                debug_fatal!("transaction outcome unset when computing spam weight");
1637                return Weight::one();
1638            };
1639            if Self::submission_spam_weight(result) == Weight::one() {
1640                return Weight::one();
1641            }
1642        }
1643        Weight::zero()
1644    }
1645
1646    fn submission_spam_weight(result: &SubmitTxResult) -> Weight {
1647        match result {
1648            SubmitTxResult::Submitted { .. } => Weight::zero(),
1649            // Non-submitted results can't be charged.
1650            SubmitTxResult::Executed { .. } | SubmitTxResult::Rejected { .. } => Weight::one(),
1651        }
1652    }
1653
1654    fn try_from_submit_tx_response(
1655        results: Vec<Option<SubmitTxResult>>,
1656    ) -> Result<RawSubmitTxResponse, SuiError> {
1657        let mut raw_results = Vec::new();
1658        for (i, result) in results.into_iter().enumerate() {
1659            let result = result.ok_or_else(|| SuiErrorKind::GenericAuthorityError {
1660                error: format!("Missing transaction result at {}", i),
1661            })?;
1662            let raw_result = result.try_into()?;
1663            raw_results.push(raw_result);
1664        }
1665        Ok(RawSubmitTxResponse {
1666            results: raw_results,
1667        })
1668    }
1669
1670    /// Extract the gas price from a batch of consensus transactions.
1671    /// Returns the minimum gas price in the batch, or 0 if no user transactions.
1672    fn extract_gas_price(transactions: &[ConsensusTransaction]) -> u64 {
1673        use sui_types::messages_consensus::ConsensusTransactionKind;
1674        transactions
1675            .iter()
1676            .filter_map(|tx| match &tx.kind {
1677                ConsensusTransactionKind::CertifiedTransaction(cert) => Some(cert.gas_price()),
1678                ConsensusTransactionKind::UserTransaction(t) => {
1679                    Some(t.data().transaction_data().gas_price())
1680                }
1681                ConsensusTransactionKind::UserTransactionV2(t) => {
1682                    Some(t.tx().data().transaction_data().gas_price())
1683                }
1684                _ => None,
1685            })
1686            .min()
1687            .unwrap_or(0)
1688    }
1689
1690    fn classify_submit_mode(&self, is_ping_request: bool) -> UserSubmissionMode {
1691        // Ping requests carry no transactions and must not wait behind queued
1692        // work; submit them directly to consensus.
1693        if is_ping_request {
1694            return UserSubmissionMode::Direct;
1695        }
1696
1697        match &self.user_submission_path {
1698            UserSubmissionPath::Direct => UserSubmissionMode::Direct,
1699            UserSubmissionPath::Pool(_) => UserSubmissionMode::Pool,
1700            UserSubmissionPath::AdmissionQueue(context) => {
1701                // If the queue actor is stuck, fall back to direct submission with the
1702                // pre-queue saturation reject until it resumes making progress.
1703                if context.load().failover_tripped() {
1704                    UserSubmissionMode::Direct
1705                } else {
1706                    UserSubmissionMode::Queue
1707                }
1708            }
1709        }
1710    }
1711
1712    async fn collect_effects_data(
1713        &self,
1714        effects: &TransactionEffects,
1715        include_events: bool,
1716        include_input_objects: bool,
1717        include_output_objects: bool,
1718    ) -> SuiResult<(Option<TransactionEvents>, Vec<Object>, Vec<Object>)> {
1719        let events = if include_events && effects.events_digest().is_some() {
1720            Some(
1721                self.state
1722                    .get_transaction_events(effects.transaction_digest())?,
1723            )
1724        } else {
1725            None
1726        };
1727
1728        let input_objects = if include_input_objects {
1729            self.state.get_transaction_input_objects(effects)?
1730        } else {
1731            vec![]
1732        };
1733
1734        let output_objects = if include_output_objects {
1735            self.state.get_transaction_output_objects(effects)?
1736        } else {
1737            vec![]
1738        };
1739
1740        Ok((events, input_objects, output_objects))
1741    }
1742}
1743
1744type WrappedServiceResponse<T> = Result<(tonic::Response<T>, Weight), tonic::Status>;
1745
1746/// RAII guard tracking the transaction digests a single submit request is actively handling, so
1747/// concurrent duplicates can be rejected. On drop, each acquired digest is removed from
1748/// the in-flight set and demoted into `recently_submitted` cache, so resubmissions
1749/// arriving shortly after the handler returns are still suppressed.
1750struct InflightTransactionsGuard {
1751    // Handle to inflight map and recently submitted cache.
1752    inflight: Arc<Mutex<HashSet<TransactionDigest>>>,
1753    recently_submitted: Cache<TransactionDigest, Instant>,
1754    window: Duration,
1755    metrics: Arc<ValidatorServiceMetrics>,
1756    /// Digests this request successfully acquired.
1757    acquired: HashSet<TransactionDigest>,
1758}
1759
1760enum AcquireOutcome {
1761    /// Transaction digest newly acquired by this request.
1762    Acquired,
1763    /// Transaction digest already acquired by this request before this internal retry attempt.
1764    AlreadyAcquiredByThisRequest,
1765    /// Transaction digest being handled by another concurrent request — reject this index.
1766    AlreadyAcquiredByAnotherRequest,
1767    /// Transaction digest recently processed — reject this index.
1768    RecentlyProcessed { since: Duration },
1769}
1770
1771impl InflightTransactionsGuard {
1772    fn new(service: &ValidatorService) -> Self {
1773        Self {
1774            inflight: service.inflight_transactions.clone(),
1775            recently_submitted: service.recently_submitted.clone(),
1776            window: service.recent_submission_window,
1777            metrics: service.metrics.clone(),
1778            acquired: HashSet::new(),
1779        }
1780    }
1781
1782    fn try_acquire(&mut self, digest: TransactionDigest) -> AcquireOutcome {
1783        // A retry of this own request re-acquires the same digests.
1784        if self.acquired.contains(&digest) {
1785            return AcquireOutcome::AlreadyAcquiredByThisRequest;
1786        }
1787
1788        // Suppress resubmissions of recently processed transactions.
1789        if let Some(outcome) = self.recently_processed_outcome(digest) {
1790            return outcome;
1791        }
1792
1793        // Atomic check-and-acquire against concurrent in-flight transactions.
1794        {
1795            let mut set = self.inflight.lock();
1796            // Only continue processing the transaction if it is not already inflight.
1797            if !set.insert(digest) {
1798                return AcquireOutcome::AlreadyAcquiredByAnotherRequest;
1799            }
1800            self.metrics.inflight_transactions.set(set.len() as i64);
1801        }
1802
1803        // Without this re-check, a duplicated transaction arriving between the first check and
1804        // the digest acquisition could slip through.
1805        if let Some(outcome) = self.recently_processed_outcome(digest) {
1806            let mut set = self.inflight.lock();
1807            set.remove(&digest);
1808            self.metrics.inflight_transactions.set(set.len() as i64);
1809            return outcome;
1810        }
1811
1812        self.acquired.insert(digest);
1813        AcquireOutcome::Acquired
1814    }
1815
1816    /// Releases a digest that was not queued, so a resubmission is handled afresh instead of
1817    /// being suppressed as recently submitted.
1818    fn release(&mut self, digest: TransactionDigest) {
1819        if !self.acquired.remove(&digest) {
1820            return;
1821        }
1822        let mut set = self.inflight.lock();
1823        set.remove(&digest);
1824        self.metrics.inflight_transactions.set(set.len() as i64);
1825    }
1826
1827    fn recently_processed_outcome(&self, digest: TransactionDigest) -> Option<AcquireOutcome> {
1828        let recorded_at = self.recently_submitted.get(&digest)?;
1829        let since = recorded_at.elapsed();
1830        (since < self.window).then_some(AcquireOutcome::RecentlyProcessed { since })
1831    }
1832}
1833
1834impl Drop for InflightTransactionsGuard {
1835    fn drop(&mut self) {
1836        if self.acquired.is_empty() {
1837            return;
1838        }
1839        // Demote inflight transactions to recently submitted cache before taking the in-flight lock,
1840        // to avoid cleaning up the cache with the lock.
1841        let now = Instant::now();
1842        for digest in &self.acquired {
1843            self.recently_submitted.insert(*digest, now);
1844        }
1845        {
1846            let mut set = self.inflight.lock();
1847            for digest in &self.acquired {
1848                set.remove(digest);
1849            }
1850            self.metrics.inflight_transactions.set(set.len() as i64);
1851        }
1852        self.metrics
1853            .recently_submitted_cache_size
1854            .set(self.recently_submitted.entry_count() as i64);
1855    }
1856}
1857
1858impl ValidatorService {
1859    async fn handle_submit_transaction_impl(
1860        &self,
1861        request: tonic::Request<RawSubmitTxRequest>,
1862    ) -> WrappedServiceResponse<RawSubmitTxResponse> {
1863        self.handle_submit_transaction(request).await
1864    }
1865
1866    async fn wait_for_effects_impl(
1867        &self,
1868        request: tonic::Request<RawWaitForEffectsRequest>,
1869    ) -> WrappedServiceResponse<RawWaitForEffectsResponse> {
1870        let request: WaitForEffectsRequest = request.into_inner().try_into()?;
1871        let epoch_store = self.state.load_epoch_store_one_call_per_task();
1872        let response = timeout(
1873            // TODO(fastpath): Tune this once we have a good estimate of the typical delay.
1874            Duration::from_secs(20),
1875            epoch_store
1876                .within_alive_epoch(self.wait_for_effects_response(request, &epoch_store))
1877                .map_err(|_| SuiErrorKind::EpochEnded(epoch_store.epoch())),
1878        )
1879        .await
1880        .map_err(|_| tonic::Status::internal("Timeout waiting for effects"))???
1881        .try_into()?;
1882        Ok((tonic::Response::new(response), Weight::zero()))
1883    }
1884
1885    #[instrument(name= "ValidatorService::wait_for_effects_response", level = "debug", skip_all, fields(consensus_position = ?request.consensus_position))]
1886    async fn wait_for_effects_response(
1887        &self,
1888        request: WaitForEffectsRequest,
1889        epoch_store: &Arc<AuthorityPerEpochStore>,
1890    ) -> SuiResult<WaitForEffectsResponse> {
1891        if request.ping_type.is_some() {
1892            return timeout(
1893                Duration::from_secs(10),
1894                self.ping_response(request, epoch_store),
1895            )
1896            .await
1897            .map_err(|_| SuiErrorKind::TimeoutError)?;
1898        }
1899
1900        let Some(tx_digest) = request.transaction_digest else {
1901            return Err(SuiErrorKind::InvalidRequest(
1902                "Transaction digest is required for wait for effects requests".to_string(),
1903            )
1904            .into());
1905        };
1906        let tx_digests = [tx_digest];
1907
1908        // When consensus_position is provided, also watch the consensus status cache
1909        // so rejected/dropped transactions get a timely response instead of waiting
1910        // forever for effects that will never be produced.
1911        let consensus_status_future = async {
1912            let consensus_position = match request.consensus_position {
1913                Some(pos) => pos,
1914                None => return futures::future::pending().await,
1915            };
1916            let consensus_tx_status_cache = &epoch_store.consensus_tx_status_cache;
1917            consensus_tx_status_cache.check_position_too_ahead(&consensus_position)?;
1918            match consensus_tx_status_cache
1919                .notify_read_transaction_status(consensus_position)
1920                .await
1921            {
1922                NotifyReadConsensusTxStatusResult::Status(
1923                    ConsensusTxStatus::Rejected | ConsensusTxStatus::Dropped,
1924                ) => Ok(WaitForEffectsResponse::Rejected {
1925                    error: epoch_store.get_rejection_vote_reason(consensus_position),
1926                }),
1927                NotifyReadConsensusTxStatusResult::Status(ConsensusTxStatus::Finalized) => {
1928                    // Effects will be produced — yield to let the effects future win.
1929                    futures::future::pending().await
1930                }
1931                NotifyReadConsensusTxStatusResult::Expired(round) => {
1932                    Ok(WaitForEffectsResponse::Expired {
1933                        epoch: epoch_store.epoch(),
1934                        round: Some(round),
1935                    })
1936                }
1937            }
1938        };
1939
1940        tokio::select! {
1941            effects_result = self.state
1942                .get_transaction_cache_reader()
1943                .notify_read_executed_effects_may_fail(
1944                    "AuthorityServer::wait_for_effects::notify_read_executed_effects_finalized",
1945                    &tx_digests,
1946                ) => {
1947                let effects = effects_result?.pop().unwrap();
1948                let effects_digest = effects.digest();
1949                self.state.check_effects_against_previously_signed(
1950                    epoch_store,
1951                    &tx_digest,
1952                    &effects_digest,
1953                    "wait_for_effects",
1954                )?;
1955                let details = if request.include_details {
1956                    Some(self.complete_executed_data(effects).await?)
1957                } else {
1958                    None
1959                };
1960                Ok(WaitForEffectsResponse::Executed {
1961                    effects_digest,
1962                    details,
1963                })
1964            }
1965            status_response = consensus_status_future => {
1966                status_response
1967            }
1968        }
1969    }
1970
1971    #[instrument(level = "error", skip_all, err(level = "debug"))]
1972    async fn ping_response(
1973        &self,
1974        request: WaitForEffectsRequest,
1975        epoch_store: &Arc<AuthorityPerEpochStore>,
1976    ) -> SuiResult<WaitForEffectsResponse> {
1977        let consensus_tx_status_cache = &epoch_store.consensus_tx_status_cache;
1978
1979        let Some(consensus_position) = request.consensus_position else {
1980            return Err(SuiErrorKind::InvalidRequest(
1981                "Consensus position is required for Ping requests".to_string(),
1982            )
1983            .into());
1984        };
1985
1986        // We assume that the caller has already checked for the existence of the `ping` field, but handling it gracefully here.
1987        let Some(ping) = request.ping_type else {
1988            return Err(SuiErrorKind::InvalidRequest(
1989                "Ping type is required for ping requests".to_string(),
1990            )
1991            .into());
1992        };
1993
1994        let _metrics_guard = self
1995            .metrics
1996            .handle_wait_for_effects_ping_latency
1997            .with_label_values(&[ping.as_str()])
1998            .start_timer();
1999
2000        consensus_tx_status_cache.check_position_too_ahead(&consensus_position)?;
2001
2002        let details = if request.include_details {
2003            Some(Box::new(ExecutedData::default()))
2004        } else {
2005            None
2006        };
2007
2008        let status = consensus_tx_status_cache
2009            .notify_read_transaction_status(consensus_position)
2010            .await;
2011        match status {
2012            NotifyReadConsensusTxStatusResult::Status(status) => match status {
2013                ConsensusTxStatus::Rejected | ConsensusTxStatus::Dropped => {
2014                    Ok(WaitForEffectsResponse::Rejected {
2015                        error: epoch_store.get_rejection_vote_reason(consensus_position),
2016                    })
2017                }
2018                ConsensusTxStatus::Finalized => Ok(WaitForEffectsResponse::Executed {
2019                    effects_digest: TransactionEffectsDigest::ZERO,
2020                    details,
2021                }),
2022            },
2023            NotifyReadConsensusTxStatusResult::Expired(round) => {
2024                Ok(WaitForEffectsResponse::Expired {
2025                    epoch: epoch_store.epoch(),
2026                    round: Some(round),
2027                })
2028            }
2029        }
2030    }
2031
2032    async fn complete_executed_data(
2033        &self,
2034        effects: TransactionEffects,
2035    ) -> SuiResult<Box<ExecutedData>> {
2036        let (events, input_objects, output_objects) = self
2037            .collect_effects_data(
2038                &effects, /* include_events */ true, /* include_input_objects */ true,
2039                /* include_output_objects */ true,
2040            )
2041            .await?;
2042        Ok(Box::new(ExecutedData {
2043            effects,
2044            events,
2045            input_objects,
2046            output_objects,
2047        }))
2048    }
2049
2050    async fn object_info_impl(
2051        &self,
2052        request: tonic::Request<ObjectInfoRequest>,
2053    ) -> WrappedServiceResponse<ObjectInfoResponse> {
2054        let request = request.into_inner();
2055        let response = self.state.handle_object_info_request(request).await?;
2056        Ok((tonic::Response::new(response), Weight::one()))
2057    }
2058
2059    async fn transaction_info_impl(
2060        &self,
2061        request: tonic::Request<TransactionInfoRequest>,
2062    ) -> WrappedServiceResponse<TransactionInfoResponse> {
2063        let request = request.into_inner();
2064        let response = self.state.handle_transaction_info_request(request).await?;
2065        Ok((tonic::Response::new(response), Weight::one()))
2066    }
2067
2068    async fn checkpoint_impl(
2069        &self,
2070        request: tonic::Request<CheckpointRequest>,
2071    ) -> WrappedServiceResponse<CheckpointResponse> {
2072        let request = request.into_inner();
2073        let response = self.state.handle_checkpoint_request(&request)?;
2074        Ok((tonic::Response::new(response), Weight::one()))
2075    }
2076
2077    async fn checkpoint_v2_impl(
2078        &self,
2079        request: tonic::Request<CheckpointRequestV2>,
2080    ) -> WrappedServiceResponse<CheckpointResponseV2> {
2081        let request = request.into_inner();
2082        let response = self.state.handle_checkpoint_request_v2(&request)?;
2083        Ok((tonic::Response::new(response), Weight::one()))
2084    }
2085
2086    async fn get_system_state_object_impl(
2087        &self,
2088        _request: tonic::Request<SystemStateRequest>,
2089    ) -> WrappedServiceResponse<SuiSystemState> {
2090        let response = self
2091            .state
2092            .get_object_cache_reader()
2093            .get_sui_system_state_object_unsafe()?;
2094        Ok((tonic::Response::new(response), Weight::one()))
2095    }
2096
2097    async fn validator_health_impl(
2098        &self,
2099        _request: tonic::Request<sui_types::messages_grpc::RawValidatorHealthRequest>,
2100    ) -> WrappedServiceResponse<sui_types::messages_grpc::RawValidatorHealthResponse> {
2101        let state = &self.state;
2102
2103        // Get epoch store once for both metrics
2104        let epoch_store = state.load_epoch_store_one_call_per_task();
2105
2106        // Get in-flight execution transactions from execution scheduler
2107        let num_inflight_execution_transactions =
2108            state.execution_scheduler().num_pending_certificates() as u64;
2109
2110        // Get in-flight consensus transactions from consensus adapter
2111        let num_inflight_consensus_transactions =
2112            self.consensus_adapter.num_inflight_transactions();
2113
2114        // Get last committed leader round from epoch store
2115        let last_committed_leader_round = epoch_store
2116            .consensus_tx_status_cache
2117            .get_last_committed_leader_round()
2118            .unwrap_or(0);
2119
2120        // Get last locally built checkpoint sequence
2121        let last_locally_built_checkpoint = epoch_store
2122            .last_built_checkpoint_summary()
2123            .ok()
2124            .flatten()
2125            .map(|(_, summary)| summary.sequence_number)
2126            .unwrap_or(0);
2127
2128        let typed_response = sui_types::messages_grpc::ValidatorHealthResponse {
2129            num_inflight_consensus_transactions,
2130            num_inflight_execution_transactions,
2131            last_locally_built_checkpoint,
2132            last_committed_leader_round,
2133            staggering_active: Some(epoch_store.staggered_submission().is_active()),
2134        };
2135
2136        let raw_response = typed_response
2137            .try_into()
2138            .map_err(|e: sui_types::error::SuiError| {
2139                tonic::Status::internal(format!("Failed to serialize health response: {}", e))
2140            })?;
2141
2142        Ok((tonic::Response::new(raw_response), Weight::one()))
2143    }
2144
2145    fn get_client_ip_addr<T>(
2146        &self,
2147        request: &tonic::Request<T>,
2148        source: &ClientIdSource,
2149    ) -> Option<IpAddr> {
2150        let forwarded_header = request.metadata().get_all("x-forwarded-for").iter().next();
2151
2152        if let Some(header) = forwarded_header {
2153            let num_hops = header
2154                .to_str()
2155                .map(|h| h.split(',').count().saturating_sub(1))
2156                .unwrap_or(0);
2157
2158            self.metrics.x_forwarded_for_num_hops.set(num_hops as f64);
2159        }
2160
2161        match source {
2162            ClientIdSource::SocketAddr => {
2163                let socket_addr: Option<SocketAddr> = request.remote_addr();
2164
2165                // We will hit this case if the IO type used does not
2166                // implement Connected or when using a unix domain socket.
2167                // TODO: once we have confirmed that no legitimate traffic
2168                // is hitting this case, we should reject such requests that
2169                // hit this case.
2170                if let Some(socket_addr) = socket_addr {
2171                    Some(socket_addr.ip())
2172                } else {
2173                    if cfg!(msim) {
2174                        // Ignore the error from simtests.
2175                    } else if cfg!(test) {
2176                        panic!("Failed to get remote address from request");
2177                    } else {
2178                        self.metrics.connection_ip_not_found.inc();
2179                        error!("Failed to get remote address from request");
2180                    }
2181                    None
2182                }
2183            }
2184            ClientIdSource::XForwardedFor(num_hops) => {
2185                let do_header_parse = |op: &MetadataValue<Ascii>| {
2186                    match op.to_str() {
2187                        Ok(header_val) => {
2188                            let header_contents =
2189                                header_val.split(',').map(str::trim).collect::<Vec<_>>();
2190                            if *num_hops == 0 {
2191                                error!(
2192                                    "x-forwarded-for: 0 specified. x-forwarded-for contents: {:?}. Please assign nonzero value for \
2193                                    number of hops here, or use `socket-addr` client-id-source type if requests are not being proxied \
2194                                    to this node. Skipping traffic controller request handling.",
2195                                    header_contents,
2196                                );
2197                                return None;
2198                            }
2199                            let contents_len = header_contents.len();
2200                            if contents_len < *num_hops {
2201                                error!(
2202                                    "x-forwarded-for header value of {:?} contains {} values, but {} hops were specified. \
2203                                    Expected at least {} values. Please correctly set the `x-forwarded-for` value under \
2204                                    `client-id-source` in the node config.",
2205                                    header_contents, contents_len, num_hops, contents_len,
2206                                );
2207                                self.metrics.client_id_source_config_mismatch.inc();
2208                                return None;
2209                            }
2210                            let Some(client_ip) = header_contents.get(contents_len - num_hops)
2211                            else {
2212                                error!(
2213                                    "x-forwarded-for header value of {:?} contains {} values, but {} hops were specified. \
2214                                    Expected at least {} values. Skipping traffic controller request handling.",
2215                                    header_contents, contents_len, num_hops, contents_len,
2216                                );
2217                                return None;
2218                            };
2219                            parse_ip(client_ip).or_else(|| {
2220                                self.metrics.forwarded_header_parse_error.inc();
2221                                None
2222                            })
2223                        }
2224                        Err(e) => {
2225                            // TODO: once we have confirmed that no legitimate traffic
2226                            // is hitting this case, we should reject such requests that
2227                            // hit this case.
2228                            self.metrics.forwarded_header_invalid.inc();
2229                            error!("Invalid UTF-8 in x-forwarded-for header: {:?}", e);
2230                            None
2231                        }
2232                    }
2233                };
2234                if let Some(op) = request.metadata().get("x-forwarded-for") {
2235                    do_header_parse(op)
2236                } else if let Some(op) = request.metadata().get("X-Forwarded-For") {
2237                    do_header_parse(op)
2238                } else {
2239                    self.metrics.forwarded_header_not_included.inc();
2240                    error!(
2241                        "x-forwarded-for header not present for request despite node configuring x-forwarded-for tracking type"
2242                    );
2243                    None
2244                }
2245            }
2246        }
2247    }
2248
2249    async fn handle_traffic_req(&self, client: Option<IpAddr>) -> Result<(), tonic::Status> {
2250        if let Some(traffic_controller) = &self.traffic_controller {
2251            if !traffic_controller.check(&client, &None).await {
2252                // Entity in blocklist
2253                Err(tonic::Status::from_error(
2254                    SuiErrorKind::TooManyRequests.into(),
2255                ))
2256            } else {
2257                Ok(())
2258            }
2259        } else {
2260            Ok(())
2261        }
2262    }
2263
2264    fn handle_traffic_resp<T>(
2265        &self,
2266        client: Option<IpAddr>,
2267        wrapped_response: WrappedServiceResponse<T>,
2268        method_name: &str,
2269    ) -> Result<tonic::Response<T>, tonic::Status> {
2270        let (error, spam_weight, unwrapped_response) = match wrapped_response {
2271            Ok((result, spam_weight)) => (None, spam_weight.clone(), Ok(result)),
2272            Err(status) => (
2273                Some(SuiError::from(status.clone())),
2274                Weight::zero(),
2275                Err(status.clone()),
2276            ),
2277        };
2278
2279        if let Some(traffic_controller) = self.traffic_controller.clone() {
2280            traffic_controller.tally(TrafficTally {
2281                direct: client,
2282                through_fullnode: None,
2283                error_info: error.map(|e| {
2284                    let error_type = String::from(e.clone().as_ref());
2285                    let error_weight = normalize(e);
2286                    (error_weight, error_type)
2287                }),
2288                spam_weight,
2289                timestamp: SystemTime::now(),
2290                method: Some(method_name.to_string()),
2291            })
2292        }
2293        unwrapped_response
2294    }
2295}
2296
2297// TODO: refine error matching here
2298fn normalize(err: SuiError) -> Weight {
2299    match err.as_inner() {
2300        SuiErrorKind::UserInputError {
2301            error: UserInputError::IncorrectUserSignature { .. },
2302        } => Weight::one(),
2303        SuiErrorKind::InvalidSignature { .. }
2304        | SuiErrorKind::SignerSignatureAbsent { .. }
2305        | SuiErrorKind::SignerSignatureNumberMismatch { .. }
2306        | SuiErrorKind::IncorrectSigner { .. }
2307        | SuiErrorKind::UnknownSigner { .. }
2308        | SuiErrorKind::WrongEpoch { .. } => Weight::one(),
2309        _ => Weight::zero(),
2310    }
2311}
2312
2313/// Implements generic pre- and post-processing. Since this is on the critical
2314/// path, any heavy lifting should be done in a separate non-blocking task
2315/// unless it is necessary to override the return value.
2316#[macro_export]
2317macro_rules! handle_with_decoration {
2318    ($self:ident, $func_name:ident, $request:ident, $method_name:expr) => {{
2319        if $self.client_id_source.is_none() {
2320            return $self.$func_name($request).await.map(|(result, _)| result);
2321        }
2322
2323        let client = $self.get_client_ip_addr(&$request, $self.client_id_source.as_ref().unwrap());
2324
2325        // check if either IP is blocked, in which case return early
2326        $self.handle_traffic_req(client.clone()).await?;
2327
2328        // handle traffic tallying
2329        let wrapped_response = $self.$func_name($request).await;
2330        $self.handle_traffic_resp(client, wrapped_response, $method_name)
2331    }};
2332}
2333
2334#[async_trait]
2335impl Validator for ValidatorService {
2336    async fn submit_transaction(
2337        &self,
2338        request: tonic::Request<RawSubmitTxRequest>,
2339    ) -> Result<tonic::Response<RawSubmitTxResponse>, tonic::Status> {
2340        let validator_service = self.clone();
2341
2342        // Spawns a task which handles the transaction. The task will unconditionally continue
2343        // processing in the event that the client connection is dropped.
2344        spawn_monitored_task!(async move {
2345            // NB: traffic tally wrapping handled within the task rather than on task exit
2346            // to prevent an attacker from subverting traffic control by severing the connection
2347            handle_with_decoration!(
2348                validator_service,
2349                handle_submit_transaction_impl,
2350                request,
2351                "submit_transaction"
2352            )
2353        })
2354        .await
2355        .unwrap()
2356    }
2357
2358    async fn wait_for_effects(
2359        &self,
2360        request: tonic::Request<RawWaitForEffectsRequest>,
2361    ) -> Result<tonic::Response<RawWaitForEffectsResponse>, tonic::Status> {
2362        handle_with_decoration!(self, wait_for_effects_impl, request, "wait_for_effects")
2363    }
2364
2365    async fn object_info(
2366        &self,
2367        request: tonic::Request<ObjectInfoRequest>,
2368    ) -> Result<tonic::Response<ObjectInfoResponse>, tonic::Status> {
2369        handle_with_decoration!(self, object_info_impl, request, "object_info")
2370    }
2371
2372    async fn transaction_info(
2373        &self,
2374        request: tonic::Request<TransactionInfoRequest>,
2375    ) -> Result<tonic::Response<TransactionInfoResponse>, tonic::Status> {
2376        handle_with_decoration!(self, transaction_info_impl, request, "transaction_info")
2377    }
2378
2379    async fn checkpoint(
2380        &self,
2381        request: tonic::Request<CheckpointRequest>,
2382    ) -> Result<tonic::Response<CheckpointResponse>, tonic::Status> {
2383        handle_with_decoration!(self, checkpoint_impl, request, "checkpoint")
2384    }
2385
2386    async fn checkpoint_v2(
2387        &self,
2388        request: tonic::Request<CheckpointRequestV2>,
2389    ) -> Result<tonic::Response<CheckpointResponseV2>, tonic::Status> {
2390        handle_with_decoration!(self, checkpoint_v2_impl, request, "checkpoint_v2")
2391    }
2392
2393    async fn get_system_state_object(
2394        &self,
2395        request: tonic::Request<SystemStateRequest>,
2396    ) -> Result<tonic::Response<SuiSystemState>, tonic::Status> {
2397        handle_with_decoration!(
2398            self,
2399            get_system_state_object_impl,
2400            request,
2401            "get_system_state_object"
2402        )
2403    }
2404
2405    async fn validator_health(
2406        &self,
2407        request: tonic::Request<sui_types::messages_grpc::RawValidatorHealthRequest>,
2408    ) -> Result<tonic::Response<sui_types::messages_grpc::RawValidatorHealthResponse>, tonic::Status>
2409    {
2410        handle_with_decoration!(self, validator_health_impl, request, "validator_health")
2411    }
2412}
2413
2414#[cfg(test)]
2415mod inflight_guard_tests {
2416    use super::*;
2417    use prometheus::Registry;
2418
2419    fn make_guard(
2420        inflight: Arc<Mutex<HashSet<TransactionDigest>>>,
2421        cache: Cache<TransactionDigest, Instant>,
2422        metrics: Arc<ValidatorServiceMetrics>,
2423    ) -> InflightTransactionsGuard {
2424        InflightTransactionsGuard {
2425            inflight,
2426            recently_submitted: cache,
2427            window: Duration::from_secs(10),
2428            metrics,
2429            acquired: HashSet::new(),
2430        }
2431    }
2432
2433    #[test]
2434    fn concurrent_acquire_rejects_other_request_and_is_idempotent_for_owner() {
2435        let inflight = Arc::new(Mutex::new(HashSet::new()));
2436        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2437        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2438        let digest = TransactionDigest::random();
2439
2440        let mut g1 = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2441        let mut g2 = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2442
2443        // First handler acquires it.
2444        assert!(matches!(g1.try_acquire(digest), AcquireOutcome::Acquired));
2445        assert_eq!(metrics.inflight_transactions.get(), 1);
2446
2447        // A concurrent handler sees another in-flight owner and is rejected.
2448        assert!(matches!(
2449            g2.try_acquire(digest),
2450            AcquireOutcome::AlreadyAcquiredByAnotherRequest
2451        ));
2452
2453        // The owning handler re-acquiring (epoch-end retry) is NOT a duplicate.
2454        assert!(matches!(
2455            g1.try_acquire(digest),
2456            AcquireOutcome::AlreadyAcquiredByThisRequest
2457        ));
2458    }
2459
2460    #[test]
2461    fn released_digest_is_not_recorded_as_recently_submitted() {
2462        let inflight = Arc::new(Mutex::new(HashSet::new()));
2463        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2464        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2465        let digest = TransactionDigest::random();
2466
2467        {
2468            let mut g = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2469            assert!(matches!(g.try_acquire(digest), AcquireOutcome::Acquired));
2470            g.release(digest);
2471            assert_eq!(inflight.lock().len(), 0);
2472            assert_eq!(metrics.inflight_transactions.get(), 0);
2473        }
2474        cache.run_pending_tasks();
2475
2476        let mut g_after = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2477        assert!(matches!(
2478            g_after.try_acquire(digest),
2479            AcquireOutcome::Acquired
2480        ));
2481    }
2482
2483    #[test]
2484    fn drop_demotes_into_recently_processed_outcome() {
2485        let inflight = Arc::new(Mutex::new(HashSet::new()));
2486        let cache = ValidatorService::new_recently_submitted_cache(Duration::from_secs(10));
2487        let metrics = Arc::new(ValidatorServiceMetrics::new(&Registry::new()));
2488        let digest = TransactionDigest::random();
2489
2490        {
2491            let mut g = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2492            assert!(matches!(g.try_acquire(digest), AcquireOutcome::Acquired));
2493            assert_eq!(inflight.lock().len(), 1);
2494        } // guard dropped here -> remove from set + demote to TTL cache
2495
2496        assert_eq!(
2497            inflight.lock().len(),
2498            0,
2499            "acquired digest must be removed from the in-flight set on drop"
2500        );
2501        assert_eq!(metrics.inflight_transactions.get(), 0);
2502
2503        // Make moka's read view deterministic before asserting the demoted entry.
2504        cache.run_pending_tasks();
2505
2506        // A fresh request now sees the digest as recently processed (tail window).
2507        let mut g_after = make_guard(inflight.clone(), cache.clone(), metrics.clone());
2508        assert!(matches!(
2509            g_after.try_acquire(digest),
2510            AcquireOutcome::RecentlyProcessed { .. }
2511        ));
2512    }
2513}