Skip to main content

iota_core/
authority.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// Modifications Copyright (c) 2024 IOTA Stiftung
4// SPDX-License-Identifier: Apache-2.0
5
6use std::{
7    cell::Cell,
8    collections::{BTreeMap, HashMap, HashSet},
9    fs::File,
10    io::Write,
11    path::{Path, PathBuf},
12    pin::Pin,
13    sync::{
14        Arc, Weak,
15        atomic::{AtomicBool, Ordering},
16    },
17    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
18    vec,
19};
20
21use arc_swap::{ArcSwap, Guard};
22use async_trait::async_trait;
23use authority_per_epoch_store::TxLockGuard;
24pub use authority_store::{AuthorityStore, ResolverWrapper, UpdateType};
25use fastcrypto::{
26    encoding::{Base58, Encoding},
27    hash::MultisetHash,
28};
29use iota_common::{debug_fatal, fatal};
30use iota_config::{
31    NodeConfig,
32    genesis::Genesis,
33    node::{AuthorityOverloadConfig, ExpensiveSafetyCheckConfig, StateDebugDumpConfig},
34};
35use iota_framework::{BuiltInFramework, SystemPackage as FrameworkSystemPackage};
36use iota_json_rpc_types::{EventFilter, IotaEvent, IotaObjectDataFilter, TransactionFilter};
37use iota_macros::{fail_point, fail_point_async, fail_point_if};
38use iota_metrics::{
39    TX_TYPE_SHARED_OBJ_TX, TX_TYPE_SINGLE_WRITER_TX, monitored_scope, spawn_monitored_task,
40};
41use iota_sdk_types::{
42    Address, CheckpointCommitment, CheckpointContents, CheckpointContentsDigest, CheckpointDigest,
43    CheckpointSummary, Digest, EndOfEpochTransactionKind, Event, ExecutionStatus, GasCostSummary,
44    InputSharedObject, MoveAuthenticator, ObjectDigest, ObjectId, ObjectReference, RandomnessRound,
45    SenderSignedTransaction, StructTag, SystemPackage, Transaction, TransactionDigest,
46    TransactionEffects, TransactionEffectsDigest, TransactionEvents, TransactionKind, TypeTag,
47    Version,
48    crypto::{Intent, IntentScope},
49};
50use iota_storage::{
51    key_value_store::{
52        KVStoreTransactionData, TransactionKeyValueStore, TransactionKeyValueStoreTrait,
53    },
54    key_value_store_metrics::KeyValueStoreMetrics,
55};
56use iota_traffic_controller::{TrafficController, metrics::TrafficControllerMetrics};
57use iota_transaction_checks::VerifierLimitsSource;
58#[cfg(msim)]
59use iota_types::committee::CommitteeTrait;
60use iota_types::{
61    account_abstraction::authenticator_function::{
62        AuthenticatorFunctionRef, AuthenticatorFunctionRefForExecution,
63        MoveAuthenticatorForExecution, MoveAuthenticatorsForExecution,
64        authenticator_function_ref_v1_from_dynamic_field_object,
65        derive_authenticator_function_ref_v1_dynamic_field_id, extract_auth_fun_refs,
66    },
67    attestation::{AttestationRecord, AttestationVerdict},
68    auth_context::AuthContextData,
69    base_types::{AuthorityName, ConciseableName, VersionNumber},
70    committee::{Committee, EpochId, ProtocolVersion},
71    crypto::{AggregateAuthorityPublicKey, AuthoritySignInfo, AuthoritySignature, Signer},
72    deny_list_v1::{check_coin_deny_list_v1, coin_types_for_denylist_check},
73    deny_rule_governance::DenyRuleConfig,
74    digests::ChainIdentifier,
75    dynamic_field::{self, DynamicFieldInfo},
76    effects::{
77        SignedTransactionEffects, TransactionEffectsAPI, TransactionEffectsExt,
78        VerifiedSignedTransactionEffects,
79    },
80    error::{ExecutionError, ExecutionErrorKind, IotaError, IotaResult, UserInputError},
81    event::{EventID, SystemEpochInfoEvent},
82    executable_transaction::VerifiedExecutableTransaction,
83    execution_config_utils::to_binary_config,
84    fp_ensure,
85    gas::IotaGasStatus,
86    gas_coin::mock_simulation_gas_coin,
87    inner_temporary_store::InnerTemporaryStore,
88    iota_system_state::{
89        IotaSystemState, IotaSystemStateTrait,
90        epoch_start_iota_system_state::EpochStartSystemStateTrait, get_iota_system_state,
91    },
92    layout_resolver::into_struct_layout,
93    message_envelope::Message,
94    messages_checkpoint::{
95        CertifiedCheckpointSummary, CheckpointContentsExt, CheckpointRequest, CheckpointResponse,
96        CheckpointSequenceNumber, CheckpointSummaryResponse, CheckpointTimestamp,
97        ECMHLiveObjectSetDigest, VerifiedCheckpoint,
98    },
99    messages_consensus::AuthorityCapabilitiesV1,
100    messages_grpc::{
101        HandleTransactionResponse, LayoutGenerationOption, ObjectInfoRequest,
102        ObjectInfoRequestKind, ObjectInfoResponse, TransactionInfoRequest, TransactionInfoResponse,
103        TransactionStatus,
104    },
105    metrics::{BytecodeVerifierMetrics, LimitsMetrics},
106    move_authenticator::MoveAuthenticatorExt,
107    object::{Object, ObjectRead, ObjectSet, PastObjectRead},
108    storage::{
109        BackingPackageStore, BackingStore, InputKey, ObjectKey, ObjectOrTombstone, ObjectStore,
110        OwnedObjectCursor, TrackingBackingStore,
111    },
112    supported_protocol_versions::{
113        ProtocolConfig, SupportedProtocolVersions, SupportedProtocolVersionsWithHashes,
114    },
115    traffic_control::{PolicyConfig, TrafficControlReconfigParams},
116    transaction::*,
117    transaction_executor::{InputCheckRules, SimulateTransactionResult, VmChecks},
118};
119use itertools::Itertools;
120use move_binary_format::{CompiledModule, binary_config::BinaryConfig};
121use move_core_types::{
122    account_address::AccountAddress, annotated_value::MoveStructLayout, language_storage::ModuleId,
123};
124use parking_lot::Mutex;
125use prometheus_filtered::{
126    Histogram, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, MetricLevel,
127    Registry, register_histogram_vec_with_registry, register_histogram_with_registry,
128    register_int_counter_vec_with_registry, register_int_counter_with_registry,
129    register_int_gauge_vec_with_registry, register_int_gauge_with_registry,
130};
131use serde::{Deserialize, Serialize, de::DeserializeOwned};
132use tap::TapFallible;
133use tokio::{
134    sync::{Notify, RwLock, mpsc, mpsc::unbounded_channel, oneshot, watch},
135    task::JoinHandle,
136};
137use tracing::{debug, error, info, instrument, trace, warn};
138use typed_store::TypedStoreError;
139
140use self::{attestation::attestation_record, authority_store::ExecutionLockWriteGuard};
141#[cfg(msim)]
142pub use crate::checkpoints::checkpoint_executor::utils::{
143    CheckpointTimeoutConfig, init_checkpoint_timeout_config,
144};
145use crate::{
146    authority::{
147        attestation_semaphore::{AttestationPermit, AttestationSemaphore},
148        authority_per_epoch_store::{AuthorityPerEpochStore, TxGuard},
149        authority_per_epoch_store_pruner::AuthorityPerEpochStorePruner,
150        authority_store::{ExecutionLockReadGuard, ObjectLockStatus},
151        epoch_start_configuration::{EpochStartConfigTrait, EpochStartConfiguration},
152        historic_ledger::HistoricLedger,
153        historic_objects::HistoricObjects,
154        object_retention::{ObjectRetention, ObjectRetentionGuard},
155        shared_object_version_manager::{AssignedVersions, Schedulable},
156    },
157    authority_client::NetworkAuthorityClient,
158    checkpoints::{CheckpointBuilderError, CheckpointBuilderResult, CheckpointStore},
159    congestion_tracker::CongestionTracker,
160    consensus_adapter::{ConsensusAdapter, InflightSlot},
161    epoch::committee_store::CommitteeStore,
162    epoch_buckets::PrunedBuckets,
163    epoch_end_db_snapshot::{EpochEndDbSnapshotHandle, HandOver},
164    execution_cache::{
165        CheckpointCache, ExecutionCacheCommit, ExecutionCacheReconfigAPI,
166        ExecutionCacheTraitPointers, ExecutionCacheWrite, ObjectCacheRead, StateSyncAPI,
167        TransactionCacheRead,
168    },
169    execution_driver::execution_process,
170    execution_scheduler::{
171        ExecutionSchedulerAPI, ExecutionSchedulerWrapper, transaction_dependency_input_key,
172        transaction_manager::AttestationSource,
173    },
174    global_state_hasher::{GlobalStateHashStore, GlobalStateHasher},
175    metrics::{LatencyObserver, RateTracker},
176    module_cache_metrics::ResolverMetrics,
177    overload_monitor::{
178        AuthorityOverloadInfo, compute_graduated_load_shedding_percentage,
179        overload_monitor_accept_tx,
180    },
181    post_consensus_input_reader::{
182        AccountVerdict, InputResolution, ReceivingObjectsResolution, ValidationAtCommit,
183        ValidationInputsAtCommit,
184        reader::{CommitIndexedReader, DenyListAtCommit},
185    },
186    rpc_indexes::{
187        RpcIndexesStore,
188        jsonrpc_api::{
189            CachingLayoutResolver, CoinInfo, OwnedObjectsPage, try_create_dynamic_field_info,
190        },
191        schema::{IndexGroup, OwnerTypeFilter},
192    },
193    stake_aggregator::StakeAggregator,
194    transaction_input_loader::TransactionInputLoader,
195    transaction_outputs::TransactionOutputs,
196    validator_tx_finalizer::ValidatorTxFinalizer,
197};
198
199#[cfg(test)]
200#[path = "unit_tests/authority_tests.rs"]
201pub mod authority_tests;
202
203#[cfg(test)]
204#[path = "unit_tests/transaction_tests.rs"]
205pub mod transaction_tests;
206
207#[cfg(test)]
208#[path = "unit_tests/batch_transaction_tests.rs"]
209mod batch_transaction_tests;
210
211#[cfg(test)]
212#[path = "unit_tests/move_integration_tests.rs"]
213pub mod move_integration_tests;
214
215#[cfg(test)]
216#[path = "unit_tests/gas_tests.rs"]
217mod gas_tests;
218
219#[cfg(test)]
220#[path = "unit_tests/batch_verification_tests.rs"]
221mod batch_verification_tests;
222
223#[cfg(test)]
224#[path = "unit_tests/coin_deny_list_tests.rs"]
225mod coin_deny_list_tests;
226
227#[cfg(test)]
228#[path = "unit_tests/pre_execution_failure_tests.rs"]
229mod pre_execution_failure_tests;
230
231#[cfg(test)]
232#[path = "unit_tests/cancelled_move_authenticator_tests.rs"]
233mod cancelled_move_authenticator_tests;
234
235#[cfg(test)]
236#[path = "unit_tests/move_authenticator_attestation_tests.rs"]
237mod move_authenticator_attestation_tests;
238
239#[cfg(test)]
240#[path = "unit_tests/transaction_dependency_execution_tests.rs"]
241mod transaction_dependency_execution_tests;
242
243#[cfg(test)]
244#[path = "unit_tests/auth_unit_test_utils.rs"]
245pub mod auth_unit_test_utils;
246
247#[cfg(test)]
248#[path = "unit_tests/abstract_account_test_utils.rs"]
249pub mod abstract_account_test_utils;
250
251#[cfg(test)]
252#[path = "unit_tests/attestation_test_utils.rs"]
253pub mod attestation_test_utils;
254
255#[cfg(test)]
256#[path = "unit_tests/attestation_verdict_tests.rs"]
257mod attestation_verdict_tests;
258
259#[cfg(any(test, feature = "test-utils"))]
260pub mod authority_test_utils;
261
262pub(crate) mod attestation;
263pub(crate) mod attestation_semaphore;
264mod attestation_verdict;
265pub mod authority_per_epoch_store;
266pub mod authority_per_epoch_store_pruner;
267
268pub mod authority_store_tables;
269pub mod authority_store_types;
270pub mod epoch_markers;
271pub mod epoch_start_configuration;
272pub mod historic_ledger;
273pub mod historic_objects;
274pub mod object_retention;
275pub mod shared_object_congestion_tracker;
276pub mod shared_object_version_manager;
277pub mod suggested_gas_price_calculator;
278#[cfg(any(test, feature = "test-utils"))]
279pub mod test_authority_builder;
280pub mod transaction_deferral;
281
282pub(crate) mod authority_store;
283pub mod backpressure;
284pub(crate) mod dropped_tx_status_cache;
285
286/// Prometheus metrics which can be displayed in Grafana, queried and alerted on
287pub struct AuthorityMetrics {
288    tx_orders: IntCounter,
289    total_certs: IntCounter,
290    total_cert_attempts: IntCounter,
291    total_effects: IntCounter,
292    pub shared_obj_tx: IntCounter,
293    sponsored_tx: IntCounter,
294    tx_already_processed: IntCounter,
295    num_input_objs: Histogram,
296    num_shared_objects: Histogram,
297    batch_size: Histogram,
298
299    authority_state_handle_transaction_latency: Histogram,
300
301    execute_certificate_latency_single_writer: Histogram,
302    execute_certificate_latency_shared_object: Histogram,
303
304    internal_execution_latency: Histogram,
305    /// Number of times the validator refused to report effects (signed or
306    /// unsigned, labeled by RPC surface) because it had previously signed
307    /// different effects for the same transaction.
308    signed_effects_equivocation_prevented: IntCounterVec,
309    execution_load_input_objects_latency: Histogram,
310    prepare_certificate_latency: Histogram,
311    commit_certificate_latency: Histogram,
312    epoch_end_db_snapshot_handover_latency: Histogram,
313    epoch_end_db_snapshots_skipped: IntCounter,
314
315    pub(crate) transaction_manager_num_enqueued_certificates: IntCounterVec,
316    pub(crate) transaction_manager_num_missing_objects: IntGauge,
317    pub(crate) transaction_manager_num_pending_certificates: IntGauge,
318    pub(crate) transaction_manager_num_executing_certificates: IntGauge,
319    pub(crate) transaction_manager_num_ready: IntGauge,
320    pub(crate) transaction_manager_object_cache_size: IntGauge,
321    pub(crate) transaction_manager_object_cache_hits: IntCounter,
322    pub(crate) transaction_manager_object_cache_misses: IntCounter,
323    pub(crate) transaction_manager_object_cache_evictions: IntCounter,
324    pub(crate) transaction_manager_package_cache_size: IntGauge,
325    pub(crate) transaction_manager_package_cache_hits: IntCounter,
326    pub(crate) transaction_manager_package_cache_misses: IntCounter,
327    pub(crate) transaction_manager_package_cache_evictions: IntCounter,
328    pub(crate) transaction_manager_executed_transaction_cache_size: IntGauge,
329    pub(crate) transaction_manager_executed_transaction_cache_hits: IntCounter,
330    pub(crate) transaction_manager_executed_transaction_cache_misses: IntCounter,
331    pub(crate) transaction_manager_transaction_queue_age_s: Histogram,
332
333    pub(crate) execution_driver_executed_transactions: IntCounter,
334    pub(crate) execution_driver_dispatch_queue: IntGauge,
335    pub(crate) execution_queueing_delay_s: Histogram,
336    pub(crate) prepare_cert_gas_latency_ratio: Histogram,
337    pub(crate) execution_gas_latency_ratio: Histogram,
338
339    /// Modified objects whose pre-image was neither an input nor a read
340    /// object of the transaction, and so was not moved to the historic store.
341    pub(crate) superseded_capture_misses: IntCounter,
342
343    pub(crate) skipped_consensus_txns: IntCounter,
344    pub(crate) skipped_consensus_txns_cache_hit: IntCounter,
345
346    /// Per store, the oldest epoch whose history the node still holds,
347    /// published each time an expiry runs with retention on. Not advancing
348    /// across epochs means expiry is stuck.
349    pub(crate) historic_bucket_earliest_retained_epoch: IntGaugeVec,
350    /// Per store, how long the expiries that dropped a bucket took.
351    pub(crate) historic_bucket_expiry_duration: HistogramVec,
352
353    pub(crate) authority_overload_status: IntGauge,
354    /// Percentage of transactions shed due to consensus queue length.
355    pub(crate) consensus_queue_load_shedding_percentage: IntGauge,
356    /// This authority's locally computed load shedding percentage, taken as the
357    /// max of its latency/rate-based, transaction-manager-queue-based, and
358    /// writeback-cache-backpressure signals.
359    pub(crate) local_post_consensus_load_shedding_percentage: IntGauge,
360
361    pub(crate) transaction_overload_sources: IntCounterVec,
362
363    /// Number of attestation dry runs running now, each counted from the
364    /// checks before it until it has ended and its transaction holds its soft
365    /// locks.
366    pub(crate) attestations_in_progress: IntGauge,
367    /// Number of submissions waiting for a free attestation dry run.
368    pub(crate) attestations_waiting: IntGauge,
369    /// Number of submissions rejected since the node started. A submission is
370    /// rejected when every attestation dry run is taken and the queue is full
371    /// or the wait for one is too long.
372    pub(crate) attestations_rejected_total: IntCounter,
373    /// How long submissions waited for a free attestation dry run, near zero
374    /// when one was free. Rejected submissions are not recorded.
375    pub(crate) attestation_permit_wait: Histogram,
376    /// Number of attestation dry runs started since the node started.
377    pub(crate) attestation_dry_runs_total: IntCounter,
378
379    /// Checkpoint-inclusion waits that timed out before the RPC indexes
380    /// caught up.
381    pub(crate) checkpoint_inclusion_index_wait_timeouts: IntCounter,
382
383    // Consensus handler metrics
384    pub consensus_handler_processed: IntCounterVec,
385    pub consensus_handler_transaction_sizes: HistogramVec,
386    pub consensus_handler_num_low_scoring_authorities: IntGauge,
387    pub consensus_handler_scores: IntGaugeVec,
388    pub consensus_handler_deferred_transactions: IntCounter,
389    pub consensus_handler_congested_transactions: IntCounter,
390    pub consensus_handler_dependency_deferred_transactions: IntCounter,
391    pub consensus_handler_cancelled_transactions: IntCounter,
392    /// Number of user transactions dropped during a consensus commit because
393    /// post-consensus conflict/lock validation rejected them. Distinct from
394    /// `consensus_handler_load_shedding_dropped_transactions`.
395    pub consensus_handler_validation_dropped_transactions: IntCounter,
396    /// Results of the commit-indexed reader in post-consensus validation, by
397    /// result (keep, drop or missing) and the reason that decided it. Only with
398    /// the P-COOL flow on.
399    pub consensus_handler_validation_reader_verdicts: IntCounterVec,
400    /// Transactions whose inputs post-consensus validation read, by when:
401    /// `ahead` of the validation loop and used by it, `inline` in the loop,
402    /// or `unused`, read ahead for a transaction the loop dropped before
403    /// its inputs mattered. Only with the P-COOL flow on.
404    pub consensus_handler_validation_input_reads: IntCounterVec,
405    /// The commit being validated minus the highest fully executed commit, set
406    /// once per commit. Only with the P-COOL flow on.
407    pub consensus_handler_commit_execution_lag: IntGauge,
408    /// Attested transactions that passed post-consensus validation, by
409    /// attestation kind; a deferred transaction counts again when reloaded.
410    pub(crate) consensus_handler_validation_attested_transactions: IntCounterVec,
411    /// Attested transactions dropped by post-consensus validation because
412    /// their attestor is cut off for the rest of the epoch.
413    pub(crate) consensus_handler_validation_attestor_cut_off_transactions: IntCounter,
414    /// Attested transactions that were cancelled for congestion, by attestor's
415    /// hostname and by whether the gas owner was charged. A transaction this
416    /// validator executes from a checkpoint is not counted.
417    pub(crate) cancelled_attested_transactions: IntCounterVec,
418    /// Number of user transactions dropped during a consensus commit by
419    /// post-consensus load shedding, i.e. probabilistically rejected at the
420    /// quorum `consensus_handler_load_shedding_percentage` rate.
421    pub consensus_handler_load_shedding_dropped_transactions: IntCounter,
422    /// Stake-weighted quorum (2f+1) load shedding percentage enforced on user
423    /// transactions in the most recent consensus commit. This is the cluster
424    /// value actually applied post-consensus, as opposed to this authority's
425    /// own `authority_load_shedding_percentage`. 0 when the P-COOL flow is
426    /// disabled.
427    pub consensus_handler_load_shedding_percentage: IntGauge,
428    pub consensus_handler_max_object_costs: IntGaugeVec,
429    pub consensus_committed_subdags: IntCounterVec,
430    pub consensus_committed_messages: IntGaugeVec,
431    pub consensus_committed_user_transactions: IntGaugeVec,
432    pub consensus_handler_leader_round: IntGauge,
433    pub consensus_calculated_throughput: IntGauge,
434    pub consensus_calculated_throughput_profile: IntGauge,
435
436    pub validator_scoreboard_scores: IntGaugeVec,
437    pub invalid_misbehavior_reports_by_authority: IntGaugeVec,
438
439    pub limits_metrics: Arc<LimitsMetrics>,
440
441    /// bytecode verifier metrics for tracking timeouts
442    pub bytecode_verifier_metrics: Arc<BytecodeVerifierMetrics>,
443
444    /// Count of multisig signatures
445    pub multisig_sig_count: IntCounter,
446
447    // Tracks recent average txn queueing delay between when it is ready for execution
448    // until it starts executing.
449    pub execution_queueing_latency: LatencyObserver,
450
451    // Tracks the rate at which transactions become ready for execution in the
452    // scheduler. The need for the Mutex is that the tracker is updated in the
453    // scheduler and read in the overload_monitor. There should be low mutex
454    // contention because the update side is effectively single threaded and the
455    // read rate in overload_monitor is low. If the update side becomes
456    // multi-threaded, we can create one rate tracker per thread.
457    pub txn_ready_rate_tracker: Arc<Mutex<RateTracker>>,
458
459    // Tracks the rate of transactions starts execution in execution driver.
460    // Similar reason for using a Mutex here as to `txn_ready_rate_tracker`.
461    pub execution_rate_tracker: Arc<Mutex<RateTracker>>,
462}
463
464/// How long the historic bucket expiry waits, once a retention guard that kept
465/// a bucket is dropped, before it tries again.
466const HELD_BACK_EXPIRY_INTERVAL: Duration = Duration::from_secs(1);
467
468/// How far behind the network a node's newest executed checkpoint may be for
469/// the node to count as caught up; until then, closed buckets are not
470/// compacted.
471const CATCH_UP_LAG: Duration = Duration::from_secs(10 * 60);
472
473/// How often the background expiry checks again whether the node has caught
474/// up, while that holds back the compaction of closed buckets.
475const COMPACTION_RECHECK_INTERVAL: Duration = Duration::from_secs(5 * 60);
476
477// Override default Prom buckets for positive numbers in 0-10M range
478const POSITIVE_INT_BUCKETS: &[f64] = &[
479    1., 2., 5., 7., 10., 20., 50., 70., 100., 200., 500., 700., 1000., 2000., 5000., 7000., 10000.,
480    20000., 50000., 70000., 100000., 200000., 500000., 700000., 1000000., 2000000., 5000000.,
481    7000000., 10000000.,
482];
483
484const LATENCY_SEC_BUCKETS: &[f64] = &[
485    0.0005, 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1., 2., 3., 4., 5., 6., 7., 8., 9.,
486    10., 20., 30., 60., 90.,
487];
488
489// Buckets for low latency samples. Starts from 10us.
490const LOW_LATENCY_SEC_BUCKETS: &[f64] = &[
491    0.00001, 0.00002, 0.00005, 0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1,
492    0.2, 0.5, 1., 2., 5., 10., 20., 50., 100.,
493];
494
495// An expiry deletes the tombstones of the epochs it drops, which can take
496// minutes.
497const HISTORIC_BUCKET_EXPIRY_SEC_BUCKETS: &[f64] = &[
498    0.01, 0.05, 0.1, 0.5, 1., 2., 5., 10., 30., 60., 120., 300., 600., 1200., 1800.,
499];
500
501const GAS_LATENCY_RATIO_BUCKETS: &[f64] = &[
502    10.0, 50.0, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 2000.0,
503    3000.0, 4000.0, 5000.0, 6000.0, 7000.0, 8000.0, 9000.0, 10000.0, 50000.0, 100000.0, 1000000.0,
504];
505
506impl AuthorityMetrics {
507    pub fn new(registry: &prometheus_filtered::Registry) -> AuthorityMetrics {
508        let execute_certificate_latency = register_histogram_vec_with_registry!(
509            "authority_state_execute_certificate_latency",
510            "Latency of executing certificates, including waiting for inputs",
511            &["tx_type"],
512            LATENCY_SEC_BUCKETS.to_vec(),
513            registry;
514            MetricLevel::Info,
515        )
516        .unwrap();
517
518        let execute_certificate_latency_single_writer =
519            execute_certificate_latency.with_label_values(&[TX_TYPE_SINGLE_WRITER_TX]);
520        let execute_certificate_latency_shared_object =
521            execute_certificate_latency.with_label_values(&[TX_TYPE_SHARED_OBJ_TX]);
522
523        Self {
524            tx_orders: register_int_counter_with_registry!(
525                "total_transaction_orders",
526                "Total number of transaction orders",
527                registry;
528                MetricLevel::Warn,
529            )
530                .unwrap(),
531            total_certs: register_int_counter_with_registry!(
532                "total_transaction_certificates",
533                "Total number of transaction certificates handled",
534                registry;
535                MetricLevel::Warn,
536            )
537                .unwrap(),
538            total_cert_attempts: register_int_counter_with_registry!(
539                "total_handle_certificate_attempts",
540                "Number of calls to handle_certificate",
541                registry,
542            )
543                .unwrap(),
544            // total_effects == total transactions finished
545            total_effects: register_int_counter_with_registry!(
546                "total_transaction_effects",
547                "Total number of transaction effects produced",
548                registry;
549                MetricLevel::Warn,
550            )
551                .unwrap(),
552
553            shared_obj_tx: register_int_counter_with_registry!(
554                "num_shared_obj_tx",
555                "Number of transactions involving shared objects",
556                registry;
557                MetricLevel::Warn,
558            )
559                .unwrap(),
560
561            sponsored_tx: register_int_counter_with_registry!(
562                "num_sponsored_tx",
563                "Number of sponsored transactions",
564                registry,
565            )
566                .unwrap(),
567
568            tx_already_processed: register_int_counter_with_registry!(
569                "num_tx_already_processed",
570                "Number of transaction orders already processed previously",
571                registry,
572            )
573                .unwrap(),
574            num_input_objs: register_histogram_with_registry!(
575                "num_input_objects",
576                "Distribution of number of input TX objects per TX",
577                POSITIVE_INT_BUCKETS.to_vec(),
578                registry;
579                MetricLevel::Warn,
580            )
581                .unwrap(),
582            num_shared_objects: register_histogram_with_registry!(
583                "num_shared_objects",
584                "Number of shared input objects per TX",
585                POSITIVE_INT_BUCKETS.to_vec(),
586                registry,
587            )
588                .unwrap(),
589            batch_size: register_histogram_with_registry!(
590                "batch_size",
591                "Distribution of size of transaction batch",
592                POSITIVE_INT_BUCKETS.to_vec(),
593                registry,
594            )
595                .unwrap(),
596            authority_state_handle_transaction_latency: register_histogram_with_registry!(
597                "authority_state_handle_transaction_latency",
598                "Latency of handling transactions",
599                LATENCY_SEC_BUCKETS.to_vec(),
600                registry,
601            )
602                .unwrap(),
603            execute_certificate_latency_single_writer,
604            execute_certificate_latency_shared_object,
605            internal_execution_latency: register_histogram_with_registry!(
606                "authority_state_internal_execution_latency",
607                "Latency of actual certificate executions",
608                LATENCY_SEC_BUCKETS.to_vec(),
609                registry,
610            )
611                .unwrap(),
612            signed_effects_equivocation_prevented: register_int_counter_vec_with_registry!(
613                "authority_state_signed_effects_equivocation_prevented",
614                "Number of times the validator refused to report effects that differ from previously signed effects for the same transaction, by RPC surface",
615                &["surface"],
616                registry,
617            )
618            .unwrap(),
619            execution_load_input_objects_latency: register_histogram_with_registry!(
620                "authority_state_execution_load_input_objects_latency",
621                "Latency of loading input objects for execution",
622                LOW_LATENCY_SEC_BUCKETS.to_vec(),
623                registry,
624            )
625                .unwrap(),
626            prepare_certificate_latency: register_histogram_with_registry!(
627                "authority_state_prepare_certificate_latency",
628                "Latency of executing certificates, before committing the results",
629                LATENCY_SEC_BUCKETS.to_vec(),
630                registry,
631            )
632                .unwrap(),
633            commit_certificate_latency: register_histogram_with_registry!(
634                "authority_state_commit_certificate_latency",
635                "Latency of committing certificate execution results",
636                LATENCY_SEC_BUCKETS.to_vec(),
637                registry,
638            )
639                .unwrap(),
640            epoch_end_db_snapshot_handover_latency: register_histogram_with_registry!(
641                "epoch_end_db_snapshot_handover_latency",
642                "Time the epoch boundary waits for the consumer to take its database \
643                 snapshot of the perpetual store",
644                LATENCY_SEC_BUCKETS.to_vec(),
645                registry,
646            ).unwrap(),
647            epoch_end_db_snapshots_skipped: register_int_counter_with_registry!(
648                "epoch_end_db_snapshots_skipped",
649                "Epoch boundaries whose database snapshot was not taken, because the \
650                 consumer was busy or did not take it in time",
651                registry,
652            ).unwrap(),
653            transaction_manager_num_enqueued_certificates: register_int_counter_vec_with_registry!(
654                "transaction_manager_num_enqueued_certificates",
655                "Current number of certificates enqueued to TransactionManager",
656                &["result"],
657                registry,
658            )
659                .unwrap(),
660            transaction_manager_num_missing_objects: register_int_gauge_with_registry!(
661                "transaction_manager_num_missing_objects",
662                "Current number of missing objects in TransactionManager",
663                registry,
664            )
665                .unwrap(),
666            transaction_manager_num_pending_certificates: register_int_gauge_with_registry!(
667                "transaction_manager_num_pending_certificates",
668                "Number of certificates pending in TransactionManager, with at least 1 missing input object",
669                registry;
670                MetricLevel::Warn,
671            )
672                .unwrap(),
673            transaction_manager_num_executing_certificates: register_int_gauge_with_registry!(
674                "transaction_manager_num_executing_certificates",
675                "Number of executing certificates, including queued and actually running certificates",
676                registry;
677                MetricLevel::Warn,
678            )
679                .unwrap(),
680            transaction_manager_num_ready: register_int_gauge_with_registry!(
681                "transaction_manager_num_ready",
682                "Number of ready transactions in TransactionManager",
683                registry,
684            )
685                .unwrap(),
686            transaction_manager_object_cache_size: register_int_gauge_with_registry!(
687                "transaction_manager_object_cache_size",
688                "Current size of object-availability cache in TransactionManager",
689                registry,
690            )
691                .unwrap(),
692            transaction_manager_object_cache_hits: register_int_counter_with_registry!(
693                "transaction_manager_object_cache_hits",
694                "Number of object-availability cache hits in TransactionManager",
695                registry,
696            )
697                .unwrap(),
698            historic_bucket_earliest_retained_epoch: register_int_gauge_vec_with_registry!(
699                "historic_bucket_earliest_retained_epoch",
700                "Earliest epoch whose history a store retains",
701                &["store"],
702                registry;
703                MetricLevel::Warn,
704            )
705            .unwrap(),
706            historic_bucket_expiry_duration: register_histogram_vec_with_registry!(
707                "historic_bucket_expiry_duration_seconds",
708                "How long a store's expiries of historic buckets that dropped a bucket took",
709                &["store"],
710                HISTORIC_BUCKET_EXPIRY_SEC_BUCKETS.to_vec(),
711                registry;
712                MetricLevel::Warn,
713            )
714            .unwrap(),
715            authority_overload_status: register_int_gauge_with_registry!(
716                "authority_overload_status",
717                "Whether authority is current experiencing overload and enters load shedding mode.",
718                registry;
719                MetricLevel::Warn,)
720                .unwrap(),
721            local_post_consensus_load_shedding_percentage: register_int_gauge_with_registry!(
722                "authority_load_shedding_percentage",
723                "This authority's locally computed load shedding percentage. In the P-COOL flow this is the value broadcast to peers, not necessarily the rate enforced (see consensus_handler_load_shedding_percentage).",
724                registry;
725                MetricLevel::Info,)
726                .unwrap(),
727            consensus_queue_load_shedding_percentage: register_int_gauge_with_registry!(
728                "consensus_queue_load_shedding_percentage",
729                "Percentage of transactions shed due to consensus queue length. Separate admission-control signal, not an input to authority_load_shedding_percentage.",
730                registry)
731                .unwrap(),
732            transaction_manager_object_cache_misses: register_int_counter_with_registry!(
733                "transaction_manager_object_cache_misses",
734                "Number of object-availability cache misses in TransactionManager",
735                registry,
736            )
737                .unwrap(),
738            transaction_manager_object_cache_evictions: register_int_counter_with_registry!(
739                "transaction_manager_object_cache_evictions",
740                "Number of object-availability cache evictions in TransactionManager",
741                registry,
742            )
743                .unwrap(),
744            transaction_manager_package_cache_size: register_int_gauge_with_registry!(
745                "transaction_manager_package_cache_size",
746                "Current size of package-availability cache in TransactionManager",
747                registry,
748            )
749                .unwrap(),
750            transaction_manager_package_cache_hits: register_int_counter_with_registry!(
751                "transaction_manager_package_cache_hits",
752                "Number of package-availability cache hits in TransactionManager",
753                registry,
754            )
755                .unwrap(),
756            transaction_manager_package_cache_misses: register_int_counter_with_registry!(
757                "transaction_manager_package_cache_misses",
758                "Number of package-availability cache misses in TransactionManager",
759                registry,
760            )
761                .unwrap(),
762            transaction_manager_package_cache_evictions: register_int_counter_with_registry!(
763                "transaction_manager_package_cache_evictions",
764                "Number of package-availability cache evictions in TransactionManager",
765                registry,
766            )
767                .unwrap(),
768            transaction_manager_executed_transaction_cache_size: register_int_gauge_with_registry!(
769                "transaction_manager_executed_transaction_cache_size",
770                "Current size of the executed-transaction cache in TransactionManager, which dependents wait on",
771                registry,
772            )
773                .unwrap(),
774            transaction_manager_executed_transaction_cache_hits: register_int_counter_with_registry!(
775                "transaction_manager_executed_transaction_cache_hits",
776                "Number of executed-transaction cache hits in TransactionManager",
777                registry,
778            )
779                .unwrap(),
780            transaction_manager_executed_transaction_cache_misses: register_int_counter_with_registry!(
781                "transaction_manager_executed_transaction_cache_misses",
782                "Number of executed-transaction cache misses in TransactionManager",
783                registry,
784            )
785                .unwrap(),
786            transaction_manager_transaction_queue_age_s: register_histogram_with_registry!(
787                "transaction_manager_transaction_queue_age_s",
788                "Time spent in waiting for transaction in the queue",
789                LATENCY_SEC_BUCKETS.to_vec(),
790                registry;
791                MetricLevel::Warn,
792            )
793                .unwrap(),
794            transaction_overload_sources: register_int_counter_vec_with_registry!(
795                "transaction_overload_sources",
796                "Number of times each source indicates transaction overload.",
797                &["source"],
798                registry)
799                .unwrap(),
800            attestations_in_progress: register_int_gauge_with_registry!(
801                "attestations_in_progress",
802                "Number of attestation dry runs running, each counted from the checks before it \
803                    until it has ended and its transaction holds its soft locks",
804                registry;
805                MetricLevel::Warn,
806            )
807                .unwrap(),
808            attestations_waiting: register_int_gauge_with_registry!(
809                "attestations_waiting",
810                "Number of submissions waiting for a free attestation dry run",
811                registry,
812            )
813                .unwrap(),
814            attestations_rejected_total: register_int_counter_with_registry!(
815                "attestations_rejected_total",
816                "Number of submissions rejected because every attestation dry run was taken \
817                    and the queue was full or the wait for one too long",
818                registry;
819                MetricLevel::Warn,
820            )
821                .unwrap(),
822            attestation_permit_wait: register_histogram_with_registry!(
823                "attestation_permit_wait",
824                "Time submissions waited for a free attestation dry run, near zero when one was \
825                    free; rejected submissions are not recorded",
826                LOW_LATENCY_SEC_BUCKETS.to_vec(),
827                registry;
828                MetricLevel::Info,
829            )
830                .unwrap(),
831            attestation_dry_runs_total: register_int_counter_with_registry!(
832                "attestation_dry_runs_total",
833                "Number of attestation dry runs started",
834                registry,
835            )
836                .unwrap(),
837            execution_driver_executed_transactions: register_int_counter_with_registry!(
838                "execution_driver_executed_transactions",
839                "Cumulative number of transaction executed by execution driver",
840                registry;
841                MetricLevel::Warn,
842            )
843                .unwrap(),
844            execution_driver_dispatch_queue: register_int_gauge_with_registry!(
845                "execution_driver_dispatch_queue",
846                "Number of transaction pending in execution driver dispatch queue",
847                registry,
848            )
849                .unwrap(),
850            execution_queueing_delay_s: register_histogram_with_registry!(
851                "execution_queueing_delay_s",
852                "Queueing delay between a transaction is ready for execution until it starts executing.",
853                LATENCY_SEC_BUCKETS.to_vec(),
854                registry
855            )
856                .unwrap(),
857            prepare_cert_gas_latency_ratio: register_histogram_with_registry!(
858                "prepare_cert_gas_latency_ratio",
859                "The ratio of computation gas divided by VM execution latency.",
860                GAS_LATENCY_RATIO_BUCKETS.to_vec(),
861                registry
862            )
863                .unwrap(),
864            execution_gas_latency_ratio: register_histogram_with_registry!(
865                "execution_gas_latency_ratio",
866                "The ratio of computation gas divided by certificate execution latency, include committing certificate.",
867                GAS_LATENCY_RATIO_BUCKETS.to_vec(),
868                registry
869            )
870                .unwrap(),
871            superseded_capture_misses: register_int_counter_with_registry!(
872                "superseded_capture_misses",
873                "Count of modified object versions whose pre-image could not be captured for relocation into the historic store",
874                registry;
875                MetricLevel::Warn,
876            )
877                .unwrap(),
878            skipped_consensus_txns: register_int_counter_with_registry!(
879                "skipped_consensus_txns",
880                "Total number of consensus transactions skipped",
881                registry,
882            )
883                .unwrap(),
884            skipped_consensus_txns_cache_hit: register_int_counter_with_registry!(
885                "skipped_consensus_txns_cache_hit",
886                "Total number of consensus transactions skipped because of local cache hit",
887                registry,
888            )
889                .unwrap(),
890            checkpoint_inclusion_index_wait_timeouts: register_int_counter_with_registry!(
891                "checkpoint_inclusion_index_wait_timeouts",
892                "Total number of checkpoint-inclusion waits that returned before the RPC indexes had caught up",
893                registry,
894            )
895                .unwrap(),
896            consensus_handler_processed: register_int_counter_vec_with_registry!(
897                "consensus_handler_processed",
898                "Number of transactions processed by consensus handler",
899                &["class"],
900                registry
901            ).unwrap(),
902            consensus_handler_transaction_sizes: register_histogram_vec_with_registry!(
903                "consensus_handler_transaction_sizes",
904                "Sizes of each type of transactions processed by consensus handler",
905                &["class"],
906                POSITIVE_INT_BUCKETS.to_vec(),
907                registry;
908                MetricLevel::Warn,
909            ).unwrap(),
910            consensus_handler_num_low_scoring_authorities: register_int_gauge_with_registry!(
911                "consensus_handler_num_low_scoring_authorities",
912                "Number of low scoring authorities based on reputation scores from consensus",
913                registry
914            ).unwrap(),
915            consensus_handler_scores: register_int_gauge_vec_with_registry!(
916                "consensus_handler_scores",
917                "scores from consensus for each authority",
918                &["authority"],
919                registry,
920            ).unwrap(),
921            validator_scoreboard_scores: register_int_gauge_vec_with_registry!(
922                "validator_scoreboard_scores",
923                "Per-authority validator scores published by the local Scoreboard after each consensus commit. Range [0, MAX_SCORE].",
924                &["authority"],
925                registry;
926                MetricLevel::Warn,
927            ).unwrap(),
928            invalid_misbehavior_reports_by_authority: register_int_gauge_vec_with_registry!(
929                "invalid_misbehavior_reports_by_authority",
930                "Cumulative count of invalid misbehavior reports received from each reporting authority in the current epoch. Bumped when a `MisbehaviorReport` consensus transaction fails sender/authority match or payload validation. Snapshot republished after each consensus commit.",
931                &["authority"],
932                registry;
933                MetricLevel::Warn,
934            ).unwrap(),
935            consensus_handler_deferred_transactions: register_int_counter_with_registry!(
936                "consensus_handler_deferred_transactions",
937                "Number of transactions deferred by consensus handler",
938                registry,
939            ).unwrap(),
940            consensus_handler_congested_transactions: register_int_counter_with_registry!(
941                "consensus_handler_congested_transactions",
942                "Number of transactions deferred by consensus handler due to congestion",
943                registry,
944            ).unwrap(),
945            consensus_handler_dependency_deferred_transactions: register_int_counter_with_registry!(
946                "consensus_handler_dependency_deferred_transactions",
947                "Number of times the consensus handler deferred a transaction for the transaction it depends on",
948                registry,
949            ).unwrap(),
950            consensus_handler_cancelled_transactions: register_int_counter_with_registry!(
951                "consensus_handler_cancelled_transactions",
952                "Number of transactions cancelled by consensus handler",
953                registry,
954            ).unwrap(),
955            consensus_handler_validation_dropped_transactions: register_int_counter_with_registry!(
956                "consensus_handler_validation_dropped_transactions",
957                "Number of user transactions dropped by post-consensus validation",
958                registry,
959            ).unwrap(),
960            consensus_handler_validation_reader_verdicts: register_int_counter_vec_with_registry!(
961                "consensus_handler_validation_reader_verdicts",
962                "Verdicts of the commit-indexed reader in post-consensus validation, by verdict and deciding reason",
963                &["verdict", "reason"],
964                registry,
965            ).unwrap(),
966            consensus_handler_validation_input_reads: register_int_counter_vec_with_registry!(
967                "consensus_handler_validation_input_reads",
968                "Transactions whose inputs post-consensus validation read, by when: ahead of the validation loop, inline in it, or unused",
969                &["when"],
970                registry,
971            ).unwrap(),
972            consensus_handler_commit_execution_lag: register_int_gauge_with_registry!(
973                "consensus_handler_commit_execution_lag",
974                "The consensus commit being validated minus the highest fully executed commit",
975                registry,
976            ).unwrap(),
977            consensus_handler_validation_attested_transactions: register_int_counter_vec_with_registry!(
978                "consensus_handler_validation_attested_transactions",
979                "Number of attested transactions that passed post-consensus validation, by attestation kind; a deferred transaction counts again when reloaded",
980                &["attestation"],
981                registry;
982                MetricLevel::Warn,
983            ).unwrap(),
984            consensus_handler_validation_attestor_cut_off_transactions: register_int_counter_with_registry!(
985                "consensus_handler_validation_attestor_cut_off_transactions",
986                "Number of attested transactions dropped by post-consensus validation because their attestor is cut off for the rest of the epoch",
987                registry,
988            ).unwrap(),
989            cancelled_attested_transactions: register_int_counter_vec_with_registry!(
990                "cancelled_attested_transactions",
991                "Number of attested transactions cancelled for congestion, by attestor's hostname and by whether the gas owner was charged. A transaction this validator executes from a checkpoint is not counted.",
992                &["authority", "charged"],
993                registry;
994                MetricLevel::Warn,
995            ).unwrap(),
996            consensus_handler_load_shedding_dropped_transactions: register_int_counter_with_registry!(
997                "consensus_handler_load_shedding_dropped_transactions",
998                "Number of user transactions dropped by post-consensus load shedding, based on the quorum load shedding percentage",
999                registry,
1000            ).unwrap(),
1001            consensus_handler_load_shedding_percentage: register_int_gauge_with_registry!(
1002                "consensus_handler_load_shedding_percentage",
1003                "Stake-weighted quorum (2f+1) load shedding percentage enforced on user transactions in the most recent consensus commit. 0 when the P-COOL flow is disabled.",
1004                registry,
1005            ).unwrap(),
1006            consensus_handler_max_object_costs: register_int_gauge_vec_with_registry!(
1007                "consensus_handler_max_congestion_control_object_costs",
1008                "Max object costs for congestion control in the current consensus commit",
1009                &["commit_type"],
1010                registry,
1011            ).unwrap(),
1012            consensus_committed_subdags: register_int_counter_vec_with_registry!(
1013                "consensus_committed_subdags",
1014                "Number of committed subdags, sliced by leader",
1015                &["authority"],
1016                registry,
1017            ).unwrap(),
1018            consensus_committed_messages: register_int_gauge_vec_with_registry!(
1019                "consensus_committed_messages",
1020                "Total number of committed consensus messages, sliced by author",
1021                &["authority"],
1022                registry;
1023                MetricLevel::Warn,
1024            ).unwrap(),
1025            consensus_committed_user_transactions: register_int_gauge_vec_with_registry!(
1026                "consensus_committed_user_transactions",
1027                "Number of committed user transactions, sliced by submitter",
1028                &["authority"],
1029                registry,
1030            ).unwrap(),
1031            consensus_handler_leader_round: register_int_gauge_with_registry!(
1032                "consensus_handler_leader_round",
1033                "The leader round of the current consensus output being processed in the consensus handler",
1034                registry;
1035                MetricLevel::Warn,
1036            ).unwrap(),
1037            limits_metrics: Arc::new(LimitsMetrics::new(registry)),
1038            bytecode_verifier_metrics: Arc::new(BytecodeVerifierMetrics::new(registry)),
1039            multisig_sig_count: register_int_counter_with_registry!(
1040                "multisig_sig_count",
1041                "Count of multisig signatures",
1042                registry,
1043            )
1044                .unwrap(),
1045            consensus_calculated_throughput: register_int_gauge_with_registry!(
1046                "consensus_calculated_throughput",
1047                "The calculated throughput from consensus output. Result is calculated based on unique transactions.",
1048                registry,
1049            ).unwrap(),
1050            consensus_calculated_throughput_profile: register_int_gauge_with_registry!(
1051                "consensus_calculated_throughput_profile",
1052                "The current active calculated throughput profile",
1053                registry
1054            ).unwrap(),
1055            execution_queueing_latency: LatencyObserver::new(),
1056            txn_ready_rate_tracker: Arc::new(Mutex::new(RateTracker::new(Duration::from_secs(10)))),
1057            execution_rate_tracker: Arc::new(Mutex::new(RateTracker::new(Duration::from_secs(10)))),
1058        }
1059    }
1060
1061    /// Reset metrics that contain `hostname` as one of the labels. This is
1062    /// needed to avoid retaining metrics for long-gone committee members and
1063    /// only exposing metrics for the committee in the current epoch.
1064    pub fn reset_on_reconfigure(&self) {
1065        self.consensus_committed_messages.reset();
1066        self.consensus_handler_scores.reset();
1067        self.validator_scoreboard_scores.reset();
1068        self.invalid_misbehavior_reports_by_authority.reset();
1069        self.consensus_committed_user_transactions.reset();
1070        self.cancelled_attested_transactions.reset();
1071    }
1072}
1073
1074/// a Trait object for `Signer` that is:
1075/// - Pin, i.e. confined to one place in memory (we don't want to copy private keys).
1076/// - Sync, i.e. can be safely shared between threads.
1077///
1078/// Typically instantiated with Box::pin(keypair) where keypair is a `KeyPair`
1079pub type StableSyncAuthoritySigner = Pin<Arc<dyn Signer<AuthoritySignature> + Send + Sync>>;
1080
1081/// The checked transaction inputs and, per Move authenticator, its checked inputs.
1082type CheckedValidationInputs = (CheckedInputObjects, Vec<CheckedInputObjects>);
1083
1084/// Execution env contains the "environment" for the transaction to be executed
1085/// in, that is, all the information necessary for execution that is not
1086/// specified by the transaction itself.
1087#[derive(Debug, Clone, Default)]
1088pub struct ExecutionEnv {
1089    /// The assigned version of each shared object for the transaction.
1090    pub assigned_versions: AssignedVersions,
1091    /// The expected digest of the effects of the transaction, if executing from
1092    /// checkpoint or other sources where the effects are known in advance.
1093    pub expected_effects_digest: Option<TransactionEffectsDigest>,
1094    /// Where the verdict on the transaction's attestation comes from.
1095    pub attestation: AttestationSource,
1096}
1097
1098impl ExecutionEnv {
1099    pub fn new() -> Self {
1100        Default::default()
1101    }
1102
1103    pub fn with_expected_effects_digest(
1104        mut self,
1105        expected_effects_digest: TransactionEffectsDigest,
1106    ) -> Self {
1107        self.expected_effects_digest = Some(expected_effects_digest);
1108        self
1109    }
1110
1111    pub fn with_assigned_versions(mut self, assigned_versions: AssignedVersions) -> Self {
1112        if !assigned_versions.is_empty() {
1113            self.assigned_versions = assigned_versions;
1114        }
1115        self
1116    }
1117
1118    pub fn with_attestation(mut self, attestation: AttestationSource) -> Self {
1119        self.attestation = attestation;
1120        self
1121    }
1122}
1123
1124/// `execute_transaction()`'s result. The fourth element holds every object read
1125/// during execution, where the pre-image of a mutated object that is not a
1126/// declared input, such as a dynamic field, is found; the last is the record of
1127/// an attested transaction's verdict.
1128type TransactionExecutionResult = (
1129    InnerTemporaryStore,
1130    TransactionEffects,
1131    Option<ExecutionError>,
1132    ObjectSet,
1133    Option<AttestationRecord>,
1134);
1135
1136/// Reads latest versions from the live object set, and exact versions from it
1137/// or, after a miss there, from the historic buckets.
1138struct ObjectStoreWithHistoricFallback<'a>(&'a AuthorityState);
1139
1140impl ObjectStore for ObjectStoreWithHistoricFallback<'_> {
1141    fn try_get_object(
1142        &self,
1143        object_id: &ObjectId,
1144    ) -> iota_types::storage::error::Result<Option<Object>> {
1145        self.0.get_object_store().try_get_object(object_id)
1146    }
1147
1148    fn try_get_object_by_key(
1149        &self,
1150        object_id: &ObjectId,
1151        version: Version,
1152    ) -> iota_types::storage::error::Result<Option<Object>> {
1153        self.0
1154            .get_object_with_historic_fallback(&ObjectKey(*object_id, version))
1155            .map_err(iota_types::storage::error::Error::custom)
1156    }
1157
1158    fn try_multi_get_objects_by_key(
1159        &self,
1160        object_keys: &[ObjectKey],
1161    ) -> iota_types::storage::error::Result<Vec<Option<Object>>> {
1162        let mut objects = self
1163            .0
1164            .get_object_store()
1165            .try_multi_get_objects_by_key(object_keys)?;
1166        self.0
1167            .historic_objects
1168            .fill_missing(object_keys, &mut objects, None)
1169            .map_err(iota_types::storage::error::Error::custom)?;
1170        Ok(objects)
1171    }
1172}
1173
1174pub struct AuthorityState {
1175    /// The `Arc` this state lives in, for [`Self::shared`].
1176    this: Weak<AuthorityState>,
1177    // Fixed size, static, identity of the authority
1178    /// The name of this authority.
1179    pub name: AuthorityName,
1180    /// The signature key of the authority.
1181    pub secret: StableSyncAuthoritySigner,
1182
1183    /// The database
1184    input_loader: TransactionInputLoader,
1185    execution_cache_trait_pointers: ExecutionCacheTraitPointers,
1186
1187    epoch_store: ArcSwap<AuthorityPerEpochStore>,
1188
1189    /// This lock denotes current 'execution epoch'.
1190    /// Execution acquires read lock, checks transaction epoch and holds it
1191    /// until all writes are complete. Reconfiguration acquires write lock,
1192    /// changes the epoch and revert all transactions from previous epoch
1193    /// that are executed but did not make into checkpoint.
1194    execution_lock: RwLock<EpochId>,
1195
1196    /// The RPC index store, absent when this node maintains no index group.
1197    pub rpc_indexes_store: Option<Arc<RpcIndexesStore>>,
1198
1199    /// Superseded object versions, bucketed by epoch.
1200    historic_objects: Arc<HistoricObjects>,
1201
1202    /// Executed transactions keyed by checkpoint, bucketed by epoch.
1203    historic_ledger: Arc<HistoricLedger>,
1204
1205    pub checkpoint_store: Arc<CheckpointStore>,
1206
1207    committee_store: Arc<CommitteeStore>,
1208
1209    /// Schedules transaction execution.
1210    execution_scheduler: Arc<ExecutionSchedulerWrapper>,
1211
1212    /// Shuts down the execution task. Used only in testing.
1213    #[cfg_attr(not(test), expect(unused))]
1214    tx_execution_shutdown: Mutex<Option<oneshot::Sender<()>>>,
1215
1216    pub metrics: Arc<AuthorityMetrics>,
1217    authority_per_epoch_pruner: AuthorityPerEpochStorePruner,
1218    /// The checkpoints execution responses still read superseded object
1219    /// versions from; bucket expiry keeps their epochs.
1220    object_retention: ObjectRetention,
1221
1222    /// Announces each epoch entered to the task that expires the historic
1223    /// buckets.
1224    historic_bucket_expiry: watch::Sender<EpochId>,
1225    /// Set by [`Self::stop_historic_bucket_work`].
1226    historic_bucket_work_stopped: Arc<AtomicBool>,
1227
1228    pub config: NodeConfig,
1229
1230    /// Current overload status in this authority. Updated periodically.
1231    pub overload_info: AuthorityOverloadInfo,
1232
1233    /// Limits the attestation dry runs of submitted transactions.
1234    attestation_semaphore: AttestationSemaphore,
1235
1236    pub validator_tx_finalizer: Option<Arc<ValidatorTxFinalizer<NetworkAuthorityClient>>>,
1237
1238    /// The chain identifier is derived from the digest of the genesis
1239    /// checkpoint.
1240    chain_identifier: ChainIdentifier,
1241
1242    pub(crate) congestion_tracker: Arc<CongestionTracker>,
1243
1244    /// Traffic controller for IOTA core servers (json-rpc, validator service)
1245    pub traffic_controller: Option<Arc<TrafficController>>,
1246    /// Set when a consumer wants a database snapshot of the perpetual store at
1247    /// each epoch boundary. See [`Self::hand_over_epoch_end_db_snapshot`].
1248    epoch_end_db_snapshots: Option<EpochEndDbSnapshotHandle>,
1249}
1250
1251/// Outputs of the shared transaction validation pipeline used by both
1252/// `handle_transaction_validation_checks` and `attest_transaction`.
1253struct ValidationOutputs<'a> {
1254    per_authenticator_inputs: Vec<(InputObjects, ObjectReadResult)>,
1255    move_authenticators: Vec<&'a MoveAuthenticator>,
1256    gas_status: IotaGasStatus,
1257    tx_checked_input_objects: CheckedInputObjects,
1258    per_authenticator_checked_inputs: Vec<(CheckedInputObjects, AuthenticatorFunctionRef)>,
1259}
1260
1261/// The authority state encapsulates all state, drives execution, and ensures
1262/// safety.
1263///
1264/// Note the authority operations can be accessed through a read ref (&) and do
1265/// not require &mut. Internally a database is synchronized through a mutex
1266/// lock.
1267///
1268/// Repeating valid commands should produce no changes and return no error.
1269impl AuthorityState {
1270    pub fn is_committee_validator(&self, epoch_store: &AuthorityPerEpochStore) -> bool {
1271        epoch_store.committee().authority_exists(&self.name)
1272    }
1273
1274    pub fn is_active_validator(&self, epoch_store: &AuthorityPerEpochStore) -> bool {
1275        epoch_store
1276            .active_validators()
1277            .iter()
1278            .any(|a| AuthorityName::from(a) == self.name)
1279    }
1280
1281    pub fn is_fullnode(&self, epoch_store: &AuthorityPerEpochStore) -> bool {
1282        !self.is_committee_validator(epoch_store)
1283    }
1284
1285    pub fn committee_store(&self) -> &Arc<CommitteeStore> {
1286        &self.committee_store
1287    }
1288
1289    pub fn clone_committee_store(&self) -> Arc<CommitteeStore> {
1290        self.committee_store.clone()
1291    }
1292
1293    pub fn overload_config(&self) -> &AuthorityOverloadConfig {
1294        &self.config.authority_overload_config
1295    }
1296
1297    pub fn get_epoch_state_commitments(
1298        &self,
1299        epoch: EpochId,
1300    ) -> IotaResult<Option<Vec<CheckpointCommitment>>> {
1301        self.checkpoint_store.get_epoch_state_commitments(epoch)
1302    }
1303
1304    /// Runs deny list, input object validation, gas checks, coin deny list, and
1305    /// MoveAuthenticator checks. Returns the owned object refs for optional
1306    /// version validation. Does NOT acquire locks or sign the transaction.
1307    ///
1308    /// [`Self::handle_transaction_validation_checks_at_commit`] repeats the
1309    /// checks after loading the inputs for post-consensus validation at a
1310    /// commit, so a check added here belongs there too.
1311    ///
1312    /// `deny_config` is the deny rule source to enforce, chosen per caller:
1313    /// the local config alone, or with `enable_pcool_flow` the local config
1314    /// combined with the governance rules.
1315    ///
1316    /// These are the checks of validator-local admission: the coin deny list
1317    /// is read at its latest value, so a denial or global pause applies at
1318    /// once, and published packages are verified with this validator's own
1319    /// `VerifierSigningConfig`. Post-consensus validation does not call this
1320    /// function: [`Self::handle_transaction_validation_checks_at_commit`]
1321    /// reads the deny list as of the commit's horizon, where a change made
1322    /// this epoch counts from the horizon distance after the commit that made
1323    /// it, and verifies packages with the protocol config's limits, so every
1324    /// validator reaches the same verdict.
1325    #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
1326    pub(crate) async fn handle_transaction_validation_checks(
1327        &self,
1328        transaction: &VerifiedTransaction,
1329        epoch_store: &Arc<AuthorityPerEpochStore>,
1330        deny_config: &dyn DenyRuleConfig,
1331    ) -> IotaResult<Vec<ObjectReference>> {
1332        let protocol_config = epoch_store.protocol_config();
1333        let tx = transaction.data().transaction();
1334
1335        let ValidationOutputs {
1336            move_authenticators,
1337            gas_status,
1338            tx_checked_input_objects,
1339            per_authenticator_checked_inputs,
1340            per_authenticator_inputs: _,
1341        } = self.run_validation_checks(transaction, epoch_store, deny_config, false)?;
1342
1343        let (_, signer, gas_data) = tx.execution_parts();
1344
1345        let (sender_authenticator_function_ref, sponsor_authenticator_function_ref) =
1346            extract_auth_fun_refs(signer, gas_data.owner, |address| {
1347                move_authenticators
1348                    .iter()
1349                    .zip(per_authenticator_checked_inputs.iter())
1350                    .find(|(move_authenticator, _)| move_authenticator.address() == address)
1351                    .map(|(_, (_, auth_fun_ref))| auth_fun_ref.clone())
1352            });
1353
1354        // Filter the authenticators and their checked inputs down to those that must
1355        // be executed pre-consensus. This is done *after* the deny-list check so
1356        // that all MoveAuthenticator input objects are covered by that check regardless
1357        // of deferral.
1358        let pre_consensus_move_authenticators =
1359            pre_consensus_move_authenticators(transaction, protocol_config);
1360        // Asserted before the zip below pairs them positionally; the two lists
1361        // come from independent computations.
1362        debug_assert_eq!(
1363            move_authenticators.len(),
1364            per_authenticator_checked_inputs.len(),
1365            "Move authenticators amount must match the number of checked authenticator inputs"
1366        );
1367        let (move_authenticators, per_authenticator_checked_inputs): (Vec<_>, Vec<_>) =
1368            move_authenticators
1369                .into_iter()
1370                .zip(per_authenticator_checked_inputs)
1371                .filter(|(a, _)| pre_consensus_move_authenticators.contains(a))
1372                .unzip();
1373        // If there are `MoveAuthenticator` signatures, execute them and check if they
1374        // all succeed.
1375        if !move_authenticators.is_empty() {
1376            let authenticators = move_authenticators
1377                .into_iter()
1378                .zip(per_authenticator_checked_inputs)
1379                .map(
1380                    |(
1381                        move_authenticator,
1382                        (authenticator_checked_input_objects, authenticator_function_ref),
1383                    )| {
1384                        (
1385                            move_authenticator.to_owned(),
1386                            authenticator_function_ref,
1387                            authenticator_checked_input_objects,
1388                        )
1389                    },
1390                )
1391                .collect();
1392            self.authenticate_before_consensus(
1393                epoch_store,
1394                transaction,
1395                gas_status,
1396                authenticators,
1397                (
1398                    sender_authenticator_function_ref,
1399                    sponsor_authenticator_function_ref,
1400                ),
1401            )?;
1402        }
1403
1404        Ok(tx_checked_input_objects.inner().filter_owned_objects())
1405    }
1406
1407    /// Runs the checks of [`Self::handle_transaction_validation_checks`]
1408    /// without running the Move authenticators, for a transaction that is
1409    /// rejected whatever they answer.
1410    #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
1411    pub(crate) fn handle_transaction_validation_checks_without_authentication(
1412        &self,
1413        transaction: &VerifiedTransaction,
1414        epoch_store: &Arc<AuthorityPerEpochStore>,
1415        deny_config: &dyn DenyRuleConfig,
1416    ) -> IotaResult<()> {
1417        self.run_validation_checks(transaction, epoch_store, deny_config, false)?;
1418
1419        Ok(())
1420    }
1421
1422    /// Runs `authenticators` on their own, metered by `gas_status`, and fails
1423    /// as admission does when one rejects the transaction or runs out of gas,
1424    /// with the error of [`Self::authentication_failure`].
1425    /// `function_refs` are the sender's and the gas owner's authenticator
1426    /// functions, resolved over every authenticator of the transaction.
1427    fn authenticate_before_consensus(
1428        &self,
1429        epoch_store: &Arc<AuthorityPerEpochStore>,
1430        transaction: &VerifiedTransaction,
1431        gas_status: IotaGasStatus,
1432        authenticators: Vec<(
1433            MoveAuthenticator,
1434            AuthenticatorFunctionRef,
1435            CheckedInputObjects,
1436        )>,
1437        function_refs: (
1438            Option<AuthenticatorFunctionRef>,
1439            Option<AuthenticatorFunctionRef>,
1440        ),
1441    ) -> IotaResult<()> {
1442        let tx = transaction.data().transaction();
1443        let (kind, signer, gas_data) = tx.execution_parts();
1444        let input_objects: Vec<_> = authenticators
1445            .iter()
1446            .map(|(_, _, input_objects)| input_objects)
1447            .collect();
1448        let aggregated_authenticator_input_objects =
1449            iota_transaction_checks::aggregate_authenticator_input_objects(&input_objects)?;
1450        let (sender_auth_digest, sponsor_auth_digest) =
1451            transaction.data().compute_auth_digests()?;
1452        let (sender_authenticator_function_ref, sponsor_authenticator_function_ref) = function_refs;
1453        let auth_context_data = AuthContextData {
1454            transaction_data_bytes: bcs::to_bytes(tx)
1455                .expect("Transaction serialization cannot fail"),
1456            sender_auth_digest,
1457            sponsor_auth_digest,
1458            sender_authenticator_function_ref,
1459            sponsor_authenticator_function_ref,
1460        };
1461
1462        let (_gas_status, result) = epoch_store.executor().authenticate_transaction(
1463            self.get_backing_store().as_ref(),
1464            epoch_store.protocol_config(),
1465            self.metrics.limits_metrics.clone(),
1466            &epoch_store.epoch_start_config().epoch_data().epoch_id(),
1467            epoch_store
1468                .epoch_start_config()
1469                .epoch_data()
1470                .epoch_start_timestamp(),
1471            gas_data,
1472            gas_status,
1473            authenticators,
1474            aggregated_authenticator_input_objects,
1475            kind,
1476            signer,
1477            transaction.digest().to_owned(),
1478            auth_context_data,
1479            &mut None,
1480        );
1481        result.map_err(|error| {
1482            self.authentication_failure(tx, epoch_store.epoch(), move_authentication_failure(error))
1483        })
1484    }
1485
1486    /// The `Arc` this state lives in.
1487    ///
1488    /// # Panics
1489    ///
1490    /// Never while the caller holds the reference: a state is only ever
1491    /// built inside its `Arc`.
1492    pub(crate) fn shared(&self) -> Arc<AuthorityState> {
1493        self.this
1494            .upgrade()
1495            .expect("a borrowed authority state is alive")
1496    }
1497
1498    /// Like [`Self::handle_transaction_validation_checks`], but for
1499    /// post-consensus validation at the commit `reader` was built for: the
1500    /// inputs and receiving objects are read as of that commit through the
1501    /// commit-indexed reader. Returns `Drop` or `Missing` when one of them is
1502    /// dropped or not visible yet. Any other failed check returns the same
1503    /// `Err` as at admission, except that the coin deny list is read as of the
1504    /// reader's horizon, with changes counting from there rather than from the
1505    /// next epoch. The Move authenticator's account is not resolved and the
1506    /// authenticators are not run here: both happen at execution. Instead,
1507    /// each account must have had its authenticator function field at the
1508    /// horizon, or the transaction is reported missing. The package that
1509    /// field names, and its dependencies, read as of the horizon, are checked
1510    /// against the deny rules of the commit, and a denial is returned as
1511    /// [`ValidationAtCommit::AuthenticatorPackageDenied`], since it holds for
1512    /// every copy of the transaction. So an account whose function is changed
1513    /// to a denied package above the horizon is not caught here.
1514    ///
1515    /// An attested transaction (`attested`) runs the input checks execution
1516    /// runs, so a kept one cannot fail them at execution. It also runs the
1517    /// checks on receiving objects; the publish verifier stays at execution.
1518    #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
1519    pub(crate) fn handle_transaction_validation_checks_at_commit(
1520        &self,
1521        reader: &CommitIndexedReader,
1522        transaction: &VerifiedTransaction,
1523        epoch_store: &Arc<AuthorityPerEpochStore>,
1524        deny_config: &dyn DenyRuleConfig,
1525        verifier_limits_source: VerifierLimitsSource<'_>,
1526        attested: bool,
1527    ) -> IotaResult<ValidationAtCommit> {
1528        let protocol_config = epoch_store.protocol_config();
1529        let reference_gas_price = epoch_store.reference_gas_price();
1530
1531        let tx = transaction.data().transaction();
1532
1533        let (tx_input_objects, tx_receiving_objects, per_authenticator_inputs) = match self
1534            .read_objects_for_validation_at_commit(reader, transaction, protocol_config)?
1535        {
1536            ValidationInputsAtCommit::Loaded {
1537                tx_input_objects,
1538                tx_receiving_objects,
1539                per_authenticator_inputs,
1540            } => (
1541                tx_input_objects,
1542                tx_receiving_objects,
1543                per_authenticator_inputs,
1544            ),
1545            ValidationInputsAtCommit::Drop(kind, reason) => {
1546                return Ok(ValidationAtCommit::Drop(kind, reason));
1547            }
1548            ValidationInputsAtCommit::Missing(kind, reason) => {
1549                return Ok(ValidationAtCommit::Missing(kind, reason));
1550            }
1551        };
1552
1553        // The deny rules load packages through the reader. They run after the
1554        // inputs are loaded, so a call into a package not visible at this
1555        // commit has already answered missing instead of failing here as not
1556        // found.
1557        iota_transaction_checks::deny::check_transaction_for_validation(
1558            tx,
1559            transaction.signatures(),
1560            &transaction
1561                .input_objects(protocol_config.move_authenticator_type_arg_packages_as_inputs())?,
1562            &tx.receiving_objects(),
1563            deny_config,
1564            reader,
1565        )?;
1566
1567        let move_authenticators = transaction.move_authenticators();
1568
1569        // The account is not resolved and the authenticators are not run
1570        // here. Both read a shared object's contents, and a shared input names
1571        // no version, so this would read whatever version this validator
1572        // holds, which differs between validators. Execution reads every
1573        // shared input at the version consensus assigned, so both happen
1574        // there, and a failure there produces failure effects. What is left
1575        // here asks only what the rows can answer: whether each authenticator
1576        // input exists and is what the transaction declares, and whether each
1577        // account had its authenticator function field at the horizon. That
1578        // field also names the package checked against the deny rules below,
1579        // and an account without it is reported missing last.
1580        let (tx_checked_input_objects, per_authenticator_checked_inputs) = if attested {
1581            self.check_attested_transaction_inputs_at_commit(
1582                protocol_config,
1583                reference_gas_price,
1584                tx,
1585                tx_input_objects,
1586                &tx_receiving_objects,
1587                &move_authenticators,
1588                per_authenticator_inputs,
1589            )?
1590        } else {
1591            self.check_transaction_inputs_at_commit(
1592                protocol_config,
1593                reference_gas_price,
1594                tx,
1595                tx_input_objects,
1596                &tx_receiving_objects,
1597                &move_authenticators,
1598                per_authenticator_inputs,
1599                verifier_limits_source,
1600            )?
1601        };
1602
1603        let per_authenticator_checked_input_objects =
1604            per_authenticator_checked_inputs.iter().collect::<Vec<_>>();
1605
1606        debug_assert!(
1607            per_authenticator_checked_input_objects
1608                .iter()
1609                .all(|objects| objects.inner().filter_owned_objects().is_empty()),
1610            "Move authenticator input objects must not contain owned objects"
1611        );
1612
1613        // Each account's authenticator function field as of the horizon, read
1614        // once: the deny check below reads the package it names, and an
1615        // account without it is reported missing last.
1616        let accounts = move_authenticators
1617            .iter()
1618            .map(|move_authenticator| {
1619                let account_kind = move_authenticator
1620                    .object_to_authenticate()
1621                    .input_object_kind()
1622                    .expect("the object to authenticate is validated before consensus");
1623                Ok((account_kind, reader.read_account(account_kind.object_id())?))
1624            })
1625            .collect::<IotaResult<Vec<_>>>()?;
1626
1627        // The package holding each authenticate function, as at admission,
1628        // but named by the account's field as of the horizon, the same on
1629        // every validator.
1630        if protocol_config.deny_authenticator_packages() && deny_config.has_denied_packages() {
1631            let mut function_refs = Vec::new();
1632            for (account_kind, verdict) in &accounts {
1633                if let AccountVerdict::Account(field) = verdict {
1634                    function_refs.extend(
1635                        reader
1636                            .visible_authenticator_function_ref(account_kind.object_id(), field)?,
1637                    );
1638                }
1639            }
1640            if let Err(error) = iota_transaction_checks::deny::check_authenticator_packages(
1641                deny_config,
1642                &function_refs,
1643                reader,
1644            ) {
1645                if error.is_storage_or_epoch_error() {
1646                    return Err(error);
1647                }
1648                return Ok(ValidationAtCommit::AuthenticatorPackageDenied(error));
1649            }
1650        }
1651
1652        // The sender against the coin types of its inputs and receiving
1653        // objects, as at admission, but with the deny list as of the horizon,
1654        // the same on every validator. A kept receiving object was loaded at
1655        // the version the transaction names. A shared input's object is not
1656        // loaded, so its type is read on its own. Execution also checks the
1657        // coins Move code receives, and the coins it writes, against the deny
1658        // list as the epoch started.
1659        let mut coin_types = coin_types_for_denylist_check(
1660            tx_checked_input_objects
1661                .inner()
1662                .iter_objects()
1663                .chain(
1664                    per_authenticator_checked_input_objects
1665                        .iter()
1666                        .flat_map(|objects| objects.inner().iter_objects()),
1667                )
1668                .chain(tx_receiving_objects.iter_objects()),
1669        );
1670        for input in tx_checked_input_objects.inner().iter().chain(
1671            per_authenticator_checked_input_objects
1672                .iter()
1673                .flat_map(|objects| objects.inner().iter()),
1674        ) {
1675            if let (
1676                InputObjectKind::SharedMoveObject {
1677                    id,
1678                    initial_shared_version,
1679                    ..
1680                },
1681                None,
1682            ) = (input.input_object_kind, input.as_object())
1683            {
1684                coin_types.extend(reader.read_shared_coin_type(id, initial_shared_version)?);
1685            }
1686        }
1687        if let Some(denial) = DenyListAtCommit::new(reader).denial(tx.sender(), coin_types)? {
1688            return Err(denial.into());
1689        }
1690
1691        // Last, so a check that rejects the transaction outright is reported
1692        // rather than a missing verdict that invites a resubmission.
1693        for (account_kind, verdict) in accounts {
1694            if let AccountVerdict::Missing(reason) = verdict {
1695                return Ok(ValidationAtCommit::Missing(account_kind, reason));
1696            }
1697        }
1698
1699        Ok(ValidationAtCommit::Keep(
1700            tx_checked_input_objects.inner().filter_owned_objects(),
1701        ))
1702    }
1703
1704    /// This is a private method and should be kept that way. It doesn't check
1705    /// whether the provided transaction is a system transaction, and hence
1706    /// can only be called internally.
1707    async fn handle_transaction_impl(
1708        &self,
1709        transaction: VerifiedTransaction,
1710        epoch_store: &Arc<AuthorityPerEpochStore>,
1711    ) -> IotaResult<VerifiedSignedTransaction> {
1712        // Ensure that validator cannot reconfigure while we are signing the tx
1713        let _execution_lock = self.execution_lock_for_signing()?;
1714
1715        let owned_objects = self
1716            .handle_transaction_validation_checks(
1717                &transaction,
1718                epoch_store,
1719                &self.config.transaction_deny_config,
1720            )
1721            .await?;
1722        // After the checks above, so a permanent local error is reported ahead
1723        // of the retriable "not executed yet".
1724        self.check_transaction_dependency_for_signing(&transaction, epoch_store)?;
1725
1726        // Concurrent requests for the same transaction are signed once; the others
1727        // get the stored signature instead of signing and writing it again.
1728        let _signing_lock = epoch_store.acquire_signing_lock(transaction.digest());
1729        if let Some(signed_transaction) =
1730            epoch_store.get_signed_transaction(transaction.digest())?
1731        {
1732            self.metrics.tx_already_processed.inc();
1733            return Ok(signed_transaction);
1734        }
1735
1736        let epoch = epoch_store.epoch();
1737        let signed_transaction =
1738            VerifiedSignedTransaction::new(epoch, transaction, self.name, &*self.secret);
1739
1740        // Check and write locks, to signed transaction, into the database
1741        // The call to self.set_transaction_lock checks the lock is not conflicting,
1742        // and returns ConflictingTransaction error in case there is a lock on a
1743        // different existing transaction.
1744        self.get_cache_writer().try_acquire_transaction_locks(
1745            epoch_store,
1746            &owned_objects,
1747            signed_transaction.clone(),
1748        )?;
1749
1750        Ok(signed_transaction)
1751    }
1752
1753    /// Initiate a new transaction.
1754    #[instrument(name = "handle_transaction", level = "trace", skip_all, fields(tx_digest = ?transaction.digest(), sender = transaction.data().transaction().gas_owner().to_string()
1755    ))]
1756    pub async fn handle_transaction(
1757        &self,
1758        epoch_store: &Arc<AuthorityPerEpochStore>,
1759        transaction: VerifiedTransaction,
1760    ) -> IotaResult<HandleTransactionResponse> {
1761        let tx_digest = *transaction.digest();
1762        debug!("handle_transaction");
1763
1764        // Ensure an idempotent answer.
1765        if let Some((_, status)) = self.get_transaction_status(&tx_digest, epoch_store)? {
1766            return Ok(HandleTransactionResponse { status });
1767        }
1768
1769        let _metrics_guard = self
1770            .metrics
1771            .authority_state_handle_transaction_latency
1772            .start_timer();
1773        self.metrics.tx_orders.inc();
1774
1775        let signed = self.handle_transaction_impl(transaction, epoch_store).await;
1776        match signed {
1777            Ok(s) => {
1778                if self.is_committee_validator(epoch_store) {
1779                    if let Some(validator_tx_finalizer) = &self.validator_tx_finalizer {
1780                        let tx = s.clone();
1781                        let validator_tx_finalizer = validator_tx_finalizer.clone();
1782                        let cache_reader = self.get_transaction_cache_reader().clone();
1783                        let epoch_store = epoch_store.clone();
1784                        spawn_monitored_task!(epoch_store.within_alive_epoch(
1785                            validator_tx_finalizer.track_signed_tx(cache_reader, &epoch_store, tx)
1786                        ));
1787                    }
1788                }
1789                Ok(HandleTransactionResponse {
1790                    status: TransactionStatus::Signed(s.into_inner().into_sig()),
1791                })
1792            }
1793            // It happens frequently that while we are checking the validity of the transaction, it
1794            // has just been executed.
1795            // In that case, we could still return Ok to avoid showing confusing errors.
1796            Err(err) => Ok(HandleTransactionResponse {
1797                status: self
1798                    .get_transaction_status(&tx_digest, epoch_store)?
1799                    .ok_or(err)?
1800                    .1,
1801            }),
1802        }
1803    }
1804
1805    pub fn check_system_overload_at_signing(&self) -> bool {
1806        self.config
1807            .authority_overload_config
1808            .check_system_overload_at_signing
1809    }
1810
1811    pub fn check_system_overload_at_execution(&self) -> bool {
1812        self.config
1813            .authority_overload_config
1814            .check_system_overload_at_execution
1815    }
1816
1817    /// Checks system overload conditions before accepting a transaction in
1818    /// the certificate flow: authority overload (execution latency), the
1819    /// execution scheduler (execution queue), consensus adapter (queue
1820    /// limit), and writeback cache backpressure.
1821    ///
1822    /// The P-COOL flow uses
1823    /// [`AuthorityState::reserve_consensus_queue_slot()`] instead, which
1824    /// counts the transaction in the consensus queue and then checks only
1825    /// the queue and the limit on attestation dry runs, since
1826    /// execution-based overload is handled after consensus.
1827    pub(crate) fn check_system_overload(
1828        &self,
1829        consensus_adapter: &Arc<ConsensusAdapter>,
1830        tx: &SenderSignedTransaction,
1831        do_authority_overload_check: bool,
1832    ) -> IotaResult {
1833        if do_authority_overload_check {
1834            self.check_authority_overload(tx).tap_err(|_| {
1835                self.update_overload_metrics("execution_queue");
1836            })?;
1837        }
1838        self.execution_scheduler
1839            .check_execution_overload(self.overload_config(), tx)
1840            .tap_err(|_| {
1841                self.update_overload_metrics("execution_pending");
1842            })?;
1843        consensus_adapter.check_consensus_overload().tap_err(|_| {
1844            self.update_overload_metrics("consensus");
1845        })?;
1846
1847        let pending_tx_count = self
1848            .get_cache_commit()
1849            .approximate_pending_transaction_count();
1850        if pending_tx_count
1851            > self
1852                .config
1853                .execution_cache_config
1854                .writeback_cache
1855                .backpressure_threshold_for_rpc()
1856        {
1857            return Err(IotaError::ValidatorOverloadedRetryAfter {
1858                retry_after_secs: 10,
1859            });
1860        }
1861
1862        Ok(())
1863    }
1864
1865    /// Takes a P-COOL transaction's [`InflightSlot`] in the consensus queue,
1866    /// then runs three checks: graduated shedding against
1867    /// [`InflightSlot::queue_length_before()`], which also counts admissions
1868    /// still in progress; the permit check of
1869    /// [`ConsensusAdapter::check_submit_permits()`]; and the check of
1870    /// [`AttestationSemaphore::check_overload()`] against the limit on
1871    /// attestation dry runs. Returns the slot, to be passed to
1872    /// [`ConsensusAdapter::submit_with_slot()`]; on an error, the slot is
1873    /// released.
1874    ///
1875    /// # Errors
1876    ///
1877    /// [`IotaError::ValidatorOverloadedRetryAfter`] when graduated shedding
1878    /// rejects the transaction between the soft and hard limits, or when every
1879    /// attestation dry run is taken and the queue for them is full or the
1880    /// expected wait for one too long, and
1881    /// [`IotaError::TooManyTransactionsPendingConsensus`] when the consensus
1882    /// queue has reached `max_pending_transactions` or `submit_semaphore` has
1883    /// no permits left.
1884    pub(crate) fn reserve_consensus_queue_slot(
1885        &self,
1886        consensus_adapter: &Arc<ConsensusAdapter>,
1887        tx: &SenderSignedTransaction,
1888    ) -> IotaResult<InflightSlot> {
1889        let slot = consensus_adapter.reserve_inflight_slot();
1890
1891        // Graduated shedding: 0% to 100% as the consensus queue fills from the
1892        // soft to the hard limit.
1893        self.check_consensus_queue_graduated_limits(
1894            consensus_adapter,
1895            tx,
1896            slot.queue_length_before() as usize,
1897        )
1898        .tap_err(|_| {
1899            self.update_overload_metrics("consensus");
1900        })?;
1901
1902        // Graduated shedding at 100% already rejects everything at or above
1903        // `max_pending_transactions`, judged by the queue before this slot.
1904        // `check_consensus_overload()` would count the queue as it is now,
1905        // including slots taken after this one, and reject transactions that
1906        // had room, so only the `submit_semaphore` permits are checked: a
1907        // separate concurrency limit that graduated shedding does not cover.
1908        consensus_adapter.check_submit_permits().tap_err(|_| {
1909            self.update_overload_metrics("consensus");
1910        })?;
1911
1912        self.attestation_semaphore.check_overload().tap_err(|_| {
1913            self.update_overload_metrics("attestation");
1914        })?;
1915
1916        Ok(slot)
1917    }
1918
1919    /// Returns a permit for one attestation dry run, waiting for a free one
1920    /// within the limits of
1921    /// [`AuthorityOverloadConfig::max_concurrent_attestations`],
1922    /// [`AuthorityOverloadConfig::max_queued_attestations`] and
1923    /// [`AuthorityOverloadConfig::max_attestation_wait`]. The permit must be
1924    /// held until the dry run has ended and the transaction's soft locks are
1925    /// taken.
1926    ///
1927    /// # Errors
1928    ///
1929    /// [`IotaError::ValidatorOverloadedRetryAfter`] when every dry run is
1930    /// taken and the queue for them is full, or when none frees within
1931    /// [`AuthorityOverloadConfig::max_attestation_wait`].
1932    pub(crate) async fn acquire_attestation_permit(&self) -> IotaResult<AttestationPermit> {
1933        self.attestation_semaphore.acquire().await.tap_err(|_| {
1934            self.update_overload_metrics("attestation");
1935        })
1936    }
1937
1938    /// Rejects `tx` via graduated shedding at `queue_length`, the number of
1939    /// other transactions in the consensus queue. Scales from 0% at the soft
1940    /// limit to 100% at `max_pending_transactions`. Returns
1941    /// [`IotaError::ValidatorOverloadedRetryAfter`] for probabilistic
1942    /// rejection (shedding percentage < 100%, via
1943    /// [`overload_monitor_accept_tx()`]) or
1944    /// [`IotaError::TooManyTransactionsPendingConsensus`] for unconditional
1945    /// rejection (shedding percentage >= 100%). Updates the
1946    /// `consensus_queue_load_shedding_percentage` metric.
1947    fn check_consensus_queue_graduated_limits(
1948        &self,
1949        consensus_adapter: &Arc<ConsensusAdapter>,
1950        tx: &SenderSignedTransaction,
1951        queue_length: usize,
1952    ) -> IotaResult {
1953        let shedding_pct = compute_graduated_load_shedding_percentage(
1954            queue_length,
1955            consensus_adapter.max_pending_transactions(),
1956            consensus_adapter.graduated_load_shedding_soft_limit_pct(),
1957        );
1958
1959        self.metrics
1960            .consensus_queue_load_shedding_percentage
1961            .set(shedding_pct as i64);
1962
1963        if shedding_pct == 0 {
1964            return Ok(());
1965        }
1966
1967        // At/above the hard limit, rejection is unconditional (not
1968        // probabilistic), so the seed-rotation retry hint of
1969        // `ValidatorOverloadedRetryAfter` doesn't apply - return the
1970        // capacity-bound error instead.
1971        if shedding_pct >= 100 {
1972            return Err(IotaError::TooManyTransactionsPendingConsensus);
1973        }
1974
1975        overload_monitor_accept_tx(shedding_pct, tx.digest())
1976    }
1977
1978    fn check_authority_overload(&self, tx: &SenderSignedTransaction) -> IotaResult {
1979        if !self.overload_info.is_overload.load(Ordering::Relaxed) {
1980            return Ok(());
1981        }
1982
1983        let load_shedding_percentage = self
1984            .overload_info
1985            .local_load_shedding_percentage
1986            .load(Ordering::Relaxed);
1987        overload_monitor_accept_tx(load_shedding_percentage, tx.digest())
1988    }
1989
1990    fn update_overload_metrics(&self, source: &str) {
1991        self.metrics
1992            .transaction_overload_sources
1993            .with_label_values(&[source])
1994            .inc();
1995    }
1996
1997    /// Wait for a certificate to be executed.
1998    /// For consensus transactions, it needs to be sequenced by the consensus.
1999    /// For owned object transactions, this function will enqueue the
2000    /// transaction for execution.
2001    #[instrument(level = "trace", skip_all)]
2002    pub async fn wait_for_certificate_execution(
2003        &self,
2004        certificate: &VerifiedCertificate,
2005        epoch_store: &Arc<AuthorityPerEpochStore>,
2006    ) -> IotaResult<TransactionEffects> {
2007        let _metrics_guard = if certificate.contains_shared_object() {
2008            self.metrics
2009                .execute_certificate_latency_shared_object
2010                .start_timer()
2011        } else {
2012            self.metrics
2013                .execute_certificate_latency_single_writer
2014                .start_timer()
2015        };
2016        trace!("wait_for_certificate_execution");
2017
2018        self.metrics.total_cert_attempts.inc();
2019
2020        if !certificate.contains_shared_object() {
2021            // Shared object transactions need to be sequenced by the consensus before
2022            // enqueueing for execution, done in
2023            // AuthorityPerEpochStore::handle_consensus_transaction(). For owned
2024            // object transactions, they can be enqueued for execution immediately.
2025            self.execution_scheduler.enqueue(
2026                vec![(
2027                    Schedulable::Transaction(VerifiedExecutableTransaction::new_from_certificate(
2028                        certificate.clone(),
2029                    )),
2030                    ExecutionEnv::new(),
2031                )],
2032                epoch_store,
2033            );
2034        }
2035
2036        // tx could be reverted when epoch ends, so we must be careful not to return a
2037        // result here after the epoch ends.
2038        epoch_store
2039            .within_alive_epoch(self.notify_read_effects(
2040                "AuthorityState::wait_for_certificate_execution",
2041                certificate,
2042            ))
2043            .await
2044            .and_then(|r| r)
2045    }
2046
2047    /// Internal logic to execute a transaction.
2048    ///
2049    /// Guarantees that
2050    /// - If input objects are available, return no permanent failure.
2051    /// - Execution and output commit are atomic. i.e. outputs are only written to storage,
2052    /// on successful execution; crashed execution has no observable effect and
2053    /// can be retried.
2054    ///
2055    /// It is caller's responsibility to ensure input objects are available,
2056    /// the transaction named by the transaction dependency input has executed
2057    /// unless this one was cancelled at sequencing, and locks are set. If this
2058    /// cannot be satisfied by the caller, `wait_for_certificate_execution()`
2059    /// should be called instead.
2060    ///
2061    /// Should only be called within iota-core.
2062    ///
2063    /// # Panics
2064    ///
2065    /// If an input object or the effects of that dependency are absent.
2066    #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
2067    pub fn try_execute_immediately(
2068        &self,
2069        transaction: &VerifiedExecutableTransaction,
2070        execution_env: ExecutionEnv,
2071        epoch_store: &Arc<AuthorityPerEpochStore>,
2072    ) -> IotaResult<(TransactionEffects, Option<ExecutionError>)> {
2073        let _scope = monitored_scope("Execution::try_execute_immediately");
2074        let _metrics_guard = self.metrics.internal_execution_latency.start_timer();
2075
2076        let tx_digest = transaction.digest();
2077
2078        // Acquire a lock to prevent concurrent executions of the same transaction.
2079        let tx_guard = epoch_store.acquire_tx_guard(transaction)?;
2080
2081        // The transaction could have been processed by a concurrent attempt of the
2082        // same transaction, so check if the effects have already been written.
2083        if let Some(effects) = self
2084            .get_transaction_cache_reader()
2085            .try_get_executed_effects(tx_digest)?
2086        {
2087            if let Some(expected_effects_digest_inner) = execution_env.expected_effects_digest {
2088                assert_eq!(
2089                    effects.digest(),
2090                    expected_effects_digest_inner,
2091                    "Unexpected effects digest for transaction {tx_digest}"
2092                );
2093            }
2094            tx_guard.release();
2095            return Ok((effects, None));
2096        }
2097
2098        let (tx_input_objects, per_authenticator_inputs) = self.read_objects_for_execution(
2099            tx_guard.as_lock_guard(),
2100            transaction,
2101            execution_env.assigned_versions,
2102            epoch_store,
2103        )?;
2104
2105        self.process_transaction(
2106            tx_guard,
2107            transaction,
2108            tx_input_objects,
2109            per_authenticator_inputs,
2110            execution_env.expected_effects_digest,
2111            &execution_env.attestation,
2112            epoch_store,
2113        )
2114        .tap_err(|e| info!(?tx_digest, "process_transaction failed: {e}"))
2115        .tap_ok(
2116            |(fx, _)| debug!(?tx_digest, fx_digest=?fx.digest(), "process_transaction succeeded"),
2117        )
2118    }
2119
2120    /// Reads the input objects of `transaction` at the versions in
2121    /// `assigned_shared_object_versions`, together with the outcome of the
2122    /// transaction named by its transaction dependency input unless
2123    /// `transaction` was cancelled at sequencing.
2124    ///
2125    /// # Panics
2126    ///
2127    /// If an input object or the effects of that dependency are absent. The
2128    /// execution scheduler releases a transaction only once both are
2129    /// available; other callers must make sure of it themselves.
2130    pub fn read_objects_for_execution(
2131        &self,
2132        tx_lock: &TxLockGuard,
2133        transaction: &VerifiedExecutableTransaction,
2134        assigned_shared_object_versions: AssignedVersions,
2135        epoch_store: &Arc<AuthorityPerEpochStore>,
2136    ) -> IotaResult<(InputObjects, Vec<(InputObjects, ObjectReadResult)>)> {
2137        let _scope = monitored_scope("Execution::load_input_objects");
2138        let _metrics_guard = self
2139            .metrics
2140            .execution_load_input_objects_latency
2141            .start_timer();
2142
2143        let input_objects = transaction.collect_all_input_object_kind_for_reading(
2144            epoch_store
2145                .protocol_config()
2146                .move_authenticator_type_arg_packages_as_inputs(),
2147        )?;
2148
2149        let mut input_objects = self.input_loader.read_objects_for_execution(
2150            &transaction.key(),
2151            tx_lock,
2152            &input_objects,
2153            &assigned_shared_object_versions,
2154            epoch_store.epoch(),
2155        )?;
2156
2157        // Read exactly when the scheduler waited for it, so that its effects
2158        // are present here.
2159        if let Some(InputKey::ExecutedTransaction { digest: dependency }) =
2160            transaction_dependency_input_key(transaction, &assigned_shared_object_versions)
2161        {
2162            let effects = self
2163                .get_transaction_cache_reader()
2164                .get_executed_effects(&dependency)
2165                .unwrap_or_else(|| {
2166                    panic!(
2167                        "transaction {:?} depends on transaction {dependency}, whose effects \
2168                         are absent although the scheduler released the dependent",
2169                        transaction.digest()
2170                    )
2171                });
2172            input_objects.set_transaction_dependency(TransactionDependencyReadResult {
2173                digest: dependency,
2174                succeeded: effects.status().is_success(),
2175            });
2176        }
2177
2178        transaction.split_input_objects_into_groups_for_reading(input_objects)
2179    }
2180
2181    /// Checks the outcome of the transaction named by `transaction`'s
2182    /// dependency input, when this node knows one: an error if the dependency
2183    /// failed, was cancelled, or executed in an earlier epoch. Returns the
2184    /// dependency's digest when it has not executed here yet, `None` when it
2185    /// has or `transaction` has no dependency input.
2186    pub fn check_transaction_dependency_known_outcome(
2187        &self,
2188        transaction: &Transaction,
2189        current_epoch: EpochId,
2190    ) -> IotaResult<Option<TransactionDigest>> {
2191        let Some(digest) = transaction.transaction_dependency() else {
2192            return Ok(None);
2193        };
2194        let Some(effects) = self
2195            .get_transaction_cache_reader()
2196            .try_get_executed_effects(&digest)?
2197        else {
2198            return Ok(Some(digest));
2199        };
2200        if !effects.status().is_success() {
2201            return Err(UserInputError::TransactionDependencyFailed { digest }.into());
2202        }
2203        if effects.epoch() < current_epoch {
2204            return Err(UserInputError::TransactionDependencyNotInCurrentEpoch {
2205                digest,
2206                executed_epoch: effects.epoch(),
2207            }
2208            .into());
2209        }
2210        Ok(None)
2211    }
2212
2213    /// The error to report when `transaction`'s Move authentication failed at
2214    /// admission with `error`: the error of
2215    /// [`Self::check_transaction_dependency_known_outcome`] if it returns one;
2216    /// the retriable `TransactionDependencyNotFound` while the dependency has
2217    /// not executed here, since the authenticators ran without its writes;
2218    /// `error` otherwise.
2219    fn authentication_failure(
2220        &self,
2221        transaction: &Transaction,
2222        current_epoch: EpochId,
2223        error: IotaError,
2224    ) -> IotaError {
2225        match self.check_transaction_dependency_known_outcome(transaction, current_epoch) {
2226            Ok(Some(digest)) => UserInputError::TransactionDependencyNotFound { digest }.into(),
2227            Ok(None) => error,
2228            Err(dependency_error) => dependency_error,
2229        }
2230    }
2231
2232    /// Checks the transaction named by `transaction`'s dependency input before
2233    /// signing it in the certificate flow: it must have executed successfully
2234    /// in the current epoch on this validator. Not executed here yet is
2235    /// reported with a retriable error, so the client's driver retries once it
2236    /// has.
2237    fn check_transaction_dependency_for_signing(
2238        &self,
2239        transaction: &VerifiedTransaction,
2240        epoch_store: &AuthorityPerEpochStore,
2241    ) -> IotaResult<()> {
2242        if let Some(digest) = self.check_transaction_dependency_known_outcome(
2243            transaction.data().transaction(),
2244            epoch_store.epoch(),
2245        )? {
2246            return Err(UserInputError::TransactionDependencyNotFound { digest }.into());
2247        }
2248        Ok(())
2249    }
2250
2251    /// Test only wrapper for `try_execute_immediately()` above, useful for
2252    /// checking errors if the pre-conditions are not satisfied, and
2253    /// executing change epoch transactions. With the P-COOL flow, a committee
2254    /// validator executes only transactions of commits its consensus handler
2255    /// assigned, so a transaction the handler has not assigned is assigned to
2256    /// a new commit first.
2257    pub fn try_execute_for_test(
2258        &self,
2259        certificate: &VerifiedCertificate,
2260        execution_env: ExecutionEnv,
2261    ) -> IotaResult<(VerifiedSignedTransactionEffects, Option<ExecutionError>)> {
2262        let epoch_store = self.epoch_store_for_testing();
2263        epoch_store.assign_to_a_new_commit_for_testing(&[certificate.key()]);
2264        let (effects, execution_error_opt) = self.try_execute_immediately(
2265            &VerifiedExecutableTransaction::new_from_certificate(certificate.clone()),
2266            execution_env,
2267            &epoch_store,
2268        )?;
2269        let signed_effects = self.sign_effects(effects, &epoch_store)?;
2270        Ok((signed_effects, execution_error_opt))
2271    }
2272
2273    /// Non-fallible version of `try_execute_for_test()`.
2274    pub fn execute_for_test(
2275        &self,
2276        certificate: &VerifiedCertificate,
2277        execution_env: ExecutionEnv,
2278    ) -> (VerifiedSignedTransactionEffects, Option<ExecutionError>) {
2279        self.try_execute_for_test(certificate, execution_env)
2280            .expect("try_execute_for_test should not fail")
2281    }
2282
2283    pub async fn notify_read_effects(
2284        &self,
2285        task_name: &'static str,
2286        certificate: &VerifiedCertificate,
2287    ) -> IotaResult<TransactionEffects> {
2288        self.get_transaction_cache_reader()
2289            .try_notify_read_executed_effects(task_name, &[*certificate.digest()])
2290            .await
2291            .map(|mut r| r.pop().expect("must return correct number of effects"))
2292    }
2293
2294    fn check_owned_locks(&self, owned_object_refs: &[ObjectReference]) -> IotaResult {
2295        self.get_object_cache_reader()
2296            .try_check_owned_objects_are_live(owned_object_refs)
2297    }
2298
2299    /// This function captures the required state to debug a forked transaction.
2300    /// The dump is written to a file in dir `path`, with name prefixed by the
2301    /// transaction digest. NOTE: Since this info escapes the validator
2302    /// context, make sure not to leak any private info here
2303    pub(crate) fn debug_dump_transaction_state(
2304        &self,
2305        tx_digest: &TransactionDigest,
2306        effects: &TransactionEffects,
2307        expected_effects_digest: TransactionEffectsDigest,
2308        inner_temporary_store: &InnerTemporaryStore,
2309        transaction: &VerifiedExecutableTransaction,
2310        transaction_dependency: Option<TransactionDependencyReadResult>,
2311        debug_dump_config: &StateDebugDumpConfig,
2312    ) -> IotaResult<PathBuf> {
2313        // Fall back to the OS temp directory if no dump directory is configured.
2314        // This is safe: dump files are named by transaction digest, so no collisions.
2315        let dump_dir = debug_dump_config
2316            .dump_file_directory
2317            .as_ref()
2318            .cloned()
2319            .unwrap_or(std::env::temp_dir());
2320        let epoch_store = self.load_epoch_store_one_call_per_task();
2321
2322        NodeStateDump::new(
2323            tx_digest,
2324            effects,
2325            expected_effects_digest,
2326            self.get_object_store().as_ref(),
2327            &epoch_store,
2328            inner_temporary_store,
2329            transaction,
2330            transaction_dependency,
2331        )?
2332        .write_to_file(&dump_dir)
2333        .map_err(|e| IotaError::FileIO(e.to_string()))
2334    }
2335
2336    #[instrument(name = "process_certificate", level = "trace", skip_all, fields(tx_digest = ?transaction.digest(), sender = ?transaction.data().transaction().gas_owner().to_string()))]
2337    pub(crate) fn process_transaction(
2338        &self,
2339        tx_guard: TxGuard,
2340        transaction: &VerifiedExecutableTransaction,
2341        tx_input_objects: InputObjects,
2342        per_authenticator_inputs: Vec<(InputObjects, ObjectReadResult)>,
2343        expected_effects_digest: Option<TransactionEffectsDigest>,
2344        attestation: &AttestationSource,
2345        epoch_store: &Arc<AuthorityPerEpochStore>,
2346    ) -> IotaResult<(TransactionEffects, Option<ExecutionError>)> {
2347        let process_transaction_start_time = tokio::time::Instant::now();
2348        let digest = *transaction.digest();
2349
2350        let _scope = monitored_scope("Execution::process_certificate");
2351
2352        fail_point_if!("correlated-crash-process-transaction", || {
2353            if iota_simulator::random::deterministic_probability_once(digest, 0.01) {
2354                iota_simulator::task::kill_current_node(None);
2355            }
2356        });
2357
2358        let execution_guard = self.execution_lock_for_executable_transaction(transaction);
2359        // Any caller that verifies the signatures on the transaction will have already
2360        // checked the epoch. But paths that don't verify sigs (e.g. execution
2361        // from checkpoint, reading from db) present the possibility of an epoch
2362        // mismatch. If this transaction is not finalized in previous epoch, then it's
2363        // invalid.
2364        let execution_guard = match execution_guard {
2365            Ok(execution_guard) => execution_guard,
2366            Err(err) => {
2367                tx_guard.release();
2368                return Err(err);
2369            }
2370        };
2371        // Since we obtain a reference to the epoch store before taking the execution
2372        // lock, it's possible that reconfiguration has happened and they no
2373        // longer match.
2374        if *execution_guard != epoch_store.epoch() {
2375            tx_guard.release();
2376            info!("The epoch of the execution_guard doesn't match the epoch store");
2377            return Err(IotaError::WrongEpoch {
2378                expected_epoch: epoch_store.epoch(),
2379                actual_epoch: *execution_guard,
2380            });
2381        }
2382
2383        // Errors originating from `execute_transaction` may be transient (failure to
2384        // read locks) or non-transient (transaction input is invalid, move vm
2385        // errors). However, all errors from this function occur before we have
2386        // written anything to the db, so we commit the tx guard and rely on the
2387        // client to retry the tx (if it was transient).
2388        let transaction_dependency = tx_input_objects.transaction_dependency();
2389        let (inner_temporary_store, effects, execution_error_opt, read_objects, attestation_record) =
2390            match self.execute_transaction(
2391                &execution_guard,
2392                transaction,
2393                tx_input_objects,
2394                per_authenticator_inputs,
2395                attestation,
2396                epoch_store,
2397            ) {
2398                Err(e) => {
2399                    info!(name = ?self.name, ?digest, "Error preparing transaction: {e}");
2400                    tx_guard.release();
2401                    return Err(e);
2402                }
2403                Ok(res) => res,
2404            };
2405
2406        if let Some(expected_effects_digest) = expected_effects_digest {
2407            if effects.digest() != expected_effects_digest {
2408                // We dont want to mask the original error, so we log it and continue.
2409                match self.debug_dump_transaction_state(
2410                    &digest,
2411                    &effects,
2412                    expected_effects_digest,
2413                    &inner_temporary_store,
2414                    transaction,
2415                    transaction_dependency,
2416                    &self.config.state_debug_dump_config,
2417                ) {
2418                    Ok(out_path) => {
2419                        info!(
2420                            "Dumped node state for transaction {} to {}",
2421                            digest,
2422                            out_path.as_path().display().to_string()
2423                        );
2424                    }
2425                    Err(e) => {
2426                        error!("Error dumping state for transaction {}: {e}", digest);
2427                    }
2428                }
2429                error!(
2430                    tx_digest = ?digest,
2431                    ?expected_effects_digest,
2432                    actual_effects = ?effects,
2433                    "fork detected!"
2434                );
2435                panic!(
2436                    "Transaction {} is expected to have effects digest {}, but got {}!",
2437                    digest,
2438                    expected_effects_digest,
2439                    effects.digest(),
2440                );
2441            }
2442        }
2443
2444        fail_point!("crash");
2445
2446        self.commit_transaction(
2447            transaction,
2448            inner_temporary_store,
2449            &effects,
2450            read_objects,
2451            attestation_record,
2452            tx_guard,
2453            execution_guard,
2454            expected_effects_digest,
2455            epoch_store,
2456        )?;
2457
2458        let elapsed = process_transaction_start_time.elapsed().as_micros() as f64;
2459        if elapsed > 0.0 {
2460            self.metrics
2461                .execution_gas_latency_ratio
2462                .observe(effects.gas_cost_summary().computation_cost as f64 / elapsed);
2463        };
2464        Ok((effects, execution_error_opt))
2465    }
2466
2467    pub async fn reconfigure_traffic_control(
2468        &self,
2469        params: TrafficControlReconfigParams,
2470    ) -> Result<TrafficControlReconfigParams, IotaError> {
2471        if let Some(traffic_controller) = self.traffic_controller.as_ref() {
2472            traffic_controller.admin_reconfigure(params)
2473        } else {
2474            Err(IotaError::InvalidAdminRequest(
2475                "Traffic controller is not configured on this node".to_string(),
2476            ))
2477        }
2478    }
2479
2480    #[instrument(level = "trace", skip_all)]
2481    fn commit_transaction(
2482        &self,
2483        transaction: &VerifiedExecutableTransaction,
2484        inner_temporary_store: InnerTemporaryStore,
2485        effects: &TransactionEffects,
2486        read_objects: ObjectSet,
2487        attestation_record: Option<AttestationRecord>,
2488        tx_guard: TxGuard,
2489        _execution_guard: ExecutionLockReadGuard<'_>,
2490        expected_effects_digest: Option<TransactionEffectsDigest>,
2491        epoch_store: &Arc<AuthorityPerEpochStore>,
2492    ) -> IotaResult {
2493        let _scope: Option<iota_metrics::MonitoredScopeGuard> =
2494            monitored_scope("Execution::commit_certificate");
2495        let _metrics_guard = self.metrics.commit_certificate_latency.start_timer();
2496
2497        let tx_digest = transaction.digest();
2498        if let (
2499            Some(record),
2500            ExecutionStatus::Failure {
2501                error:
2502                    ExecutionErrorKind::ExecutionCanceledDueToSharedObjectCongestion { .. }
2503                    | ExecutionErrorKind::ExecutionCanceledDueToSharedObjectCongestionV2 { .. }
2504                    | ExecutionErrorKind::ExecutionCanceledDueToExecutionWorkerCongestion { .. },
2505                ..
2506            },
2507        ) = (&attestation_record, effects.status())
2508        {
2509            // The attestor is an authority index, which does not name the same
2510            // validator across epochs. `committee_validators()` is not in that
2511            // order, so the validator is found by its name.
2512            let hostname = epoch_store
2513                .committee()
2514                .authority_by_index(u32::from(record.attestor))
2515                .and_then(|name| {
2516                    epoch_store
2517                        .epoch_start_state()
2518                        .committee_validators()
2519                        .iter()
2520                        .find(|validator| validator.authority_name() == *name)
2521                })
2522                .map(|validator| validator.hostname.as_str());
2523            let charged = effects.gas_cost_summary().gas_used() > 0;
2524            if let Some(hostname) = hostname {
2525                self.metrics
2526                    .cancelled_attested_transactions
2527                    .with_label_values(&[hostname, &charged.to_string()])
2528                    .inc();
2529            }
2530            debug!(
2531                ?tx_digest,
2532                attestor = record.attestor,
2533                ?hostname,
2534                charged,
2535                "attested transaction cancelled for congestion"
2536            );
2537        }
2538        let input_object_count = inner_temporary_store.input_objects.len();
2539        let shared_object_count = effects.input_shared_objects().len();
2540
2541        let output_keys = inner_temporary_store.get_output_keys(effects);
2542
2543        // The insertion to epoch_store is not atomic with the insertion to the
2544        // perpetual store. This is OK because we insert to the epoch store
2545        // first. And during lookups we always look up in the perpetual store first.
2546        epoch_store.insert_executed_in_epoch(tx_digest);
2547
2548        let key = transaction.key();
2549        if !matches!(key, TransactionKey::Digest(_)) {
2550            epoch_store.insert_tx_key(key, *tx_digest)?;
2551        }
2552
2553        // Genesis and the change-epoch transaction are roots of no consensus
2554        // commit, so the handler never assigns them one and recording them
2555        // would stop the node. Without rows, the reader takes what they wrote
2556        // as epoch-start state: genesis writes the first epoch's, the
2557        // change-epoch transaction the next epoch's. The migration transactions
2558        // executed at epoch 0 are genesis transactions too: node startup first
2559        // re-executes each one through `execute_genesis_transaction`, which
2560        // asserts its kind, to collect the genesis objects.
2561        if epoch_store.pcool_bookkeeping_enabled()
2562            && !transaction.transaction().is_genesis_tx()
2563            && !transaction.transaction().is_end_of_epoch_tx()
2564        {
2565            epoch_store.record_executed_transaction(&key, effects);
2566        }
2567
2568        // Allow testing what happens if we crash here.
2569        fail_point!("crash");
2570
2571        let transaction_outputs = TransactionOutputs::build_transaction_outputs(
2572            transaction.clone().into_unsigned(),
2573            effects.clone(),
2574            inner_temporary_store,
2575            read_objects,
2576            &self.metrics,
2577            attestation_record,
2578        );
2579        self.get_cache_writer()
2580            .try_write_transaction_outputs(epoch_store.epoch(), transaction_outputs.into())?;
2581
2582        self.report_failed_deny_rule_update_execution(
2583            transaction,
2584            effects,
2585            expected_effects_digest,
2586            epoch_store,
2587        );
2588
2589        if transaction.transaction().is_end_of_epoch_tx() {
2590            // At the end of epoch, since system packages may have been upgraded, force
2591            // reload them in the cache.
2592            self.get_object_cache_reader()
2593                .force_reload_system_packages(&BuiltInFramework::all_package_ids());
2594        }
2595
2596        // `commit_transaction()` finished, the tx is fully committed to the store.
2597        tx_guard.commit_tx();
2598
2599        match self.execution_scheduler.as_ref() {
2600            ExecutionSchedulerWrapper::ExecutionScheduler(_) => {}
2601            ExecutionSchedulerWrapper::TransactionManager(tm) => {
2602                // Notifies transaction manager about transaction and output objects committed.
2603                // This provides necessary information to transaction manager to start executing
2604                // additional ready transactions.
2605                tm.notify_commit(tx_digest, output_keys, epoch_store);
2606                // A transaction with a non-digest key can execute from a synced
2607                // checkpoint, in which case local randomness generation — the only
2608                // other caller of `notify_transaction_key` — never runs for that
2609                // round and would leave the env parked under its key forever. The
2610                // enqueue this triggers is filtered out as already executed.
2611                if let Some(key) = transaction.non_digest_key() {
2612                    tm.notify_transaction_key(epoch_store, key, *tx_digest);
2613                }
2614            }
2615        }
2616
2617        self.update_metrics(transaction, input_object_count, shared_object_count);
2618
2619        Ok(())
2620    }
2621
2622    fn update_metrics(
2623        &self,
2624        transaction: &VerifiedExecutableTransaction,
2625        input_object_count: usize,
2626        shared_object_count: usize,
2627    ) {
2628        // count signature by scheme, for multisig
2629        if transaction.has_multisig() {
2630            self.metrics.multisig_sig_count.inc();
2631        }
2632
2633        self.metrics.total_effects.inc();
2634        self.metrics.total_certs.inc();
2635
2636        if shared_object_count > 0 {
2637            self.metrics.shared_obj_tx.inc();
2638        }
2639
2640        if transaction.is_sponsored_tx() {
2641            self.metrics.sponsored_tx.inc();
2642        }
2643
2644        self.metrics
2645            .num_input_objs
2646            .observe(input_object_count as f64);
2647        self.metrics
2648            .num_shared_objects
2649            .observe(shared_object_count as f64);
2650        self.metrics
2651            .batch_size
2652            .observe(transaction.data().transaction().kind().num_commands() as f64);
2653    }
2654
2655    /// `execute_transaction()` validates the transaction input, and executes
2656    /// the transaction, returning effects, output objects, events, etc.
2657    ///
2658    /// It reads state from the db (both owned and shared locks), but it has no
2659    /// side effects.
2660    ///
2661    /// It can be generally understood that a failure of `execute_transaction`
2662    /// indicates a non-transient error, e.g. the transaction input is
2663    /// somehow invalid, the correct locks are not held, etc. However, this
2664    /// is not entirely true, as a transient db read error may also cause
2665    /// this function to fail.
2666    #[instrument(level = "trace", skip_all)]
2667    fn execute_transaction(
2668        &self,
2669        _execution_guard: &ExecutionLockReadGuard<'_>,
2670        transaction: &VerifiedExecutableTransaction,
2671        tx_input_objects: InputObjects,
2672        per_authenticator_inputs: Vec<(InputObjects, ObjectReadResult)>,
2673        attestation: &AttestationSource,
2674        epoch_store: &Arc<AuthorityPerEpochStore>,
2675    ) -> IotaResult<TransactionExecutionResult> {
2676        let _scope = monitored_scope("Execution::execute_certificate");
2677        let _metrics_guard = self.metrics.prepare_certificate_latency.start_timer();
2678        let prepare_transaction_start_time = tokio::time::Instant::now();
2679
2680        let protocol_config = epoch_store.protocol_config();
2681
2682        let reference_gas_price = epoch_store.reference_gas_price();
2683
2684        let epoch_id = epoch_store.epoch_start_config().epoch_data().epoch_id();
2685        let epoch_start_timestamp = epoch_store
2686            .epoch_start_config()
2687            .epoch_data()
2688            .epoch_start_timestamp();
2689
2690        let tracking_store = TrackingBackingStore::new(self.get_backing_store().as_ref());
2691        let backing_store = &tracking_store;
2692
2693        let tx_digest = *transaction.digest();
2694
2695        // TODO: We need to move this to a more appropriate place to avoid redundant
2696        // checks.
2697        let tx = transaction.data().transaction();
2698        tx.validity_check(protocol_config)?;
2699
2700        // The user signature of an attested transaction is verified pre-consensus
2701        // in the block verifier (`IotaTxValidator::validate_transactions`), exactly
2702        // as for an unattested one, and is not re-checked here.
2703
2704        let (kind, signer, gas_data) = tx.execution_parts();
2705
2706        let move_authenticators = transaction.move_authenticators();
2707
2708        #[cfg_attr(not(any(msim, fail_points)), expect(unused_mut))]
2709        let (inner_temp_store, mut effects, execution_error, outcome) = if move_authenticators
2710            .is_empty()
2711        {
2712            // No Move authentication required, proceed to execute the transaction directly.
2713
2714            // The cost of partially re-auditing a transaction before execution is
2715            // tolerated.
2716            let (tx_gas_status, tx_checked_input_objects) =
2717                iota_transaction_checks::check_certificate_input(
2718                    transaction,
2719                    tx_input_objects,
2720                    protocol_config,
2721                    reference_gas_price,
2722                )?;
2723
2724            let owned_object_refs = tx_checked_input_objects.inner().filter_owned_objects();
2725            self.check_owned_locks(&owned_object_refs)?;
2726            let (inner_temp_store, _gas_status, effects, _timings, execution_error_opt) =
2727                epoch_store.executor().execute_transaction_to_effects(
2728                    backing_store,
2729                    protocol_config,
2730                    self.metrics.limits_metrics.clone(),
2731                    // TODO: would be nice to pass the whole NodeConfig here, but it creates a
2732                    // cyclic dependency w/ iota-adapter
2733                    self.config
2734                        .expensive_safety_check_config
2735                        .enable_deep_per_tx_iota_conservation_check(),
2736                    self.config.certificate_deny_config.certificate_deny_set(),
2737                    &epoch_id,
2738                    epoch_start_timestamp,
2739                    tx_checked_input_objects,
2740                    gas_data,
2741                    tx_gas_status,
2742                    kind,
2743                    signer,
2744                    tx_digest,
2745                    &mut None,
2746                );
2747            // Without Move authentication there is nothing to judge.
2748            let outcome = attestation_verdict::ExecutionOutcome {
2749                blame: None,
2750                consumed_units: 0,
2751                body_ran: attestation_verdict::body_ran(false, execution_error_opt.as_ref().err()),
2752            };
2753            (inner_temp_store, effects, execution_error_opt, outcome)
2754        } else {
2755            // One or more `MoveAuthenticator` signatures present — authenticate each and
2756            // then execute the transaction.
2757            // It is supposed that `MoveAuthenticator` availability is checked in
2758            // `SenderSignedTransaction::validity_check`.
2759
2760            debug_assert_eq!(
2761                move_authenticators.len(),
2762                per_authenticator_inputs.len(),
2763                "Move authenticators amount must match the number of authenticator inputs"
2764            );
2765
2766            // The first account that cannot be resolved fails the transaction. The
2767            // remaining inputs are still collected, since the failure effects need
2768            // them whether or not anyone is charged gas.
2769            let mut per_authenticator_input_objects = Vec::with_capacity(move_authenticators.len());
2770            let mut resolutions = Vec::with_capacity(move_authenticators.len());
2771            for (move_authenticator, (authenticator_input_objects, account_object)) in
2772                move_authenticators.iter().zip(per_authenticator_inputs)
2773            {
2774                // The shape of the object to authenticate is decided by the
2775                // transaction bytes, and `validity_check` settles it in the
2776                // consensus handler, so it cannot be wrong here.
2777                let (account_object_id, account_object_version, account_object_digest) =
2778                    move_authenticator
2779                        .object_to_authenticate_components()
2780                        .expect(
2781                            "the object to authenticate is validated before consensus and cannot \
2782                                be invalid during execution",
2783                        );
2784
2785                // Unlike the shape above, resolving the account depends on state,
2786                // so post-consensus validation cannot keep this check. Here, an
2787                // account that fails it produces failure effects instead of a
2788                // panic.
2789                resolutions.push(self.check_move_account_for_execution(
2790                    account_object_id,
2791                    account_object_version,
2792                    account_object_digest,
2793                    account_object,
2794                    &move_authenticator.address(),
2795                    protocol_config,
2796                ));
2797
2798                per_authenticator_input_objects.push(authenticator_input_objects);
2799            }
2800
2801            // Serialize the Transaction for the auth context.
2802            let tx_bytes = bcs::to_bytes(tx).expect("Transaction serialization cannot fail");
2803
2804            let (sender_auth_digest, sponsor_auth_digest) =
2805                transaction.data().compute_auth_digests()?;
2806
2807            // Check the `MoveAuthenticator` input objects. These are needed to build the
2808            // effect whether or not authentication ultimately runs.
2809            // The `MoveAuthenticator` receiving objects are checked on the signing step.
2810            // `max_auth_gas` is used here as a Move authenticator gas budget until it is
2811            // not a part of the transaction data.
2812            let authenticator_gas_budget = protocol_config.max_auth_gas();
2813            let (
2814                gas_status,
2815                per_authenticator_checked_input_objects,
2816                authenticator_and_tx_checked_input_objects,
2817            ) = iota_transaction_checks::check_certificate_and_move_authenticator_input(
2818                tx,
2819                tx_input_objects,
2820                per_authenticator_input_objects,
2821                None,
2822                authenticator_gas_budget,
2823                protocol_config,
2824                reference_gas_price,
2825            )?;
2826
2827            let owned_object_refs = authenticator_and_tx_checked_input_objects
2828                .inner()
2829                .filter_owned_objects();
2830            self.check_owned_locks(&owned_object_refs)?;
2831
2832            // Only attested transactions can have their Move-authentication
2833            // failure attributed, and only they carry recorded versions to judge.
2834            let attestation_verdict_context = match attestation {
2835                AttestationSource::Attestation {
2836                    attestation,
2837                    sequenced_at,
2838                } => Some(attestation_verdict::AttestationVerdictContext {
2839                    attestation,
2840                    attested_versions: attestation_verdict::AttestedObjectVersions {
2841                        object_cache: self.get_object_cache_reader().as_ref(),
2842                        transaction_cache: self.get_transaction_cache_reader().as_ref(),
2843                        epoch_store,
2844                        sequenced_at: *sequenced_at,
2845                        window: protocol_config.attestation_freshness_window_commits_as_option(),
2846                    },
2847                    store: backing_store,
2848                    executor: epoch_store.executor().as_ref(),
2849                    protocol_config,
2850                    metrics: self.metrics.limits_metrics.clone(),
2851                    epoch_id,
2852                    epoch_timestamp_ms: epoch_start_timestamp,
2853                    reference_gas_price,
2854                    gas_data: gas_data.clone(),
2855                    authenticators: attestation_verdict::authenticator_inputs(
2856                        &move_authenticators,
2857                        &per_authenticator_checked_input_objects,
2858                    ),
2859                    executed_versions: attestation_verdict::executed_versions(
2860                        &per_authenticator_checked_input_objects,
2861                        &resolutions,
2862                    ),
2863                    transaction: transaction.data(),
2864                    transaction_signer: signer,
2865                    transaction_digest: tx_digest,
2866                    sender_auth_digest,
2867                    sponsor_auth_digest,
2868                }),
2869                AttestationSource::Unattested | AttestationSource::Certified(_) => None,
2870            };
2871
2872            // The function refs of the accounts that resolved, so an
2873            // authenticator that runs after another account failed to resolve
2874            // still finds its own.
2875            let (sender_authenticator_function_ref, sponsor_authenticator_function_ref) =
2876                extract_auth_fun_refs(signer, gas_data.owner, |address| {
2877                    move_authenticators
2878                        .iter()
2879                        .zip(&resolutions)
2880                        .find(|(move_authenticator, _)| move_authenticator.address() == address)
2881                        .and_then(|(_, resolution)| resolution.as_ref().ok())
2882                        .map(|function_ref| function_ref.authenticator_function_ref.clone())
2883                });
2884
2885            let accounts = move_authenticators
2886                .iter()
2887                .map(|move_authenticator| move_authenticator.address())
2888                .collect::<Vec<_>>();
2889            debug_assert_eq!(
2890                move_authenticators.len(),
2891                per_authenticator_checked_input_objects.len(),
2892                "Move authenticators amount must match the number of checked authenticator inputs"
2893            );
2894            // Under the P-COOL flow, a gas owner that cannot be resolved did
2895            // not authorize the transaction, so `gas_owner_pays` decides who
2896            // pays; its own failure is then the one reported.
2897            let pcool_flow = protocol_config.enable_pcool_flow();
2898            let mut first_resolution_error = None;
2899            let mut gas_owner_resolution_error = None;
2900            let mut resolved_authenticators = Vec::with_capacity(move_authenticators.len());
2901            for ((move_authenticator, resolution), authenticator_checked_input_objects) in
2902                move_authenticators
2903                    .into_iter()
2904                    .zip(resolutions)
2905                    .zip(per_authenticator_checked_input_objects)
2906            {
2907                match resolution {
2908                    Ok(function_ref) => {
2909                        resolved_authenticators.push(MoveAuthenticatorForExecution {
2910                            authenticator: move_authenticator.to_owned(),
2911                            function_ref,
2912                            input_objects: authenticator_checked_input_objects,
2913                        })
2914                    }
2915                    Err(error) if pcool_flow && move_authenticator.address() == gas_data.owner => {
2916                        gas_owner_resolution_error = Some(error);
2917                    }
2918                    Err(error) => {
2919                        first_resolution_error.get_or_insert(error);
2920                    }
2921                }
2922            }
2923
2924            let auth_context_data = AuthContextData {
2925                transaction_data_bytes: tx_bytes,
2926                sender_auth_digest,
2927                sponsor_auth_digest,
2928                sender_authenticator_function_ref,
2929                sponsor_authenticator_function_ref,
2930            };
2931
2932            // With a resolution failure, the executor gets the failure in place
2933            // of the list, with the accounts the authenticators would have
2934            // authenticated and, under the P-COOL flow, the gas owner's
2935            // authenticator if its account resolved, so it can decide who pays.
2936            let authenticators = match gas_owner_resolution_error.or(first_resolution_error) {
2937                Some(error) => MoveAuthenticatorsForExecution::ResolutionFailed {
2938                    error,
2939                    accounts,
2940                    gas_owner: if pcool_flow {
2941                        resolved_authenticators
2942                            .into_iter()
2943                            .find(|resolved| resolved.authenticator.address() == gas_data.owner)
2944                            .map(Box::new)
2945                    } else {
2946                        None
2947                    },
2948                },
2949                None => MoveAuthenticatorsForExecution::Resolved(resolved_authenticators),
2950            };
2951
2952            // Whether the gas owner pays when its own authentication fails: the
2953            // judge answers for an attested transaction, the certified record
2954            // when executing from a checkpoint, and nobody pays otherwise. The
2955            // judge's answer is kept for the record.
2956            let blame = Cell::new(None);
2957            let gas_owner_pays = |kind: &ExecutionErrorKind| {
2958                match (attestation, &attestation_verdict_context) {
2959                    (AttestationSource::Attestation { .. }, Some(context)) => {
2960                        let answer = context.decide_authentication_failure_blame(kind);
2961                        blame.set(Some(answer));
2962
2963                        answer == attestation_verdict::AuthenticationFailureBlame::Account
2964                    }
2965                    (AttestationSource::Certified(record), _)
2966                        if protocol_config.enable_validator_attestation() =>
2967                    {
2968                        // Only the records other than `Valid` are certified, and
2969                        // consensus requires an attestation on every user
2970                        // transaction while the flag is on, so no record means
2971                        // the attestation was valid. The gas owner pays unless
2972                        // the judge blamed the attestor or could not decide.
2973                        !matches!(
2974                            record,
2975                            Some(AttestationRecord {
2976                                verdict: AttestationVerdict::Refuted { .. }
2977                                    | AttestationVerdict::Undecided,
2978                                ..
2979                            })
2980                        )
2981                    }
2982                    _ => false,
2983                }
2984            };
2985
2986            let (
2987                inner_temp_store,
2988                gas_status,
2989                effects,
2990                _timings,
2991                execution_error_opt,
2992                authentication_error,
2993            ) = epoch_store
2994                .executor()
2995                .authenticate_then_execute_transaction_to_effects(
2996                    backing_store,
2997                    protocol_config,
2998                    self.metrics.limits_metrics.clone(),
2999                    self.config
3000                        .expensive_safety_check_config
3001                        .enable_deep_per_tx_iota_conservation_check(),
3002                    self.config.certificate_deny_config.certificate_deny_set(),
3003                    &epoch_id,
3004                    epoch_start_timestamp,
3005                    gas_data,
3006                    gas_status,
3007                    authenticators,
3008                    authenticator_and_tx_checked_input_objects,
3009                    Some(&gas_owner_pays),
3010                    kind,
3011                    signer,
3012                    tx_digest,
3013                    auth_context_data,
3014                    &mut None,
3015                );
3016            // Only an attested transaction whose authentication failed is
3017            // judged, on the authentication error itself: the body's own
3018            // checks may have replaced it in the effects. The engine asks the
3019            // judge only when a gas owner that is a Move account did not
3020            // authorize the transaction, and never about a cancellation or an
3021            // invariant violation; any other failure is judged here, for the
3022            // record.
3023            let blame = blame.get().or_else(|| {
3024                match (&attestation_verdict_context, &authentication_error) {
3025                    (Some(context), Some(kind)) => {
3026                        Some(context.decide_authentication_failure_blame(kind))
3027                    }
3028                    _ => None,
3029                }
3030            });
3031            let outcome = attestation_verdict::ExecutionOutcome {
3032                blame,
3033                consumed_units: attestation_verdict::consumed_units(gas_status),
3034                body_ran: attestation_verdict::body_ran(
3035                    authentication_error.is_some(),
3036                    execution_error_opt.as_ref().err(),
3037                ),
3038            };
3039
3040            (inner_temp_store, effects, execution_error_opt, outcome)
3041        };
3042
3043        // Judged before the fork-injecting fail point below, so that only the
3044        // effects diverge under it.
3045        let attestation_record = attestation_record(
3046            attestation,
3047            &outcome,
3048            &effects,
3049            tx.gas_price(),
3050            protocol_config,
3051        );
3052
3053        fail_point_if!("cp_execution_nondeterminism", || {
3054            #[cfg(msim)]
3055            self.create_fail_state(transaction, epoch_store, &mut effects);
3056        });
3057
3058        let elapsed = prepare_transaction_start_time.elapsed().as_micros() as f64;
3059        if elapsed > 0.0 {
3060            self.metrics
3061                .prepare_cert_gas_latency_ratio
3062                .observe(effects.gas_cost_summary().computation_cost as f64 / elapsed);
3063        }
3064
3065        let read_objects = tracking_store.into_read_objects();
3066
3067        Ok((
3068            inner_temp_store,
3069            effects,
3070            execution_error.err(),
3071            read_objects,
3072            attestation_record,
3073        ))
3074    }
3075
3076    pub fn prepare_transaction_for_benchmark(
3077        &self,
3078        transaction: &VerifiedExecutableTransaction,
3079        input_objects: InputObjects,
3080        epoch_store: &Arc<AuthorityPerEpochStore>,
3081    ) -> IotaResult<(
3082        InnerTemporaryStore,
3083        TransactionEffects,
3084        Option<ExecutionError>,
3085    )> {
3086        let lock = RwLock::new(epoch_store.epoch());
3087        let execution_guard = lock.try_read().unwrap();
3088
3089        self.execute_transaction(
3090            &execution_guard,
3091            transaction,
3092            input_objects,
3093            vec![],
3094            &AttestationSource::Unattested,
3095            epoch_store,
3096        )
3097        .map(|(inner_temp_store, effects, execution_error, _, _)| {
3098            (inner_temp_store, effects, execution_error)
3099        })
3100    }
3101
3102    /// Simulate a transaction without committing it.
3103    ///
3104    /// `checks` selects the Move VM semantics: `VmChecks::Enabled` runs the
3105    /// transaction as it would run on chain (a dry run), while
3106    /// `VmChecks::Disabled` relaxes the checks around entry functions and
3107    /// argument values (a dev inspect). Both report the per-command return
3108    /// values in [`SimulateTransactionResult::execution_result`].
3109    ///
3110    /// Under either `checks`, the simulation fills in whatever gas the
3111    /// transaction leaves unset, so that a caller with no gas to declare can
3112    /// leave all of it out: no gas payment mints a mock gas coin, whose ID is
3113    /// reported back in [`SimulateTransactionResult::mock_gas_id`]; a zero gas
3114    /// price becomes the epoch's reference gas price; and a zero gas budget
3115    /// becomes as much as the gas coins can back, up to
3116    /// [`max_tx_gas`](iota_protocol_config::ProtocolConfig::max_tx_gas).
3117    /// Anything the transaction does declare is metered as given, so a dry run
3118    /// still rejects the gas a validator would.
3119    ///
3120    /// Under either `checks`, the packages the transaction publishes are run
3121    /// through the bytecode verifier with this node's signing-time limits, so a
3122    /// package that signing would reject with
3123    /// [`UserInputError::PackageVerificationTimedout`] is rejected here too.
3124    ///
3125    /// Whatever the budget resolves to, the gas coins have to cover it, since
3126    /// execution reserves the whole budget from them before running any command
3127    /// and refunds it afterwards. A caller leaving the budget at zero to have
3128    /// the cost estimated therefore gets an estimate whatever its coins hold,
3129    /// but the reserved budget is off limits for the duration of the
3130    /// programmable transaction: a transaction that also pays out of its gas
3131    /// coin has to declare a budget leaving room for that, exactly as it would
3132    /// on chain. A balance too small to declare the minimum budget at all is
3133    /// rejected with [`UserInputError::GasBalanceTooLow`].
3134    pub fn simulate_transaction(
3135        &self,
3136        transaction: Transaction,
3137        checks: VmChecks,
3138    ) -> IotaResult<SimulateTransactionResult> {
3139        let epoch_store = self.load_epoch_store_one_call_per_task();
3140        self.simulate_transaction_in_epoch(&epoch_store, transaction, checks)
3141    }
3142
3143    /// Same as [`AuthorityState::simulate_transaction`], for callers that
3144    /// already hold an epoch store.
3145    ///
3146    /// Callers that derive gas parameters from an epoch, or resolve types
3147    /// against its executor once the simulation returns, should pass that same
3148    /// epoch store here so the whole operation observes one epoch.
3149    ///
3150    /// Nothing here checks that `epoch_store` is the current one — pinning a
3151    /// superseded epoch is the point, and is what
3152    /// [`AuthorityState::simulate_transaction`] does for the span of its own
3153    /// call. Keeping one across an unbounded period is the caller's problem:
3154    /// the simulation would run against that epoch's protocol config,
3155    /// executor, and reference gas price.
3156    #[instrument("simulate_tx", level = "trace", skip_all)]
3157    pub fn simulate_transaction_in_epoch(
3158        &self,
3159        epoch_store: &AuthorityPerEpochStore,
3160        transaction: Transaction,
3161        checks: VmChecks,
3162    ) -> IotaResult<SimulateTransactionResult> {
3163        if !self.is_fullnode(epoch_store) {
3164            return Err(IotaError::UnsupportedFeature {
3165                error: "simulate is only supported on fullnodes".to_string(),
3166            });
3167        }
3168
3169        self.simulate_transaction_inner(epoch_store, transaction, checks)
3170    }
3171
3172    /// Same as [`AuthorityState::simulate_transaction`], but runs on a
3173    /// validator too. Only the single-node benchmark, which has no fullnode
3174    /// to run against, needs this.
3175    pub fn simulate_transaction_for_benchmark(
3176        &self,
3177        transaction: Transaction,
3178        checks: VmChecks,
3179    ) -> IotaResult<SimulateTransactionResult> {
3180        let epoch_store = self.load_epoch_store_one_call_per_task();
3181        self.simulate_transaction_inner(&epoch_store, transaction, checks)
3182    }
3183
3184    #[instrument(level = "trace", skip_all)]
3185    fn simulate_transaction_inner(
3186        &self,
3187        epoch_store: &AuthorityPerEpochStore,
3188        mut transaction: Transaction,
3189        checks: VmChecks,
3190    ) -> IotaResult<SimulateTransactionResult> {
3191        // A missing gas payment is allowed here, since we may create a mock gas
3192        // object below.
3193        iota_transaction_checks::check_user_transaction_validity(
3194            &transaction,
3195            epoch_store.protocol_config(),
3196        )?;
3197
3198        let input_object_kinds = transaction.input_objects()?;
3199        let receiving_object_refs = transaction.receiving_objects();
3200
3201        // Since we need to simulate a validator signing the transaction, the first step
3202        // is to check if some transaction elements are denied.
3203        iota_transaction_checks::deny::check_transaction_for_validation(
3204            &transaction,
3205            &[],
3206            &input_object_kinds,
3207            &receiving_object_refs,
3208            &self.config.transaction_deny_config,
3209            self.get_backing_package_store().as_ref(),
3210        )?;
3211
3212        // Load input and receiving objects
3213        let (mut input_objects, receiving_objects) = self.input_loader.read_objects_for_signing(
3214            // We don't want to cache this transaction since it's a simulation.
3215            None,
3216            &input_object_kinds,
3217            &receiving_object_refs,
3218            epoch_store.epoch(),
3219        )?;
3220
3221        // The transaction dependency is evaluated when its effects are here,
3222        // and assumed to have succeeded otherwise, so a dependent can be
3223        // simulated before its dependency executes.
3224        if let Some(dependency) = transaction.transaction_dependency() {
3225            let succeeded = self
3226                .get_transaction_cache_reader()
3227                .try_get_executed_effects(&dependency)?
3228                .is_none_or(|effects| effects.status().is_success());
3229            input_objects.set_transaction_dependency(TransactionDependencyReadResult {
3230                digest: dependency,
3231                succeeded,
3232            });
3233        }
3234
3235        // Create a mock gas object if one was not provided
3236        let mock_gas_id = if transaction.gas().is_empty() {
3237            let mock_gas_object = mock_simulation_gas_coin(transaction.gas_data().owner);
3238            let mock_gas_object_ref = mock_gas_object.object_ref();
3239            transaction.gas_data_mut().objects = vec![mock_gas_object_ref];
3240            input_objects.push(ObjectReadResult::new_from_gas_object(&mock_gas_object));
3241            Some(mock_gas_object.id())
3242        } else {
3243            None
3244        };
3245
3246        let protocol_config = epoch_store.protocol_config();
3247
3248        iota_types::gas::fill_in_unset_simulation_gas(
3249            &mut transaction,
3250            &input_objects,
3251            epoch_store.reference_gas_price(),
3252            protocol_config,
3253        );
3254
3255        // `MoveAuthenticator`s are not supported in simulation, so we set the
3256        // `authenticator_gas_budget` to 0.
3257        let authenticator_gas_budget = 0;
3258
3259        let (gas_status, checked_input_objects) = iota_transaction_checks::check_transaction_input(
3260            protocol_config,
3261            epoch_store.reference_gas_price(),
3262            &transaction,
3263            input_objects,
3264            &receiving_objects,
3265            &self.metrics.bytecode_verifier_metrics,
3266            VerifierLimitsSource::NodeConfig(&self.config.verifier_signing_config),
3267            authenticator_gas_budget,
3268            false,
3269            InputCheckRules::from(checks),
3270        )?;
3271
3272        // Create a new executor for the simulation
3273        let executor = iota_execution::executor(
3274            protocol_config,
3275            true, // silent
3276            None,
3277        )
3278        .expect("Creating an executor should not fail here");
3279
3280        // Execute the simulation
3281        let (kind, signer, gas_data) = transaction.execution_parts();
3282        let (inner_temp_store, _, effects, execution_result) = executor.dev_inspect_transaction(
3283            self.get_backing_store().as_ref(),
3284            protocol_config,
3285            self.metrics.limits_metrics.clone(),
3286            false, // expensive_checks
3287            self.config.certificate_deny_config.certificate_deny_set(),
3288            &epoch_store.epoch_start_config().epoch_data().epoch_id(),
3289            epoch_store
3290                .epoch_start_config()
3291                .epoch_data()
3292                .epoch_start_timestamp(),
3293            checked_input_objects,
3294            gas_data,
3295            gas_status,
3296            kind,
3297            signer,
3298            transaction.digest(),
3299            checks.disabled(),
3300        );
3301
3302        let mut input_objects = inner_temp_store.input_objects;
3303        iota_types::storage::extend_input_objects_with_loaded_runtime_objects(
3304            &mut input_objects,
3305            &effects,
3306            &inner_temp_store.loaded_runtime_objects,
3307            self.get_backing_store().as_object_store(),
3308        );
3309
3310        Ok(SimulateTransactionResult {
3311            input_objects,
3312            output_objects: inner_temp_store.written,
3313            events: effects.events_digest().map(|_| inner_temp_store.events),
3314            effects,
3315            execution_result,
3316            suggested_gas_price: self
3317                .congestion_tracker
3318                .get_prediction_suggested_gas_price(&transaction),
3319            mock_gas_id,
3320            gas_data: transaction.gas_data().clone(),
3321        })
3322    }
3323
3324    // Only used for testing because of how epoch store is loaded.
3325    pub fn reference_gas_price_for_testing(&self) -> Result<u64, anyhow::Error> {
3326        let epoch_store = self.epoch_store_for_testing();
3327        Ok(epoch_store.reference_gas_price())
3328    }
3329
3330    #[instrument(level = "trace", skip_all)]
3331    pub fn try_is_tx_already_executed(&self, digest: &TransactionDigest) -> IotaResult<bool> {
3332        self.get_transaction_cache_reader()
3333            .try_is_tx_already_executed(digest)
3334    }
3335
3336    /// Non-fallible version of `try_is_tx_already_executed`.
3337    pub fn is_tx_already_executed(&self, digest: &TransactionDigest) -> bool {
3338        self.try_is_tx_already_executed(digest)
3339            .expect("storage access failed")
3340    }
3341
3342    #[cfg(msim)]
3343    fn create_fail_state(
3344        &self,
3345        transaction: &VerifiedExecutableTransaction,
3346        epoch_store: &Arc<AuthorityPerEpochStore>,
3347        effects: &mut TransactionEffects,
3348    ) {
3349        use std::cell::RefCell;
3350
3351        use iota_types::effects::TransactionEffectsAPIForTesting;
3352        thread_local! {
3353            static FAIL_STATE: RefCell<(u64, HashSet<AuthorityName>)> = RefCell::new((0, HashSet::new()));
3354        }
3355        if !transaction.data().transaction().is_system_tx() {
3356            let committee = epoch_store.committee();
3357            let cur_stake = (**committee).weight(&self.name);
3358            if cur_stake > 0 {
3359                FAIL_STATE.with_borrow_mut(|fail_state| {
3360                    // let (&mut failing_stake, &mut failing_validators) = fail_state;
3361                    if fail_state.0 < committee.validity_threshold() {
3362                        fail_state.0 += cur_stake;
3363                        fail_state.1.insert(self.name);
3364                    }
3365
3366                    if fail_state.1.contains(&self.name) {
3367                        info!("cp_exec failing tx");
3368                        effects.gas_cost_summary_mut_for_testing().computation_cost += 1;
3369                    }
3370                });
3371            }
3372        }
3373    }
3374
3375    pub fn unixtime_now_ms() -> u64 {
3376        let now = SystemTime::now()
3377            .duration_since(UNIX_EPOCH)
3378            .expect("Time went backwards")
3379            .as_millis();
3380        u64::try_from(now).expect("Travelling in time machine")
3381    }
3382
3383    #[instrument(level = "trace", skip_all)]
3384    pub async fn handle_transaction_info_request(
3385        &self,
3386        request: TransactionInfoRequest,
3387    ) -> IotaResult<TransactionInfoResponse> {
3388        let epoch_store = self.load_epoch_store_one_call_per_task();
3389        let (transaction, status) = self
3390            .get_transaction_status(&request.transaction_digest, &epoch_store)?
3391            .ok_or(IotaError::TransactionNotFound {
3392                digest: request.transaction_digest,
3393            })?;
3394        Ok(TransactionInfoResponse {
3395            transaction,
3396            status,
3397        })
3398    }
3399
3400    #[instrument(level = "trace", skip_all)]
3401    pub async fn handle_object_info_request(
3402        &self,
3403        request: ObjectInfoRequest,
3404    ) -> IotaResult<ObjectInfoResponse> {
3405        let epoch_store = self.load_epoch_store_one_call_per_task();
3406
3407        let requested_object_seq = match request.request_kind {
3408            ObjectInfoRequestKind::LatestObjectInfo => {
3409                self.try_get_object_or_tombstone(request.object_id)?
3410                    .ok_or_else(|| {
3411                        IotaError::from(UserInputError::ObjectNotFound {
3412                            object_id: request.object_id,
3413                            version: None,
3414                        })
3415                    })?
3416                    .version
3417            }
3418            ObjectInfoRequestKind::PastObjectInfoDebug(seq) => seq,
3419        };
3420
3421        // A `PastObjectInfoDebug` request names an older version, which may
3422        // have been moved out of the live table.
3423        let object = self
3424            .get_object_with_historic_fallback(&ObjectKey(request.object_id, requested_object_seq))?
3425            .ok_or_else(|| {
3426                IotaError::from(UserInputError::ObjectNotFound {
3427                    object_id: request.object_id,
3428                    version: Some(requested_object_seq),
3429                })
3430            })?;
3431
3432        let layout = if let (LayoutGenerationOption::Generate, Some(move_obj)) =
3433            (request.generate_layout, object.data.as_opt_struct())
3434        {
3435            Some(into_struct_layout(
3436                epoch_store
3437                    .executor()
3438                    .type_layout_resolver(Box::new(self.get_backing_package_store().as_ref()))
3439                    .get_annotated_layout(move_obj.struct_tag())?,
3440            )?)
3441        } else {
3442            None
3443        };
3444
3445        let lock = if !object.is_address_owned() {
3446            // Only address owned objects have locks.
3447            None
3448        } else {
3449            self.get_transaction_lock(&object.object_ref(), &epoch_store)?
3450                .map(|s| s.into_inner())
3451        };
3452
3453        Ok(ObjectInfoResponse {
3454            object,
3455            layout,
3456            lock_for_debugging: lock,
3457        })
3458    }
3459
3460    #[instrument(level = "trace", skip_all)]
3461    pub fn handle_checkpoint_request(
3462        &self,
3463        request: &CheckpointRequest,
3464    ) -> IotaResult<CheckpointResponse> {
3465        let summary = if request.certified {
3466            let summary = match request.sequence_number {
3467                Some(seq) => self
3468                    .checkpoint_store
3469                    .get_checkpoint_by_sequence_number(seq)?,
3470                None => self.checkpoint_store.get_latest_certified_checkpoint()?,
3471            }
3472            .map(|v| v.into_inner());
3473            summary.map(CheckpointSummaryResponse::Certified)
3474        } else {
3475            let summary = match request.sequence_number {
3476                Some(seq) => self.checkpoint_store.get_locally_computed_checkpoint(seq)?,
3477                None => self
3478                    .checkpoint_store
3479                    .get_latest_locally_computed_checkpoint()?,
3480            };
3481            summary.map(CheckpointSummaryResponse::Pending)
3482        };
3483        let contents = match &summary {
3484            Some(s) => self
3485                .checkpoint_store
3486                .get_checkpoint_contents(&s.contents_digest())?,
3487            None => None,
3488        };
3489        Ok(CheckpointResponse {
3490            checkpoint: summary,
3491            contents,
3492        })
3493    }
3494
3495    fn check_protocol_version(
3496        supported_protocol_versions: SupportedProtocolVersions,
3497        current_version: ProtocolVersion,
3498    ) {
3499        info!("current protocol version is now {:?}", current_version);
3500        info!("supported versions are: {:?}", supported_protocol_versions);
3501        if !supported_protocol_versions.is_version_supported(current_version) {
3502            let msg = format!(
3503                "Unsupported protocol version. The network is at {current_version:?}, but this IotaNode only supports: {supported_protocol_versions:?}. Shutting down.",
3504            );
3505
3506            error!("{}", msg);
3507            eprintln!("{msg}");
3508
3509            #[cfg(not(msim))]
3510            std::process::exit(1);
3511
3512            #[cfg(msim)]
3513            iota_simulator::task::shutdown_current_node();
3514        }
3515    }
3516
3517    #[expect(clippy::disallowed_methods)] // allow unbounded_channel()
3518    pub async fn new(
3519        name: AuthorityName,
3520        secret: StableSyncAuthoritySigner,
3521        supported_protocol_versions: SupportedProtocolVersions,
3522        store: Arc<AuthorityStore>,
3523        execution_cache_trait_pointers: ExecutionCacheTraitPointers,
3524        epoch_store: Arc<AuthorityPerEpochStore>,
3525        committee_store: Arc<CommitteeStore>,
3526        rpc_indexes_store: Option<Arc<RpcIndexesStore>>,
3527        checkpoint_store: Arc<CheckpointStore>,
3528        prometheus_registry: &Registry,
3529        config: NodeConfig,
3530        validator_tx_finalizer: Option<Arc<ValidatorTxFinalizer<NetworkAuthorityClient>>>,
3531        chain_identifier: ChainIdentifier,
3532        policy_config: Option<PolicyConfig>,
3533        epoch_end_db_snapshots: Option<EpochEndDbSnapshotHandle>,
3534    ) -> Arc<Self> {
3535        Self::check_protocol_version(supported_protocol_versions, epoch_store.protocol_version());
3536
3537        // Read by the quarantine flush and the execution watcher; the next
3538        // epoch's store inherits it.
3539        epoch_store.set_effects_store(
3540            execution_cache_trait_pointers
3541                .transaction_cache_reader
3542                .clone(),
3543        );
3544
3545        let metrics = Arc::new(AuthorityMetrics::new(prometheus_registry));
3546        let (tx_ready_transactions, rx_ready_transactions) = unbounded_channel();
3547        let execution_scheduler = Arc::new(ExecutionSchedulerWrapper::new(
3548            execution_cache_trait_pointers.object_cache_reader.clone(),
3549            execution_cache_trait_pointers
3550                .transaction_cache_reader
3551                .clone(),
3552            tx_ready_transactions,
3553            &epoch_store,
3554            metrics.clone(),
3555        ));
3556        let (tx_execution_shutdown, rx_execution_shutdown) = oneshot::channel();
3557
3558        let authority_per_epoch_pruner = AuthorityPerEpochStorePruner::new(
3559            epoch_store.get_parent_path(),
3560            config
3561                .authority_store_pruning_config
3562                .num_latest_epoch_dbs_to_retain,
3563        )
3564        .await;
3565        let object_retention = ObjectRetention::new(
3566            checkpoint_store
3567                .get_highest_executed_checkpoint_seq_number()
3568                .ok()
3569                .flatten()
3570                .unwrap_or_default(),
3571        );
3572        let num_epochs_to_retain = config.authority_store_pruning_config.num_epochs_to_retain;
3573        if epoch_store.committee().authority_exists(&name)
3574            && num_epochs_to_retain > 0
3575            && num_epochs_to_retain < u64::MAX
3576        {
3577            warn!(
3578                num_epochs_to_retain,
3579                "this validator retains superseded object versions it does not serve, which \
3580                 grows its database; lowering the retention later expires every epoch they \
3581                 span at once, at the next start. Set 0 to retain none beyond the current epoch."
3582            );
3583        }
3584        if let Some(delay_days) = config
3585            .authority_store_pruning_config
3586            .periodic_compaction_threshold_days
3587        {
3588            store.perpetual_tables.spawn_periodic_compaction(delay_days);
3589        }
3590        let input_loader =
3591            TransactionInputLoader::new(execution_cache_trait_pointers.object_cache_reader.clone());
3592        let epoch = epoch_store.epoch();
3593        let (historic_bucket_expiry, expiry_epochs) = watch::channel(epoch);
3594        let rgp = epoch_store.reference_gas_price();
3595        let traffic_controller_metrics =
3596            Arc::new(TrafficControllerMetrics::new(prometheus_registry));
3597        let traffic_controller = policy_config.map(|policy_config| {
3598            Arc::new(TrafficController::init(
3599                policy_config,
3600                traffic_controller_metrics,
3601            ))
3602        });
3603        let attestation_semaphore =
3604            AttestationSemaphore::new(&config.authority_overload_config, &metrics);
3605        let state = Arc::new_cyclic(|this| AuthorityState {
3606            this: this.clone(),
3607            name,
3608            secret,
3609            execution_lock: RwLock::new(epoch),
3610            epoch_store: ArcSwap::new(epoch_store.clone()),
3611            input_loader,
3612            execution_cache_trait_pointers,
3613            rpc_indexes_store,
3614            historic_objects: store.get_historic_objects().clone(),
3615            historic_ledger: store.get_historic_ledger().clone(),
3616            checkpoint_store,
3617            committee_store,
3618            execution_scheduler,
3619            tx_execution_shutdown: Mutex::new(Some(tx_execution_shutdown)),
3620            metrics,
3621            authority_per_epoch_pruner,
3622            object_retention,
3623            config,
3624            overload_info: AuthorityOverloadInfo::default(),
3625            attestation_semaphore,
3626            validator_tx_finalizer,
3627            chain_identifier,
3628            congestion_tracker: Arc::new(CongestionTracker::new(rgp)),
3629            traffic_controller,
3630            epoch_end_db_snapshots,
3631            historic_bucket_expiry,
3632            historic_bucket_work_stopped: Arc::new(AtomicBool::new(false)),
3633        });
3634        // An expiry cut short by a crash may not have recorded what it
3635        // dropped.
3636        Self::record_pruned_checkpoints(&state.historic_ledger, &state.checkpoint_store);
3637
3638        let expiry = Self::expire_historic_buckets_in_background(
3639            Arc::downgrade(&state),
3640            expiry_epochs,
3641            state.object_retention.released(),
3642        );
3643        spawn_monitored_task!(expiry);
3644
3645        // Start a task to execute ready transactions.
3646        let authority_state = Arc::downgrade(&state);
3647        spawn_monitored_task!(execution_process(
3648            authority_state,
3649            rx_ready_transactions,
3650            rx_execution_shutdown,
3651        ));
3652
3653        state
3654    }
3655
3656    pub fn epoch_db_pruner(&self) -> &AuthorityPerEpochStorePruner {
3657        &self.authority_per_epoch_pruner
3658    }
3659
3660    /// Superseded object versions, bucketed by epoch.
3661    pub fn get_historic_objects(&self) -> &Arc<HistoricObjects> {
3662        &self.historic_objects
3663    }
3664
3665    /// Executed transactions keyed by checkpoint, bucketed by epoch.
3666    pub fn get_historic_ledger(&self) -> &Arc<HistoricLedger> {
3667        &self.historic_ledger
3668    }
3669
3670    // TODO: Consolidate our traits to reduce the number of methods here.
3671    pub fn get_object_cache_reader(&self) -> &Arc<dyn ObjectCacheRead> {
3672        &self.execution_cache_trait_pointers.object_cache_reader
3673    }
3674
3675    pub fn get_transaction_cache_reader(&self) -> &Arc<dyn TransactionCacheRead> {
3676        &self.execution_cache_trait_pointers.transaction_cache_reader
3677    }
3678
3679    pub fn get_cache_writer(&self) -> &Arc<dyn ExecutionCacheWrite> {
3680        &self.execution_cache_trait_pointers.cache_writer
3681    }
3682
3683    pub fn get_backing_store(&self) -> &Arc<dyn BackingStore + Send + Sync> {
3684        &self.execution_cache_trait_pointers.backing_store
3685    }
3686
3687    pub fn get_backing_package_store(&self) -> &Arc<dyn BackingPackageStore + Send + Sync> {
3688        &self.execution_cache_trait_pointers.backing_package_store
3689    }
3690
3691    pub fn get_object_store(&self) -> &Arc<dyn ObjectStore + Send + Sync> {
3692        &self.execution_cache_trait_pointers.object_store
3693    }
3694
3695    pub fn get_reconfig_api(&self) -> &Arc<dyn ExecutionCacheReconfigAPI> {
3696        &self.execution_cache_trait_pointers.reconfig_api
3697    }
3698
3699    pub fn get_global_state_hash_store(&self) -> &Arc<dyn GlobalStateHashStore> {
3700        &self.execution_cache_trait_pointers.global_state_hash_store
3701    }
3702
3703    pub fn get_checkpoint_cache(&self) -> &Arc<dyn CheckpointCache> {
3704        &self.execution_cache_trait_pointers.checkpoint_cache
3705    }
3706
3707    pub fn get_state_sync_store(&self) -> &Arc<dyn StateSyncAPI> {
3708        &self.execution_cache_trait_pointers.state_sync_store
3709    }
3710
3711    pub fn get_cache_commit(&self) -> &Arc<dyn ExecutionCacheCommit> {
3712        &self.execution_cache_trait_pointers.cache_commit
3713    }
3714
3715    pub fn database_for_testing(&self) -> Arc<AuthorityStore> {
3716        self.execution_cache_trait_pointers
3717            .testing_api
3718            .database_for_testing()
3719    }
3720
3721    /// Drops the committed versions of `id` the execution cache holds in
3722    /// memory, so the next read of a committed version goes to the database.
3723    #[cfg(test)]
3724    pub fn evict_committed_object_for_testing(&self, id: &ObjectId) {
3725        self.execution_cache_trait_pointers
3726            .testing_api
3727            .evict_committed_object_for_testing(id)
3728    }
3729
3730    /// Drops the execution cache's copies of committed data, so that reads
3731    /// issued afterwards answer from the store.
3732    pub fn clear_execution_caches_for_testing(&self) {
3733        self.execution_cache_trait_pointers
3734            .testing_api
3735            .clear_caches_for_testing()
3736    }
3737
3738    /// The executed transactions whose outputs are not committed yet.
3739    #[cfg(test)]
3740    pub fn uncommitted_transactions_for_testing(&self) -> Vec<TransactionDigest> {
3741        self.execution_cache_trait_pointers
3742            .testing_api
3743            .uncommitted_transactions_for_testing()
3744    }
3745
3746    pub fn execution_scheduler(&self) -> &Arc<ExecutionSchedulerWrapper> {
3747        &self.execution_scheduler
3748    }
3749
3750    /// Whether this authority runs the `ExecutionScheduler` rather than the
3751    /// `TransactionManager`.
3752    pub fn uses_execution_scheduler(&self) -> bool {
3753        self.execution_scheduler.uses_execution_scheduler()
3754    }
3755
3756    /// Attempts to acquire execution lock for an executable transaction.
3757    /// Returns the lock if the transaction is matching current executed epoch
3758    /// Returns None otherwise
3759    pub fn execution_lock_for_executable_transaction(
3760        &self,
3761        transaction: &VerifiedExecutableTransaction,
3762    ) -> IotaResult<ExecutionLockReadGuard<'_>> {
3763        let lock = self
3764            .execution_lock
3765            .try_read()
3766            .map_err(|_| IotaError::ValidatorHaltedAtEpochEnd)?;
3767        if *lock == transaction.auth_sig().epoch() {
3768            Ok(lock)
3769        } else {
3770            Err(IotaError::WrongEpoch {
3771                expected_epoch: *lock,
3772                actual_epoch: transaction.auth_sig().epoch(),
3773            })
3774        }
3775    }
3776
3777    /// Acquires the execution lock for the duration of a transaction signing
3778    /// request. This prevents reconfiguration from starting until we are
3779    /// finished handling the signing request. Otherwise, in-memory lock
3780    /// state could be cleared (by `ObjectLocks::clear_cached_locks`)
3781    /// while we are attempting to acquire locks for the transaction.
3782    pub fn execution_lock_for_signing(&self) -> IotaResult<ExecutionLockReadGuard<'_>> {
3783        self.execution_lock
3784            .try_read()
3785            .map_err(|_| IotaError::ValidatorHaltedAtEpochEnd)
3786    }
3787
3788    pub async fn execution_lock_for_reconfiguration(&self) -> ExecutionLockWriteGuard<'_> {
3789        self.execution_lock.write().await
3790    }
3791
3792    /// Reports a mirror that diverged from the object at the epoch boundary,
3793    /// where the two must agree. Reporting is the remedy: reconfiguration
3794    /// re-seeds the mirror from the object, so failing here would only pin
3795    /// the node to the diverged state. A missing object is fatal instead.
3796    /// Objects cannot be deleted, so the local store lost it and there is
3797    /// nothing to re-seed from. Nodes outside the closing committee are
3798    /// exempt. So is an epoch this node's consensus did not close. A
3799    /// checkpoint catch-up leaves the mirror legitimately behind until the
3800    /// re-seed.
3801    pub(crate) fn check_transaction_deny_rules_consistency(
3802        &self,
3803        cur_epoch_store: &AuthorityPerEpochStore,
3804        epoch_start_configuration: &EpochStartConfiguration,
3805    ) {
3806        if self.is_fullnode(cur_epoch_store) {
3807            return;
3808        }
3809        let Some(walked_deny_rules) = epoch_start_configuration.transaction_deny_rules_state()
3810        else {
3811            if cur_epoch_store
3812                .epoch_start_config()
3813                .transaction_deny_rules_obj_initial_shared_version()
3814                .is_some()
3815            {
3816                fatal!(
3817                    "TransactionDenyRules object existed in epoch {} but is missing from the \
3818                     state walked for the next epoch — the local store is corrupted; restore or \
3819                     state-sync before rejoining",
3820                    cur_epoch_store.epoch(),
3821                );
3822            }
3823            return;
3824        };
3825        // RejectAllTx proves this node's consensus processed every commit of
3826        // the epoch, so the mirror is complete. Otherwise the tail came from
3827        // synced checkpoints and the mirror's lag carries no signal.
3828        if cur_epoch_store
3829            .get_reconfig_state_read_lock_guard()
3830            .should_accept_tx()
3831        {
3832            info!(
3833                "skipping the deny-rule mirror comparison: consensus did not close epoch {} on \
3834                 this node",
3835                cur_epoch_store.epoch(),
3836            );
3837            return;
3838        }
3839        let mirrored_deny_rules = cur_epoch_store.get_mirrored_transaction_deny_rules();
3840        if *walked_deny_rules != *mirrored_deny_rules {
3841            debug_fatal!(
3842                "TransactionDenyRules object diverged from the mirrored state at the end of \
3843                 epoch {}; continuing from the object (walked: {walked_deny_rules:?}, mirrored: \
3844                 {mirrored_deny_rules:?})",
3845                cur_epoch_store.epoch(),
3846            );
3847            cur_epoch_store.metrics.deny_rule_mirror_divergence.set(1);
3848        }
3849    }
3850
3851    /// Reports a `TransactionDenyRulesUpdate` whose execution failed — an
3852    /// invariant violation, the update is built to exclude every expected
3853    /// failure. The object misses the delta until the epoch boundary re-seeds
3854    /// the mirror. Identification is by kind, so the report needs no tracking
3855    /// state and holds across restarts and replays.
3856    ///
3857    /// `expected_effects_digest` is `Some` when these effects were handed to
3858    /// this node with the transaction, which is the case while executing a
3859    /// certified checkpoint: the failure is then part of agreed history, so it
3860    /// is reported without asserting. Effects the node derived itself assert,
3861    /// because only then is the broken invariant its own.
3862    pub(crate) fn report_failed_deny_rule_update_execution(
3863        &self,
3864        transaction: &VerifiedExecutableTransaction,
3865        effects: &TransactionEffects,
3866        expected_effects_digest: Option<TransactionEffectsDigest>,
3867        epoch_store: &AuthorityPerEpochStore,
3868    ) {
3869        if !matches!(
3870            transaction.transaction().kind(),
3871            TransactionKind::TransactionDenyRulesUpdate(_)
3872        ) || effects.status().is_success()
3873        {
3874            return;
3875        }
3876        epoch_store
3877            .metrics
3878            .deny_rule_update_execution_failures
3879            .inc();
3880        if expected_effects_digest.is_some() {
3881            error!(
3882                digest = ?transaction.digest(),
3883                status = ?effects.status(),
3884                "TransactionDenyRulesUpdate failed execution; the object misses its delta until \
3885                 the epoch boundary re-seeds the mirror"
3886            );
3887            return;
3888        }
3889        debug_fatal!(
3890            "TransactionDenyRulesUpdate failed execution; the object misses its delta until the \
3891             epoch boundary re-seeds the mirror (digest: {:?}, status: {:?})",
3892            transaction.digest(),
3893            effects.status(),
3894        );
3895    }
3896
3897    #[instrument(level = "error", skip_all)]
3898    pub async fn reconfigure(
3899        &self,
3900        cur_epoch_store: &AuthorityPerEpochStore,
3901        supported_protocol_versions: SupportedProtocolVersions,
3902        new_committee: Committee,
3903        epoch_start_configuration: EpochStartConfiguration,
3904        state_hasher: Arc<GlobalStateHasher>,
3905        expensive_safety_check_config: &ExpensiveSafetyCheckConfig,
3906        epoch_supply_change: i64,
3907        epoch_last_checkpoint: CheckpointSequenceNumber,
3908    ) -> IotaResult<Arc<AuthorityPerEpochStore>> {
3909        Self::check_protocol_version(
3910            supported_protocol_versions,
3911            epoch_start_configuration
3912                .epoch_start_state()
3913                .protocol_version(),
3914        );
3915        self.metrics.reset_on_reconfigure();
3916        self.committee_store.insert_new_committee(&new_committee)?;
3917
3918        // Wait until no transactions are being executed.
3919        let mut execution_lock = self.execution_lock_for_reconfiguration().await;
3920
3921        // Terminate all epoch-specific tasks (those started with within_alive_epoch).
3922        cur_epoch_store.epoch_terminated().await;
3923
3924        let highest_locally_built_checkpoint_seq = self
3925            .checkpoint_store
3926            .get_latest_locally_computed_checkpoint()?
3927            .map(|c| c.sequence_number())
3928            .unwrap_or(0);
3929
3930        assert!(
3931            epoch_last_checkpoint >= highest_locally_built_checkpoint_seq,
3932            "expected {epoch_last_checkpoint} >= {highest_locally_built_checkpoint_seq}"
3933        );
3934
3935        // Safe to reconfigure now. No transactions are being executed,
3936        // and no epoch-specific tasks are running.
3937
3938        // TODO: revert_uncommitted_epoch_transactions will soon be unnecessary -
3939        // clear_state_end_of_epoch() can simply drop all uncommitted transactions
3940        self.revert_uncommitted_epoch_transactions(cur_epoch_store)
3941            .await?;
3942        self.get_reconfig_api()
3943            .clear_state_end_of_epoch(&execution_lock);
3944        self.check_system_consistency(
3945            cur_epoch_store,
3946            state_hasher,
3947            expensive_safety_check_config,
3948            epoch_supply_change,
3949        )?;
3950        self.check_transaction_deny_rules_consistency(cur_epoch_store, &epoch_start_configuration);
3951
3952        self.get_reconfig_api()
3953            .try_set_epoch_start_configuration(&epoch_start_configuration)?;
3954        self.hand_over_epoch_end_db_snapshot(cur_epoch_store.epoch())
3955            .await;
3956
3957        let new_epoch = new_committee.epoch;
3958        self.open_historic_buckets(new_epoch)?;
3959        let new_epoch_store = self
3960            .reopen_epoch_db(
3961                cur_epoch_store,
3962                new_committee,
3963                epoch_start_configuration,
3964                expensive_safety_check_config,
3965                epoch_last_checkpoint,
3966            )
3967            .await?;
3968        assert_eq!(new_epoch_store.epoch(), new_epoch);
3969        match self.execution_scheduler.as_ref() {
3970            ExecutionSchedulerWrapper::ExecutionScheduler(_) => {}
3971            ExecutionSchedulerWrapper::TransactionManager(tm) => {
3972                tm.reconfigure(new_epoch);
3973            }
3974        }
3975        *execution_lock = new_epoch;
3976        // Not before the lock reads the new epoch: `try_execute_immediately`
3977        // reads a receiving object's marker before it takes the lock.
3978        if let Err(err) = self
3979            .get_reconfig_api()
3980            .expire_epoch_markers(new_epoch, &execution_lock)
3981        {
3982            error!("Failed to expire the epoch marker buckets: {err:?}");
3983        }
3984        // The other histories expire in the background, so that execution
3985        // resumes without waiting for their deletions.
3986        self.historic_bucket_expiry.send_replace(new_epoch);
3987        // drop execution_lock after epoch store was updated
3988        // see also assert in AuthorityState::process_transaction
3989        // on the epoch store and execution lock epoch match
3990        Ok(new_epoch_store)
3991    }
3992
3993    /// Opens `new_epoch`'s bucket in each per-epoch history, while execution
3994    /// is stopped.
3995    ///
3996    /// # Errors
3997    ///
3998    /// Fails if a history cannot create its bucket for `new_epoch`.
3999    fn open_historic_buckets(&self, new_epoch: EpochId) -> IotaResult<()> {
4000        self.historic_objects.ensure(new_epoch)?;
4001        self.historic_ledger.ensure(new_epoch)?;
4002        self.checkpoint_store
4003            .historic_checkpoints
4004            .ensure(new_epoch)?;
4005        // Not fatal: checkpoint ingest also creates this bucket on demand.
4006        if let Some(indexes) = &self.rpc_indexes_store {
4007            if let Err(err) = indexes.ensure_history_bucket_exists(new_epoch) {
4008                error!("Failed to open the RPC index history bucket of {new_epoch}: {err:?}");
4009            }
4010        }
4011        Ok(())
4012    }
4013
4014    /// Opens `new_epoch`'s buckets and expires the ones outside their
4015    /// retention, as an epoch change and the expiry task that follows it do.
4016    #[cfg(test)]
4017    async fn advance_historic_buckets(&self, new_epoch: EpochId) -> IotaResult<()> {
4018        self.open_historic_buckets(new_epoch)?;
4019        self.expire_historic_buckets(new_epoch).await;
4020        Ok(())
4021    }
4022
4023    /// Expires the buckets of every per-epoch history, the RPC index history
4024    /// included, that fell outside their configured retention as counted from
4025    /// `current_epoch`. A historic-object bucket from the epoch of a
4026    /// checkpoint a [`Self::retain_objects`] guard still holds on is kept.
4027    /// Returns whether such a guard kept a bucket that would otherwise have
4028    /// expired.
4029    ///
4030    /// Errors are logged: an unfinished expiry is retried by the next one, or
4031    /// at the next open.
4032    async fn expire_historic_buckets(&self, current_epoch: EpochId) -> bool {
4033        // Both count the epochs kept below the current one, as `prune` does.
4034        // `None` turns retention off.
4035        let pruning_config = &self.config.authority_store_pruning_config;
4036        let configured_objects_to_retain = pruning_config.num_epochs_to_retain();
4037        let objects_to_retain = configured_objects_to_retain.map(|epochs| {
4038            self.oldest_epoch_retained_for_object_reads()
4039                .map_or(epochs, |epoch| {
4040                    epochs.max(current_epoch.saturating_sub(epoch))
4041                })
4042        });
4043        let held_back = objects_to_retain != configured_objects_to_retain;
4044        let checkpoints_to_retain = pruning_config.num_epochs_to_retain_for_checkpoints();
4045
4046        // Blocks for as long as the deletes and drops take.
4047        let historic_objects = self.historic_objects.clone();
4048        let historic_ledger = self.historic_ledger.clone();
4049        let checkpoint_store = self.checkpoint_store.clone();
4050        let rpc_indexes_store = self.rpc_indexes_store.clone();
4051        let stopped = self.historic_bucket_work_stopped.clone();
4052        let metrics = self.metrics.clone();
4053        let expired = tokio::task::spawn_blocking(move || {
4054            // The index first, so it never names a transaction the ledger has
4055            // dropped, and the checkpoints before the ledger, so a checkpoint
4056            // is never served without its transactions.
4057            // Only runs that dropped a bucket: the ones that drop nothing, as at
4058            // every start, would bury the durations of the ones that do.
4059            let expire =
4060                |store: &str, prune: &dyn Fn() -> Result<Option<PrunedBuckets>, String>| {
4061                    if stopped.load(Ordering::Acquire) {
4062                        return;
4063                    }
4064                    let started = Instant::now();
4065                    match prune() {
4066                        Ok(Some(pruned)) => {
4067                            if pruned.dropped > 0 {
4068                                metrics
4069                                    .historic_bucket_expiry_duration
4070                                    .with_label_values(&[store])
4071                                    .observe(started.elapsed().as_secs_f64());
4072                            }
4073                            metrics
4074                                .historic_bucket_earliest_retained_epoch
4075                                .with_label_values(&[store])
4076                                .set(pruned.earliest_retained as i64);
4077                        }
4078                        Ok(None) => {}
4079                        Err(err) if stopped.load(Ordering::Acquire) => {
4080                            info!(
4081                                store,
4082                                "Stopped expiring historic buckets for a shutdown: {err}"
4083                            )
4084                        }
4085                        Err(err) => error!(store, "Failed to expire historic buckets: {err}"),
4086                    }
4087                };
4088            if let Some(indexes) = &rpc_indexes_store {
4089                expire("indexes", &|| {
4090                    indexes.prune(current_epoch).map_err(|e| format!("{e:?}"))
4091                });
4092            }
4093            if let Some(epochs_to_retain) = checkpoints_to_retain {
4094                // Before each drop, so that no reader is told a checkpoint is
4095                // available after its contents or transactions are gone.
4096                let record =
4097                    || Self::record_pruned_checkpoints(&historic_ledger, &checkpoint_store);
4098                expire("checkpoints", &|| {
4099                    checkpoint_store
4100                        .historic_checkpoints
4101                        .prune(current_epoch, epochs_to_retain, &record)
4102                        .map_err(|e| format!("{e:?}"))
4103                });
4104                expire("ledger", &|| {
4105                    historic_ledger
4106                        .prune(current_epoch, epochs_to_retain, &record)
4107                        .map_err(|e| format!("{e:?}"))
4108                });
4109                // Also when nothing was dropped, after a floor moved without a
4110                // drop, as after a crash between the two.
4111                record();
4112            }
4113            if let Some(epochs_to_retain) = objects_to_retain {
4114                expire("objects", &|| {
4115                    historic_objects
4116                        .prune(current_epoch, epochs_to_retain)
4117                        .map_err(|e| format!("{e:?}"))
4118                });
4119            }
4120        })
4121        .await;
4122        if let Err(err) = expired {
4123            error!("The historic bucket expiry task failed: {err:?}");
4124        }
4125        held_back
4126    }
4127
4128    /// Stops the background expiry and compaction of historic buckets, for a
4129    /// shutdown: an expiry stops before its next store or its next batch of
4130    /// tombstones, and a compaction before its next bucket, as the blocking
4131    /// task holds up the runtime's shutdown. A bucket already being compacted
4132    /// is finished first, which for a large one can take minutes. What is cut
4133    /// short is finished at the next start.
4134    pub fn stop_historic_bucket_work(&self) {
4135        self.historic_bucket_work_stopped
4136            .store(true, Ordering::Release);
4137        self.historic_objects.stop();
4138    }
4139
4140    /// How far this node's newest executed checkpoint is behind the local
4141    /// clock, `None` when nothing is executed yet.
4142    fn execution_lag(&self) -> Option<Duration> {
4143        let Ok(Some(executed)) = self.checkpoint_store.get_highest_executed_checkpoint() else {
4144            return None;
4145        };
4146        let executed_at = UNIX_EPOCH + Duration::from_millis(executed.timestamp_ms);
4147        Some(
4148            SystemTime::now()
4149                .duration_since(executed_at)
4150                .unwrap_or_default(),
4151        )
4152    }
4153
4154    /// Whether this node is still catching up with the network: nothing is
4155    /// executed yet, or its newest executed checkpoint is more than
4156    /// [`CATCH_UP_LAG`] old.
4157    fn is_catching_up(&self) -> bool {
4158        self.execution_lag().is_none_or(|lag| lag > CATCH_UP_LAG)
4159    }
4160
4161    /// Compacts the buckets of every store's closed epochs that are not
4162    /// compacted yet, so a lookup that walks the buckets probes each of them
4163    /// once. Stops before the next bucket once `epochs` announces a new epoch
4164    /// or its sender is dropped, or `stopped` is set, which leaves the rest
4165    /// for the next call.
4166    async fn compact_closed_buckets(
4167        current_epoch: EpochId,
4168        historic_objects: Arc<HistoricObjects>,
4169        historic_ledger: Arc<HistoricLedger>,
4170        checkpoint_store: Arc<CheckpointStore>,
4171        rpc_indexes_store: Option<Arc<RpcIndexesStore>>,
4172        epochs: watch::Receiver<EpochId>,
4173        stopped: Arc<AtomicBool>,
4174    ) {
4175        let compacted = tokio::task::spawn_blocking(move || {
4176            let stop =
4177                || stopped.load(Ordering::Acquire) || !matches!(epochs.has_changed(), Ok(false));
4178            if let Some(indexes) = &rpc_indexes_store {
4179                // The history backfill writes closed epochs' buckets until it
4180                // is done; the next pass compacts them.
4181                if indexes.is_history_backfill_running() {
4182                    info!("Not compacting the RPC index history while its backfill runs");
4183                } else if let Err(err) = indexes.compact_closed_buckets(current_epoch, &stop) {
4184                    error!("Failed to compact the RPC index history: {err:?}");
4185                }
4186            }
4187            if let Err(err) = checkpoint_store
4188                .historic_checkpoints
4189                .compact_closed_buckets(current_epoch, &stop)
4190            {
4191                error!("Failed to compact historic checkpoint buckets: {err:?}");
4192            }
4193            if let Err(err) = historic_ledger.compact_closed_buckets(current_epoch, &stop) {
4194                error!("Failed to compact historic ledger buckets: {err:?}");
4195            }
4196            if let Err(err) = historic_objects.compact_closed_buckets(current_epoch, &stop) {
4197                error!("Failed to compact historic object buckets: {err:?}");
4198            }
4199        })
4200        .await;
4201        if let Err(err) = compacted {
4202            error!("The closed bucket compaction task failed: {err:?}");
4203        }
4204    }
4205
4206    /// Expires the historic buckets for each epoch `epochs` announces,
4207    /// starting with the one it holds, and again once a retention guard is
4208    /// dropped after one kept a bucket; then compacts the closed buckets.
4209    /// Returns once `state` is dropped.
4210    ///
4211    /// Compaction waits while a guard keeps a bucket, which may expire at the
4212    /// next run, and while the node catches up: epochs then close one after
4213    /// another, the oldest expire soon after, and nothing on the catch-up path
4214    /// walks the closed buckets. It is retried every
4215    /// [`COMPACTION_RECHECK_INTERVAL`] until the node has caught up.
4216    async fn expire_historic_buckets_in_background(
4217        state: Weak<AuthorityState>,
4218        mut epochs: watch::Receiver<EpochId>,
4219        released: Arc<Notify>,
4220    ) {
4221        // Logged when it changes, so an operator can see why closed buckets
4222        // are not compacted.
4223        let mut compaction_held_back = None;
4224        loop {
4225            let epoch = *epochs.borrow_and_update();
4226            let Some(this) = state.upgrade() else {
4227                return;
4228            };
4229            if this.historic_bucket_work_stopped.load(Ordering::Acquire) {
4230                return;
4231            }
4232            let held_back = this.expire_historic_buckets(epoch).await;
4233            let catching_up = this.is_catching_up();
4234            let state = (held_back, catching_up);
4235            if compaction_held_back != Some(state) {
4236                match state {
4237                    (false, false) => info!("Compacting the buckets of closed epochs"),
4238                    (true, _) => info!(
4239                        "Not compacting the buckets of closed epochs while a read holds back the \
4240                         expiry of historic objects"
4241                    ),
4242                    (false, true) => info!(
4243                        lag_s = this.execution_lag().map(|lag| lag.as_secs()),
4244                        "Not compacting the buckets of closed epochs while the node catches up: \
4245                         its newest executed checkpoint is more than {} minutes old",
4246                        CATCH_UP_LAG.as_secs() / 60
4247                    ),
4248                }
4249                compaction_held_back = Some(state);
4250            }
4251            let compaction = (!held_back && !catching_up).then(|| {
4252                (
4253                    this.historic_objects.clone(),
4254                    this.historic_ledger.clone(),
4255                    this.checkpoint_store.clone(),
4256                    this.rpc_indexes_store.clone(),
4257                    this.historic_bucket_work_stopped.clone(),
4258                )
4259            });
4260            drop(this);
4261            if let Some((
4262                historic_objects,
4263                historic_ledger,
4264                checkpoint_store,
4265                rpc_indexes_store,
4266                stopped,
4267            )) = compaction
4268            {
4269                Self::compact_closed_buckets(
4270                    epoch,
4271                    historic_objects,
4272                    historic_ledger,
4273                    checkpoint_store,
4274                    rpc_indexes_store,
4275                    epochs.clone(),
4276                    stopped,
4277                )
4278                .await;
4279            }
4280            tokio::select! {
4281                changed = epochs.changed() => {
4282                    if changed.is_err() {
4283                        return;
4284                    }
4285                }
4286                () = released.notified(), if held_back => {
4287                    // Guards come and go with requests, so one expiry per
4288                    // interval rather than one per dropped guard.
4289                    tokio::time::sleep(HELD_BACK_EXPIRY_INTERVAL).await;
4290                }
4291                () = tokio::time::sleep(COMPACTION_RECHECK_INTERVAL), if catching_up && !held_back => {}
4292            }
4293        }
4294    }
4295
4296    /// Moves `HighestPruned` up to the last checkpoint below the epochs both
4297    /// the ledger and the checkpoint history still retain: a checkpoint whose
4298    /// contents or transactions are gone can no longer be served to state-sync
4299    /// peers or RPC clients. Either expiry can fail or be cut short without the
4300    /// other, so this takes whichever floor is higher.
4301    fn record_pruned_checkpoints(
4302        historic_ledger: &HistoricLedger,
4303        checkpoint_store: &CheckpointStore,
4304    ) {
4305        let floor = historic_ledger.earliest_retained_epoch().max(
4306            checkpoint_store
4307                .historic_checkpoints
4308                .earliest_retained_epoch(),
4309        );
4310        if let Err(err) = checkpoint_store.advance_highest_pruned_checkpoint(floor) {
4311            error!("Failed to record the pruned checkpoint range: {err:?}");
4312        }
4313    }
4314
4315    /// The oldest epoch whose historic-object bucket a [`Self::retain_objects`]
4316    /// guard may still read from, `None` when no guard is held.
4317    fn oldest_epoch_retained_for_object_reads(&self) -> Option<EpochId> {
4318        let retained = self.object_retention.lowest_retained()?;
4319        // A retained checkpoint was executed, so its summary is here; without
4320        // it, nothing is known to be safe to drop.
4321        match self
4322            .checkpoint_store
4323            .get_checkpoint_by_sequence_number(retained)
4324        {
4325            Ok(checkpoint) => Some(checkpoint.map_or(0, |checkpoint| checkpoint.epoch())),
4326            Err(err) => {
4327                error!(
4328                    "Failed to read the epoch of retained checkpoint {retained}, keeping \
4329                     every historic object bucket: {err:?}"
4330                );
4331                Some(0)
4332            }
4333        }
4334    }
4335
4336    /// Advance the epoch store to the next epoch for testing only.
4337    /// This only manually sets all the places where we have the epoch number.
4338    /// It doesn't properly reconfigure the node, hence should be only used for
4339    /// testing.
4340    pub async fn reconfigure_for_testing(&self) {
4341        self.reconfigure_for_testing_impl(None).await;
4342    }
4343
4344    /// Like [`Self::reconfigure_for_testing`], but the next epoch uses the
4345    /// given protocol config.
4346    pub async fn reconfigure_for_testing_with_protocol_config(
4347        &self,
4348        protocol_config: ProtocolConfig,
4349    ) {
4350        self.reconfigure_for_testing_impl(Some(protocol_config))
4351            .await;
4352    }
4353
4354    async fn reconfigure_for_testing_impl(&self, protocol_config: Option<ProtocolConfig>) {
4355        let mut execution_lock = self.execution_lock_for_reconfiguration().await;
4356        let epoch_store = self.epoch_store_for_testing().clone();
4357        // Default to the epoch store's config, whose override guard may have
4358        // been dropped. Read it under the lock so config and epoch store are
4359        // one snapshot.
4360        let protocol_config =
4361            protocol_config.unwrap_or_else(|| epoch_store.protocol_config().clone());
4362        let _guard =
4363            ProtocolConfig::apply_overrides_for_testing(move |_, _| protocol_config.clone());
4364        let new_epoch_store = epoch_store.new_at_next_epoch_for_testing(
4365            self.get_backing_package_store().clone(),
4366            &self.config.expensive_safety_check_config,
4367            self.checkpoint_store
4368                .get_epoch_last_checkpoint(epoch_store.epoch())
4369                .unwrap()
4370                .map(|c| c.sequence_number())
4371                .unwrap_or_default(),
4372        );
4373        let new_epoch = new_epoch_store.epoch();
4374        match self.execution_scheduler.as_ref() {
4375            ExecutionSchedulerWrapper::ExecutionScheduler(_) => {}
4376            ExecutionSchedulerWrapper::TransactionManager(tm) => {
4377                tm.reconfigure(new_epoch);
4378            }
4379        }
4380        self.epoch_store.store(new_epoch_store);
4381        epoch_store.epoch_terminated().await;
4382        *execution_lock = new_epoch;
4383    }
4384
4385    #[instrument(level = "error", skip_all)]
4386    fn check_system_consistency(
4387        &self,
4388        cur_epoch_store: &AuthorityPerEpochStore,
4389        state_hasher: Arc<GlobalStateHasher>,
4390        expensive_safety_check_config: &ExpensiveSafetyCheckConfig,
4391        epoch_supply_change: i64,
4392    ) -> IotaResult<()> {
4393        info!(
4394            "Performing iota conservation consistency check for epoch {}",
4395            cur_epoch_store.epoch()
4396        );
4397
4398        if cfg!(debug_assertions) {
4399            cur_epoch_store.check_all_executed_transactions_in_checkpoint();
4400        }
4401
4402        self.get_reconfig_api()
4403            .try_expensive_check_iota_conservation(cur_epoch_store, Some(epoch_supply_change))?;
4404
4405        // check for root state hash consistency with live object set
4406        if expensive_safety_check_config.enable_state_consistency_check() {
4407            info!(
4408                "Performing state consistency check for epoch {}",
4409                cur_epoch_store.epoch()
4410            );
4411            self.expensive_check_is_consistent_state(state_hasher, cur_epoch_store);
4412        }
4413
4414        Ok(())
4415    }
4416
4417    fn expensive_check_is_consistent_state(
4418        &self,
4419        state_hasher: Arc<GlobalStateHasher>,
4420        cur_epoch_store: &AuthorityPerEpochStore,
4421    ) {
4422        let live_object_set_hash = state_hasher
4423            .digest_live_object_set()
4424            .expect("Reading the live object set cannot fail");
4425
4426        let root_state_hash: ECMHLiveObjectSetDigest = self
4427            .get_global_state_hash_store()
4428            .get_root_state_hash_for_epoch(cur_epoch_store.epoch())
4429            .expect("Retrieving root state hash cannot fail")
4430            .expect("Root state hash for epoch must exist")
4431            .1
4432            .digest()
4433            .into();
4434
4435        let is_inconsistent = root_state_hash != live_object_set_hash;
4436        if is_inconsistent {
4437            debug_fatal!(
4438                "Inconsistent state detected: root state hash: {:?}, live object set hash: {:?}",
4439                root_state_hash,
4440                live_object_set_hash
4441            );
4442        } else {
4443            info!("State consistency check passed");
4444        }
4445
4446        state_hasher.set_inconsistent_state(is_inconsistent);
4447    }
4448
4449    pub fn current_epoch_for_testing(&self) -> EpochId {
4450        self.epoch_store_for_testing().epoch()
4451    }
4452
4453    /// Lets the consumer take its database snapshot of the perpetual store as
4454    /// the epoch ends, when there is one. See
4455    /// [`EpochEndDbSnapshotHandle::hand_over`].
4456    #[instrument(level = "error", skip_all)]
4457    async fn hand_over_epoch_end_db_snapshot(&self, epoch: EpochId) {
4458        let Some(handle) = &self.epoch_end_db_snapshots else {
4459            return;
4460        };
4461        let _metrics_guard = self
4462            .metrics
4463            .epoch_end_db_snapshot_handover_latency
4464            .start_timer();
4465        if handle.hand_over(epoch).await == HandOver::Skipped {
4466            self.metrics.epoch_end_db_snapshots_skipped.inc();
4467        }
4468    }
4469
4470    /// Load the current epoch store. This can change during reconfiguration. To
4471    /// ensure that we never end up accessing different epoch stores in a
4472    /// single task, we need to make sure that this is called once per task.
4473    /// Each call needs to be carefully audited to ensure it is
4474    /// the case. This also means we should minimize the number of call-sites.
4475    /// Only call it when there is no way to obtain it from somewhere else.
4476    pub fn load_epoch_store_one_call_per_task(&self) -> Guard<Arc<AuthorityPerEpochStore>> {
4477        self.epoch_store.load()
4478    }
4479
4480    // Load the epoch store, should be used in tests only.
4481    pub fn epoch_store_for_testing(&self) -> Guard<Arc<AuthorityPerEpochStore>> {
4482        self.load_epoch_store_one_call_per_task()
4483    }
4484
4485    pub fn clone_committee_for_testing(&self) -> Committee {
4486        Committee::clone(self.epoch_store_for_testing().committee())
4487    }
4488
4489    #[instrument(level = "trace", skip_all)]
4490    pub fn try_get_object(&self, object_id: &ObjectId) -> IotaResult<Option<Object>> {
4491        self.get_object_store()
4492            .try_get_object(object_id)
4493            .map_err(Into::into)
4494    }
4495
4496    /// Non-fallible version of `try_get_object`.
4497    pub fn get_object(&self, object_id: &ObjectId) -> Option<Object> {
4498        self.try_get_object(object_id)
4499            .expect("storage access failed")
4500    }
4501
4502    pub fn get_iota_system_package_object_ref(&self) -> IotaResult<ObjectReference> {
4503        Ok(self
4504            .try_get_object(&ObjectId::SYSTEM)?
4505            .expect("system package should always exist")
4506            .object_ref())
4507    }
4508
4509    // This function is only used for testing.
4510    pub fn get_iota_system_state_object_for_testing(&self) -> IotaResult<IotaSystemState> {
4511        self.get_object_cache_reader()
4512            .try_get_iota_system_state_object_unsafe()
4513    }
4514
4515    #[instrument(level = "trace", skip_all)]
4516    pub fn get_checkpoint_by_sequence_number(
4517        &self,
4518        sequence_number: CheckpointSequenceNumber,
4519    ) -> IotaResult<Option<VerifiedCheckpoint>> {
4520        Ok(self
4521            .checkpoint_store
4522            .get_checkpoint_by_sequence_number(sequence_number)?)
4523    }
4524
4525    /// Wait until the given transactions are in a checkpoint and the RPC
4526    /// indexes include that checkpoint.
4527    ///
4528    /// Returns a mapping from transaction digest to
4529    /// `(checkpoint_sequence_number, checkpoint_timestamp_ms)`.
4530    /// On timeout, returns partial results for any transactions that were
4531    /// already checkpointed.
4532    ///
4533    /// If the timeout runs out before the indexes include a checkpointed
4534    /// transaction, the transaction is returned all the same, as it is final,
4535    /// and the wait is counted in `checkpoint_inclusion_index_wait_timeouts`.
4536    /// A caller that promises a read right after to see the transaction checks
4537    /// [`Self::is_checkpoint_indexed`] for it.
4538    ///
4539    /// The wait survives epoch boundaries: a transaction in flight at a
4540    /// boundary may only be checkpointed in the next epoch, and still resolves
4541    /// here under the original deadline.
4542    pub async fn wait_for_checkpoint_inclusion(
4543        &self,
4544        digests: &[TransactionDigest],
4545        timeout: Duration,
4546    ) -> IotaResult<BTreeMap<TransactionDigest, (CheckpointSequenceNumber, u64)>> {
4547        let deadline = tokio::time::Instant::now() + timeout;
4548        let results = self.wait_for_checkpoint_mapping(digests, deadline).await?;
4549
4550        // The index commit precedes the `highest_executed_checkpoint` bump,
4551        // in checkpoint order, so the bump means "indexed through this
4552        // checkpoint". Nodes without indexes bump too, so this cannot hang.
4553        if let Some(max_seq) = results.values().map(|(seq, _)| *seq).max() {
4554            let indexed = self
4555                .checkpoint_store
4556                .notify_read_executed_checkpoint(max_seq);
4557            let all_checkpointed = digests.iter().all(|digest| results.contains_key(digest));
4558            // A partial result already reports a timeout to the caller.
4559            if tokio::time::timeout_at(deadline, indexed).await.is_err() && all_checkpointed {
4560                self.metrics.checkpoint_inclusion_index_wait_timeouts.inc();
4561                warn!(
4562                    checkpoint_seq = max_seq,
4563                    "transactions are checkpointed but the RPC indexes have not caught up \
4564                     within the timeout; reporting them without the read-after-write guarantee"
4565                );
4566            }
4567        }
4568
4569        Ok(results)
4570    }
4571
4572    /// Whether the RPC indexes include checkpoint `seq`, so that a read sent
4573    /// now sees its transactions.
4574    pub fn is_checkpoint_indexed(&self, seq: CheckpointSequenceNumber) -> IotaResult<bool> {
4575        Ok(self
4576            .checkpoint_store
4577            .get_highest_executed_checkpoint_seq_number()?
4578            .is_some_and(|indexed| seq <= indexed))
4579    }
4580
4581    /// Wait until each of `digests` is in a checkpoint, or until `deadline`
4582    /// passes. Maps each found digest to `(checkpoint_sequence_number,
4583    /// checkpoint_timestamp_ms)`. Unlike `wait_for_checkpoint_inclusion`, this
4584    /// does not wait until the checkpoint is indexed.
4585    pub(crate) async fn wait_for_checkpoint_mapping(
4586        &self,
4587        digests: &[TransactionDigest],
4588        deadline: tokio::time::Instant,
4589    ) -> IotaResult<BTreeMap<TransactionDigest, (CheckpointSequenceNumber, u64)>> {
4590        let mut checkpoint_timestamp_cache = HashMap::<CheckpointSequenceNumber, u64>::new();
4591        let mut results = BTreeMap::new();
4592        let mut remaining = digests.to_vec();
4593        let mut epoch_store = self.load_epoch_store_one_call_per_task().clone();
4594
4595        loop {
4596            let wait = epoch_store.wait_for_transactions_in_checkpoint_with_timeout(
4597                &remaining,
4598                deadline.saturating_duration_since(tokio::time::Instant::now()),
4599                |seq| self.checkpoint_timestamp_ms_cached(seq, &mut checkpoint_timestamp_cache),
4600            );
4601            tokio::select! {
4602                wait_results = wait => {
4603                    for (digest, seq_and_ts) in remaining.iter().zip(wait_results?) {
4604                        if let Some(seq_and_ts) = seq_and_ts {
4605                            results.insert(*digest, seq_and_ts);
4606                        }
4607                    }
4608                    return Ok(results);
4609                }
4610                _ = epoch_store.wait_epoch_terminated() => {}
4611            }
4612
4613            // The epoch ended mid-wait, and this epoch store's notifications
4614            // can no longer fire: whatever is still uncheckpointed here is
4615            // checkpointed in the next epoch, on the next store. Cancelling
4616            // the wait may also have dropped notifications it had already
4617            // received, but the table write precedes each notification, so
4618            // re-reading the table recovers them.
4619            let found = match epoch_store.multi_get_transaction_checkpoint(&remaining) {
4620                Ok(found) => found,
4621                // The table handles were already released. They are released
4622                // long after the epoch's checkpoints are executed, so nothing
4623                // waited on here can still be checkpointed in the old epoch;
4624                // move on to the next store.
4625                Err(IotaError::EpochEnded(_)) => vec![None; remaining.len()],
4626                Err(err) => return Err(err),
4627            };
4628            let mut still_uncheckpointed = Vec::new();
4629            for (digest, found_seq) in remaining.iter().zip(found) {
4630                match found_seq {
4631                    Some(seq) => {
4632                        let ts = self
4633                            .checkpoint_timestamp_ms_cached(seq, &mut checkpoint_timestamp_cache);
4634                        results.insert(*digest, (seq, ts));
4635                    }
4636                    None => still_uncheckpointed.push(*digest),
4637                }
4638            }
4639            remaining = still_uncheckpointed;
4640            if remaining.is_empty() {
4641                return Ok(results);
4642            }
4643
4644            match self
4645                .wait_for_next_epoch_store(epoch_store.epoch(), deadline)
4646                .await
4647            {
4648                Some(next) => epoch_store = next,
4649                None => return Ok(results),
4650            }
4651        }
4652    }
4653
4654    /// Wait for the epoch store to be swapped to an epoch later than
4655    /// `prev_epoch`, returning `None` if `deadline` passes first.
4656    async fn wait_for_next_epoch_store(
4657        &self,
4658        prev_epoch: EpochId,
4659        deadline: tokio::time::Instant,
4660    ) -> Option<Arc<AuthorityPerEpochStore>> {
4661        // There is no notification for the epoch-store swap, and termination
4662        // and swap can come in either order (`reconfigure` terminates the old
4663        // epoch first, `reconfigure_for_testing` swaps first), so the swap is
4664        // polled at this interval.
4665        const EPOCH_STORE_SWAP_POLL_INTERVAL: Duration = Duration::from_millis(100);
4666
4667        loop {
4668            // Deliberately re-loaded on each poll; the one-call-per-task rule
4669            // guards against *unaware* mixing of epoch stores within a task.
4670            let current = self.load_epoch_store_one_call_per_task().clone();
4671            if current.epoch() > prev_epoch {
4672                return Some(current);
4673            }
4674            if tokio::time::Instant::now() >= deadline {
4675                return None;
4676            }
4677            tokio::time::sleep(EPOCH_STORE_SWAP_POLL_INTERVAL).await;
4678        }
4679    }
4680
4681    /// Resolve a checkpoint's timestamp, memoizing lookups in `cache` so
4682    /// multiple transactions in the same checkpoint trigger a single
4683    /// checkpoint summary lookup.
4684    fn checkpoint_timestamp_ms_cached(
4685        &self,
4686        seq: CheckpointSequenceNumber,
4687        cache: &mut HashMap<CheckpointSequenceNumber, u64>,
4688    ) -> u64 {
4689        *cache.entry(seq).or_insert_with(|| {
4690            self.get_checkpoint_by_sequence_number(seq)
4691                .ok()
4692                .flatten()
4693                .map(|c| c.timestamp_ms)
4694                .unwrap_or(0)
4695        })
4696    }
4697
4698    #[instrument(level = "trace", skip_all)]
4699    pub fn get_transaction_checkpoint_for_tests(
4700        &self,
4701        digest: &TransactionDigest,
4702        epoch_store: &AuthorityPerEpochStore,
4703    ) -> IotaResult<Option<VerifiedCheckpoint>> {
4704        let checkpoint = epoch_store.get_transaction_checkpoint(digest)?;
4705        let Some(checkpoint) = checkpoint else {
4706            return Ok(None);
4707        };
4708        let checkpoint = self
4709            .checkpoint_store
4710            .get_checkpoint_by_sequence_number(checkpoint)?;
4711        Ok(checkpoint)
4712    }
4713
4714    #[instrument(level = "trace", skip_all)]
4715    pub fn get_object_read(&self, object_id: &ObjectId) -> IotaResult<ObjectRead> {
4716        Ok(
4717            match self
4718                .get_object_cache_reader()
4719                .try_get_latest_object_or_tombstone(*object_id)?
4720            {
4721                Some((_, ObjectOrTombstone::Object(object))) => {
4722                    let layout = self.get_object_layout(&object)?;
4723                    ObjectRead::Exists(object.object_ref(), object, layout)
4724                }
4725                Some((_, ObjectOrTombstone::Tombstone(objref))) => ObjectRead::Deleted(objref),
4726                None => ObjectRead::NotExists(*object_id),
4727            },
4728        )
4729    }
4730
4731    /// Chain Identifier is the digest of the genesis checkpoint.
4732    pub fn get_chain_identifier(&self) -> ChainIdentifier {
4733        self.chain_identifier
4734    }
4735
4736    #[instrument(level = "trace", skip_all)]
4737    pub fn get_move_object<T>(&self, object_id: &ObjectId) -> IotaResult<T>
4738    where
4739        T: DeserializeOwned,
4740    {
4741        let o = self.get_object_read(object_id)?.into_object()?;
4742        if let Some(move_object) = o.data.as_opt_struct() {
4743            Ok(bcs::from_bytes(move_object.contents()).map_err(|e| {
4744                IotaError::ObjectDeserialization {
4745                    error: format!("{e}"),
4746                }
4747            })?)
4748        } else {
4749            Err(IotaError::ObjectDeserialization {
4750                error: format!("Provided object : [{object_id}] is not a Move object."),
4751            })
4752        }
4753    }
4754
4755    /// This function aims to serve rpc reads on past objects and
4756    /// we don't expect it to be called for other purposes.
4757    /// Depending on the object pruning policies that will be enforced in the
4758    /// future there is no software-level guarantee/SLA to retrieve an object
4759    /// with an old version even if it exists/existed.
4760    #[instrument(level = "trace", skip_all)]
4761    pub fn get_past_object_read(
4762        &self,
4763        object_id: &ObjectId,
4764        version: Version,
4765    ) -> IotaResult<PastObjectRead> {
4766        // Firstly we see if the object ever existed by getting its latest data
4767        let Some(obj_ref) = self
4768            .get_object_cache_reader()
4769            .try_get_latest_object_ref_or_tombstone(*object_id)?
4770        else {
4771            return Ok(PastObjectRead::ObjectNotExists(*object_id));
4772        };
4773
4774        if version > obj_ref.version {
4775            return Ok(PastObjectRead::VersionTooHigh {
4776                object_id: *object_id,
4777                asked_version: version,
4778                latest_version: obj_ref.version,
4779            });
4780        }
4781
4782        if version < obj_ref.version {
4783            // Read past objects
4784            return Ok(match self.read_object_at_version(object_id, version)? {
4785                Some((object, layout)) => {
4786                    let obj_ref = object.object_ref();
4787                    PastObjectRead::VersionFound(obj_ref, object, layout)
4788                }
4789
4790                None => PastObjectRead::VersionNotFound(*object_id, version),
4791            });
4792        }
4793
4794        if !obj_ref.digest.is_alive() {
4795            return Ok(PastObjectRead::ObjectDeleted(obj_ref));
4796        }
4797
4798        match self.read_object_at_version(object_id, obj_ref.version)? {
4799            Some((object, layout)) => Ok(PastObjectRead::VersionFound(obj_ref, object, layout)),
4800            None => {
4801                debug_fatal!(
4802                    "Object with in parent_entry is missing from object store, datastore is \
4803                     inconsistent",
4804                );
4805                Err(UserInputError::ObjectNotFound {
4806                    object_id: *object_id,
4807                    version: Some(obj_ref.version),
4808                }
4809                .into())
4810            }
4811        }
4812    }
4813
4814    #[instrument(level = "trace", skip_all)]
4815    fn read_object_at_version(
4816        &self,
4817        object_id: &ObjectId,
4818        version: Version,
4819    ) -> IotaResult<Option<(Object, Option<MoveStructLayout>)>> {
4820        let Some(object) =
4821            self.get_object_with_historic_fallback(&ObjectKey(*object_id, version))?
4822        else {
4823            return Ok(None);
4824        };
4825
4826        let layout = self.get_object_layout(&object)?;
4827        Ok(Some((object, layout)))
4828    }
4829
4830    /// The object at an exact version: the live table first, the buckets after
4831    /// a miss there. See [`HistoricObjects::fill_missing`] for when a read may
4832    /// do this.
4833    pub(crate) fn get_object_with_historic_fallback(
4834        &self,
4835        key: &ObjectKey,
4836    ) -> IotaResult<Option<Object>> {
4837        match self
4838            .get_object_cache_reader()
4839            .try_get_object_by_key(&key.0, key.1)?
4840        {
4841            Some(object) => Ok(Some(object)),
4842            None => self.historic_objects.get(key),
4843        }
4844    }
4845
4846    /// The object store for reads that resolve index rows: the row's version
4847    /// leaves the live table when its checkpoint's outputs commit, which is
4848    /// before the index update that replaces the row.
4849    fn object_store_with_historic_fallback(&self) -> ObjectStoreWithHistoricFallback<'_> {
4850        ObjectStoreWithHistoricFallback(self)
4851    }
4852
4853    /// The layout of `object`'s Move struct, `None` for a package.
4854    pub fn get_object_layout(&self, object: &Object) -> IotaResult<Option<MoveStructLayout>> {
4855        let layout = object
4856            .data
4857            .as_opt_struct()
4858            .map(|object| {
4859                into_struct_layout(
4860                    self.load_epoch_store_one_call_per_task()
4861                        .executor()
4862                        // TODO(cache) - must read through cache
4863                        .type_layout_resolver(Box::new(self.get_backing_package_store().as_ref()))
4864                        .get_annotated_layout(object.struct_tag())?,
4865                )
4866            })
4867            .transpose()?;
4868        Ok(layout)
4869    }
4870
4871    /// One page of `owner`'s objects; see
4872    /// [`RpcIndexesStore::get_owner_objects`].
4873    #[instrument(level = "trace", skip_all)]
4874    pub fn get_owner_objects(
4875        &self,
4876        owner: Address,
4877        // If `Some`, the query will start from the next item after the specified cursor
4878        cursor: Option<&OwnedObjectCursor>,
4879        limit: usize,
4880        filter: Option<IotaObjectDataFilter>,
4881        max_read: usize,
4882    ) -> IotaResult<OwnedObjectsPage> {
4883        let indexes = self.require_jsonrpc_indexes()?;
4884        indexes.get_owner_objects(
4885            owner,
4886            cursor,
4887            limit,
4888            filter,
4889            max_read,
4890            &self.object_store_with_historic_fallback(),
4891        )
4892    }
4893
4894    /// One page of `owner`'s coins in the owner index's order, of
4895    /// `coin_type` alone when it is given. The returned type tag is the
4896    /// coin's own `T`, not the `Coin<T>` the object carries.
4897    #[instrument(level = "trace", skip_all)]
4898    pub fn get_owned_coins_page(
4899        &self,
4900        owner: Address,
4901        // If `Some`, the query will start from the next item after the specified cursor
4902        cursor: Option<&OwnedObjectCursor>,
4903        coin_type: Option<TypeTag>,
4904        limit: usize,
4905    ) -> IotaResult<Vec<(TypeTag, ObjectId, CoinInfo, OwnedObjectCursor)>> {
4906        let indexes = self.require_jsonrpc_indexes()?;
4907        indexes.get_owned_coins(
4908            owner,
4909            cursor,
4910            coin_type,
4911            limit,
4912            &self.object_store_with_historic_fallback(),
4913        )
4914    }
4915
4916    /// The cursors of `owner`'s next rows of `Coin<coin_type>`; see
4917    /// [`RpcIndexesStore::owned_coin_rows`].
4918    #[instrument(level = "trace", skip_all)]
4919    pub fn owned_coin_rows(
4920        &self,
4921        owner: Address,
4922        cursor: Option<&OwnedObjectCursor>,
4923        coin_type: TypeTag,
4924        limit: usize,
4925    ) -> IotaResult<Vec<OwnedObjectCursor>> {
4926        self.require_jsonrpc_indexes()?
4927            .owned_coin_rows(owner, cursor, coin_type, limit)
4928    }
4929
4930    #[instrument(level = "trace", skip_all)]
4931    pub fn get_move_objects<T>(&self, owner: Address, tag: StructTag) -> IotaResult<Vec<T>>
4932    where
4933        T: DeserializeOwned,
4934    {
4935        let indexes = self.require_jsonrpc_indexes()?;
4936        // The filter matches the type's identifier only, so the exact type is
4937        // checked per row.
4938        let object_ids = indexes
4939            .owner_iter(owner, None, OwnerTypeFilter::from_struct_tag(Some(&tag)))?
4940            .filter_ok(|(_, info)| info.object_type == tag)
4941            .map_ok(|(key, info)| ObjectKey(key.object_id, info.version))
4942            .collect::<Result<Vec<_>, _>>()?;
4943        let mut move_objects = vec![];
4944
4945        let objects = self
4946            .object_store_with_historic_fallback()
4947            .try_multi_get_objects_by_key(&object_ids)?;
4948
4949        for (o, id) in objects.into_iter().zip(object_ids) {
4950            let object = o.ok_or_else(|| {
4951                IotaError::from(UserInputError::ObjectNotFound {
4952                    object_id: id.0,
4953                    version: Some(id.1),
4954                })
4955            })?;
4956            let move_object = object.data.as_opt_struct().ok_or_else(|| {
4957                IotaError::from(UserInputError::MovePackageAsObject { object_id: id.0 })
4958            })?;
4959            move_objects.push(bcs::from_bytes(move_object.contents()).map_err(|e| {
4960                IotaError::ObjectDeserialization {
4961                    error: format!("{e}"),
4962                }
4963            })?);
4964        }
4965        Ok(move_objects)
4966    }
4967
4968    /// Returns one entry per index row, in index order, for at most `limit`
4969    /// rows. A `None` info marks a field that no longer resolves, so callers
4970    /// can derive pagination cursors from the returned ids alone.
4971    #[instrument(level = "trace", skip_all)]
4972    pub fn get_dynamic_fields(
4973        &self,
4974        owner: ObjectId,
4975        // If `Some`, the query will start from the next item after the specified cursor
4976        cursor: Option<ObjectId>,
4977        limit: usize,
4978    ) -> IotaResult<Vec<(ObjectId, Option<DynamicFieldInfo>)>> {
4979        let indexes = self.require_jsonrpc_indexes()?;
4980        let epoch_store = self.load_epoch_store_one_call_per_task();
4981        let mut base_resolver = epoch_store
4982            .executor()
4983            .type_layout_resolver(Box::new(self.get_backing_package_store().clone()));
4984        // One layout per distinct field type for the whole page.
4985        let mut layout_resolver = CachingLayoutResolver::new(base_resolver.as_mut());
4986        let object_store = self.get_object_store();
4987
4988        let mut fields = Vec::new();
4989        for field_id in indexes
4990            .get_dynamic_field_ids_iterator(owner, cursor)?
4991            .take(limit)
4992        {
4993            let field_id = field_id?;
4994            let Some(field_object) = object_store.try_get_object(&field_id)? else {
4995                fields.push((field_id, None));
4996                continue;
4997            };
4998            match try_create_dynamic_field_info(
4999                &field_object,
5000                object_store.as_ref(),
5001                &mut layout_resolver,
5002            ) {
5003                Ok(info) => fields.push((field_id, info)),
5004                Err(e) => {
5005                    warn!(?field_id, "failed to resolve dynamic field: {e}");
5006                    fields.push((field_id, None));
5007                }
5008            }
5009        }
5010        Ok(fields)
5011    }
5012
5013    #[instrument(level = "trace", skip_all)]
5014    /// The objects of `owner`'s first `limit` dynamic fields, in the order
5015    /// [`Self::get_dynamic_fields`] lists them; `None` for a field whose
5016    /// object is gone.
5017    pub fn get_dynamic_field_objects(
5018        &self,
5019        owner: ObjectId,
5020        limit: usize,
5021    ) -> IotaResult<Vec<(ObjectId, Option<Object>)>> {
5022        let indexes = self.require_jsonrpc_indexes()?;
5023        let object_store = self.get_object_store();
5024        indexes
5025            .get_dynamic_field_ids_iterator(owner, None)?
5026            .take(limit)
5027            .map(|field_id| {
5028                let field_id = field_id?;
5029                Ok((field_id, object_store.try_get_object(&field_id)?))
5030            })
5031            .collect()
5032    }
5033
5034    /// The id of the object `get_dynamic_field_object` returns.
5035    pub fn get_dynamic_field_object_id(
5036        &self,
5037        owner: ObjectId,
5038        name_type: TypeTag,
5039        name_bcs_bytes: &[u8],
5040    ) -> IotaResult<Option<ObjectId>> {
5041        Ok(self
5042            .get_dynamic_field_object(owner, name_type, name_bcs_bytes)?
5043            .map(|object| object.id()))
5044    }
5045
5046    /// The object of `owner`'s dynamic field named `name_type` and
5047    /// `name_bcs_bytes`: the value object for a dynamic object field, the
5048    /// `Field` object otherwise; `None` when there is no such field.
5049    pub fn get_dynamic_field_object(
5050        &self,
5051        owner: ObjectId,
5052        name_type: TypeTag,
5053        name_bcs_bytes: &[u8],
5054    ) -> IotaResult<Option<Object>> {
5055        let derive = |name_type: &TypeTag| {
5056            dynamic_field::derive_dynamic_field_id(owner, name_type, name_bcs_bytes).map_err(|e| {
5057                IotaError::Unknown(format!(
5058                    "Unable to generate dynamic field id. Got error: {e:?}"
5059                ))
5060            })
5061        };
5062        let object_store = self.get_object_store();
5063
5064        // A field's id is derived from its parent and name, so the object
5065        // store alone says whether it exists, as soon as the transaction that
5066        // created it has executed.
5067        if let Some(field) = object_store.try_get_object(&derive(&name_type)?)? {
5068            let wrapped_name = matches!(
5069                &name_type,
5070                TypeTag::Struct(tag) if DynamicFieldInfo::is_dynamic_object_field_wrapper(tag)
5071            );
5072            if !wrapped_name {
5073                return Ok(Some(field));
5074            }
5075            // An already-wrapped name type addresses a dynamic object field
5076            // directly.
5077            return self.dynamic_field_value(field);
5078        }
5079
5080        // A dynamic object field is stored under its `Field` wrapper, which
5081        // holds the id of the value object the caller is after.
5082        let wrapper_type = TypeTag::Struct(Box::new(
5083            DynamicFieldInfo::dynamic_object_field_wrapper(name_type),
5084        ));
5085        match object_store.try_get_object(&derive(&wrapper_type)?)? {
5086            Some(wrapper) => self.dynamic_field_value(wrapper),
5087            None => Ok(None),
5088        }
5089    }
5090
5091    /// The object a dynamic-field lookup resolves `field` to: the value
5092    /// object for a dynamic object field, `field` itself otherwise, or `None`
5093    /// when the field no longer resolves.
5094    fn dynamic_field_value(&self, field: Object) -> IotaResult<Option<Object>> {
5095        let epoch_store = self.load_epoch_store_one_call_per_task();
5096        let mut layout_resolver = epoch_store
5097            .executor()
5098            .type_layout_resolver(Box::new(self.get_backing_package_store().clone()));
5099        let Some(info) = try_create_dynamic_field_info(
5100            &field,
5101            self.get_object_store().as_ref(),
5102            layout_resolver.as_mut(),
5103        )?
5104        else {
5105            return Ok(None);
5106        };
5107        if info.object_id == field.id() {
5108            return Ok(Some(field));
5109        }
5110        Ok(self.get_object_store().try_get_object(&info.object_id)?)
5111    }
5112
5113    #[instrument(level = "trace", skip_all)]
5114    pub fn get_total_transaction_blocks(&self) -> IotaResult<u64> {
5115        Ok(self.get_indexes()?.next_sequence_number())
5116    }
5117
5118    #[instrument(level = "trace", skip_all)]
5119    pub async fn get_executed_transaction_and_effects(
5120        &self,
5121        digest: TransactionDigest,
5122        kv_store: Arc<TransactionKeyValueStore>,
5123    ) -> IotaResult<(TransactionEnvelope, TransactionEffects)> {
5124        let transaction = kv_store.get_tx(digest).await?;
5125        let effects = kv_store.get_fx_by_tx_digest(digest).await?;
5126        Ok((transaction, effects))
5127    }
5128
5129    #[instrument(level = "trace", skip_all)]
5130    pub fn multi_get_checkpoint_by_sequence_number(
5131        &self,
5132        sequence_numbers: &[CheckpointSequenceNumber],
5133    ) -> IotaResult<Vec<Option<VerifiedCheckpoint>>> {
5134        Ok(self
5135            .checkpoint_store
5136            .multi_get_checkpoint_by_sequence_number(sequence_numbers)?)
5137    }
5138
5139    #[instrument(level = "trace", skip_all)]
5140    pub fn get_transaction_events(
5141        &self,
5142        digest: &TransactionDigest,
5143    ) -> IotaResult<TransactionEvents> {
5144        self.get_transaction_cache_reader()
5145            .try_get_events(digest)?
5146            .ok_or(IotaError::TransactionEventsNotFound { digest: *digest })
5147    }
5148
5149    /// The transaction's input objects, reaching the historic buckets for
5150    /// versions the live table no longer holds. Use for assembling responses
5151    /// only — see [`HistoricObjects::fill_missing`].
5152    pub fn get_transaction_input_objects(
5153        &self,
5154        effects: &TransactionEffects,
5155    ) -> anyhow::Result<Vec<Object>> {
5156        historic_objects::get_transaction_input_objects(
5157            self.get_object_store().as_ref(),
5158            &self.historic_objects,
5159            effects,
5160        )
5161        .map_err(Into::into)
5162    }
5163
5164    /// The transaction's output objects, reaching the historic buckets for
5165    /// versions the live table no longer holds. Use for assembling responses
5166    /// only — see [`HistoricObjects::fill_missing`].
5167    pub fn get_transaction_output_objects(
5168        &self,
5169        effects: &TransactionEffects,
5170    ) -> anyhow::Result<Vec<Object>> {
5171        historic_objects::get_transaction_output_objects(
5172            self.get_object_store().as_ref(),
5173            &self.historic_objects,
5174            effects,
5175        )
5176        .map_err(Into::into)
5177    }
5178
5179    fn get_indexes(&self) -> IotaResult<Arc<RpcIndexesStore>> {
5180        match self.jsonrpc_indexes() {
5181            Some(i) => Ok(i.clone()),
5182            None => Err(IotaError::UnsupportedFeature {
5183                error: "extended object indexing is not enabled on this server".into(),
5184            }),
5185        }
5186    }
5187
5188    /// The index store, `None` unless this node maintains the JSON-RPC group.
5189    fn jsonrpc_indexes(&self) -> Option<&Arc<RpcIndexesStore>> {
5190        self.rpc_indexes_store
5191            .as_ref()
5192            .filter(|store| store.serves(IndexGroup::JsonRpc))
5193    }
5194
5195    /// [`Self::jsonrpc_indexes`] for the reads that cannot be served without
5196    /// it.
5197    fn require_jsonrpc_indexes(&self) -> IotaResult<&Arc<RpcIndexesStore>> {
5198        self.jsonrpc_indexes()
5199            .ok_or(IotaError::IndexStoreNotAvailable)
5200    }
5201
5202    pub async fn get_transactions_for_tests(
5203        self: &Arc<Self>,
5204        filter: Option<TransactionFilter>,
5205        cursor: Option<TransactionDigest>,
5206        limit: Option<usize>,
5207        reverse: bool,
5208    ) -> IotaResult<Vec<TransactionDigest>> {
5209        let metrics = KeyValueStoreMetrics::new_for_tests();
5210        let kv_store = Arc::new(TransactionKeyValueStore::new(
5211            "rocksdb",
5212            metrics,
5213            self.clone(),
5214        ));
5215        self.get_transactions(&kv_store, filter, cursor, limit, reverse)
5216            .await
5217    }
5218
5219    #[instrument(level = "trace", skip_all)]
5220    pub async fn get_transactions(
5221        &self,
5222        kv_store: &Arc<TransactionKeyValueStore>,
5223        filter: Option<TransactionFilter>,
5224        // If `Some`, the query will start from the next item after the specified cursor
5225        cursor: Option<TransactionDigest>,
5226        limit: Option<usize>,
5227        reverse: bool,
5228    ) -> IotaResult<Vec<TransactionDigest>> {
5229        if let Some(TransactionFilter::Checkpoint(sequence_number)) = filter {
5230            let checkpoint_contents = kv_store.get_checkpoint_contents(sequence_number).await?;
5231            let iter = checkpoint_contents.iter().map(|c| c.transaction);
5232            if reverse {
5233                let iter = iter
5234                    .rev()
5235                    .skip_while(|d| cursor.is_some() && Some(*d) != cursor)
5236                    .skip(usize::from(cursor.is_some()));
5237                return Ok(iter.take(limit.unwrap_or(usize::MAX)).collect());
5238            } else {
5239                let iter = iter
5240                    .skip_while(|d| cursor.is_some() && Some(*d) != cursor)
5241                    .skip(usize::from(cursor.is_some()));
5242                return Ok(iter.take(limit.unwrap_or(usize::MAX)).collect());
5243            }
5244        }
5245        self.get_indexes()?
5246            .get_transactions(filter, cursor, limit, reverse)
5247    }
5248
5249    pub fn get_checkpoint_store(&self) -> &Arc<CheckpointStore> {
5250        &self.checkpoint_store
5251    }
5252
5253    /// Keeps the object versions that transactions in the highest executed
5254    /// checkpoint, and in every later one, supersede readable until the
5255    /// returned guard is dropped. Take it before submitting a transaction whose
5256    /// input or output objects are read from the store once it is
5257    /// checkpointed.
5258    pub fn retain_objects(&self) -> ObjectRetentionGuard {
5259        self.object_retention.retain()
5260    }
5261
5262    /// Records `executed_seq` as the highest executed checkpoint, which new
5263    /// [`Self::retain_objects`] guards start from. Call it once the
5264    /// checkpoint's outputs are readable.
5265    pub(crate) fn record_executed_for_object_retention(
5266        &self,
5267        executed_seq: CheckpointSequenceNumber,
5268    ) {
5269        self.object_retention.record_executed(executed_seq);
5270    }
5271
5272    pub fn get_latest_checkpoint_sequence_number(&self) -> IotaResult<CheckpointSequenceNumber> {
5273        self.get_checkpoint_store()
5274            .get_highest_executed_checkpoint_seq_number()?
5275            .ok_or(IotaError::UserInput {
5276                error: UserInputError::LatestCheckpointSequenceNumberNotFound,
5277            })
5278    }
5279
5280    #[instrument(level = "trace", skip_all)]
5281    pub fn get_checkpoint_summary_by_sequence_number(
5282        &self,
5283        sequence_number: CheckpointSequenceNumber,
5284    ) -> IotaResult<CheckpointSummary> {
5285        let verified_checkpoint = self
5286            .get_checkpoint_store()
5287            .get_checkpoint_by_sequence_number(sequence_number)?;
5288        match verified_checkpoint {
5289            Some(verified_checkpoint) => Ok(verified_checkpoint.into_inner().into_data()),
5290            None => Err(IotaError::UserInput {
5291                error: UserInputError::VerifiedCheckpointNotFound(sequence_number),
5292            }),
5293        }
5294    }
5295
5296    #[instrument(level = "trace", skip_all)]
5297    pub fn get_checkpoint_summary_by_digest(
5298        &self,
5299        digest: CheckpointDigest,
5300    ) -> IotaResult<CheckpointSummary> {
5301        let verified_checkpoint = self
5302            .get_checkpoint_store()
5303            .get_checkpoint_by_digest(&digest)?;
5304        match verified_checkpoint {
5305            Some(verified_checkpoint) => Ok(verified_checkpoint.into_inner().into_data()),
5306            None => Err(IotaError::UserInput {
5307                error: UserInputError::VerifiedCheckpointDigestNotFound(Base58::encode(digest)),
5308            }),
5309        }
5310    }
5311
5312    #[instrument(level = "trace", skip_all)]
5313    pub fn find_publish_txn_digest(&self, package_id: ObjectId) -> IotaResult<TransactionDigest> {
5314        if package_id.is_system_package() {
5315            return self.find_genesis_txn_digest();
5316        }
5317        Ok(self
5318            .get_object_read(&package_id)?
5319            .into_object()?
5320            .previous_transaction)
5321    }
5322
5323    #[instrument(level = "trace", skip_all)]
5324    pub fn find_genesis_txn_digest(&self) -> IotaResult<TransactionDigest> {
5325        let summary = self
5326            .get_verified_checkpoint_by_sequence_number(0)?
5327            .into_message();
5328        let content = self.get_checkpoint_contents(summary.contents_digest)?;
5329        let genesis_transaction = content.enumerate_transactions(&summary).next();
5330        Ok(genesis_transaction
5331            .ok_or(IotaError::UserInput {
5332                error: UserInputError::GenesisTransactionNotFound,
5333            })?
5334            .1
5335            .transaction)
5336    }
5337
5338    #[instrument(level = "trace", skip_all)]
5339    pub fn get_verified_checkpoint_by_sequence_number(
5340        &self,
5341        sequence_number: CheckpointSequenceNumber,
5342    ) -> IotaResult<VerifiedCheckpoint> {
5343        let verified_checkpoint = self
5344            .get_checkpoint_store()
5345            .get_checkpoint_by_sequence_number(sequence_number)?;
5346        match verified_checkpoint {
5347            Some(verified_checkpoint) => Ok(verified_checkpoint),
5348            None => Err(IotaError::UserInput {
5349                error: UserInputError::VerifiedCheckpointNotFound(sequence_number),
5350            }),
5351        }
5352    }
5353
5354    #[instrument(level = "trace", skip_all)]
5355    pub fn get_verified_checkpoint_summary_by_digest(
5356        &self,
5357        digest: CheckpointDigest,
5358    ) -> IotaResult<VerifiedCheckpoint> {
5359        let verified_checkpoint = self
5360            .get_checkpoint_store()
5361            .get_checkpoint_by_digest(&digest)?;
5362        match verified_checkpoint {
5363            Some(verified_checkpoint) => Ok(verified_checkpoint),
5364            None => Err(IotaError::UserInput {
5365                error: UserInputError::VerifiedCheckpointDigestNotFound(Base58::encode(digest)),
5366            }),
5367        }
5368    }
5369
5370    #[instrument(level = "trace", skip_all)]
5371    pub fn get_checkpoint_contents(
5372        &self,
5373        digest: CheckpointContentsDigest,
5374    ) -> IotaResult<CheckpointContents> {
5375        self.get_checkpoint_store()
5376            .get_checkpoint_contents(&digest)?
5377            .ok_or(IotaError::UserInput {
5378                error: UserInputError::CheckpointContentsNotFound(digest),
5379            })
5380    }
5381
5382    #[instrument(level = "trace", skip_all)]
5383    pub fn get_checkpoint_contents_by_sequence_number(
5384        &self,
5385        sequence_number: CheckpointSequenceNumber,
5386    ) -> IotaResult<CheckpointContents> {
5387        let verified_checkpoint = self
5388            .get_checkpoint_store()
5389            .get_checkpoint_by_sequence_number(sequence_number)?;
5390        match verified_checkpoint {
5391            Some(verified_checkpoint) => {
5392                let contents_digest = verified_checkpoint.into_inner().contents_digest;
5393                self.get_checkpoint_contents(contents_digest)
5394            }
5395            None => Err(IotaError::UserInput {
5396                error: UserInputError::VerifiedCheckpointNotFound(sequence_number),
5397            }),
5398        }
5399    }
5400
5401    #[instrument(level = "trace", skip_all)]
5402    pub async fn query_events(
5403        &self,
5404        kv_store: &Arc<TransactionKeyValueStore>,
5405        query: EventFilter,
5406        // If `Some`, the query will start from the next item after the specified cursor
5407        cursor: Option<EventID>,
5408        limit: usize,
5409        descending: bool,
5410    ) -> IotaResult<Vec<IotaEvent>> {
5411        let stored_events = match query {
5412            // A transaction's own events answer this as soon as it has
5413            // executed, without waiting for its checkpoint to be indexed.
5414            EventFilter::Transaction(digest) => {
5415                Self::transaction_events_page(kv_store, digest, cursor, limit, descending).await?
5416            }
5417            query => {
5418                self.indexed_events_page(kv_store, query, cursor, limit, descending)
5419                    .await?
5420            }
5421        };
5422
5423        let epoch_store = self.load_epoch_store_one_call_per_task();
5424        let backing_store = self.get_backing_package_store().as_ref();
5425        let mut layout_resolver = epoch_store
5426            .executor()
5427            .type_layout_resolver(Box::new(backing_store));
5428        let mut events = vec![];
5429        for (e, tx_digest, event_seq, timestamp) in stored_events.into_iter() {
5430            events.push(IotaEvent::try_from(
5431                e.clone(),
5432                tx_digest,
5433                event_seq as u64,
5434                timestamp,
5435                layout_resolver.get_annotated_layout(&e.struct_tag)?,
5436            )?)
5437        }
5438        Ok(events)
5439    }
5440
5441    /// The page of events matching `query` after `cursor`, from the RPC
5442    /// index; at most `limit`.
5443    async fn indexed_events_page(
5444        &self,
5445        kv_store: &Arc<TransactionKeyValueStore>,
5446        query: EventFilter,
5447        cursor: Option<EventID>,
5448        limit: usize,
5449        descending: bool,
5450    ) -> IotaResult<Vec<(Event, TransactionDigest, usize, Option<u64>)>> {
5451        let index_store = self.get_indexes()?;
5452
5453        // Get the tx_num from tx_digest
5454        let (tx_num, event_num) = if let Some(cursor) = cursor.as_ref() {
5455            let tx_seq = index_store.lookup_digest(&cursor.tx_digest)?.ok_or(
5456                IotaError::TransactionNotFound {
5457                    digest: cursor.tx_digest,
5458                },
5459            )?;
5460            let event = (tx_seq, cursor.event_seq as usize);
5461            // The page below drops its first row as the cursor's own, which is
5462            // only right for an event the index holds.
5463            if index_store.event_timestamp(event)?.is_none() {
5464                return Err(IotaError::TransactionEventsNotFound {
5465                    digest: cursor.tx_digest,
5466                });
5467            }
5468            event
5469        } else if descending {
5470            (u64::MAX, usize::MAX)
5471        } else {
5472            (0, 0)
5473        };
5474
5475        let limit = limit + 1;
5476        let mut event_keys = match query {
5477            EventFilter::All(filters) => {
5478                if filters.is_empty() {
5479                    index_store.all_events(tx_num, event_num, limit, descending)?
5480                } else {
5481                    return Err(IotaError::UserInput {
5482                        error: UserInputError::Unsupported(
5483                            "This query type does not currently support filter combinations"
5484                                .to_string(),
5485                        ),
5486                    });
5487                }
5488            }
5489            EventFilter::MoveModule { package, module } => {
5490                let module_id = ModuleId::new(
5491                    AccountAddress::new(package.into_bytes()),
5492                    move_core_types::identifier::Identifier::new(module.as_str()).unwrap(),
5493                );
5494                index_store.events_by_module_id(&module_id, tx_num, event_num, limit, descending)?
5495            }
5496            EventFilter::MoveEventType(struct_name) => index_store
5497                .events_by_move_event_struct_name(
5498                    &struct_name,
5499                    tx_num,
5500                    event_num,
5501                    limit,
5502                    descending,
5503                )?,
5504            EventFilter::Sender(sender) => {
5505                index_store.events_by_sender(&sender, tx_num, event_num, limit, descending)?
5506            }
5507            EventFilter::TimeRange {
5508                start_time,
5509                end_time,
5510            } => index_store.event_iterator(
5511                start_time,
5512                end_time,
5513                cursor.is_some().then_some((tx_num, event_num)),
5514                limit,
5515                descending,
5516            )?,
5517            EventFilter::MoveEventModule { package, module } => index_store
5518                .events_by_move_event_module(
5519                    &ModuleId::new(
5520                        AccountAddress::new(package.into_bytes()),
5521                        move_core_types::identifier::Identifier::new(module.as_str()).unwrap(),
5522                    ),
5523                    tx_num,
5524                    event_num,
5525                    limit,
5526                    descending,
5527                )?,
5528            // not using "_ =>" because we want to make sure we remember to add new variants here
5529            // Answered by `query_events` from the transaction's own events.
5530            EventFilter::Transaction(_)
5531            | EventFilter::Package(_)
5532            | EventFilter::MoveEventField { .. }
5533            | EventFilter::Any(_)
5534            | EventFilter::And(_, _)
5535            | EventFilter::Or(_, _) => {
5536                return Err(IotaError::UserInput {
5537                    error: UserInputError::Unsupported(
5538                        "This query type is not supported by the full node.".to_string(),
5539                    ),
5540                });
5541            }
5542        };
5543
5544        // skip one event if exclusive cursor is provided,
5545        // otherwise truncate to the original limit.
5546        if cursor.is_some() {
5547            if !event_keys.is_empty() {
5548                event_keys.remove(0);
5549            }
5550        } else {
5551            event_keys.truncate(limit - 1);
5552        }
5553
5554        // get the unique set of digests from the event_keys
5555        let transaction_digests = event_keys
5556            .iter()
5557            .map(|(_, digest, _, _)| *digest)
5558            .collect::<HashSet<_>>()
5559            .into_iter()
5560            .collect::<Vec<_>>();
5561
5562        let events = kv_store
5563            .multi_get_events_by_tx_digests(&transaction_digests)
5564            .await?;
5565
5566        let events_map: HashMap<_, _> = transaction_digests.iter().zip(events).collect();
5567
5568        event_keys
5569            .into_iter()
5570            .map(|k| {
5571                (
5572                    k,
5573                    events_map
5574                        .get(&k.1)
5575                        .expect("fetched digest is missing")
5576                        .clone()
5577                        .and_then(|e| e.get(k.2).cloned()),
5578                )
5579            })
5580            .map(
5581                |((_event_digest, tx_digest, event_seq, timestamp), event)| {
5582                    event
5583                        .map(|e| (e, tx_digest, event_seq, Some(timestamp)))
5584                        .ok_or(IotaError::TransactionEventsNotFound { digest: tx_digest })
5585                },
5586            )
5587            .collect::<Result<Vec<_>, _>>()
5588    }
5589
5590    /// The page of `digest`'s events after `cursor`, which must name one of
5591    /// them, read from the transaction's stored events; at most `limit`.
5592    async fn transaction_events_page(
5593        kv_store: &Arc<TransactionKeyValueStore>,
5594        digest: TransactionDigest,
5595        cursor: Option<EventID>,
5596        limit: usize,
5597        descending: bool,
5598    ) -> IotaResult<Vec<(Event, TransactionDigest, usize, Option<u64>)>> {
5599        let effects = kv_store.get_fx_by_tx_digest(digest).await?;
5600        let events = match effects.events_digest() {
5601            None => Vec::new(),
5602            Some(_) => {
5603                kv_store
5604                    .multi_get_events_by_tx_digests(&[digest])
5605                    .await?
5606                    .into_iter()
5607                    .next()
5608                    .flatten()
5609                    .ok_or(IotaError::TransactionEventsNotFound { digest })?
5610                    .0
5611            }
5612        };
5613        let after = match cursor {
5614            None => None,
5615            Some(cursor) if cursor.tx_digest != digest => {
5616                return Err(IotaError::UserInput {
5617                    error: UserInputError::Unsupported(
5618                        "the cursor must name an event of the transaction the filter names"
5619                            .to_string(),
5620                    ),
5621                });
5622            }
5623            Some(cursor) if cursor.event_seq as usize >= events.len() => {
5624                return Err(IotaError::TransactionEventsNotFound { digest });
5625            }
5626            Some(cursor) => Some(cursor.event_seq as usize),
5627        };
5628        let page: Vec<usize> = if descending {
5629            (0..after.unwrap_or(events.len()))
5630                .rev()
5631                .take(limit)
5632                .collect()
5633        } else {
5634            (after.map_or(0, |seq| seq + 1)..events.len())
5635                .take(limit)
5636                .collect()
5637        };
5638        if page.is_empty() {
5639            return Ok(Vec::new());
5640        }
5641        // The checkpoint's time, as the index records it; none before the
5642        // transaction is in a checkpoint.
5643        let timestamp = match kv_store
5644            .get_transaction_perpetual_checkpoint(digest)
5645            .await?
5646        {
5647            Some(checkpoint) => Some(
5648                kv_store
5649                    .get_checkpoint_summary(checkpoint)
5650                    .await?
5651                    .timestamp_ms,
5652            ),
5653            None => None,
5654        };
5655        Ok(page
5656            .into_iter()
5657            .map(|seq| (events[seq].clone(), digest, seq, timestamp))
5658            .collect())
5659    }
5660
5661    pub fn insert_genesis_object(&self, object: Object) {
5662        self.get_reconfig_api()
5663            .try_insert_genesis_object(object)
5664            .expect("Cannot insert genesis object")
5665    }
5666
5667    pub fn insert_genesis_objects(&self, objects: &[Object]) {
5668        for o in objects {
5669            self.insert_genesis_object(o.clone());
5670        }
5671    }
5672
5673    /// Make a status response for a transaction
5674    #[instrument(level = "trace", skip_all)]
5675    pub fn get_transaction_status(
5676        &self,
5677        transaction_digest: &TransactionDigest,
5678        epoch_store: &Arc<AuthorityPerEpochStore>,
5679    ) -> IotaResult<Option<(SenderSignedTransaction, TransactionStatus)>> {
5680        // TODO: In the case of read path, we should not have to re-sign the effects.
5681        if let Some(effects) =
5682            self.get_signed_effects_and_maybe_resign(transaction_digest, epoch_store)?
5683        {
5684            if let Some(transaction) = self
5685                .get_transaction_cache_reader()
5686                .try_get_transaction_block(transaction_digest)?
5687            {
5688                let cert_sig = epoch_store.get_transaction_cert_sig(transaction_digest)?;
5689                let events = if effects.events_digest().is_some() {
5690                    self.get_transaction_events(effects.transaction_digest())?
5691                } else {
5692                    TransactionEvents::default()
5693                };
5694                return Ok(Some((
5695                    (*transaction).clone().into_message(),
5696                    TransactionStatus::Executed(cert_sig, effects.into_inner(), events),
5697                )));
5698            } else {
5699                // The read of effects and read of transaction are not atomic. It's possible
5700                // that we reverted the transaction (during epoch change) in
5701                // between the above two reads, and we end up having effects but
5702                // not transaction. In this case, we just fall through.
5703                debug!(tx_digest=?transaction_digest, "Signed effects exist but no transaction found");
5704            }
5705        }
5706        if let Some(signed) = epoch_store.get_signed_transaction(transaction_digest)? {
5707            self.metrics.tx_already_processed.inc();
5708            let (transaction, sig) = signed.into_inner().into_data_and_sig();
5709            Ok(Some((transaction, TransactionStatus::Signed(sig))))
5710        } else {
5711            Ok(None)
5712        }
5713    }
5714
5715    /// Get the signed effects of the given transaction. If the effects was
5716    /// signed in a previous epoch, re-sign it so that the caller is able to
5717    /// form a cert of the effects in the current epoch.
5718    #[instrument(level = "trace", skip_all)]
5719    pub fn get_signed_effects_and_maybe_resign(
5720        &self,
5721        transaction_digest: &TransactionDigest,
5722        epoch_store: &Arc<AuthorityPerEpochStore>,
5723    ) -> IotaResult<Option<VerifiedSignedTransactionEffects>> {
5724        let effects = self
5725            .get_transaction_cache_reader()
5726            .try_get_executed_effects(transaction_digest)?;
5727        match effects {
5728            Some(effects) => {
5729                // If the transaction was executed in previous epochs, the validator will
5730                // re-sign the effects with new current epoch so that a client is always able to
5731                // obtain an effects certificate at the current epoch.
5732                //
5733                // Why is this necessary? Consider the following case:
5734                // - assume there are 4 validators
5735                // - Quorum driver gets 2 signed effects before reconfig halt
5736                // - The tx makes it into final checkpoint.
5737                // - 2 validators go away and are replaced in the new epoch.
5738                // - The new epoch begins.
5739                // - The quorum driver cannot complete the partial effects cert from the previous
5740                //   epoch, because it may not be able to reach either of the 2 former validators.
5741                // - But, if the 2 validators that stayed are willing to re-sign the effects in the
5742                //   new epoch, the QD can make a new effects cert and return it to the client.
5743                //
5744                // This is a considered a short-term workaround. Eventually, Quorum Driver
5745                // should be able to return either an effects certificate, -or-
5746                // a proof of inclusion in a checkpoint. In the case above, the
5747                // Quorum Driver would return a proof of inclusion in the final
5748                // checkpoint, and this code would no longer be necessary.
5749                if effects.epoch() != epoch_store.epoch() {
5750                    debug!(
5751                        tx_digest=?transaction_digest,
5752                        effects_epoch=?effects.epoch(),
5753                        epoch=?epoch_store.epoch(),
5754                        "Re-signing the effects with the current epoch"
5755                    );
5756                }
5757                Ok(Some(self.sign_effects(effects, epoch_store)?))
5758            }
5759            None => Ok(None),
5760        }
5761    }
5762
5763    /// A client aggregating effects signatures towards a quorum assumes
5764    /// finality once it collects 2f+1 of them, so within an epoch this
5765    /// validator must never assert two different effects for the same
5766    /// transaction on any RPC surface, signed or unsigned. Executed effects
5767    /// can change across a restart if an uncommitted transaction is
5768    /// re-executed with divergent results (e.g. by a new binary), so every
5769    /// effects-reporting path calls this before returning effects, and
5770    /// refuses to contradict a signature that may already be in a client's
5771    /// hands.
5772    pub fn check_effects_against_previously_signed(
5773        &self,
5774        epoch_store: &AuthorityPerEpochStore,
5775        tx_digest: &TransactionDigest,
5776        effects_digest: &TransactionEffectsDigest,
5777        surface: &'static str,
5778    ) -> IotaResult<()> {
5779        if let Some(previously_signed_digest) = epoch_store.get_signed_effects_digest(tx_digest)? {
5780            if previously_signed_digest != *effects_digest {
5781                self.metrics
5782                    .signed_effects_equivocation_prevented
5783                    .with_label_values(&[surface])
5784                    .inc();
5785                error!(
5786                    ?tx_digest,
5787                    ?previously_signed_digest,
5788                    executed_digest = ?effects_digest,
5789                    surface,
5790                    "refusing to report effects that differ from previously signed effects"
5791                );
5792                return Err(IotaError::GenericAuthority {
5793                    error: format!(
5794                        "Refusing to report effects for transaction {tx_digest}: effects digest \
5795                         {effects_digest} differs from previously signed effects digest \
5796                         {previously_signed_digest}"
5797                    ),
5798                });
5799            }
5800        }
5801        Ok(())
5802    }
5803
5804    #[instrument(level = "trace", skip_all)]
5805    pub(crate) fn sign_effects(
5806        &self,
5807        effects: TransactionEffects,
5808        epoch_store: &Arc<AuthorityPerEpochStore>,
5809    ) -> IotaResult<VerifiedSignedTransactionEffects> {
5810        let tx_digest = *effects.transaction_digest();
5811
5812        self.check_effects_against_previously_signed(
5813            epoch_store,
5814            &tx_digest,
5815            &effects.digest(),
5816            "sign_effects",
5817        )?;
5818
5819        let signed_effects = match epoch_store.get_effects_signature(&tx_digest)? {
5820            Some(sig) => {
5821                debug_assert!(sig.epoch == epoch_store.epoch());
5822                SignedTransactionEffects::new_from_data_and_sig(effects, sig)
5823            }
5824            _ => {
5825                let sig = AuthoritySignInfo::new(
5826                    epoch_store.epoch(),
5827                    &effects,
5828                    Intent::iota_app(IntentScope::TransactionEffects),
5829                    self.name,
5830                    &*self.secret,
5831                );
5832
5833                let effects = SignedTransactionEffects::new_from_data_and_sig(effects, sig.clone());
5834
5835                epoch_store.insert_effects_digest_and_signature(
5836                    &tx_digest,
5837                    effects.digest(),
5838                    &sig,
5839                )?;
5840
5841                effects
5842            }
5843        };
5844
5845        Ok(VerifiedSignedTransactionEffects::new_unchecked(
5846            signed_effects,
5847        ))
5848    }
5849
5850    /// Get the transaction envelope that currently locks the given object, if
5851    /// any. Since object locks are only valid for one epoch, we also need
5852    /// the epoch_id in the query. Returns UserInputError::ObjectNotFound if
5853    /// no lock records for the given object can be found.
5854    /// Returns UserInputError::ObjectVersionUnavailableForConsumption if the
5855    /// object record is at a different version.
5856    /// Returns Some(VerifiedEnvelope) if the given ObjectReference is locked by
5857    /// a certain transaction. Returns None if the a lock record is
5858    /// initialized for the given ObjectReference but not yet locked by any
5859    /// transaction,     or cannot find the transaction in transaction
5860    /// table, because of data race etc.
5861    #[instrument(level = "trace", skip_all)]
5862    pub fn get_transaction_lock(
5863        &self,
5864        object_ref: &ObjectReference,
5865        epoch_store: &AuthorityPerEpochStore,
5866    ) -> IotaResult<Option<VerifiedSignedTransaction>> {
5867        let lock_info = self
5868            .get_object_cache_reader()
5869            .try_get_lock(*object_ref, epoch_store)?;
5870        let lock_info = match lock_info {
5871            ObjectLockStatus::LockedAtDifferentVersion { locked_ref } => {
5872                return Err(UserInputError::ObjectVersionUnavailableForConsumption {
5873                    provided_obj_ref: *object_ref,
5874                    current_version: locked_ref.version,
5875                }
5876                .into());
5877            }
5878            ObjectLockStatus::Initialized => {
5879                return Ok(None);
5880            }
5881            ObjectLockStatus::LockedToTx { locked_by_tx } => locked_by_tx,
5882        };
5883
5884        epoch_store.get_signed_transaction(&lock_info)
5885    }
5886
5887    pub fn try_get_objects(&self, objects: &[ObjectId]) -> IotaResult<Vec<Option<Object>>> {
5888        self.get_object_cache_reader().try_get_objects(objects)
5889    }
5890
5891    /// Non-fallible version of `try_get_objects`.
5892    pub fn get_objects(&self, objects: &[ObjectId]) -> Vec<Option<Object>> {
5893        self.try_get_objects(objects)
5894            .expect("storage access failed")
5895    }
5896
5897    pub fn try_get_object_or_tombstone(
5898        &self,
5899        object_id: ObjectId,
5900    ) -> IotaResult<Option<ObjectReference>> {
5901        self.get_object_cache_reader()
5902            .try_get_latest_object_ref_or_tombstone(object_id)
5903    }
5904
5905    /// Non-fallible version of `try_get_object_or_tombstone`.
5906    pub fn get_object_or_tombstone(&self, object_id: ObjectId) -> Option<ObjectReference> {
5907        self.try_get_object_or_tombstone(object_id)
5908            .expect("storage access failed")
5909    }
5910
5911    /// Ordinarily, protocol upgrades occur when 2f + 1 + (f *
5912    /// ProtocolConfig::buffer_stake_for_protocol_upgrade_bps) vote for the
5913    /// upgrade.
5914    ///
5915    /// This method can be used to dynamic adjust the amount of buffer. If set
5916    /// to 0, the upgrade will go through with only 2f+1 votes.
5917    ///
5918    /// IMPORTANT: If this is used, it must be used on >=2f+1 validators (all
5919    /// should have the same value), or you risk halting the chain.
5920    pub fn set_override_protocol_upgrade_buffer_stake(
5921        &self,
5922        expected_epoch: EpochId,
5923        buffer_stake_bps: u64,
5924    ) -> IotaResult {
5925        let epoch_store = self.load_epoch_store_one_call_per_task();
5926        let actual_epoch = epoch_store.epoch();
5927        if actual_epoch != expected_epoch {
5928            return Err(IotaError::WrongEpoch {
5929                expected_epoch,
5930                actual_epoch,
5931            });
5932        }
5933
5934        epoch_store.set_override_protocol_upgrade_buffer_stake(buffer_stake_bps)
5935    }
5936
5937    pub fn clear_override_protocol_upgrade_buffer_stake(
5938        &self,
5939        expected_epoch: EpochId,
5940    ) -> IotaResult {
5941        let epoch_store = self.load_epoch_store_one_call_per_task();
5942        let actual_epoch = epoch_store.epoch();
5943        if actual_epoch != expected_epoch {
5944            return Err(IotaError::WrongEpoch {
5945                expected_epoch,
5946                actual_epoch,
5947            });
5948        }
5949
5950        epoch_store.clear_override_protocol_upgrade_buffer_stake()
5951    }
5952
5953    /// Get the set of system packages that are compiled in to this build, if
5954    /// those packages are compatible with the current versions of those
5955    /// packages on-chain.
5956    pub async fn get_available_system_packages(
5957        &self,
5958        protocol_config: &ProtocolConfig,
5959    ) -> Vec<ObjectReference> {
5960        let mut results = vec![];
5961
5962        let system_packages = BuiltInFramework::iter_system_packages();
5963
5964        // Add extra framework packages during simtest
5965        #[cfg(msim)]
5966        let extra_packages = framework_injection::get_extra_packages(self.name);
5967        #[cfg(msim)]
5968        let system_packages = {
5969            let mut packages: Vec<_> = system_packages.collect();
5970            packages.extend(extra_packages.iter());
5971            packages
5972        };
5973
5974        for system_package in system_packages {
5975            let modules = system_package.modules().to_vec();
5976            // In simtests, we could override the current built-in framework packages.
5977            #[cfg(msim)]
5978            let modules = framework_injection::get_override_modules(&system_package.id, self.name)
5979                .unwrap_or(modules);
5980
5981            let Some(obj_ref) = iota_framework::compare_system_package(
5982                &self.get_object_store(),
5983                &system_package.id,
5984                &modules,
5985                system_package.dependencies.to_vec(),
5986                protocol_config,
5987            )
5988            .await
5989            else {
5990                return vec![];
5991            };
5992            results.push(obj_ref);
5993        }
5994
5995        results
5996    }
5997
5998    /// Return the new versions, module bytes, and dependencies for the packages
5999    /// that have been committed to for a framework upgrade, in
6000    /// `system_packages`.  Loads the module contents from the binary, and
6001    /// performs the following checks:
6002    ///
6003    /// - Whether its contents matches what is on-chain already, in which case no upgrade is
6004    ///   required, and its contents are omitted from the output.
6005    /// - Whether the contents in the binary can form a package whose digest matches the input,
6006    ///   meaning the framework will be upgraded, and this authority can satisfy that upgrade, in
6007    ///   which case the contents are included in the output.
6008    ///
6009    /// If a needed version of the framework can't be loaded, the binary does
6010    /// not contain the bytes for that framework ID, or the resulting
6011    /// package fails the digest check, `None` is returned indicating that
6012    /// this authority cannot run the upgrade that the network voted on.
6013    ///
6014    /// All object lookups are pinned to the versions in `system_packages`
6015    /// instead of using the latest versions, so that the result is
6016    /// deterministic even if the change epoch transaction that performs the
6017    /// upgrade has already been executed locally (e.g. via state sync). In
6018    /// that case the reconstructed change epoch transaction is byte-identical
6019    /// to the executed one, and the caller detects it as already executed.
6020    async fn get_system_package_bytes(
6021        &self,
6022        system_packages: Vec<ObjectReference>,
6023        binary_config: &BinaryConfig,
6024    ) -> Option<Vec<SystemPackage>> {
6025        let object_store = self.get_object_cache_reader();
6026
6027        let mut res = Vec::with_capacity(system_packages.len());
6028        for system_package_ref in system_packages {
6029            if object_store
6030                .get_object_by_key(&system_package_ref.object_id, system_package_ref.version)
6031                .is_some_and(|object| object.object_ref() == system_package_ref)
6032            {
6033                // Skip this one because it doesn't need to be upgraded.
6034                info!(
6035                    "Framework {} does not need updating",
6036                    system_package_ref.object_id
6037                );
6038                continue;
6039            }
6040
6041            // The digest in `system_package_ref` commits to a package built on top of the
6042            // predecessor version's `previous_transaction` (see `compare_system_package`),
6043            // so it must be re-derived from that version. A ref at
6044            // `Version::OBJECT_START` is a freshly created package with no predecessor.
6045            let prev_transaction = if system_package_ref.version == Version::OBJECT_START {
6046                TransactionDigest::GENESIS_MARKER
6047            } else {
6048                let prev_version = system_package_ref
6049                    .version
6050                    .previous()
6051                    .expect("version is greater than Version::OBJECT_START");
6052                let Some(prev_object) =
6053                    object_store.get_object_by_key(&system_package_ref.object_id, prev_version)
6054                else {
6055                    error!(
6056                        "Framework {} not available locally at version {prev_version:?}, cannot \
6057                         derive upgrade to {system_package_ref:?}",
6058                        system_package_ref.object_id
6059                    );
6060                    return None;
6061                };
6062                prev_object.previous_transaction
6063            };
6064
6065            #[cfg(msim)]
6066            let FrameworkSystemPackage {
6067                id: _,
6068                bytes,
6069                dependencies,
6070            } = framework_injection::get_override_system_package(
6071                &system_package_ref.object_id,
6072                self.name,
6073            )
6074            .unwrap_or_else(|| {
6075                BuiltInFramework::get_package_by_id(&system_package_ref.object_id).clone()
6076            });
6077
6078            #[cfg(not(msim))]
6079            let FrameworkSystemPackage {
6080                id: _,
6081                bytes,
6082                dependencies,
6083            } = BuiltInFramework::get_package_by_id(&system_package_ref.object_id).clone();
6084
6085            let modules: Vec<_> = bytes
6086                .iter()
6087                .map(|m| CompiledModule::deserialize_with_config(m, binary_config).unwrap())
6088                .collect();
6089
6090            let new_object = Object::new_system_package(
6091                &modules,
6092                system_package_ref.version,
6093                dependencies.clone(),
6094                prev_transaction,
6095            );
6096
6097            let new_ref = new_object.object_ref();
6098            if new_ref != system_package_ref {
6099                debug_fatal!(
6100                    "Framework mismatch -- binary: {new_ref:?}\n  upgrade: {system_package_ref:?}"
6101                );
6102                return None;
6103            }
6104
6105            res.push(SystemPackage {
6106                version: system_package_ref.version,
6107                modules: bytes,
6108                dependencies,
6109            });
6110        }
6111
6112        Some(res)
6113    }
6114
6115    /// Returns the new protocol version and system packages that the network
6116    /// has voted to upgrade to. If the proposed protocol version is not
6117    /// supported, None is returned.
6118    fn is_protocol_version_supported_v1(
6119        proposed_protocol_version: ProtocolVersion,
6120        committee: &Committee,
6121        capabilities: Vec<AuthorityCapabilitiesV1>,
6122        mut buffer_stake_bps: u64,
6123    ) -> Option<(ProtocolVersion, Digest, Vec<ObjectReference>)> {
6124        if buffer_stake_bps > 10000 {
6125            warn!("clamping buffer_stake_bps to 10000");
6126            buffer_stake_bps = 10000;
6127        }
6128
6129        // For each validator, gather the protocol version and system packages that it
6130        // would like to upgrade to in the next epoch.
6131        let mut desired_upgrades: Vec<_> = capabilities
6132            .into_iter()
6133            .filter_map(|mut cap| {
6134                // A validator that lists no packages is voting against any change at all.
6135                if cap.available_system_packages.is_empty() {
6136                    return None;
6137                }
6138
6139                cap.available_system_packages.sort();
6140
6141                info!(
6142                    "validator {:?} supports {:?} with system packages: {:?}",
6143                    cap.authority.concise(),
6144                    cap.supported_protocol_versions,
6145                    cap.available_system_packages,
6146                );
6147
6148                // A validator that only supports the current protocol version is also voting
6149                // against any change, because framework upgrades always require a protocol
6150                // version bump.
6151                cap.supported_protocol_versions
6152                    .get_version_digest(proposed_protocol_version)
6153                    .map(|digest| (digest, cap.available_system_packages, cap.authority))
6154            })
6155            .collect();
6156
6157        // There can only be one set of votes that have a majority, find one if it
6158        // exists.
6159        desired_upgrades.sort();
6160        desired_upgrades
6161            .into_iter()
6162            .chunk_by(|(digest, packages, _authority)| (*digest, packages.clone()))
6163            .into_iter()
6164            .find_map(|((digest, packages), group)| {
6165                // should have been filtered out earlier.
6166                assert!(!packages.is_empty());
6167
6168                let mut stake_aggregator: StakeAggregator<(), true> =
6169                    StakeAggregator::new(Arc::new(committee.clone()));
6170
6171                for (_, _, authority) in group {
6172                    stake_aggregator.insert_generic(authority, ());
6173                }
6174
6175                let total_votes = stake_aggregator.total_votes();
6176                let quorum_threshold = committee.quorum_threshold();
6177                let effective_threshold = committee.effective_threshold(buffer_stake_bps);
6178
6179                info!(
6180                    protocol_config_digest = ?digest,
6181                    ?total_votes,
6182                    ?quorum_threshold,
6183                    ?buffer_stake_bps,
6184                    ?effective_threshold,
6185                    ?proposed_protocol_version,
6186                    ?packages,
6187                    "support for upgrade"
6188                );
6189
6190                let has_support = total_votes >= effective_threshold;
6191                has_support.then_some((proposed_protocol_version, digest, packages))
6192            })
6193    }
6194
6195    /// Selects the highest supported protocol version and system packages that
6196    /// the network has voted to upgrade to. If no upgrade is supported,
6197    /// returns the current protocol version and system packages.
6198    fn choose_protocol_version_and_system_packages_v1(
6199        current_protocol_version: ProtocolVersion,
6200        current_protocol_digest: Digest,
6201        committee: &Committee,
6202        capabilities: Vec<AuthorityCapabilitiesV1>,
6203        buffer_stake_bps: u64,
6204    ) -> (ProtocolVersion, Digest, Vec<ObjectReference>) {
6205        let mut next_protocol_version = current_protocol_version;
6206        let mut system_packages = vec![];
6207        let mut protocol_version_digest = current_protocol_digest;
6208
6209        // Finds the highest supported protocol version and system packages by
6210        // incrementing the proposed protocol version by one until no further
6211        // upgrades are supported.
6212        while let Some((version, digest, packages)) = Self::is_protocol_version_supported_v1(
6213            next_protocol_version + 1,
6214            committee,
6215            capabilities.clone(),
6216            buffer_stake_bps,
6217        ) {
6218            next_protocol_version = version;
6219            protocol_version_digest = digest;
6220            system_packages = packages;
6221        }
6222
6223        (
6224            next_protocol_version,
6225            protocol_version_digest,
6226            system_packages,
6227        )
6228    }
6229
6230    /// Returns the indices of validators that support the given protocol
6231    /// version and digest. This includes both committee and non-committee
6232    /// validators based on their capabilities. Uses active validators
6233    /// instead of committee indices.
6234    fn get_validators_supporting_protocol_version(
6235        target_protocol_version: ProtocolVersion,
6236        target_digest: Digest,
6237        active_validators: &[AggregateAuthorityPublicKey],
6238        capabilities: &[AuthorityCapabilitiesV1],
6239    ) -> Vec<u64> {
6240        let mut eligible_validators = Vec::new();
6241
6242        for capability in capabilities {
6243            // Check if this validator supports the target protocol version and digest
6244            if let Some(digest) = capability
6245                .supported_protocol_versions
6246                .get_version_digest(target_protocol_version)
6247            {
6248                if digest == target_digest {
6249                    // Find the validator's index in the active validators list
6250                    if let Some(index) = active_validators
6251                        .iter()
6252                        .position(|name| AuthorityName::from(name) == capability.authority)
6253                    {
6254                        eligible_validators.push(index as u64);
6255                    }
6256                }
6257            }
6258        }
6259
6260        // Sort indices for deterministic behavior
6261        eligible_validators.sort();
6262        eligible_validators
6263    }
6264
6265    /// Calculates the sum of weights for eligible validators that are part of
6266    /// the committee. Takes the indices from
6267    /// get_validators_supporting_protocol_version and maps them back
6268    /// to committee members to get their weights.
6269    fn calculate_eligible_validators_weight(
6270        eligible_validator_indices: &[u64],
6271        active_validators: &[AggregateAuthorityPublicKey],
6272        committee: &Committee,
6273    ) -> u64 {
6274        let mut total_weight = 0u64;
6275
6276        for &index in eligible_validator_indices {
6277            let authority_pubkey = &active_validators[index as usize];
6278            // Check if this validator is in the committee and get their weight
6279            if let Some((_, weight)) = committee
6280                .members()
6281                .find(|(name, _)| *name == AuthorityName::from(authority_pubkey))
6282            {
6283                total_weight += weight;
6284            }
6285        }
6286
6287        total_weight
6288    }
6289
6290    /// Creates and execute the advance epoch transaction to effects without
6291    /// committing it to the database. The effects of the change epoch tx
6292    /// are only written to the database after a certified checkpoint has been
6293    /// formed and executed by CheckpointExecutor.
6294    ///
6295    /// When a framework upgraded has been decided on, but the validator does
6296    /// not have the new versions of the packages locally, the validator
6297    /// cannot form the ChangeEpochTx. In this case it returns Err,
6298    /// indicating that the checkpoint builder should give up trying to make the
6299    /// final checkpoint. As long as the network is able to create a certified
6300    /// checkpoint (which should be ensured by the capabilities vote), it
6301    /// will arrive via state sync and be executed by CheckpointExecutor.
6302    ///
6303    /// `scores` is indexed by authority index, see
6304    /// [`Committee::authority_index()`], as `Scoreboard::current_scores()`
6305    /// returns it. The transaction carries the scores in committee member
6306    /// index order, which is where Move reads them, so this is the one place
6307    /// that converts between the two orders.
6308    #[instrument(level = "error", skip_all)]
6309    pub async fn create_and_execute_advance_epoch_tx(
6310        &self,
6311        epoch_store: &Arc<AuthorityPerEpochStore>,
6312        gas_cost_summary: &GasCostSummary,
6313        checkpoint: CheckpointSequenceNumber,
6314        epoch_start_timestamp_ms: CheckpointTimestamp,
6315        scores: Vec<u64>,
6316    ) -> CheckpointBuilderResult<(
6317        IotaSystemState,
6318        Option<SystemEpochInfoEvent>,
6319        TransactionEffects,
6320    )> {
6321        let mut txns = Vec::new();
6322
6323        // Create the TransactionDenyRules object once: the epoch-start
6324        // configuration is identical on every validator, so the whole
6325        // committee injects (or skips) the kind together. If this epoch
6326        // change falls into safe mode the creation is dropped with it, the
6327        // object stays absent, and the next epoch end injects it again.
6328        if epoch_store.protocol_config().enable_pcool_flow()
6329            && epoch_store
6330                .epoch_start_config()
6331                .transaction_deny_rules_obj_initial_shared_version()
6332                .is_none()
6333        {
6334            txns.push(EndOfEpochTransactionKind::TransactionDenyRulesCreate);
6335        }
6336
6337        let next_epoch = epoch_store.epoch() + 1;
6338
6339        let buffer_stake_bps = epoch_store.get_effective_buffer_stake_bps();
6340        let authority_capabilities = epoch_store
6341            .get_capabilities_v1()
6342            .expect("read capabilities from db cannot fail");
6343        let (next_epoch_protocol_version, next_epoch_protocol_digest, next_epoch_system_packages) =
6344            Self::choose_protocol_version_and_system_packages_v1(
6345                epoch_store.protocol_version(),
6346                SupportedProtocolVersionsWithHashes::protocol_config_digest(
6347                    epoch_store.protocol_config(),
6348                ),
6349                epoch_store.committee(),
6350                authority_capabilities.clone(),
6351                buffer_stake_bps,
6352            );
6353
6354        // since system packages are created during the current epoch, they should abide
6355        // by the rules of the current epoch, including the current epoch's max
6356        // Move binary format version
6357        let config = epoch_store.protocol_config();
6358        let binary_config = to_binary_config(config, None);
6359        let Some(next_epoch_system_package_bytes) = self
6360            .get_system_package_bytes(next_epoch_system_packages.clone(), &binary_config)
6361            .await
6362        else {
6363            debug_fatal!(
6364                "upgraded system packages {:?} are not locally available, cannot create \
6365                ChangeEpochTx. validator binary must be upgraded to the correct version!",
6366                next_epoch_system_packages
6367            );
6368            // the checkpoint builder will keep retrying forever when it hits this error.
6369            // Eventually, one of two things will happen:
6370            // - The operator will upgrade this binary to one that has the new packages locally, and
6371            //   this function will succeed.
6372            // - The final checkpoint will be certified by other validators, we will receive it via
6373            //   state sync, and execute it. This will upgrade the framework packages, reconfigure,
6374            //   and most likely shut down in the new epoch (this validator likely doesn't support
6375            //   the new protocol version, or else it should have had the packages.)
6376            return Err(CheckpointBuilderError::SystemPackagesMissing);
6377        };
6378
6379        // Use ChangeEpochV3 or ChangeEpochV4 when the feature flags are enabled and
6380        // ChangeEpochV2 requirements are met
6381        if config.select_committee_from_eligible_validators() {
6382            // Get the list of eligible validators that support the target protocol version
6383            let active_validators = epoch_store.epoch_start_state().get_active_validators();
6384
6385            let mut eligible_active_validators = (0..active_validators.len() as u64).collect();
6386
6387            // Use validators supporting the target protocol version as eligible validators
6388            // in the next version if select_committee_supporting_next_epoch_version feature
6389            // flag is set to true.
6390            if config.select_committee_supporting_next_epoch_version() {
6391                eligible_active_validators = Self::get_validators_supporting_protocol_version(
6392                    next_epoch_protocol_version,
6393                    next_epoch_protocol_digest,
6394                    &active_validators,
6395                    &authority_capabilities,
6396                );
6397
6398                // Calculate the total weight of eligible validators in the committee
6399                let eligible_validators_weight = Self::calculate_eligible_validators_weight(
6400                    &eligible_active_validators,
6401                    &active_validators,
6402                    epoch_store.committee(),
6403                );
6404
6405                // Safety check: ensure eligible validators have enough stake
6406                // Use the same effective threshold calculation that was used to decide the
6407                // protocol version
6408                let committee = epoch_store.committee();
6409                let effective_threshold = committee.effective_threshold(buffer_stake_bps);
6410
6411                if eligible_validators_weight < effective_threshold {
6412                    error!(
6413                        "Eligible validators weight {eligible_validators_weight} is less than effective threshold {effective_threshold}. \
6414                        This could indicate a bug in validator selection logic or inconsistency with protocol version decision.",
6415                    );
6416                    // Pass all active validator indices as eligible validators
6417                    // to perform selection among all of them.
6418                    eligible_active_validators = (0..active_validators.len() as u64).collect();
6419                }
6420            }
6421
6422            // Use ChangeEpochV4 when the pass_validator_scores_to_advance_epoch feature
6423            // flag is enabled.
6424            if config.pass_validator_scores_to_advance_epoch() {
6425                txns.push(EndOfEpochTransactionKind::new_change_epoch_v4(
6426                    next_epoch,
6427                    next_epoch_protocol_version.as_u64(),
6428                    gas_cost_summary.storage_cost,
6429                    gas_cost_summary.computation_cost,
6430                    gas_cost_summary.computation_cost_burned,
6431                    gas_cost_summary.storage_rebate,
6432                    gas_cost_summary.non_refundable_storage_fee,
6433                    epoch_start_timestamp_ms,
6434                    next_epoch_system_package_bytes,
6435                    eligible_active_validators,
6436                    epoch_store.scores_for_advance_epoch(&scores),
6437                    config.adjust_rewards_by_score(),
6438                ));
6439            } else {
6440                txns.push(EndOfEpochTransactionKind::new_change_epoch_v3(
6441                    next_epoch,
6442                    next_epoch_protocol_version.as_u64(),
6443                    gas_cost_summary.storage_cost,
6444                    gas_cost_summary.computation_cost,
6445                    gas_cost_summary.computation_cost_burned,
6446                    gas_cost_summary.storage_rebate,
6447                    gas_cost_summary.non_refundable_storage_fee,
6448                    epoch_start_timestamp_ms,
6449                    next_epoch_system_package_bytes,
6450                    eligible_active_validators,
6451                ));
6452            }
6453        } else if config.protocol_defined_base_fee()
6454            && config.max_committee_members_count_as_option().is_some()
6455        {
6456            txns.push(EndOfEpochTransactionKind::new_change_epoch_v2(
6457                next_epoch,
6458                next_epoch_protocol_version.as_u64(),
6459                gas_cost_summary.storage_cost,
6460                gas_cost_summary.computation_cost,
6461                gas_cost_summary.computation_cost_burned,
6462                gas_cost_summary.storage_rebate,
6463                gas_cost_summary.non_refundable_storage_fee,
6464                epoch_start_timestamp_ms,
6465                next_epoch_system_package_bytes,
6466            ));
6467        } else {
6468            txns.push(EndOfEpochTransactionKind::new_change_epoch(
6469                next_epoch,
6470                next_epoch_protocol_version.as_u64(),
6471                gas_cost_summary.storage_cost,
6472                gas_cost_summary.computation_cost,
6473                gas_cost_summary.storage_rebate,
6474                gas_cost_summary.non_refundable_storage_fee,
6475                epoch_start_timestamp_ms,
6476                next_epoch_system_package_bytes,
6477            ));
6478        }
6479
6480        let tx = VerifiedTransaction::new_end_of_epoch_transaction(txns);
6481
6482        let executable_tx = VerifiedExecutableTransaction::new_from_checkpoint(
6483            tx.clone(),
6484            epoch_store.epoch(),
6485            checkpoint,
6486        );
6487
6488        let tx_digest = executable_tx.digest();
6489
6490        info!(
6491            ?next_epoch,
6492            ?next_epoch_protocol_version,
6493            ?next_epoch_system_packages,
6494            computation_cost=?gas_cost_summary.computation_cost,
6495            computation_cost_burned=?gas_cost_summary.computation_cost_burned,
6496            storage_cost=?gas_cost_summary.storage_cost,
6497            storage_rebate=?gas_cost_summary.storage_rebate,
6498            non_refundable_storage_fee=?gas_cost_summary.non_refundable_storage_fee,
6499            ?tx_digest,
6500            "Creating advance epoch transaction"
6501        );
6502
6503        fail_point_async!("change_epoch_tx_delay");
6504        let tx_lock = epoch_store.acquire_tx_lock(tx_digest);
6505
6506        // The tx could have been executed by state sync already - if so simply return
6507        // an error. The checkpoint builder will shortly be terminated by
6508        // reconfiguration anyway.
6509        if self
6510            .get_transaction_cache_reader()
6511            .try_is_tx_already_executed(tx_digest)?
6512        {
6513            warn!("change epoch tx has already been executed via state sync");
6514            return Err(CheckpointBuilderError::ChangeEpochTxAlreadyExecuted);
6515        }
6516
6517        let execution_guard = self.execution_lock_for_executable_transaction(&executable_tx)?;
6518
6519        // We must manually assign the shared object versions to the transaction before
6520        // executing it. This is because we do not sequence end-of-epoch
6521        // transactions through consensus.
6522        let assigned_versions = epoch_store.assign_shared_object_versions_idempotent(
6523            self.get_object_cache_reader().as_ref(),
6524            std::iter::once(&Schedulable::Transaction(&executable_tx)),
6525        )?;
6526
6527        assert_eq!(assigned_versions.0.len(), 1);
6528        let assigned_versions = assigned_versions.0.into_iter().next().unwrap().1;
6529
6530        let (input_objects, _) = self.read_objects_for_execution(
6531            &tx_lock,
6532            &executable_tx,
6533            assigned_versions,
6534            epoch_store,
6535        )?;
6536
6537        let (temporary_store, effects, _execution_error_opt, _, _) = self.execute_transaction(
6538            &execution_guard,
6539            &executable_tx,
6540            input_objects,
6541            vec![],
6542            &AttestationSource::Unattested,
6543            epoch_store,
6544        )?;
6545        let system_obj = get_iota_system_state(&temporary_store.written)
6546            .expect("change epoch tx must write to system object");
6547        // Find the SystemEpochInfoEvent emitted by the advance_epoch transaction.
6548        let system_epoch_info_event = temporary_store
6549            .events
6550            .0
6551            .into_iter()
6552            .find(|event| event.is_system_epoch_info_event())
6553            .map(SystemEpochInfoEvent::from);
6554        // The system epoch info event can be `None` in case if the `advance_epoch`
6555        // Move function call failed and was executed in the safe mode.
6556        assert!(system_epoch_info_event.is_some() || system_obj.safe_mode());
6557
6558        // We must write tx and effects to the state sync tables so that state sync is
6559        // able to deliver to the transaction to CheckpointExecutor after it is
6560        // included in a certified checkpoint.
6561        self.get_state_sync_store()
6562            .try_insert_transaction_and_effects(&tx, &effects)?;
6563
6564        info!(
6565            "Effects summary of the change epoch transaction: {:?}",
6566            effects.summary_for_debug()
6567        );
6568        epoch_store.record_checkpoint_builder_is_safe_mode_metric(system_obj.safe_mode());
6569        // The change epoch transaction cannot fail to execute.
6570        assert!(effects.status().is_success());
6571        Ok((system_obj, system_epoch_info_event, effects))
6572    }
6573
6574    /// This function is called at the very end of the epoch.
6575    /// This step is required before updating new epoch in the db and calling
6576    /// reopen_epoch_db.
6577    #[instrument(level = "error", skip_all)]
6578    async fn revert_uncommitted_epoch_transactions(
6579        &self,
6580        epoch_store: &AuthorityPerEpochStore,
6581    ) -> IotaResult {
6582        {
6583            let state = epoch_store.get_reconfig_state_write_lock_guard();
6584            if state.should_accept_user_certs() {
6585                // Need to change this so that consensus adapter do not accept certificates from
6586                // user. This can happen if our local validator did not initiate
6587                // epoch change locally, but 2f+1 nodes already concluded the
6588                // epoch.
6589                //
6590                // This lock is essentially a barrier (in the certificate mode only) for
6591                // `epoch_store.pending_consensus_certificates` table we are reading on the line
6592                // after this block
6593                epoch_store.close_user_certs(state);
6594            }
6595            // lock is dropped here
6596        }
6597
6598        // In the P-COOL flow, the list of pending consensus certificates is
6599        // always empty, so the reverting below is only for the certificate mode.
6600        if !epoch_store.protocol_config().enable_pcool_flow() {
6601            let pending_certificates = epoch_store.pending_consensus_certificates();
6602            info!(
6603                "Reverting {} locally executed transactions that was not included in the epoch: \
6604                    {:?}",
6605                pending_certificates.len(),
6606                pending_certificates,
6607            );
6608            for digest in pending_certificates {
6609                if epoch_store.is_transaction_executed_in_checkpoint(&digest)? {
6610                    info!(
6611                        "Not reverting pending consensus transaction {:?} - it was included in \
6612                            checkpoint",
6613                        digest
6614                    );
6615                    continue;
6616                }
6617                info!("Reverting {:?} at the end of epoch", digest);
6618                epoch_store.revert_executed_transaction(&digest)?;
6619                self.get_reconfig_api().try_revert_state_update(&digest)?;
6620            }
6621            info!("All uncommitted local transactions reverted");
6622        } else {
6623            info!("P-COOL mode: skipping revert of uncommitted epoch transactions");
6624        }
6625
6626        Ok(())
6627    }
6628
6629    #[instrument(level = "error", skip_all)]
6630    async fn reopen_epoch_db(
6631        &self,
6632        cur_epoch_store: &AuthorityPerEpochStore,
6633        new_committee: Committee,
6634        epoch_start_configuration: EpochStartConfiguration,
6635        expensive_safety_check_config: &ExpensiveSafetyCheckConfig,
6636        epoch_last_checkpoint: CheckpointSequenceNumber,
6637    ) -> IotaResult<Arc<AuthorityPerEpochStore>> {
6638        let new_epoch = new_committee.epoch;
6639        info!(new_epoch = ?new_epoch, "re-opening AuthorityEpochTables for new epoch");
6640        assert_eq!(
6641            epoch_start_configuration.epoch_start_state().epoch(),
6642            new_committee.epoch
6643        );
6644        fail_point!("before-open-new-epoch-store");
6645        let new_epoch_store = cur_epoch_store.new_at_next_epoch(
6646            self.name,
6647            new_committee,
6648            epoch_start_configuration,
6649            self.get_backing_package_store().clone(),
6650            expensive_safety_check_config,
6651            epoch_last_checkpoint,
6652        )?;
6653        self.epoch_store.store(new_epoch_store.clone());
6654        Ok(new_epoch_store)
6655    }
6656
6657    /// Resolves the account's `AuthenticatorFunctionRef` on the execution path,
6658    /// where the certificate has already passed validation before consensus.
6659    ///
6660    /// A deleted or cancelled account object is not an error here: its version
6661    /// is returned so execution can proceed and surface the proper effect
6662    /// (e.g. `InputObjectDeleted` or a shared-object congestion cancellation).
6663    ///
6664    /// # Errors
6665    ///
6666    /// Any failure the check finds with the transaction, as the error the
6667    /// transaction fails with, with the original error as the source:
6668    /// [`ExecutionErrorKind::AuthenticatorFunctionNotFound`] when the account
6669    /// has no authenticator function field or the field cannot be read, and
6670    /// [`ExecutionErrorKind::AccountNotSharedObject`] when the account is not a
6671    /// shared object.
6672    ///
6673    /// # Panics
6674    ///
6675    /// When the store cannot be read: that says nothing about the transaction,
6676    /// so nobody can be charged for it, and effects written here would differ
6677    /// from the other validators'.
6678    fn check_move_account_for_execution(
6679        &self,
6680        auth_account_object_id: ObjectId,
6681        auth_account_object_seq_number: Option<Version>,
6682        auth_account_object_digest: Option<ObjectDigest>,
6683        account_object: ObjectReadResult,
6684        signer: &Address,
6685        protocol_config: &ProtocolConfig,
6686    ) -> Result<AuthenticatorFunctionRefForExecution, ExecutionError> {
6687        match self.check_move_account(
6688            auth_account_object_id,
6689            auth_account_object_seq_number,
6690            auth_account_object_digest,
6691            account_object,
6692            signer,
6693            true,
6694            protocol_config,
6695        ) {
6696            Ok(function_ref) => Ok(function_ref),
6697            Err(MoveAccountCheckError::Input(input_error)) => {
6698                let status = match &input_error {
6699                    UserInputError::MoveAuthenticatorNotFound {
6700                        account_object_id, ..
6701                    }
6702                    | UserInputError::InvalidAuthenticatorFunctionRefField { account_object_id } => {
6703                        ExecutionErrorKind::AuthenticatorFunctionNotFound {
6704                            object_id: *account_object_id,
6705                        }
6706                    }
6707                    // Since protocol version 36, only a shared account named with
6708                    // an immutable or owned reference reaches the digest
6709                    // comparison. The fix is the same as for the other two: name
6710                    // the account as a shared object.
6711                    UserInputError::AccountObjectNotSupported { object_id }
6712                    | UserInputError::ImmutableAccountObjectNotSupported { object_id }
6713                    | UserInputError::InvalidAccountObjectDigest { object_id, .. } => {
6714                        ExecutionErrorKind::AccountNotSharedObject {
6715                            object_id: *object_id,
6716                        }
6717                    }
6718                    // The check produces nothing else today. A failure added to
6719                    // it gets this status until it is given one of its own.
6720                    _ => ExecutionErrorKind::FunctionNotFound,
6721                };
6722                Err(ExecutionError::new_with_source(status, input_error))
6723            }
6724            Err(MoveAccountCheckError::Storage(storage_error)) => {
6725                panic!("failed to read the store while checking a Move account: {storage_error}")
6726            }
6727        }
6728    }
6729
6730    /// Resolves the account's `AuthenticatorFunctionRef` on the validation
6731    /// (signing) path, rejecting the transaction when the account object was
6732    /// deleted or belongs to a cancelled transaction.
6733    fn check_move_account_for_validation(
6734        &self,
6735        auth_account_object_id: ObjectId,
6736        auth_account_object_seq_number: Option<Version>,
6737        auth_account_object_digest: Option<ObjectDigest>,
6738        account_object: ObjectReadResult,
6739        signer: &Address,
6740        protocol_config: &ProtocolConfig,
6741    ) -> IotaResult<AuthenticatorFunctionRefForExecution> {
6742        self.check_move_account(
6743            auth_account_object_id,
6744            auth_account_object_seq_number,
6745            auth_account_object_digest,
6746            account_object,
6747            signer,
6748            false,
6749            protocol_config,
6750        )
6751        .map_err(IotaError::from)
6752    }
6753
6754    /// Checks whether `authenticator` unlocks a valid Move account and returns
6755    /// the account-related `AuthenticatorFunctionRef`. Where the protocol
6756    /// config requires it, the account object must be shared, so that a
6757    /// transaction carrying a `MoveAuthenticator` always has a shared input
6758    /// and is ordered by consensus. When `is_execution` is
6759    /// set, a deleted or cancelled account object yields its version instead of
6760    /// an error, so execution can proceed to the proper effect. Prefer the
6761    /// `check_move_account_for_execution` / `check_move_account_for_validation`
6762    /// wrappers over calling this directly.
6763    ///
6764    /// # Errors
6765    ///
6766    /// [`MoveAccountCheckError::Input`] when the account fails a check, and
6767    /// [`MoveAccountCheckError::Storage`] when the store cannot be read. The
6768    /// error type is what lets the execution path distinguish the two, so a
6769    /// new kind of failure here has to be added to it.
6770    fn check_move_account(
6771        &self,
6772        auth_account_object_id: ObjectId,
6773        auth_account_object_seq_number: Option<Version>,
6774        auth_account_object_digest: Option<ObjectDigest>,
6775        account_object: ObjectReadResult,
6776        signer: &Address,
6777        is_execution: bool,
6778        protocol_config: &ProtocolConfig,
6779    ) -> Result<AuthenticatorFunctionRefForExecution, MoveAccountCheckError> {
6780        let auth_account_object_seq_number = match (&account_object.object, is_execution) {
6781            // In any case, if the account object is loaded, we can check its version and digest.
6782            // Then we return the version of the account object to be used for reading the
6783            // authenticator function ref dynamic field.
6784            (ObjectReadResultKind::Object(object), _) => {
6785                let account_object_addr = Address::from(auth_account_object_id);
6786                fp_ensure!(
6787                    signer == &account_object_addr,
6788                    UserInputError::IncorrectUserSignature {
6789                        error: format!("Move authenticator is trying to unlock {account_object_addr:?}, but given signer address is {signer:?}")
6790                    }
6791                    .into()
6792                );
6793
6794                if protocol_config.reject_immutable_account_objects() {
6795                    fp_ensure!(
6796                        !object.is_immutable(),
6797                        UserInputError::ImmutableAccountObjectNotSupported {
6798                            object_id: auth_account_object_id
6799                        }
6800                        .into()
6801                    );
6802                }
6803
6804                fp_ensure!(
6805                    object.is_shared() || object.is_immutable(),
6806                    UserInputError::AccountObjectNotSupported {
6807                        object_id: auth_account_object_id
6808                    }
6809                    .into()
6810                );
6811
6812                let auth_account_object_seq_number =
6813                    if let Some(expected_version) = auth_account_object_seq_number {
6814                        let account_object_version = object.version();
6815
6816                        fp_ensure!(
6817                            account_object_version == expected_version,
6818                            UserInputError::AccountObjectVersionMismatch {
6819                                object_id: auth_account_object_id,
6820                                expected_version,
6821                                actual_version: account_object_version,
6822                            }
6823                            .into()
6824                        );
6825
6826                        expected_version
6827                    } else {
6828                        object.version()
6829                    };
6830
6831                if let Some(expected_digest) = auth_account_object_digest {
6832                    let account_object_digest = object.digest();
6833                    fp_ensure!(
6834                        account_object_digest == expected_digest,
6835                        UserInputError::InvalidAccountObjectDigest {
6836                            object_id: auth_account_object_id,
6837                            expected_digest,
6838                            actual_digest: account_object_digest,
6839                        }
6840                        .into()
6841                    );
6842                }
6843
6844                Ok(auth_account_object_seq_number)
6845            }
6846            // If the account object is not loaded because it was deleted, we return the error in
6847            // the case in which we are not executing the transaction right after.
6848            (ObjectReadResultKind::DeletedSharedObject(version, digest), false) => {
6849                Err(UserInputError::AccountObjectDeleted {
6850                    account_id: account_object.id(),
6851                    account_version: *version,
6852                    transaction_digest: *digest,
6853                })
6854            }
6855            // If the account object is not loaded because the transaction was canceled, we return
6856            // the error in the case in which we are not executing the transaction right
6857            // after.
6858            (ObjectReadResultKind::CancelledTransactionObject(version), false) => {
6859                Err(UserInputError::AccountObjectInCanceledTransaction {
6860                    account_id: account_object.id(),
6861                    account_version: *version,
6862                })
6863            }
6864            // If the account object is not loaded because it was deleted, we return the version in
6865            // the case in which we are executing the transaction right after.
6866            // This version is used to read the authenticator function ref dynamic field because it
6867            // is greater than the version of the child dynamic field.
6868            (ObjectReadResultKind::DeletedSharedObject(version, _), true) => Ok(*version),
6869            // If the account object is not loaded because the transaction was canceled, we return
6870            // the version in the case in which we are executing the transaction right
6871            // after. This version is used to read the authenticator function ref
6872            // dynamic field because it is greater than the version of the child dynamic
6873            // field.
6874            (ObjectReadResultKind::CancelledTransactionObject(version), true) => Ok(*version),
6875            // A transaction validated post-consensus at a commit leaves its account
6876            // to execution, which reads it, so no check gets an unread account.
6877            (ObjectReadResultKind::UnreadSharedObject, _) => unreachable!(
6878                "only post-consensus validation at a commit lists an unread shared object, and \
6879                    it leaves the account to execution"
6880            ),
6881        }?;
6882
6883        let authenticator_function_ref_field_id =
6884            derive_authenticator_function_ref_v1_dynamic_field_id(auth_account_object_id)?;
6885
6886        let authenticator_function_ref_field = self
6887            .get_object_cache_reader()
6888            .try_find_object_lt_or_eq_version(
6889                authenticator_function_ref_field_id,
6890                auth_account_object_seq_number,
6891            )
6892            .map_err(MoveAccountCheckError::Storage)?;
6893
6894        if let Some(authenticator_function_ref_field_obj) = authenticator_function_ref_field {
6895            Ok(authenticator_function_ref_v1_from_dynamic_field_object(
6896                auth_account_object_id,
6897                &authenticator_function_ref_field_obj,
6898            )?)
6899        } else {
6900            Err(UserInputError::MoveAuthenticatorNotFound {
6901                authenticator_function_ref_id: authenticator_function_ref_field_id,
6902                account_object_id: auth_account_object_id,
6903                account_object_version: auth_account_object_seq_number,
6904            }
6905            .into())
6906        }
6907    }
6908
6909    /// Shared validation pipeline for `handle_transaction_validation_checks`
6910    /// and `attest_transaction`. Runs, in order:
6911    ///   1. transaction deny checks
6912    ///   2. input object loading (transaction + per-authenticator + account)
6913    ///   3. `MoveAuthenticator` collection
6914    ///   4. input checks (gas budget covers transaction + authenticators)
6915    ///   5. coin deny list check
6916    ///
6917    /// Keep this helper as the single source of truth so the two callers
6918    /// cannot drift. Raw inputs are cloned and returned only on the
6919    /// `attest_transaction` path (`is_execute_transaction_to_effects`), where
6920    /// the MoveAuthenticator path needs them; the clone is skipped for
6921    /// `handle_transaction_validation_checks`, which ignores them.
6922    fn run_validation_checks<'a>(
6923        &self,
6924        transaction: &'a VerifiedTransaction,
6925        epoch_store: &Arc<AuthorityPerEpochStore>,
6926        deny_config: &dyn DenyRuleConfig,
6927        is_execute_transaction_to_effects: bool,
6928    ) -> IotaResult<ValidationOutputs<'a>> {
6929        let protocol_config = epoch_store.protocol_config();
6930        let reference_gas_price = epoch_store.reference_gas_price();
6931        let epoch = epoch_store.epoch();
6932        let tx = transaction.data().transaction();
6933
6934        // Step 1: deny checks.
6935        // Note: the deny checks may do redundant package loads but:
6936        // - they only load packages when there is an active package deny map
6937        // - the loads are cached anyway
6938        iota_transaction_checks::deny::check_transaction_for_validation(
6939            tx,
6940            transaction.signatures(),
6941            &transaction
6942                .input_objects(protocol_config.move_authenticator_type_arg_packages_as_inputs())?,
6943            &tx.receiving_objects(),
6944            deny_config,
6945            self.get_backing_package_store().as_ref(),
6946        )?;
6947
6948        // Step 2: load all input objects (including authenticator + account).
6949        let (tx_input_objects, tx_receiving_objects, per_authenticator_inputs) =
6950            self.read_objects_for_validation(transaction, epoch, protocol_config)?;
6951
6952        // Step 3: get Move authenticators.
6953        let move_authenticators = transaction.move_authenticators();
6954
6955        // Step 4: check inputs (validates gas budget covers auth + tx). When
6956        // `is_execute_transaction_to_effects` is true (attest_transaction), the
6957        // gas budget is metered for full execution; otherwise (signing) it only
6958        // covers authentication.
6959        //
6960        // Only the attest_transaction path consumes the raw per-authenticator
6961        // inputs from `ValidationOutputs` (to recover each
6962        // `AuthenticatorFunctionRefForExecution`);
6963        // `handle_transaction_validation_checks` discards them. So only clone
6964        // for the output when we actually need it, and move the original into
6965        // the input check otherwise.
6966        let per_authenticator_inputs_for_output = if is_execute_transaction_to_effects {
6967            per_authenticator_inputs.clone()
6968        } else {
6969            Vec::new()
6970        };
6971        let (gas_status, tx_checked_input_objects, per_authenticator_checked_inputs) = self
6972            .check_transaction_inputs_for_validation(
6973                protocol_config,
6974                reference_gas_price,
6975                tx,
6976                tx_input_objects,
6977                &tx_receiving_objects,
6978                &move_authenticators,
6979                per_authenticator_inputs,
6980                VerifierLimitsSource::NodeConfig(&self.config.verifier_signing_config),
6981                is_execute_transaction_to_effects,
6982            )?;
6983
6984        // Move authenticators cannot use owned objects, so their inputs never
6985        // acquire owned-object locks.
6986        debug_assert!(
6987            per_authenticator_checked_inputs
6988                .iter()
6989                .all(|(objects, _)| objects.inner().filter_owned_objects().is_empty()),
6990            "Move authenticator input objects must not contain owned objects"
6991        );
6992
6993        // The package holding each authenticate function is known only now that the
6994        // `AuthenticatorFunctionRef`s are loaded, so the deny-list check for it
6995        // stands apart from the one above. It must stay ahead of two things: the
6996        // filtering below, which drops authenticators that do not run
6997        // pre-consensus, so that every authenticator is covered; and the
6998        // authenticator execution itself, so that a denied package is never run.
6999        if protocol_config.deny_authenticator_packages() {
7000            iota_transaction_checks::deny::check_authenticator_packages(
7001                deny_config,
7002                per_authenticator_checked_inputs
7003                    .iter()
7004                    .map(|(_, authenticator_function_ref)| authenticator_function_ref),
7005                self.get_backing_package_store().as_ref(),
7006            )?;
7007        }
7008
7009        // Step 5: coin deny list. Pass raw `&InputObjects` (the deny-list
7010        // check only reads coin types and does not need the
7011        // `CheckedInputObjects` type-state — which is still enforced fresh
7012        // at `check_certificate_input` in `execute_transaction`).
7013        let per_authenticator_input_objects: Vec<&InputObjects> = per_authenticator_checked_inputs
7014            .iter()
7015            .map(|i| i.0.inner())
7016            .collect();
7017        check_coin_deny_list_v1(
7018            tx.sender(),
7019            tx_checked_input_objects.inner(),
7020            &tx_receiving_objects,
7021            &per_authenticator_input_objects,
7022            &self.get_object_store(),
7023        )?;
7024
7025        Ok(ValidationOutputs {
7026            per_authenticator_inputs: per_authenticator_inputs_for_output,
7027            move_authenticators,
7028            gas_status,
7029            tx_checked_input_objects,
7030            per_authenticator_checked_inputs,
7031        })
7032    }
7033
7034    #[allow(clippy::type_complexity)]
7035    fn read_objects_for_validation(
7036        &self,
7037        transaction: &VerifiedTransaction,
7038        epoch: u64,
7039        protocol_config: &ProtocolConfig,
7040    ) -> IotaResult<(
7041        InputObjects,
7042        ReceivingObjects,
7043        Vec<(InputObjects, ObjectReadResult)>,
7044    )> {
7045        let (input_objects, tx_receiving_objects) = self.input_loader.read_objects_for_signing(
7046            Some(transaction.digest()),
7047            &transaction.collect_all_input_object_kind_for_reading(
7048                protocol_config.move_authenticator_type_arg_packages_as_inputs(),
7049            )?,
7050            &transaction.data().transaction().receiving_objects(),
7051            epoch,
7052        )?;
7053
7054        transaction
7055            .split_input_objects_into_groups_for_reading(input_objects)
7056            .map(|(tx_input_objects, per_authenticator_inputs)| {
7057                (
7058                    tx_input_objects,
7059                    tx_receiving_objects,
7060                    per_authenticator_inputs,
7061                )
7062            })
7063    }
7064
7065    /// Like [`Self::read_objects_for_validation`], but for post-consensus
7066    /// validation at the commit `reader` was built for. Every input and every
7067    /// receiving object comes from the commit-indexed reader, the inputs
7068    /// first, so the first of them that decides is reported as at admission.
7069    fn read_objects_for_validation_at_commit(
7070        &self,
7071        reader: &CommitIndexedReader,
7072        transaction: &VerifiedTransaction,
7073        protocol_config: &ProtocolConfig,
7074    ) -> IotaResult<ValidationInputsAtCommit> {
7075        let input_objects = match self.input_loader.read_objects_at_commit(
7076            reader,
7077            &transaction.collect_all_input_object_kind_for_reading(
7078                protocol_config.move_authenticator_type_arg_packages_as_inputs(),
7079            )?,
7080        )? {
7081            InputResolution::Loaded(input_objects) => input_objects,
7082            InputResolution::Drop(kind, reason) => {
7083                return Ok(ValidationInputsAtCommit::Drop(kind, reason));
7084            }
7085            InputResolution::Missing(kind, reason) => {
7086                return Ok(ValidationInputsAtCommit::Missing(kind, reason));
7087            }
7088        };
7089        let tx_receiving_objects = match self.input_loader.read_receiving_objects_at_commit(
7090            reader,
7091            &transaction.data().transaction().receiving_objects(),
7092        )? {
7093            ReceivingObjectsResolution::Loaded(receiving_objects) => receiving_objects,
7094            ReceivingObjectsResolution::Drop(kind, reason) => {
7095                return Ok(ValidationInputsAtCommit::Drop(kind, reason));
7096            }
7097            ReceivingObjectsResolution::Missing(kind, reason) => {
7098                return Ok(ValidationInputsAtCommit::Missing(kind, reason));
7099            }
7100        };
7101
7102        let (tx_input_objects, per_authenticator_inputs) =
7103            transaction.split_input_objects_into_groups_for_reading(input_objects)?;
7104        Ok(ValidationInputsAtCommit::Loaded {
7105            tx_input_objects,
7106            tx_receiving_objects,
7107            per_authenticator_inputs,
7108        })
7109    }
7110
7111    #[allow(clippy::type_complexity)]
7112    fn check_transaction_inputs_for_validation(
7113        &self,
7114        protocol_config: &ProtocolConfig,
7115        reference_gas_price: u64,
7116        tx: &Transaction,
7117        tx_input_objects: InputObjects,
7118        tx_receiving_objects: &ReceivingObjects,
7119        move_authenticators: &Vec<&MoveAuthenticator>,
7120        per_authenticator_inputs: Vec<(InputObjects, ObjectReadResult)>,
7121        verifier_limits_source: VerifierLimitsSource<'_>,
7122        is_execute_transaction_to_effects: bool,
7123    ) -> IotaResult<(
7124        IotaGasStatus,
7125        CheckedInputObjects,
7126        Vec<(CheckedInputObjects, AuthenticatorFunctionRef)>,
7127    )> {
7128        let authenticator_gas_budget = if move_authenticators.is_empty() {
7129            0
7130        } else {
7131            // `max_auth_gas` is used here as a Move authenticator gas budget until it is
7132            // not a part of the transaction data.
7133            protocol_config.max_auth_gas()
7134        };
7135
7136        debug_assert_eq!(
7137            move_authenticators.len(),
7138            per_authenticator_inputs.len(),
7139            "Move authenticators amount must match the number of authenticator inputs"
7140        );
7141
7142        let per_authenticator_checked_inputs = move_authenticators
7143            .iter()
7144            .zip(per_authenticator_inputs)
7145            .map(
7146                |(move_authenticator, (authenticator_input_objects, account_object))| {
7147                    // Check basic `object_to_authenticate` preconditions and get its components.
7148                    let (
7149                        auth_account_object_id,
7150                        auth_account_object_seq_number,
7151                        auth_account_object_digest,
7152                    ) = move_authenticator.object_to_authenticate_components()?;
7153
7154                    let signer = move_authenticator.address();
7155
7156                    // Make sure the signer is a Move account.
7157                    let AuthenticatorFunctionRefForExecution {
7158                        authenticator_function_ref,
7159                        ..
7160                    } = self.check_move_account_for_validation(
7161                        auth_account_object_id,
7162                        auth_account_object_seq_number,
7163                        auth_account_object_digest,
7164                        account_object,
7165                        &signer,
7166                        protocol_config,
7167                    )?;
7168
7169                    // Check the MoveAuthenticator input objects.
7170                    let authenticator_checked_input_objects =
7171                        iota_transaction_checks::check_move_authenticator_input_for_validation(
7172                            authenticator_input_objects,
7173                        )?;
7174
7175                    Ok((
7176                        authenticator_checked_input_objects,
7177                        authenticator_function_ref,
7178                    ))
7179                },
7180            )
7181            .collect::<IotaResult<Vec<_>>>()?;
7182
7183        // Check the transaction inputs.
7184        let (gas_status, tx_checked_input_objects) =
7185            iota_transaction_checks::check_transaction_input(
7186                protocol_config,
7187                reference_gas_price,
7188                tx,
7189                tx_input_objects,
7190                tx_receiving_objects,
7191                &self.metrics.bytecode_verifier_metrics,
7192                verifier_limits_source,
7193                authenticator_gas_budget,
7194                is_execute_transaction_to_effects,
7195                InputCheckRules::STRICT,
7196            )?;
7197
7198        Ok((
7199            gas_status,
7200            tx_checked_input_objects,
7201            per_authenticator_checked_inputs,
7202        ))
7203    }
7204
7205    /// Like [`Self::check_transaction_inputs_for_validation`], but for
7206    /// post-consensus validation at a commit. One difference: the Move
7207    /// account is not resolved here, only the authenticator's inputs are
7208    /// checked, so no function reference comes back. The receiving objects,
7209    /// read through the commit-indexed reader, get the same checks as at
7210    /// admission.
7211    fn check_transaction_inputs_at_commit(
7212        &self,
7213        protocol_config: &ProtocolConfig,
7214        reference_gas_price: u64,
7215        tx: &Transaction,
7216        tx_input_objects: InputObjects,
7217        tx_receiving_objects: &ReceivingObjects,
7218        move_authenticators: &[&MoveAuthenticator],
7219        per_authenticator_inputs: Vec<(InputObjects, ObjectReadResult)>,
7220        verifier_limits_source: VerifierLimitsSource<'_>,
7221    ) -> IotaResult<CheckedValidationInputs> {
7222        let authenticator_gas_budget = if move_authenticators.is_empty() {
7223            0
7224        } else {
7225            protocol_config.max_auth_gas()
7226        };
7227
7228        debug_assert_eq!(
7229            move_authenticators.len(),
7230            per_authenticator_inputs.len(),
7231            "Move authenticators amount must match the number of authenticator inputs"
7232        );
7233
7234        // The account object is among the authenticator's inputs. An account
7235        // named as a shared object was decided by the reader from its rows,
7236        // which drop the transaction when the object was not created shared.
7237        // An account named as an immutable-or-owned input was read at the
7238        // version the transaction names, and this check rejects one owned by
7239        // an address or by another object. An immutable account is not
7240        // rejected here. Looking up the account's authenticator function is
7241        // left to execution.
7242        let per_authenticator_checked_inputs = per_authenticator_inputs
7243            .into_iter()
7244            .map(|(authenticator_input_objects, _account_object)| {
7245                iota_transaction_checks::check_move_authenticator_input_for_validation(
7246                    authenticator_input_objects,
7247                )
7248            })
7249            .collect::<IotaResult<Vec<_>>>()?;
7250
7251        // The gas status only meters an authenticator run, which does not happen here.
7252        let (_gas_status, tx_checked_input_objects) =
7253            iota_transaction_checks::check_transaction_input(
7254                protocol_config,
7255                reference_gas_price,
7256                tx,
7257                tx_input_objects,
7258                tx_receiving_objects,
7259                &self.metrics.bytecode_verifier_metrics,
7260                verifier_limits_source,
7261                authenticator_gas_budget,
7262                false,
7263                InputCheckRules::STRICT,
7264            )?;
7265
7266        Ok((tx_checked_input_objects, per_authenticator_checked_inputs))
7267    }
7268
7269    #[cfg(test)]
7270    pub(crate) fn iter_live_object_set_for_testing(
7271        &self,
7272    ) -> impl Iterator<Item = authority_store_tables::LiveObject> + '_ {
7273        self.get_global_state_hash_store()
7274            .iter_cached_live_object_set_for_testing()
7275            .map(|live_object| live_object.expect("reading the live object set cannot fail"))
7276    }
7277
7278    #[cfg(test)]
7279    pub(crate) fn shutdown_execution_for_test(&self) {
7280        self.tx_execution_shutdown
7281            .lock()
7282            .take()
7283            .unwrap()
7284            .send(())
7285            .unwrap();
7286    }
7287
7288    /// NOTE: this function is only to be used for fuzzing and testing. Never
7289    /// use in prod
7290    pub async fn insert_objects_unsafe_for_testing_only(&self, objects: &[Object]) {
7291        self.get_reconfig_api().bulk_insert_genesis_objects(objects);
7292        self.get_object_cache_reader()
7293            .force_reload_system_packages(&BuiltInFramework::all_package_ids());
7294        self.get_reconfig_api()
7295            .clear_state_end_of_epoch(&self.execution_lock_for_reconfiguration().await);
7296    }
7297}
7298
7299pub struct RandomnessRoundReceiver {
7300    authority_state: Arc<AuthorityState>,
7301    randomness_rx: mpsc::Receiver<(EpochId, RandomnessRound, Vec<u8>)>,
7302}
7303
7304impl RandomnessRoundReceiver {
7305    pub fn spawn(
7306        authority_state: Arc<AuthorityState>,
7307        randomness_rx: mpsc::Receiver<(EpochId, RandomnessRound, Vec<u8>)>,
7308    ) -> JoinHandle<()> {
7309        let rrr = RandomnessRoundReceiver {
7310            authority_state,
7311            randomness_rx,
7312        };
7313        spawn_monitored_task!(rrr.run())
7314    }
7315
7316    async fn run(mut self) {
7317        info!("RandomnessRoundReceiver event loop started");
7318
7319        loop {
7320            tokio::select! {
7321                maybe_recv = self.randomness_rx.recv() => {
7322                    if let Some((epoch, round, bytes)) = maybe_recv {
7323                        self.handle_new_randomness(epoch, round, bytes).await;
7324                    } else {
7325                        break;
7326                    }
7327                },
7328            }
7329        }
7330
7331        info!("RandomnessRoundReceiver event loop ended");
7332    }
7333
7334    #[instrument(level = "debug", skip_all, fields(?epoch, ?round))]
7335    async fn handle_new_randomness(&self, epoch: EpochId, round: RandomnessRound, bytes: Vec<u8>) {
7336        fail_point_async!("randomness-delay");
7337
7338        let epoch_store = self.authority_state.load_epoch_store_one_call_per_task();
7339        if epoch_store.epoch() != epoch {
7340            warn!(
7341                "dropping randomness for epoch {epoch}, round {round}, because we are in epoch {}",
7342                epoch_store.epoch()
7343            );
7344            return;
7345        }
7346        let key = TransactionKey::RandomnessRound(epoch, round);
7347        let transaction = VerifiedTransaction::new_randomness_state_update(
7348            epoch,
7349            round,
7350            bytes,
7351            epoch_store
7352                .epoch_start_config()
7353                .randomness_obj_initial_shared_version(),
7354        );
7355        debug!(
7356            "created randomness state update transaction with digest: {:?}",
7357            transaction.digest()
7358        );
7359        let transaction = VerifiedExecutableTransaction::new_system(transaction, epoch);
7360        let digest = *transaction.digest();
7361
7362        // Randomness state updates contain the full bls signature for the random round,
7363        // which cannot necessarily be reconstructed again later. Therefore we must
7364        // immediately persist this transaction. If we crash before its outputs
7365        // are committed, this ensures we will be able to re-execute it.
7366        self.authority_state
7367            .get_cache_commit()
7368            .persist_transaction(&transaction);
7369
7370        // Notify the scheduler that the transaction key now has a known digest
7371        if epoch_store.insert_tx_key(key, digest).is_err() {
7372            warn!("epoch ended while handling new randomness");
7373        }
7374
7375        // TODO: delete this when transaction manager is deleted
7376        match self.authority_state.execution_scheduler().as_ref() {
7377            ExecutionSchedulerWrapper::ExecutionScheduler(_) => {}
7378            ExecutionSchedulerWrapper::TransactionManager(manager) => {
7379                // Notifies transaction manager about transaction and output objects
7380                // committed. This provides necessary information to transaction manager
7381                // to start executing additional ready transactions.
7382                manager.notify_transaction_key(&epoch_store, key, digest);
7383            }
7384        }
7385
7386        let authority_state = self.authority_state.clone();
7387        spawn_monitored_task!(async move {
7388            // Wait for transaction execution in a separate task, to avoid deadlock in case
7389            // of out-of-order randomness generation. (Each
7390            // RandomnessStateUpdate depends on the output of the
7391            // RandomnessStateUpdate from the previous round.)
7392            //
7393            // We set a very long timeout so that in case this gets stuck for some reason,
7394            // the validator will eventually crash rather than continuing in a
7395            // zombie mode.
7396            const RANDOMNESS_STATE_UPDATE_EXECUTION_TIMEOUT: Duration = Duration::from_secs(300);
7397            let result = tokio::time::timeout(
7398                RANDOMNESS_STATE_UPDATE_EXECUTION_TIMEOUT,
7399                authority_state
7400                    .get_transaction_cache_reader()
7401                    .try_notify_read_executed_effects(
7402                        "RandomnessRoundReceiver::notify_read_executed_effects_first",
7403                        &[digest],
7404                    ),
7405            )
7406            .await;
7407            let result = match result {
7408                Ok(result) => result,
7409                Err(_) => {
7410                    if cfg!(debug_assertions) {
7411                        // Crash on randomness update execution timeout in debug builds.
7412                        panic!(
7413                            "randomness state update transaction execution timed out at epoch {epoch}, round {round}"
7414                        );
7415                    }
7416                    warn!(
7417                        "randomness state update transaction execution timed out at epoch {epoch}, round {round}"
7418                    );
7419                    // Continue waiting as long as necessary in non-debug builds.
7420                    authority_state
7421                        .get_transaction_cache_reader()
7422                        .try_notify_read_executed_effects(
7423                            "RandomnessRoundReceiver::notify_read_executed_effects_second",
7424                            &[digest],
7425                        )
7426                        .await
7427                }
7428            };
7429
7430            let mut effects = result.unwrap_or_else(|_| panic!("failed to get effects for randomness state update transaction at epoch {epoch}, round {round}"));
7431            let effects = effects.pop().expect("should return effects");
7432            if *effects.status() != ExecutionStatus::Success {
7433                fatal!(
7434                    "failed to execute randomness state update transaction at epoch {epoch}, round {round}: {effects:?}"
7435                );
7436            }
7437            debug!(
7438                "successfully executed randomness state update transaction at epoch {epoch}, round {round}"
7439            );
7440        });
7441    }
7442}
7443
7444#[async_trait]
7445impl TransactionKeyValueStoreTrait for AuthorityState {
7446    async fn multi_get(
7447        &self,
7448        transaction_keys: &[TransactionDigest],
7449        effects_keys: &[TransactionDigest],
7450    ) -> IotaResult<KVStoreTransactionData> {
7451        let txns = if !transaction_keys.is_empty() {
7452            self.get_transaction_cache_reader()
7453                .try_multi_get_transaction_blocks(transaction_keys)?
7454                .into_iter()
7455                .map(|t| t.map(|t| (*t).clone().into_inner()))
7456                .collect()
7457        } else {
7458            vec![]
7459        };
7460
7461        let fx = if !effects_keys.is_empty() {
7462            self.get_transaction_cache_reader()
7463                .try_multi_get_executed_effects(effects_keys)?
7464        } else {
7465            vec![]
7466        };
7467
7468        Ok((txns, fx))
7469    }
7470
7471    async fn multi_get_checkpoints(
7472        &self,
7473        checkpoint_summaries: &[CheckpointSequenceNumber],
7474        checkpoint_contents: &[CheckpointSequenceNumber],
7475        checkpoint_summaries_by_digest: &[CheckpointDigest],
7476    ) -> IotaResult<(
7477        Vec<Option<CertifiedCheckpointSummary>>,
7478        Vec<Option<CheckpointContents>>,
7479        Vec<Option<CertifiedCheckpointSummary>>,
7480    )> {
7481        // TODO: use multi-get methods if it ever becomes important (unlikely)
7482        let mut summaries = Vec::with_capacity(checkpoint_summaries.len());
7483        let store = self.get_checkpoint_store();
7484        for seq in checkpoint_summaries {
7485            let checkpoint = store
7486                .get_checkpoint_by_sequence_number(*seq)?
7487                .map(|c| c.into_inner());
7488
7489            summaries.push(checkpoint);
7490        }
7491
7492        let mut contents = Vec::with_capacity(checkpoint_contents.len());
7493        for seq in checkpoint_contents {
7494            let checkpoint = store
7495                .get_checkpoint_by_sequence_number(*seq)?
7496                .and_then(|summary| {
7497                    store
7498                        .get_checkpoint_contents(&summary.contents_digest)
7499                        .expect("db read cannot fail")
7500                });
7501            contents.push(checkpoint);
7502        }
7503
7504        let mut summaries_by_digest = Vec::with_capacity(checkpoint_summaries_by_digest.len());
7505        for digest in checkpoint_summaries_by_digest {
7506            let checkpoint = store
7507                .get_checkpoint_by_digest(digest)?
7508                .map(|c| c.into_inner());
7509            summaries_by_digest.push(checkpoint);
7510        }
7511
7512        Ok((summaries, contents, summaries_by_digest))
7513    }
7514
7515    async fn get_transaction_perpetual_checkpoint(
7516        &self,
7517        digest: TransactionDigest,
7518    ) -> IotaResult<Option<CheckpointSequenceNumber>> {
7519        self.get_checkpoint_cache()
7520            .try_get_transaction_perpetual_checkpoint(&digest)
7521            .map(|res| res.map(|(_epoch, checkpoint)| checkpoint))
7522    }
7523
7524    async fn get_object(
7525        &self,
7526        object_id: ObjectId,
7527        version: VersionNumber,
7528    ) -> IotaResult<Option<Object>> {
7529        self.get_object_with_historic_fallback(&ObjectKey(object_id, version))
7530    }
7531
7532    #[instrument(skip_all)]
7533    async fn multi_get_objects(
7534        &self,
7535        object_keys: &[ObjectKey],
7536    ) -> IotaResult<Vec<Option<Object>>> {
7537        let mut objects = self
7538            .get_object_cache_reader()
7539            .multi_get_objects_by_key(object_keys);
7540        self.historic_objects
7541            .fill_missing(object_keys, &mut objects, None)?;
7542        Ok(objects)
7543    }
7544
7545    async fn multi_get_transactions_perpetual_checkpoints(
7546        &self,
7547        digests: &[TransactionDigest],
7548    ) -> IotaResult<Vec<Option<CheckpointSequenceNumber>>> {
7549        let res = self
7550            .get_checkpoint_cache()
7551            .try_multi_get_transactions_perpetual_checkpoints(digests)?;
7552
7553        Ok(res
7554            .into_iter()
7555            .map(|maybe| maybe.map(|(_epoch, checkpoint)| checkpoint))
7556            .collect())
7557    }
7558
7559    #[instrument(skip(self, digests), fields(digests = digests.iter().map(|d| d.to_string()).collect::<Vec<String>>().join(", ")))]
7560    async fn multi_get_events_by_tx_digests(
7561        &self,
7562        digests: &[TransactionDigest],
7563    ) -> IotaResult<Vec<Option<TransactionEvents>>> {
7564        if digests.is_empty() {
7565            return Ok(vec![]);
7566        }
7567
7568        Ok(self
7569            .get_transaction_cache_reader()
7570            .multi_get_events(digests))
7571    }
7572}
7573
7574#[cfg(msim)]
7575pub mod framework_injection {
7576    use std::{
7577        cell::RefCell,
7578        collections::{BTreeMap, BTreeSet},
7579    };
7580
7581    use iota_framework::{BuiltInFramework, SystemPackage};
7582    use iota_sdk_types::ObjectId;
7583    use iota_types::base_types::AuthorityName;
7584    use move_binary_format::CompiledModule;
7585
7586    type FrameworkOverrideConfig = BTreeMap<ObjectId, PackageOverrideConfig>;
7587
7588    // Thread local cache because all simtests run in a single unique thread.
7589    thread_local! {
7590        static OVERRIDE: RefCell<FrameworkOverrideConfig> = RefCell::new(FrameworkOverrideConfig::default());
7591    }
7592
7593    type Framework = Vec<CompiledModule>;
7594
7595    pub type PackageUpgradeCallback =
7596        Box<dyn Fn(AuthorityName) -> Option<Framework> + Send + Sync + 'static>;
7597
7598    enum PackageOverrideConfig {
7599        Global(Framework),
7600        PerValidator(PackageUpgradeCallback),
7601    }
7602
7603    fn compiled_modules_to_bytes(modules: &[CompiledModule]) -> Vec<Vec<u8>> {
7604        modules
7605            .iter()
7606            .map(|m| {
7607                let mut buf = Vec::new();
7608                m.serialize_with_version(m.version, &mut buf).unwrap();
7609                buf
7610            })
7611            .collect()
7612    }
7613
7614    pub fn set_override(package_id: ObjectId, modules: Vec<CompiledModule>) {
7615        OVERRIDE.with(|bs| {
7616            bs.borrow_mut()
7617                .insert(package_id, PackageOverrideConfig::Global(modules))
7618        });
7619    }
7620
7621    pub fn set_override_cb(package_id: ObjectId, func: PackageUpgradeCallback) {
7622        OVERRIDE.with(|bs| {
7623            bs.borrow_mut()
7624                .insert(package_id, PackageOverrideConfig::PerValidator(func))
7625        });
7626    }
7627
7628    pub fn get_override_bytes(package_id: &ObjectId, name: AuthorityName) -> Option<Vec<Vec<u8>>> {
7629        OVERRIDE.with(|cfg| {
7630            cfg.borrow().get(package_id).and_then(|entry| match entry {
7631                PackageOverrideConfig::Global(framework) => {
7632                    Some(compiled_modules_to_bytes(framework))
7633                }
7634                PackageOverrideConfig::PerValidator(func) => {
7635                    func(name).map(|fw| compiled_modules_to_bytes(&fw))
7636                }
7637            })
7638        })
7639    }
7640
7641    pub fn get_override_modules(
7642        package_id: &ObjectId,
7643        name: AuthorityName,
7644    ) -> Option<Vec<CompiledModule>> {
7645        OVERRIDE.with(|cfg| {
7646            cfg.borrow().get(package_id).and_then(|entry| match entry {
7647                PackageOverrideConfig::Global(framework) => Some(framework.clone()),
7648                PackageOverrideConfig::PerValidator(func) => func(name),
7649            })
7650        })
7651    }
7652
7653    pub fn get_override_system_package(
7654        package_id: &ObjectId,
7655        name: AuthorityName,
7656    ) -> Option<SystemPackage> {
7657        let bytes = get_override_bytes(package_id, name)?;
7658        // A built-in package keeps the dependencies it declares. Anything else is a
7659        // package being added -- including one at a system address that is not built in
7660        // yet -- and is assumed to depend on all existing system packages.
7661        let dependencies = BuiltInFramework::try_get_package_by_id(package_id)
7662            .map_or_else(BuiltInFramework::all_package_ids, |package| {
7663                package.dependencies.to_vec()
7664            });
7665        Some(SystemPackage {
7666            id: *package_id,
7667            bytes,
7668            dependencies,
7669        })
7670    }
7671
7672    pub fn get_extra_packages(name: AuthorityName) -> Vec<SystemPackage> {
7673        let built_in = BTreeSet::from_iter(BuiltInFramework::all_package_ids());
7674        let extra: Vec<ObjectId> = OVERRIDE.with(|cfg| {
7675            cfg.borrow()
7676                .keys()
7677                .filter_map(|package| (!built_in.contains(package)).then_some(*package))
7678                .collect()
7679        });
7680
7681        extra
7682            .into_iter()
7683            .map(|package| SystemPackage {
7684                id: package,
7685                bytes: get_override_bytes(&package, name).unwrap(),
7686                dependencies: BuiltInFramework::all_package_ids(),
7687            })
7688            .collect()
7689    }
7690}
7691
7692#[derive(Debug, Serialize, Deserialize, Clone)]
7693pub struct ObjDumpFormat {
7694    pub id: ObjectId,
7695    pub version: VersionNumber,
7696    pub digest: ObjectDigest,
7697    pub object: Object,
7698}
7699
7700impl ObjDumpFormat {
7701    fn new(object: Object) -> Self {
7702        let oref = object.object_ref();
7703        Self {
7704            id: oref.object_id,
7705            version: oref.version,
7706            digest: oref.digest,
7707            object,
7708        }
7709    }
7710}
7711
7712#[derive(Debug, Serialize, Deserialize, Clone)]
7713pub struct NodeStateDump {
7714    pub tx_digest: TransactionDigest,
7715    pub sender_signed_data: SenderSignedTransaction,
7716    pub executed_epoch: u64,
7717    pub reference_gas_price: u64,
7718    pub protocol_version: u64,
7719    pub epoch_start_timestamp_ms: u64,
7720    pub computed_effects: TransactionEffects,
7721    pub expected_effects_digest: TransactionEffectsDigest,
7722    pub relevant_system_packages: Vec<ObjDumpFormat>,
7723    pub shared_objects: Vec<ObjDumpFormat>,
7724    pub loaded_child_objects: Vec<ObjDumpFormat>,
7725    pub modified_at_versions: Vec<ObjDumpFormat>,
7726    pub runtime_reads: Vec<ObjDumpFormat>,
7727    pub input_objects: Vec<ObjDumpFormat>,
7728    /// The outcome of the transaction named by the transaction dependency
7729    /// input, when execution read it.
7730    pub transaction_dependency: Option<TransactionDependencyReadResult>,
7731}
7732
7733impl NodeStateDump {
7734    pub fn new(
7735        tx_digest: &TransactionDigest,
7736        effects: &TransactionEffects,
7737        expected_effects_digest: TransactionEffectsDigest,
7738        object_store: &dyn ObjectStore,
7739        epoch_store: &Arc<AuthorityPerEpochStore>,
7740        inner_temporary_store: &InnerTemporaryStore,
7741        transaction: &VerifiedExecutableTransaction,
7742        transaction_dependency: Option<TransactionDependencyReadResult>,
7743    ) -> IotaResult<Self> {
7744        // Epoch info
7745        let executed_epoch = epoch_store.epoch();
7746        let reference_gas_price = epoch_store.reference_gas_price();
7747        let epoch_start_config = epoch_store.epoch_start_config();
7748        let protocol_version = epoch_store.protocol_version().as_u64();
7749        let epoch_start_timestamp_ms = epoch_start_config.epoch_data().epoch_start_timestamp();
7750
7751        // Record all system packages at this version
7752        let mut relevant_system_packages = Vec::new();
7753        for sys_package_id in BuiltInFramework::all_package_ids() {
7754            if let Some(w) = object_store.try_get_object(&sys_package_id)? {
7755                relevant_system_packages.push(ObjDumpFormat::new(w))
7756            }
7757        }
7758
7759        // Record all the shared objects
7760        let mut shared_objects = Vec::new();
7761        for kind in effects.input_shared_objects() {
7762            match kind {
7763                InputSharedObject::Mutate(obj_ref) | InputSharedObject::ReadOnly(obj_ref) => {
7764                    if let Some(w) =
7765                        object_store.try_get_object_by_key(&obj_ref.object_id, obj_ref.version)?
7766                    {
7767                        shared_objects.push(ObjDumpFormat::new(w))
7768                    }
7769                }
7770                InputSharedObject::ReadDeleted(..)
7771                | InputSharedObject::MutateDeleted(..)
7772                // TODO: consider record congested objects.
7773                | InputSharedObject::Canceled(..) => (),
7774                _ => unimplemented!(
7775                    "a new InputSharedObject enum variant was added and needs to be handled"
7776                ),
7777            }
7778        }
7779
7780        // Record all loaded child objects
7781        // Child objects which are read but not mutated are not tracked anywhere else
7782        let mut loaded_child_objects = Vec::new();
7783        for (id, meta) in &inner_temporary_store.loaded_runtime_objects {
7784            if let Some(w) = object_store.try_get_object_by_key(id, meta.version)? {
7785                loaded_child_objects.push(ObjDumpFormat::new(w))
7786            }
7787        }
7788
7789        // Record all modified objects
7790        let mut modified_at_versions = Vec::new();
7791        for modified in effects.modified_at_versions() {
7792            let (id, ver) = (modified.object_id(), modified.version());
7793            if let Some(w) = object_store.try_get_object_by_key(id, ver)? {
7794                modified_at_versions.push(ObjDumpFormat::new(w))
7795            }
7796        }
7797
7798        // Packages read at runtime, which were not previously loaded into the temoorary
7799        // store Some packages may be fetched at runtime and wont show up in
7800        // input objects
7801        let mut runtime_reads = Vec::new();
7802        for obj in inner_temporary_store
7803            .runtime_packages_loaded_from_db
7804            .values()
7805        {
7806            runtime_reads.push(ObjDumpFormat::new(obj.object().clone()));
7807        }
7808
7809        // All other input objects should already be in `inner_temporary_store.objects`
7810
7811        Ok(Self {
7812            tx_digest: *tx_digest,
7813            executed_epoch,
7814            reference_gas_price,
7815            epoch_start_timestamp_ms,
7816            protocol_version,
7817            relevant_system_packages,
7818            shared_objects,
7819            loaded_child_objects,
7820            modified_at_versions,
7821            runtime_reads,
7822            sender_signed_data: transaction.clone().into_message(),
7823            input_objects: inner_temporary_store
7824                .input_objects
7825                .values()
7826                .map(|o| ObjDumpFormat::new(o.clone()))
7827                .collect(),
7828            computed_effects: effects.clone(),
7829            expected_effects_digest,
7830            transaction_dependency,
7831        })
7832    }
7833
7834    pub fn all_objects(&self) -> Vec<ObjDumpFormat> {
7835        let mut objects = Vec::new();
7836        objects.extend(self.relevant_system_packages.clone());
7837        objects.extend(self.shared_objects.clone());
7838        objects.extend(self.loaded_child_objects.clone());
7839        objects.extend(self.modified_at_versions.clone());
7840        objects.extend(self.runtime_reads.clone());
7841        objects.extend(self.input_objects.clone());
7842        objects
7843    }
7844
7845    pub fn write_to_file(&self, path: &Path) -> Result<PathBuf, anyhow::Error> {
7846        let file_name = format!(
7847            "{}_{}_NODE_DUMP.json",
7848            self.tx_digest,
7849            AuthorityState::unixtime_now_ms()
7850        );
7851        let mut path = path.to_path_buf();
7852        path.push(&file_name);
7853        let mut file = File::create(path.clone())?;
7854        file.write_all(serde_json::to_string_pretty(self)?.as_bytes())?;
7855        Ok(path)
7856    }
7857
7858    pub fn read_from_file(path: &PathBuf) -> Result<Self, anyhow::Error> {
7859        let file = File::open(path)?;
7860        serde_json::from_reader(file).map_err(|e| anyhow::anyhow!(e))
7861    }
7862}
7863
7864/// The reason [`AuthorityState::check_move_account`] failed.
7865/// Execution treats the two cases differently, because only
7866/// the first one is about the transaction.
7867enum MoveAccountCheckError {
7868    /// The account fails a check.
7869    Input(UserInputError),
7870
7871    /// This validator failed to read its store.
7872    Storage(IotaError),
7873}
7874
7875impl From<UserInputError> for MoveAccountCheckError {
7876    fn from(error: UserInputError) -> Self {
7877        Self::Input(error)
7878    }
7879}
7880
7881impl From<MoveAccountCheckError> for IotaError {
7882    fn from(error: MoveAccountCheckError) -> Self {
7883        match error {
7884            MoveAccountCheckError::Input(error) => IotaError::UserInput { error },
7885            MoveAccountCheckError::Storage(error) => error,
7886        }
7887    }
7888}
7889
7890/// The error that admission and the attestor give for a transaction whose
7891/// Move authentication failed with `error`, unless
7892/// [`AuthorityState::authentication_failure`] gives its transaction
7893/// dependency's error instead.
7894fn move_authentication_failure(error: ExecutionError) -> IotaError {
7895    IotaError::MoveAuthenticatorExecutionFailure {
7896        error: error.to_string(),
7897    }
7898}
7899
7900/// Returns the [`MoveAuthenticator`]s to execute during the pre-consensus
7901/// phase.
7902///
7903/// When `pre_consensus_sponsor_only_move_authentication` is enabled:
7904/// - For sponsored transactions: only the sponsor's [`MoveAuthenticator`] is returned (empty if the
7905///   sponsor does not use one).
7906/// - For non-sponsored transactions: all [`MoveAuthenticator`]s are returned (currently only the
7907///   sender's).
7908///
7909/// When the flag is not set, all [`MoveAuthenticator`]s are returned for
7910/// compatibility.
7911fn pre_consensus_move_authenticators<'a>(
7912    tx: &'a VerifiedTransaction,
7913    protocol_config: &ProtocolConfig,
7914) -> Vec<&'a MoveAuthenticator> {
7915    if protocol_config.pre_consensus_sponsor_only_move_authentication() {
7916        if tx.transaction().is_sponsored_tx() {
7917            if let Some(sponsor_move_authenticator) = tx.sponsor_move_authenticator() {
7918                vec![sponsor_move_authenticator]
7919            } else {
7920                vec![]
7921            }
7922        } else {
7923            tx.move_authenticators()
7924        }
7925    } else {
7926        tx.move_authenticators()
7927    }
7928}