sui_types/
error.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::{
6    base_types::*,
7    committee::{Committee, EpochId, StakeUnit},
8    digests::CheckpointContentsDigest,
9    execution_status::{CommandArgumentError, CommandIndex, ExecutionErrorKind, ExecutionFailure},
10    messages_checkpoint::CheckpointSequenceNumber,
11    object::Owner,
12};
13
14use schemars::JsonSchema;
15use serde::{Deserialize, Serialize};
16use std::{collections::BTreeMap, fmt::Debug, slice::SliceIndex};
17use strum_macros::{AsRefStr, IntoStaticStr};
18use thiserror::Error;
19use tonic::Status;
20use typed_store_error::TypedStoreError;
21
22pub const TRANSACTION_NOT_FOUND_MSG_PREFIX: &str = "Could not find the referenced transaction";
23pub const TRANSACTIONS_NOT_FOUND_MSG_PREFIX: &str = "Could not find the referenced transactions";
24
25#[macro_export]
26macro_rules! fp_bail {
27    ($e:expr) => {
28        return Err($e)
29    };
30}
31
32#[macro_export(local_inner_macros)]
33macro_rules! fp_ensure {
34    ($cond:expr, $e:expr) => {
35        if !($cond) {
36            fp_bail!($e);
37        }
38    };
39}
40
41#[macro_export]
42macro_rules! exit_main {
43    ($result:expr) => {
44        match $result {
45            Ok(_) => (),
46            Err(err) => {
47                let err = format!("{:?}", err);
48                println!("{}", err.bold().red());
49                std::process::exit(1);
50            }
51        }
52    };
53}
54
55#[macro_export]
56macro_rules! make_invariant_violation {
57    ($($args:expr),* $(,)?) => {{
58        if cfg!(debug_assertions) {
59            panic!($($args),*)
60        }
61        $crate::error::ExecutionError::invariant_violation(format!($($args),*))
62    }}
63}
64
65#[macro_export]
66macro_rules! invariant_violation {
67    ($($args:expr),* $(,)?) => {
68        return Err(make_invariant_violation!($($args),*).into())
69    };
70}
71
72#[macro_export]
73macro_rules! assert_invariant {
74    ($cond:expr, $($args:expr),* $(,)?) => {{
75        if !$cond {
76            invariant_violation!($($args),*)
77        }
78    }};
79}
80
81/// A helper macro for performing a checked cast from one type to another, returning a
82/// ExecutionError invariant violation if the cast fails.
83#[macro_export]
84macro_rules! checked_as {
85    ($value:expr, $target_type:ty) => {{
86        let v = $value;
87        <$target_type>::try_from(v).map_err(|e| {
88            $crate::make_invariant_violation!(
89                "Value {} cannot be safely cast to {}: {:?}",
90                v,
91                stringify!($target_type),
92                e
93            )
94        })
95    }};
96}
97
98/// A trait for safe indexing into collections that returns a ExecutionError as long as the
99/// collection implements `AsRef<[T]>`.
100/// This is useful for avoiding panics on out-of-bounds access, and instead returning a proper
101/// error.
102pub trait SafeIndex<T> {
103    /// Get a reference to the element at the given `index`, or return invariant violation error
104    /// if the index is out of bounds.
105    fn safe_get<'a, I>(&'a self, index: I) -> Result<&'a I::Output, ExecutionError>
106    where
107        I: SliceIndex<[T]>,
108        T: 'a;
109
110    /// Get a mutable reference to the element at the given `index`, or return invariant violation
111    /// error if the index is out of bounds.
112    fn safe_get_mut<'a, I>(&'a mut self, index: I) -> Result<&'a mut I::Output, ExecutionError>
113    where
114        I: SliceIndex<[T]>,
115        T: 'a;
116}
117
118impl<T, C> SafeIndex<T> for C
119where
120    C: AsRef<[T]> + AsMut<[T]>,
121{
122    fn safe_get<'a, I>(&'a self, index: I) -> Result<&'a I::Output, ExecutionError>
123    where
124        I: SliceIndex<[T]>,
125        T: 'a,
126    {
127        let slice = self.as_ref();
128        let len = slice.len();
129        slice.get(index).ok_or_else(|| {
130            crate::make_invariant_violation!("Index out of bounds for collection of length {}", len)
131        })
132    }
133
134    fn safe_get_mut<'a, I>(&'a mut self, index: I) -> Result<&'a mut I::Output, ExecutionError>
135    where
136        I: SliceIndex<[T]>,
137        T: 'a,
138    {
139        let slice = self.as_mut();
140        let len = slice.len();
141        slice.get_mut(index).ok_or_else(|| {
142            crate::make_invariant_violation!("Index out of bounds for collection of length {}", len)
143        })
144    }
145}
146
147#[derive(
148    Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Error, Hash, AsRefStr, IntoStaticStr,
149)]
150pub enum UserInputError {
151    #[error("Mutable object {object_id} cannot appear more than one in one transaction")]
152    MutableObjectUsedMoreThanOnce { object_id: ObjectID },
153    #[error("Wrong number of parameters for the transaction")]
154    ObjectInputArityViolation,
155    #[error(
156        "Could not find the referenced object {} at version {:?}",
157        object_id,
158        version
159    )]
160    ObjectNotFound {
161        object_id: ObjectID,
162        version: Option<SequenceNumber>,
163    },
164    #[error(
165        "Transaction needs to be rebuilt because object {} version {} ({}) is unavailable for consumption, current version: {current_version}",
166        .provided_obj_ref.0, .provided_obj_ref.1, .provided_obj_ref.2
167    )]
168    ObjectVersionUnavailableForConsumption {
169        provided_obj_ref: ObjectRef,
170        current_version: SequenceNumber,
171    },
172    #[error("Package verification failed: {err}")]
173    PackageVerificationTimeout { err: String },
174    #[error("Dependent package not found on-chain: {package_id}")]
175    DependentPackageNotFound { package_id: ObjectID },
176    #[error("Mutable parameter provided, immutable parameter expected")]
177    ImmutableParameterExpectedError { object_id: ObjectID },
178    #[error("Size limit exceeded: {limit} is {value}")]
179    SizeLimitExceeded { limit: String, value: String },
180    #[error(
181        "Object {child_id} is owned by object {parent_id}. \
182        Objects owned by other objects cannot be used as input arguments"
183    )]
184    InvalidChildObjectArgument {
185        child_id: ObjectID,
186        parent_id: ObjectID,
187    },
188    #[error("Invalid Object digest for object {object_id}. Expected digest : {expected_digest}")]
189    InvalidObjectDigest {
190        object_id: ObjectID,
191        expected_digest: ObjectDigest,
192    },
193    #[error("Sequence numbers above the maximal value are not usable for transfers")]
194    InvalidSequenceNumber,
195    #[error("A move object is expected, instead a move package is passed: {object_id}")]
196    MovePackageAsObject { object_id: ObjectID },
197    #[error("A move package is expected, instead a move object is passed: {object_id}")]
198    MoveObjectAsPackage { object_id: ObjectID },
199    #[error("Transaction was not signed by the correct sender: {}", error)]
200    IncorrectUserSignature { error: String },
201
202    #[error("Object used as shared is not shared")]
203    NotSharedObjectError,
204    #[error("The transaction inputs contain duplicated ObjectRef's")]
205    DuplicateObjectRefInput,
206
207    // Gas related errors
208    #[error("Transaction gas payment missing")]
209    MissingGasPayment,
210    #[error("Gas object is not an owned object with owner: {:?}", owner)]
211    GasObjectNotOwnedObject { owner: Owner },
212    #[error("Gas budget: {gas_budget} is higher than max: {max_budget}")]
213    GasBudgetTooHigh { gas_budget: u64, max_budget: u64 },
214    #[error("Gas budget: {gas_budget} is lower than min: {min_budget}")]
215    GasBudgetTooLow { gas_budget: u64, min_budget: u64 },
216    #[error(
217        "Balance of gas object {gas_balance} is lower than the needed amount: {needed_gas_amount}"
218    )]
219    GasBalanceTooLow {
220        gas_balance: u128,
221        needed_gas_amount: u128,
222    },
223    #[error("Transaction kind does not support Sponsored Transaction")]
224    UnsupportedSponsoredTransactionKind,
225    #[error("Gas price {gas_price} under reference gas price (RGP) {reference_gas_price}")]
226    GasPriceUnderRGP {
227        gas_price: u64,
228        reference_gas_price: u64,
229    },
230    #[error("Gas price cannot exceed {max_gas_price} mist")]
231    GasPriceTooHigh { max_gas_price: u64 },
232    #[error("Object {object_id} is not a gas object")]
233    InvalidGasObject { object_id: ObjectID },
234    #[error("Gas object does not have enough balance to cover minimal gas spend")]
235    InsufficientBalanceToCoverMinimalGas,
236
237    #[error(
238        "Could not find the referenced object {object_id} as the asked version {asked_version:?} is higher than the latest {latest_version:?}"
239    )]
240    ObjectSequenceNumberTooHigh {
241        object_id: ObjectID,
242        asked_version: SequenceNumber,
243        latest_version: SequenceNumber,
244    },
245    #[error("Object deleted at reference ({}, {:?}, {})", object_ref.0, object_ref.1, object_ref.2)]
246    ObjectDeleted { object_ref: ObjectRef },
247    #[error("Invalid Batch Transaction: {error}")]
248    InvalidBatchTransaction { error: String },
249    #[error("This Move function is currently disabled and not available for call")]
250    BlockedMoveFunction,
251    #[error("Empty input coins for Pay related transaction")]
252    EmptyInputCoins,
253
254    #[error(
255        "SUI payment transactions use first input coin for gas payment, but found a different gas object"
256    )]
257    UnexpectedGasPaymentObject,
258
259    #[error("Wrong initial version given for shared object")]
260    SharedObjectStartingVersionMismatch,
261
262    #[error(
263        "Attempt to transfer object {object_id} that does not have public transfer. Object transfer must be done instead using a distinct Move function call"
264    )]
265    TransferObjectWithoutPublicTransferError { object_id: ObjectID },
266
267    #[error(
268        "TransferObjects, MergeCoin, and Publish cannot have empty arguments. \
269        If MakeMoveVec has empty arguments, it must have a type specified"
270    )]
271    EmptyCommandInput,
272
273    #[error("Transaction is denied: {error}")]
274    TransactionDenied { error: String },
275
276    #[error("Feature is not supported: {0}")]
277    Unsupported(String),
278
279    #[error("Query transactions with move function input error: {0}")]
280    MoveFunctionInputError(String),
281
282    #[error("Verified checkpoint not found for sequence number: {0}")]
283    VerifiedCheckpointNotFound(CheckpointSequenceNumber),
284
285    #[error("Verified checkpoint not found for digest: {0}")]
286    VerifiedCheckpointDigestNotFound(String),
287
288    #[error("Latest checkpoint sequence number not found")]
289    LatestCheckpointSequenceNumberNotFound,
290
291    #[error("Checkpoint contents not found for digest: {0}")]
292    CheckpointContentsNotFound(CheckpointContentsDigest),
293
294    #[error("Genesis transaction not found")]
295    GenesisTransactionNotFound,
296
297    #[error("Transaction {0} not found")]
298    TransactionCursorNotFound(u64),
299
300    #[error(
301        "Object {} is a system object and cannot be accessed by user transactions",
302        object_id
303    )]
304    InaccessibleSystemObject { object_id: ObjectID },
305    #[error(
306        "{max_publish_commands} max publish/upgrade commands allowed, {publish_count} provided"
307    )]
308    MaxPublishCountExceeded {
309        max_publish_commands: u64,
310        publish_count: u64,
311    },
312
313    #[error("Immutable parameter provided, mutable parameter expected")]
314    MutableParameterExpected { object_id: ObjectID },
315
316    #[error("Address {address} is denied for coin {coin_type}")]
317    AddressDeniedForCoin {
318        address: SuiAddress,
319        coin_type: String,
320    },
321
322    #[error("Commands following a command with Random can only be TransferObjects or MergeCoins")]
323    PostRandomCommandRestrictions,
324
325    // Soft Bundle related errors
326    #[error("Number of transactions ({size}) exceeds the maximum allowed ({limit}) in a batch")]
327    TooManyTransactionsInBatch { size: usize, limit: u64 },
328    #[error(
329        "Total transactions size ({size}) bytes exceeds the maximum allowed ({limit}) bytes in a Soft Bundle"
330    )]
331    TotalTransactionSizeTooLargeInBatch { size: usize, limit: u64 },
332    #[error("Transaction {digest} in Soft Bundle contains no shared objects")]
333    NoSharedObjectError { digest: TransactionDigest },
334    #[error("Transaction {digest} in Soft Bundle has already been executed")]
335    AlreadyExecutedInSoftBundleError { digest: TransactionDigest },
336    #[error("At least one certificate in Soft Bundle has already been processed")]
337    CertificateAlreadyProcessed,
338    #[error("Transaction {digest} was already executed")]
339    TransactionAlreadyExecuted { digest: TransactionDigest },
340    #[error(
341        "Gas price for transaction {digest} in Soft Bundle mismatch: want {expected}, have {actual}"
342    )]
343    GasPriceMismatchError {
344        digest: TransactionDigest,
345        expected: u64,
346        actual: u64,
347    },
348
349    #[error("Coin type is globally paused for use: {coin_type}")]
350    CoinTypeGlobalPause { coin_type: String },
351
352    #[error("Invalid identifier found in the transaction: {error}")]
353    InvalidIdentifier { error: String },
354
355    #[error("Object used as owned is not owned")]
356    NotOwnedObjectError,
357
358    #[error("Invalid withdraw reservation: {error}")]
359    InvalidWithdrawReservation { error: String },
360
361    #[error("Transaction with empty gas payment must specify an expiration.")]
362    MissingTransactionExpiration,
363
364    #[error("Invalid transaction expiration: {error}")]
365    InvalidExpiration { error: String },
366
367    #[error("Transaction chain ID {provided} does not match network chain ID {expected}.")]
368    InvalidChainId { provided: String, expected: String },
369
370    #[error("Transaction {digest} appears more than once in the request")]
371    RepeatedTransactions { digest: TransactionDigest },
372}
373
374#[derive(
375    Eq,
376    PartialEq,
377    Clone,
378    Debug,
379    Serialize,
380    Deserialize,
381    Hash,
382    AsRefStr,
383    IntoStaticStr,
384    JsonSchema,
385    Error,
386)]
387#[serde(tag = "code", rename = "ObjectResponseError", rename_all = "camelCase")]
388pub enum SuiObjectResponseError {
389    #[error("Object {object_id} does not exist")]
390    NotExists { object_id: ObjectID },
391    #[error("Cannot find dynamic field for parent object {parent_object_id}")]
392    DynamicFieldNotFound { parent_object_id: ObjectID },
393    #[error(
394        "Object has been deleted object_id: {object_id} at version: {version:?} in digest {digest}"
395    )]
396    Deleted {
397        object_id: ObjectID,
398        /// Object version.
399        version: SequenceNumber,
400        /// Base64 string representing the object digest
401        digest: ObjectDigest,
402    },
403    #[error("Unknown Error")]
404    Unknown,
405    #[error("Display Error: {error}")]
406    DisplayError { error: String },
407    // TODO: also integrate SuiPastObjectResponse (VersionNotFound,  VersionTooHigh)
408}
409
410/// Custom error type for Sui.
411#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Error, Hash)]
412#[error(transparent)]
413pub struct SuiError(#[from] pub Box<SuiErrorKind>);
414
415/// Custom error type for Sui.
416#[derive(
417    Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Error, Hash, AsRefStr, IntoStaticStr,
418)]
419pub enum SuiErrorKind {
420    #[error("Error checking transaction input objects: {error}")]
421    UserInputError { error: UserInputError },
422
423    #[error("Error checking transaction object: {error}")]
424    SuiObjectResponseError { error: SuiObjectResponseError },
425
426    #[error("Expecting a single owner, shared ownership found")]
427    UnexpectedOwnerType,
428
429    #[error("There are already {queue_len} transactions pending, above threshold of {threshold}")]
430    TooManyTransactionsPendingExecution { queue_len: usize, threshold: usize },
431
432    #[error("There are too many transactions pending in consensus")]
433    TooManyTransactionsPendingConsensus,
434
435    #[error(
436        "Input {object_id} already has {queue_len} transactions pending, above threshold of {threshold}"
437    )]
438    TooManyTransactionsPendingOnObject {
439        object_id: ObjectID,
440        queue_len: usize,
441        threshold: usize,
442    },
443
444    #[error(
445        "Input {object_id} has a transaction {txn_age_sec} seconds old pending, above threshold of {threshold} seconds"
446    )]
447    TooOldTransactionPendingOnObject {
448        object_id: ObjectID,
449        txn_age_sec: u64,
450        threshold: u64,
451    },
452
453    #[error("Soft bundle must only contain transactions of UserTransaction kind")]
454    InvalidTxKindInSoftBundle,
455
456    // Signature verification
457    #[error("Signature is not valid: {}", error)]
458    InvalidSignature { error: String },
459    #[error("Required Signature from {expected} is absent {:?}", actual)]
460    SignerSignatureAbsent {
461        expected: String,
462        actual: Vec<String>,
463    },
464    #[error("Expect {expected} signer signatures but got {actual}")]
465    SignerSignatureNumberMismatch { expected: usize, actual: usize },
466    #[error("Value was not signed by the correct sender: {}", error)]
467    IncorrectSigner { error: String },
468    #[error(
469        "Value was not signed by a known authority. signer: {:?}, index: {:?}, committee: {committee}",
470        signer,
471        index
472    )]
473    UnknownSigner {
474        signer: Option<String>,
475        index: Option<u32>,
476        committee: Box<Committee>,
477    },
478    #[error(
479        "Validator {:?} responded multiple signatures for the same message, conflicting: {:?}",
480        signer,
481        conflicting_sig
482    )]
483    StakeAggregatorRepeatedSigner {
484        signer: AuthorityName,
485        conflicting_sig: bool,
486    },
487    // TODO: Used for distinguishing between different occurrences of invalid signatures, to allow retries in some cases.
488    #[error(
489        "Signature is not valid, but a retry may result in a valid one: {}",
490        error
491    )]
492    PotentiallyTemporarilyInvalidSignature { error: String },
493
494    // Certificate verification and execution
495    #[error(
496        "Signature or certificate from wrong epoch, expected {expected_epoch}, got {actual_epoch}"
497    )]
498    WrongEpoch {
499        expected_epoch: EpochId,
500        actual_epoch: EpochId,
501    },
502    #[error("Signatures in a certificate must form a quorum")]
503    CertificateRequiresQuorum,
504    #[allow(non_camel_case_types)]
505    #[error("DEPRECATED")]
506    DEPRECATED_ErrorWhileProcessingCertificate,
507    #[error(
508        "Failed to get a quorum of signed effects when processing transaction: {effects_map:?}"
509    )]
510    QuorumFailedToGetEffectsQuorumWhenProcessingTransaction {
511        effects_map: BTreeMap<TransactionEffectsDigest, (Vec<AuthorityName>, StakeUnit)>,
512    },
513    #[error(
514        "Failed to verify Tx certificate with executed effects, error: {error:?}, validator: {validator_name:?}"
515    )]
516    FailedToVerifyTxCertWithExecutedEffects {
517        validator_name: AuthorityName,
518        error: String,
519    },
520    #[error("Transaction is already finalized but with different user signatures")]
521    TxAlreadyFinalizedWithDifferentUserSigs,
522
523    // Account access
524    #[error("Invalid authenticator")]
525    InvalidAuthenticator,
526    #[error("Invalid address")]
527    InvalidAddress,
528    #[error("Invalid transaction digest.")]
529    InvalidTransactionDigest,
530
531    #[error("Invalid digest length. Expected {expected}, got {actual}")]
532    InvalidDigestLength { expected: usize, actual: usize },
533    #[error("Invalid DKG message size")]
534    InvalidDkgMessageSize,
535
536    #[error("Unexpected message: {0}")]
537    UnexpectedMessage(String),
538
539    // Move module publishing related errors
540    #[error("Failed to verify the Move module, reason: {error}.")]
541    ModuleVerificationFailure { error: String },
542    #[error("Failed to deserialize the Move module, reason: {error}.")]
543    ModuleDeserializationFailure { error: String },
544    #[error("Failed to publish the Move module(s), reason: {error}")]
545    ModulePublishFailure { error: String },
546    #[error("Failed to build Move modules: {error}.")]
547    ModuleBuildFailure { error: String },
548
549    // Move call related errors
550    #[error("Function resolution failure: {error}.")]
551    FunctionNotFound { error: String },
552    #[error("Module not found in package: {module_name}.")]
553    ModuleNotFound { module_name: String },
554    #[error("Type error while binding function arguments: {error}.")]
555    TypeError { error: String },
556    #[error("Circular object ownership detected")]
557    CircularObjectOwnership,
558
559    // Internal state errors
560    #[error("Attempt to re-initialize a transaction lock for objects {:?}.", refs)]
561    ObjectLockAlreadyInitialized { refs: Vec<ObjectRef> },
562    #[error(
563        "Object {obj_ref:?} already locked by a different transaction: {pending_transaction:?}"
564    )]
565    ObjectLockConflict {
566        obj_ref: ObjectRef,
567        pending_transaction: TransactionDigest,
568    },
569    #[error(
570        "Objects {obj_refs:?} are already locked by a transaction from a future epoch {locked_epoch:?}), attempt to override with a transaction from epoch {new_epoch:?}"
571    )]
572    ObjectLockedAtFutureEpoch {
573        obj_refs: Vec<ObjectRef>,
574        locked_epoch: EpochId,
575        new_epoch: EpochId,
576        locked_by_tx: TransactionDigest,
577    },
578    #[error("{TRANSACTION_NOT_FOUND_MSG_PREFIX} [{:?}].", digest)]
579    TransactionNotFound { digest: TransactionDigest },
580    #[error("{TRANSACTIONS_NOT_FOUND_MSG_PREFIX} [{:?}].", digests)]
581    TransactionsNotFound { digests: Vec<TransactionDigest> },
582    #[error("Could not find the referenced transaction events [{digest:?}].")]
583    TransactionEventsNotFound { digest: TransactionDigest },
584    #[error("Could not find the referenced transaction effects [{digest:?}].")]
585    TransactionEffectsNotFound { digest: TransactionDigest },
586    #[error(
587        "Attempt to move to `Executed` state an transaction that has already been executed: {:?}.",
588        digest
589    )]
590    TransactionAlreadyExecuted { digest: TransactionDigest },
591    #[error("Transaction reject reason not found for transaction {digest:?}")]
592    TransactionRejectReasonNotFound { digest: TransactionDigest },
593    #[error("Object ID did not have the expected type")]
594    BadObjectType { error: String },
595    #[error("Fail to retrieve Object layout for {st}")]
596    FailObjectLayout { st: String },
597
598    #[error("Execution invariant violated")]
599    ExecutionInvariantViolation,
600    #[error("Validator {authority:?} is faulty in a Byzantine manner: {reason:?}")]
601    ByzantineAuthoritySuspicion {
602        authority: AuthorityName,
603        reason: String,
604    },
605    #[allow(non_camel_case_types)]
606    #[serde(rename = "StorageError")]
607    #[error("DEPRECATED")]
608    DEPRECATED_StorageError,
609    #[allow(non_camel_case_types)]
610    #[serde(rename = "GenericStorageError")]
611    #[error("DEPRECATED")]
612    DEPRECATED_GenericStorageError,
613    #[error(
614        "Attempted to access {object} through parent {given_parent}, \
615        but it's actual parent is {actual_owner}"
616    )]
617    InvalidChildObjectAccess {
618        object: ObjectID,
619        given_parent: ObjectID,
620        actual_owner: Owner,
621    },
622
623    #[allow(non_camel_case_types)]
624    #[serde(rename = "StorageMissingFieldError")]
625    #[error("DEPRECATED")]
626    DEPRECATED_StorageMissingFieldError,
627    #[allow(non_camel_case_types)]
628    #[serde(rename = "StorageCorruptedFieldError")]
629    #[error("DEPRECATED")]
630    DEPRECATED_StorageCorruptedFieldError,
631
632    #[error("Authority Error: {error}")]
633    GenericAuthorityError { error: String },
634
635    #[error("Generic Bridge Error: {error}")]
636    GenericBridgeError { error: String },
637
638    #[error("Failed to dispatch subscription: {error}")]
639    FailedToDispatchSubscription { error: String },
640
641    #[error("Failed to serialize Owner: {error}")]
642    OwnerFailedToSerialize { error: String },
643
644    #[error("Failed to deserialize fields into JSON: {error}")]
645    ExtraFieldFailedToDeserialize { error: String },
646
647    #[error("Failed to execute transaction locally by Orchestrator: {error}")]
648    TransactionOrchestratorLocalExecutionError { error: String },
649
650    // Errors returned by authority and client read API's
651    #[error("Failure serializing transaction in the requested format: {error}")]
652    TransactionSerializationError { error: String },
653    #[error("Failure deserializing transaction from the provided format: {error}")]
654    TransactionDeserializationError { error: String },
655    #[error("Failure serializing transaction effects from the provided format: {error}")]
656    TransactionEffectsSerializationError { error: String },
657    #[error("Failure deserializing transaction effects from the provided format: {error}")]
658    TransactionEffectsDeserializationError { error: String },
659    #[error("Failure serializing transaction events from the provided format: {error}")]
660    TransactionEventsSerializationError { error: String },
661    #[error("Failure deserializing transaction events from the provided format: {error}")]
662    TransactionEventsDeserializationError { error: String },
663    #[error("Failure serializing object in the requested format: {error}")]
664    ObjectSerializationError { error: String },
665    #[error("Failure deserializing object in the requested format: {error}")]
666    ObjectDeserializationError { error: String },
667    #[error("Event store component is not active on this node")]
668    NoEventStore,
669
670    // Client side error
671    #[error("Too many authority errors were detected for {}: {:?}", action, errors)]
672    TooManyIncorrectAuthorities {
673        errors: Vec<(AuthorityName, SuiError)>,
674        action: String,
675    },
676    #[error("Invalid transaction range query to the fullnode: {error}")]
677    FullNodeInvalidTxRangeQuery { error: String },
678
679    // Errors related to the authority-consensus interface.
680    #[error("Failed to submit transaction to consensus: {0}")]
681    FailedToSubmitToConsensus(String),
682    #[error("Failed to connect with consensus node: {0}")]
683    ConsensusConnectionBroken(String),
684    #[error("Failed to execute handle_consensus_transaction on Sui: {0}")]
685    HandleConsensusTransactionFailure(String),
686
687    // Cryptography errors.
688    #[error("Signature key generation error: {0}")]
689    SignatureKeyGenError(String),
690    #[error("Key Conversion Error: {0}")]
691    KeyConversionError(String),
692    #[error("Invalid Private Key provided")]
693    InvalidPrivateKey,
694
695    // Unsupported Operations on Fullnode
696    #[error("Fullnode does not support handle_certificate")]
697    FullNodeCantHandleCertificate,
698
699    // Epoch related errors.
700    #[error("Validator temporarily stopped processing transactions due to epoch change")]
701    ValidatorHaltedAtEpochEnd,
702    #[error("Operations for epoch {0} have ended")]
703    EpochEnded(EpochId),
704    #[error("Error when advancing epoch: {error}")]
705    AdvanceEpochError { error: String },
706
707    #[error("Transaction Expired")]
708    TransactionExpired,
709
710    // These are errors that occur when an RPC fails and is simply the utf8 message sent in a
711    // Tonic::Status
712    #[error("{1} - {0}")]
713    RpcError(String, String),
714
715    #[error("Method not allowed")]
716    InvalidRpcMethodError,
717
718    #[error("Use of disabled feature: {error}")]
719    UnsupportedFeatureError { error: String },
720
721    #[error("Unable to communicate with the Quorum Driver channel: {error}")]
722    QuorumDriverCommunicationError { error: String },
723
724    #[error("Operation timed out")]
725    TimeoutError,
726
727    #[error("Error executing {0}")]
728    ExecutionError(String),
729
730    #[error("Invalid committee composition")]
731    InvalidCommittee(String),
732
733    #[error("Missing committee information for epoch {0}")]
734    MissingCommitteeAtEpoch(EpochId),
735
736    #[error("Index store not available on this Fullnode.")]
737    IndexStoreNotAvailable,
738
739    #[error("Failed to read dynamic field from table in the object store: {0}")]
740    DynamicFieldReadError(String),
741
742    #[error("Failed to read or deserialize system state related data structures on-chain: {0}")]
743    SuiSystemStateReadError(String),
744
745    #[error("Failed to read or deserialize bridge related data structures on-chain: {0}")]
746    SuiBridgeReadError(String),
747
748    #[error("Unexpected version error: {0}")]
749    UnexpectedVersion(String),
750
751    #[error("Message version is not supported at the current protocol version: {error}")]
752    WrongMessageVersion { error: String },
753
754    #[error("unknown error: {0}")]
755    Unknown(String),
756
757    #[error("Failed to perform file operation: {0}")]
758    FileIOError(String),
759
760    #[error("Failed to get JWK")]
761    JWKRetrievalError,
762
763    #[error("Storage error: {0}")]
764    Storage(String),
765
766    #[error(
767        "Validator cannot handle the request at the moment. Please retry after at least {retry_after_secs} seconds."
768    )]
769    ValidatorOverloadedRetryAfter { retry_after_secs: u64 },
770
771    #[error("Too many requests")]
772    TooManyRequests,
773
774    #[error("The request did not contain a certificate")]
775    NoCertificateProvidedError,
776
777    #[error("Nitro attestation verify failed: {0}")]
778    NitroAttestationFailedToVerify(String),
779
780    #[error("Failed to serialize {type_info}, error: {error}")]
781    GrpcMessageSerializeError { type_info: String, error: String },
782
783    #[error("Failed to deserialize {type_info}, error: {error}")]
784    GrpcMessageDeserializeError { type_info: String, error: String },
785
786    #[error(
787        "Validator consensus rounds are lagging behind. last committed leader round: {last_committed_round}, requested round: {round}"
788    )]
789    ValidatorConsensusLagging {
790        round: u32,
791        last_committed_round: u32,
792    },
793
794    #[error("Invalid admin request: {0}")]
795    InvalidAdminRequest(String),
796
797    #[error("Invalid request: {0}")]
798    InvalidRequest(String),
799
800    #[error(
801        "The current set of aliases for a required signer changed after the transaction was submitted"
802    )]
803    AliasesChanged,
804
805    // Retriable by client because another validator can create the correct claim.
806    #[error("Object {object_id} not found among input objects.")]
807    ImmutableObjectClaimNotFoundInInput { object_id: ObjectID },
808
809    // Retriable by client because another validator can create the correct claim.
810    #[error("Immutable object {object_id} was not included in immutable claims.")]
811    ImmutableObjectNotClaimed { object_id: ObjectID },
812
813    // Retriable by client because the object can be frozen in the future.
814    #[error(
815        "Claimed object {claimed_object_id} is not immutable. Found object ref: {found_object_ref:?}"
816    )]
817    InvalidImmutableObjectClaim {
818        claimed_object_id: ObjectID,
819        found_object_ref: ObjectRef,
820    },
821
822    #[error(
823        "Transaction was outbid by higher-gas-price transactions in the admission queue (current minimum gas price required: {min_gas_price})"
824    )]
825    TransactionRejectedDueToOutbiddingDuringCongestion { min_gas_price: u64 },
826
827    #[error("Transaction {digest} is being processed post-consensus: {status}")]
828    TransactionProcessing {
829        digest: TransactionDigest,
830        status: String,
831    },
832
833    #[error("Transaction {digest} has been recently submitted to this validator.")]
834    TransactionSubmitted { digest: TransactionDigest },
835}
836
837#[repr(u64)]
838#[allow(non_camel_case_types)]
839#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
840/// Sub-status codes for the `UNKNOWN_VERIFICATION_ERROR` VM Status Code which provides more context
841/// TODO: add more Vm Status errors. We use `UNKNOWN_VERIFICATION_ERROR` as a catchall for now.
842pub enum VMMVerifierErrorSubStatusCode {
843    MULTIPLE_RETURN_VALUES_NOT_ALLOWED = 0,
844    INVALID_OBJECT_CREATION = 1,
845}
846
847#[repr(u64)]
848#[allow(non_camel_case_types)]
849#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
850/// Sub-status codes for the `MEMORY_LIMIT_EXCEEDED` VM Status Code which provides more context
851pub enum VMMemoryLimitExceededSubStatusCode {
852    EVENT_COUNT_LIMIT_EXCEEDED = 0,
853    EVENT_SIZE_LIMIT_EXCEEDED = 1,
854    NEW_ID_COUNT_LIMIT_EXCEEDED = 2,
855    DELETED_ID_COUNT_LIMIT_EXCEEDED = 3,
856    TRANSFER_ID_COUNT_LIMIT_EXCEEDED = 4,
857    OBJECT_RUNTIME_CACHE_LIMIT_EXCEEDED = 5,
858    OBJECT_RUNTIME_STORE_LIMIT_EXCEEDED = 6,
859    TOTAL_EVENT_SIZE_LIMIT_EXCEEDED = 7,
860}
861
862pub type SuiResult<T = ()> = Result<T, SuiError>;
863pub type UserInputResult<T = ()> = Result<T, UserInputError>;
864
865impl From<SuiErrorKind> for SuiError {
866    fn from(error: SuiErrorKind) -> Self {
867        SuiError(Box::new(error))
868    }
869}
870
871impl std::ops::Deref for SuiError {
872    type Target = SuiErrorKind;
873
874    fn deref(&self) -> &Self::Target {
875        &self.0
876    }
877}
878
879impl From<sui_protocol_config::Error> for SuiError {
880    fn from(error: sui_protocol_config::Error) -> Self {
881        SuiErrorKind::WrongMessageVersion { error: error.0 }.into()
882    }
883}
884
885impl From<ExecutionError> for SuiError {
886    fn from(error: ExecutionError) -> Self {
887        SuiErrorKind::ExecutionError(error.to_string()).into()
888    }
889}
890
891impl From<Status> for SuiError {
892    fn from(status: Status) -> Self {
893        if status.message() == "Too many requests" {
894            return SuiErrorKind::TooManyRequests.into();
895        }
896
897        let result = bcs::from_bytes::<SuiError>(status.details());
898        if let Ok(sui_error) = result {
899            sui_error
900        } else {
901            SuiErrorKind::RpcError(
902                status.message().to_owned(),
903                status.code().description().to_owned(),
904            )
905            .into()
906        }
907    }
908}
909
910impl From<TypedStoreError> for SuiError {
911    fn from(e: TypedStoreError) -> Self {
912        SuiErrorKind::Storage(e.to_string()).into()
913    }
914}
915
916impl From<crate::storage::error::Error> for SuiError {
917    fn from(e: crate::storage::error::Error) -> Self {
918        SuiErrorKind::Storage(e.to_string()).into()
919    }
920}
921
922impl From<SuiErrorKind> for Status {
923    fn from(error: SuiErrorKind) -> Self {
924        let bytes = bcs::to_bytes(&error).unwrap();
925        Status::with_details(tonic::Code::Internal, error.to_string(), bytes.into())
926    }
927}
928
929impl From<SuiError> for Status {
930    fn from(error: SuiError) -> Self {
931        Status::from(error.into_inner())
932    }
933}
934
935impl From<ExecutionErrorKind> for SuiError {
936    fn from(kind: ExecutionErrorKind) -> Self {
937        ExecutionError::from_kind(kind).into()
938    }
939}
940
941impl From<&str> for SuiError {
942    fn from(error: &str) -> Self {
943        SuiErrorKind::GenericAuthorityError {
944            error: error.to_string(),
945        }
946        .into()
947    }
948}
949
950impl From<String> for SuiError {
951    fn from(error: String) -> Self {
952        SuiErrorKind::GenericAuthorityError { error }.into()
953    }
954}
955
956impl TryFrom<SuiErrorKind> for UserInputError {
957    type Error = anyhow::Error;
958
959    fn try_from(err: SuiErrorKind) -> Result<Self, Self::Error> {
960        match err {
961            SuiErrorKind::UserInputError { error } => Ok(error),
962            other => anyhow::bail!("error {:?} is not UserInputError", other),
963        }
964    }
965}
966
967impl TryFrom<SuiError> for UserInputError {
968    type Error = anyhow::Error;
969
970    fn try_from(err: SuiError) -> Result<Self, Self::Error> {
971        err.into_inner().try_into()
972    }
973}
974
975impl From<UserInputError> for SuiError {
976    fn from(error: UserInputError) -> Self {
977        SuiErrorKind::UserInputError { error }.into()
978    }
979}
980
981impl From<SuiObjectResponseError> for SuiError {
982    fn from(error: SuiObjectResponseError) -> Self {
983        SuiErrorKind::SuiObjectResponseError { error }.into()
984    }
985}
986
987impl PartialEq<SuiErrorKind> for SuiError {
988    fn eq(&self, other: &SuiErrorKind) -> bool {
989        &*self.0 == other
990    }
991}
992
993impl PartialEq<SuiError> for SuiErrorKind {
994    fn eq(&self, other: &SuiError) -> bool {
995        self == &*other.0
996    }
997}
998
999impl SuiError {
1000    pub fn as_inner(&self) -> &SuiErrorKind {
1001        &self.0
1002    }
1003
1004    pub fn into_inner(self) -> SuiErrorKind {
1005        *self.0
1006    }
1007}
1008
1009impl SuiErrorKind {
1010    /// Returns the variant name of the error. Sub-variants within UserInputError are unpacked too.
1011    pub fn to_variant_name(&self) -> &'static str {
1012        match &self {
1013            SuiErrorKind::UserInputError { error } => error.into(),
1014            _ => self.into(),
1015        }
1016    }
1017
1018    pub fn individual_error_indicates_epoch_change(&self) -> bool {
1019        matches!(
1020            self,
1021            SuiErrorKind::ValidatorHaltedAtEpochEnd | SuiErrorKind::MissingCommitteeAtEpoch(_)
1022        )
1023    }
1024
1025    /// Returns if the error is retryable and if the error's retryability is
1026    /// explicitly categorized.
1027    /// There should be only a handful of retryable errors. For now we list common
1028    /// non-retryable error below to help us find more retryable errors in logs.
1029    pub fn is_retryable(&self) -> (bool, bool) {
1030        let retryable = match self {
1031            // Network error
1032            SuiErrorKind::RpcError { .. } => true,
1033
1034            // Reconfig error
1035            SuiErrorKind::ValidatorHaltedAtEpochEnd => true,
1036            SuiErrorKind::MissingCommitteeAtEpoch(..) => true,
1037            SuiErrorKind::WrongEpoch { .. } => true,
1038            SuiErrorKind::EpochEnded(..) => true,
1039
1040            SuiErrorKind::UserInputError { error } => {
1041                match error {
1042                    // Only ObjectNotFound and DependentPackageNotFound is potentially retryable
1043                    UserInputError::ObjectNotFound { .. } => true,
1044                    UserInputError::DependentPackageNotFound { .. } => true,
1045                    _ => false,
1046                }
1047            }
1048
1049            SuiErrorKind::PotentiallyTemporarilyInvalidSignature { .. } => true,
1050
1051            // Overload errors
1052            SuiErrorKind::TooManyTransactionsPendingExecution { .. } => true,
1053            SuiErrorKind::TooManyTransactionsPendingOnObject { .. } => true,
1054            SuiErrorKind::TooOldTransactionPendingOnObject { .. } => true,
1055            SuiErrorKind::TooManyTransactionsPendingConsensus => true,
1056            SuiErrorKind::TransactionRejectedDueToOutbiddingDuringCongestion { .. } => true,
1057            SuiErrorKind::ValidatorOverloadedRetryAfter { .. } => true,
1058
1059            // The transaction is already being processed by consensus, so a fresh
1060            // submission is pointless. The client should retry by waiting for effects
1061            // rather than resubmitting.
1062            SuiErrorKind::TransactionProcessing { .. } => true,
1063            SuiErrorKind::TransactionSubmitted { .. } => true,
1064
1065            // Non retryable error
1066            SuiErrorKind::ExecutionError(..) => false,
1067            SuiErrorKind::ByzantineAuthoritySuspicion { .. } => false,
1068            SuiErrorKind::QuorumFailedToGetEffectsQuorumWhenProcessingTransaction { .. } => false,
1069            SuiErrorKind::TxAlreadyFinalizedWithDifferentUserSigs => false,
1070            SuiErrorKind::FailedToVerifyTxCertWithExecutedEffects { .. } => false,
1071            SuiErrorKind::ObjectLockConflict { .. } => false,
1072
1073            // NB: This is not an internal overload, but instead an imposed rate
1074            // limit / blocking of a client. It must be non-retryable otherwise
1075            // we will make the threat worse through automatic retries.
1076            SuiErrorKind::TooManyRequests => false,
1077
1078            // For all un-categorized errors, return here with categorized = false.
1079            _ => return (false, false),
1080        };
1081
1082        (retryable, true)
1083    }
1084
1085    pub fn is_object_or_package_not_found(&self) -> bool {
1086        match self {
1087            SuiErrorKind::UserInputError { error } => {
1088                matches!(
1089                    error,
1090                    UserInputError::ObjectNotFound { .. }
1091                        | UserInputError::DependentPackageNotFound { .. }
1092                )
1093            }
1094            _ => false,
1095        }
1096    }
1097
1098    pub fn is_overload(&self) -> bool {
1099        matches!(
1100            self,
1101            SuiErrorKind::TooManyTransactionsPendingExecution { .. }
1102                | SuiErrorKind::TooManyTransactionsPendingOnObject { .. }
1103                | SuiErrorKind::TooOldTransactionPendingOnObject { .. }
1104                | SuiErrorKind::TooManyTransactionsPendingConsensus
1105                | SuiErrorKind::TransactionRejectedDueToOutbiddingDuringCongestion { .. }
1106        )
1107    }
1108
1109    pub fn is_retryable_overload(&self) -> bool {
1110        matches!(self, SuiErrorKind::ValidatorOverloadedRetryAfter { .. })
1111    }
1112
1113    pub fn retry_after_secs(&self) -> u64 {
1114        match self {
1115            SuiErrorKind::ValidatorOverloadedRetryAfter { retry_after_secs } => *retry_after_secs,
1116            _ => 0,
1117        }
1118    }
1119
1120    /// Categorizes SuiError into ErrorCategory.
1121    pub fn categorize(&self) -> ErrorCategory {
1122        match self {
1123            SuiErrorKind::UserInputError { error } => {
1124                match error {
1125                    // ObjectNotFound and DependentPackageNotFound are potentially valid because the missing
1126                    // input can be created by other transactions.
1127                    UserInputError::ObjectNotFound { .. } => ErrorCategory::Aborted,
1128                    UserInputError::DependentPackageNotFound { .. } => ErrorCategory::Aborted,
1129                    // Other UserInputError variants indeed indicate invalid transaction.
1130                    _ => ErrorCategory::InvalidTransaction,
1131                }
1132            }
1133
1134            SuiErrorKind::InvalidSignature { .. }
1135            | SuiErrorKind::SignerSignatureAbsent { .. }
1136            | SuiErrorKind::SignerSignatureNumberMismatch { .. }
1137            | SuiErrorKind::IncorrectSigner { .. }
1138            | SuiErrorKind::UnknownSigner { .. }
1139            | SuiErrorKind::TransactionExpired => ErrorCategory::InvalidTransaction,
1140
1141            SuiErrorKind::ObjectLockConflict { .. } => ErrorCategory::LockConflict,
1142
1143            SuiErrorKind::Unknown { .. }
1144            | SuiErrorKind::GrpcMessageSerializeError { .. }
1145            | SuiErrorKind::GrpcMessageDeserializeError { .. }
1146            | SuiErrorKind::ByzantineAuthoritySuspicion { .. }
1147            | SuiErrorKind::InvalidTxKindInSoftBundle
1148            | SuiErrorKind::UnsupportedFeatureError { .. }
1149            | SuiErrorKind::InvalidRequest { .. } => ErrorCategory::Internal,
1150
1151            SuiErrorKind::TooManyTransactionsPendingExecution { .. }
1152            | SuiErrorKind::TooManyTransactionsPendingOnObject { .. }
1153            | SuiErrorKind::TooOldTransactionPendingOnObject { .. }
1154            | SuiErrorKind::TooManyTransactionsPendingConsensus
1155            | SuiErrorKind::TransactionRejectedDueToOutbiddingDuringCongestion { .. }
1156            | SuiErrorKind::ValidatorOverloadedRetryAfter { .. } => {
1157                ErrorCategory::ValidatorOverloaded
1158            }
1159
1160            SuiErrorKind::TimeoutError => ErrorCategory::Unavailable,
1161
1162            // Other variants are assumed to be retriable with new transaction submissions.
1163            _ => ErrorCategory::Aborted,
1164        }
1165    }
1166}
1167
1168impl Ord for SuiError {
1169    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1170        Ord::cmp(self.as_ref(), other.as_ref())
1171    }
1172}
1173
1174impl PartialOrd for SuiError {
1175    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1176        Some(self.cmp(other))
1177    }
1178}
1179
1180impl std::fmt::Debug for SuiError {
1181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1182        self.as_inner().fmt(f)
1183    }
1184}
1185
1186pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
1187pub type ExecutionErrorMetadata = BTreeMap<String, String>;
1188
1189/// A trait for execution errors that provides common methods for accessing error information and creating new errors.
1190pub trait ExecutionErrorTrait:
1191    From<ExecutionError> + Debug + std::error::Error + Send + Sync + Sized + 'static
1192{
1193    fn new(
1194        failure: ExecutionFailure,
1195        source: Option<BoxError>,
1196        metadata: ExecutionErrorMetadata,
1197    ) -> Self;
1198
1199    fn from_execution_failure(failure: ExecutionFailure) -> Self {
1200        Self::new(failure, None, ExecutionErrorMetadata::default())
1201    }
1202
1203    fn from_kind(kind: ExecutionErrorKind) -> Self {
1204        Self::from_execution_failure(ExecutionFailure::new(kind, None))
1205    }
1206
1207    fn new_with_source<E>(kind: ExecutionErrorKind, source: E) -> Self
1208    where
1209        E: Into<BoxError>,
1210    {
1211        Self::new(
1212            ExecutionFailure::new(kind, None),
1213            Some(source.into()),
1214            ExecutionErrorMetadata::default(),
1215        )
1216    }
1217
1218    fn with_command_index(self, command: CommandIndex) -> Self;
1219    fn kind(&self) -> &ExecutionErrorKind;
1220    fn command(&self) -> Option<CommandIndex>;
1221
1222    fn to_execution_failure(&self) -> ExecutionFailure {
1223        ExecutionFailure::new(self.kind().clone(), self.command())
1224    }
1225}
1226
1227#[derive(Debug)]
1228pub struct ExecutionError {
1229    inner: Box<ExecutionErrorInner>,
1230}
1231
1232#[derive(Debug)]
1233struct ExecutionErrorInner {
1234    kind: ExecutionErrorKind,
1235    source: Option<BoxError>,
1236    command: Option<CommandIndex>,
1237}
1238
1239impl ExecutionError {
1240    pub fn new(kind: ExecutionErrorKind, source: Option<BoxError>) -> Self {
1241        Self {
1242            inner: Box::new(ExecutionErrorInner {
1243                kind,
1244                source,
1245                command: None,
1246            }),
1247        }
1248    }
1249
1250    pub fn new_with_source<E: Into<BoxError>>(kind: ExecutionErrorKind, source: E) -> Self {
1251        Self::new(kind, Some(source.into()))
1252    }
1253
1254    pub fn invariant_violation<E: Into<BoxError>>(source: E) -> Self {
1255        Self::new_with_source(ExecutionErrorKind::InvariantViolation, source)
1256    }
1257
1258    pub fn with_command_index(mut self, command: CommandIndex) -> Self {
1259        self.inner.command = Some(command);
1260        self
1261    }
1262
1263    pub fn from_kind(kind: ExecutionErrorKind) -> Self {
1264        Self::new(kind, None)
1265    }
1266
1267    pub fn kind(&self) -> &ExecutionErrorKind {
1268        &self.inner.kind
1269    }
1270
1271    pub fn command(&self) -> Option<CommandIndex> {
1272        self.inner.command
1273    }
1274
1275    pub fn source(&self) -> &Option<BoxError> {
1276        &self.inner.source
1277    }
1278
1279    pub fn to_execution_status(&self) -> (ExecutionErrorKind, Option<CommandIndex>) {
1280        (self.kind().clone(), self.command())
1281    }
1282}
1283
1284impl ExecutionErrorTrait for ExecutionError {
1285    fn new(
1286        failure: ExecutionFailure,
1287        source: Option<BoxError>,
1288        _metadata: ExecutionErrorMetadata,
1289    ) -> Self {
1290        let ExecutionFailure { error, command } = failure;
1291        let err = ExecutionError::new(error, source);
1292        if let Some(command) = command {
1293            err.with_command_index(command)
1294        } else {
1295            err
1296        }
1297    }
1298
1299    fn with_command_index(self, command: CommandIndex) -> Self {
1300        self.with_command_index(command)
1301    }
1302
1303    fn kind(&self) -> &ExecutionErrorKind {
1304        self.kind()
1305    }
1306
1307    fn command(&self) -> Option<CommandIndex> {
1308        self.command()
1309    }
1310}
1311
1312#[derive(Debug)]
1313pub struct ExecutionErrorContext {
1314    kind: ExecutionErrorKind,
1315    metadata: ExecutionErrorMetadata,
1316    source: Option<BoxError>,
1317    command: Option<CommandIndex>,
1318}
1319
1320impl ExecutionErrorContext {
1321    pub fn kind(&self) -> &ExecutionErrorKind {
1322        &self.kind
1323    }
1324
1325    pub fn command(&self) -> Option<CommandIndex> {
1326        self.command
1327    }
1328
1329    pub fn metadata_with_source(&self) -> Option<ExecutionErrorMetadata> {
1330        let mut metadata = self.metadata.clone();
1331        if let Some(source) = self.source.as_ref() {
1332            metadata.insert("source".to_string(), source.to_string());
1333        }
1334
1335        (!metadata.is_empty()).then_some(metadata)
1336    }
1337
1338    pub fn to_execution_status(&self) -> (ExecutionErrorKind, Option<CommandIndex>) {
1339        (self.kind().clone(), self.command())
1340    }
1341}
1342
1343impl ExecutionErrorTrait for ExecutionErrorContext {
1344    fn new(
1345        failure: ExecutionFailure,
1346        source: Option<BoxError>,
1347        metadata: ExecutionErrorMetadata,
1348    ) -> Self {
1349        let ExecutionFailure { error, command } = failure;
1350        Self {
1351            kind: error,
1352            metadata,
1353            source,
1354            command,
1355        }
1356    }
1357
1358    fn with_command_index(self, command: CommandIndex) -> Self {
1359        Self {
1360            command: Some(command),
1361            ..self
1362        }
1363    }
1364
1365    fn kind(&self) -> &ExecutionErrorKind {
1366        self.kind()
1367    }
1368
1369    fn command(&self) -> Option<CommandIndex> {
1370        self.command()
1371    }
1372}
1373
1374impl std::fmt::Display for ExecutionErrorContext {
1375    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1376        write!(f, "ExecutionErrorContext: {:?}", self)
1377    }
1378}
1379
1380impl std::error::Error for ExecutionErrorContext {
1381    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1382        self.source.as_deref().map(|e| e as _)
1383    }
1384}
1385
1386impl From<ExecutionErrorKind> for ExecutionErrorContext {
1387    fn from(kind: ExecutionErrorKind) -> Self {
1388        <Self as ExecutionErrorTrait>::from_kind(kind)
1389    }
1390}
1391
1392impl From<ExecutionFailure> for ExecutionErrorContext {
1393    fn from(value: ExecutionFailure) -> Self {
1394        <Self as ExecutionErrorTrait>::from_execution_failure(value)
1395    }
1396}
1397
1398impl From<ExecutionError> for ExecutionErrorContext {
1399    fn from(value: ExecutionError) -> Self {
1400        let ExecutionError { inner } = value;
1401        let ExecutionErrorInner {
1402            kind,
1403            source,
1404            command,
1405        } = *inner;
1406        Self {
1407            kind,
1408            metadata: BTreeMap::new(),
1409            source,
1410            command,
1411        }
1412    }
1413}
1414
1415impl From<ExecutionErrorContext> for ExecutionError {
1416    fn from(value: ExecutionErrorContext) -> Self {
1417        let ExecutionErrorContext {
1418            kind,
1419            metadata: _,
1420            source,
1421            command,
1422        } = value;
1423        let err = ExecutionError::new(kind, source);
1424        if let Some(command) = command {
1425            err.with_command_index(command)
1426        } else {
1427            err
1428        }
1429    }
1430}
1431
1432impl std::fmt::Display for ExecutionError {
1433    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1434        write!(f, "ExecutionError: {:?}", self)
1435    }
1436}
1437
1438impl std::error::Error for ExecutionError {
1439    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1440        self.inner.source.as_ref().map(|e| &**e as _)
1441    }
1442}
1443
1444impl From<ExecutionErrorKind> for ExecutionError {
1445    fn from(kind: ExecutionErrorKind) -> Self {
1446        Self::from_kind(kind)
1447    }
1448}
1449
1450impl From<ExecutionFailure> for ExecutionError {
1451    fn from(value: ExecutionFailure) -> Self {
1452        <Self as ExecutionErrorTrait>::from_execution_failure(value)
1453    }
1454}
1455
1456pub fn command_argument_error(e: CommandArgumentError, arg_idx: usize) -> ExecutionError {
1457    ExecutionError::from_kind(ExecutionErrorKind::command_argument_error(
1458        e,
1459        arg_idx as u16,
1460    ))
1461}
1462
1463/// Types of SuiError.
1464#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, IntoStaticStr)]
1465pub enum ErrorCategory {
1466    // A generic error that is retriable with new transaction resubmissions.
1467    Aborted,
1468    // Any validator or full node can check if a transaction is valid.
1469    InvalidTransaction,
1470    // Lock conflict on the transaction input.
1471    LockConflict,
1472    // Unexpected client error, for example generating invalid request or entering into invalid state.
1473    // And unexpected error from the remote peer. The validator may be malicious or there is a software bug.
1474    Internal,
1475    // Validator is overloaded.
1476    ValidatorOverloaded,
1477    // Target validator is down or there are network issues.
1478    Unavailable,
1479}
1480
1481impl ErrorCategory {
1482    // Whether the failure is retriable with new transaction submission.
1483    pub fn is_submission_retriable(&self) -> bool {
1484        matches!(
1485            self,
1486            ErrorCategory::Aborted
1487                | ErrorCategory::ValidatorOverloaded
1488                | ErrorCategory::Unavailable
1489        )
1490    }
1491}