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},
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        "Object ID {} Version {} Digest {} is not available 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
371#[derive(
372    Eq,
373    PartialEq,
374    Clone,
375    Debug,
376    Serialize,
377    Deserialize,
378    Hash,
379    AsRefStr,
380    IntoStaticStr,
381    JsonSchema,
382    Error,
383)]
384#[serde(tag = "code", rename = "ObjectResponseError", rename_all = "camelCase")]
385pub enum SuiObjectResponseError {
386    #[error("Object {object_id} does not exist")]
387    NotExists { object_id: ObjectID },
388    #[error("Cannot find dynamic field for parent object {parent_object_id}")]
389    DynamicFieldNotFound { parent_object_id: ObjectID },
390    #[error(
391        "Object has been deleted object_id: {object_id} at version: {version:?} in digest {digest}"
392    )]
393    Deleted {
394        object_id: ObjectID,
395        /// Object version.
396        version: SequenceNumber,
397        /// Base64 string representing the object digest
398        digest: ObjectDigest,
399    },
400    #[error("Unknown Error")]
401    Unknown,
402    #[error("Display Error: {error}")]
403    DisplayError { error: String },
404    // TODO: also integrate SuiPastObjectResponse (VersionNotFound,  VersionTooHigh)
405}
406
407/// Custom error type for Sui.
408#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Error, Hash)]
409#[error(transparent)]
410pub struct SuiError(#[from] pub Box<SuiErrorKind>);
411
412/// Custom error type for Sui.
413#[derive(
414    Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Error, Hash, AsRefStr, IntoStaticStr,
415)]
416pub enum SuiErrorKind {
417    #[error("Error checking transaction input objects: {error}")]
418    UserInputError { error: UserInputError },
419
420    #[error("Error checking transaction object: {error}")]
421    SuiObjectResponseError { error: SuiObjectResponseError },
422
423    #[error("Expecting a single owner, shared ownership found")]
424    UnexpectedOwnerType,
425
426    #[error("There are already {queue_len} transactions pending, above threshold of {threshold}")]
427    TooManyTransactionsPendingExecution { queue_len: usize, threshold: usize },
428
429    #[error("There are too many transactions pending in consensus")]
430    TooManyTransactionsPendingConsensus,
431
432    #[error(
433        "Input {object_id} already has {queue_len} transactions pending, above threshold of {threshold}"
434    )]
435    TooManyTransactionsPendingOnObject {
436        object_id: ObjectID,
437        queue_len: usize,
438        threshold: usize,
439    },
440
441    #[error(
442        "Input {object_id} has a transaction {txn_age_sec} seconds old pending, above threshold of {threshold} seconds"
443    )]
444    TooOldTransactionPendingOnObject {
445        object_id: ObjectID,
446        txn_age_sec: u64,
447        threshold: u64,
448    },
449
450    #[error("Soft bundle must only contain transactions of UserTransaction kind")]
451    InvalidTxKindInSoftBundle,
452
453    // Signature verification
454    #[error("Signature is not valid: {}", error)]
455    InvalidSignature { error: String },
456    #[error("Required Signature from {expected} is absent {:?}", actual)]
457    SignerSignatureAbsent {
458        expected: String,
459        actual: Vec<String>,
460    },
461    #[error("Expect {expected} signer signatures but got {actual}")]
462    SignerSignatureNumberMismatch { expected: usize, actual: usize },
463    #[error("Value was not signed by the correct sender: {}", error)]
464    IncorrectSigner { error: String },
465    #[error(
466        "Value was not signed by a known authority. signer: {:?}, index: {:?}, committee: {committee}",
467        signer,
468        index
469    )]
470    UnknownSigner {
471        signer: Option<String>,
472        index: Option<u32>,
473        committee: Box<Committee>,
474    },
475    #[error(
476        "Validator {:?} responded multiple signatures for the same message, conflicting: {:?}",
477        signer,
478        conflicting_sig
479    )]
480    StakeAggregatorRepeatedSigner {
481        signer: AuthorityName,
482        conflicting_sig: bool,
483    },
484    // TODO: Used for distinguishing between different occurrences of invalid signatures, to allow retries in some cases.
485    #[error(
486        "Signature is not valid, but a retry may result in a valid one: {}",
487        error
488    )]
489    PotentiallyTemporarilyInvalidSignature { error: String },
490
491    // Certificate verification and execution
492    #[error(
493        "Signature or certificate from wrong epoch, expected {expected_epoch}, got {actual_epoch}"
494    )]
495    WrongEpoch {
496        expected_epoch: EpochId,
497        actual_epoch: EpochId,
498    },
499    #[error("Signatures in a certificate must form a quorum")]
500    CertificateRequiresQuorum,
501    #[allow(non_camel_case_types)]
502    #[error("DEPRECATED")]
503    DEPRECATED_ErrorWhileProcessingCertificate,
504    #[error(
505        "Failed to get a quorum of signed effects when processing transaction: {effects_map:?}"
506    )]
507    QuorumFailedToGetEffectsQuorumWhenProcessingTransaction {
508        effects_map: BTreeMap<TransactionEffectsDigest, (Vec<AuthorityName>, StakeUnit)>,
509    },
510    #[error(
511        "Failed to verify Tx certificate with executed effects, error: {error:?}, validator: {validator_name:?}"
512    )]
513    FailedToVerifyTxCertWithExecutedEffects {
514        validator_name: AuthorityName,
515        error: String,
516    },
517    #[error("Transaction is already finalized but with different user signatures")]
518    TxAlreadyFinalizedWithDifferentUserSigs,
519
520    // Account access
521    #[error("Invalid authenticator")]
522    InvalidAuthenticator,
523    #[error("Invalid address")]
524    InvalidAddress,
525    #[error("Invalid transaction digest.")]
526    InvalidTransactionDigest,
527
528    #[error("Invalid digest length. Expected {expected}, got {actual}")]
529    InvalidDigestLength { expected: usize, actual: usize },
530    #[error("Invalid DKG message size")]
531    InvalidDkgMessageSize,
532
533    #[error("Unexpected message: {0}")]
534    UnexpectedMessage(String),
535
536    // Move module publishing related errors
537    #[error("Failed to verify the Move module, reason: {error}.")]
538    ModuleVerificationFailure { error: String },
539    #[error("Failed to deserialize the Move module, reason: {error}.")]
540    ModuleDeserializationFailure { error: String },
541    #[error("Failed to publish the Move module(s), reason: {error}")]
542    ModulePublishFailure { error: String },
543    #[error("Failed to build Move modules: {error}.")]
544    ModuleBuildFailure { error: String },
545
546    // Move call related errors
547    #[error("Function resolution failure: {error}.")]
548    FunctionNotFound { error: String },
549    #[error("Module not found in package: {module_name}.")]
550    ModuleNotFound { module_name: String },
551    #[error("Type error while binding function arguments: {error}.")]
552    TypeError { error: String },
553    #[error("Circular object ownership detected")]
554    CircularObjectOwnership,
555
556    // Internal state errors
557    #[error("Attempt to re-initialize a transaction lock for objects {:?}.", refs)]
558    ObjectLockAlreadyInitialized { refs: Vec<ObjectRef> },
559    #[error(
560        "Object {obj_ref:?} already locked by a different transaction: {pending_transaction:?}"
561    )]
562    ObjectLockConflict {
563        obj_ref: ObjectRef,
564        pending_transaction: TransactionDigest,
565    },
566    #[error(
567        "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:?}"
568    )]
569    ObjectLockedAtFutureEpoch {
570        obj_refs: Vec<ObjectRef>,
571        locked_epoch: EpochId,
572        new_epoch: EpochId,
573        locked_by_tx: TransactionDigest,
574    },
575    #[error("{TRANSACTION_NOT_FOUND_MSG_PREFIX} [{:?}].", digest)]
576    TransactionNotFound { digest: TransactionDigest },
577    #[error("{TRANSACTIONS_NOT_FOUND_MSG_PREFIX} [{:?}].", digests)]
578    TransactionsNotFound { digests: Vec<TransactionDigest> },
579    #[error("Could not find the referenced transaction events [{digest:?}].")]
580    TransactionEventsNotFound { digest: TransactionDigest },
581    #[error("Could not find the referenced transaction effects [{digest:?}].")]
582    TransactionEffectsNotFound { digest: TransactionDigest },
583    #[error(
584        "Attempt to move to `Executed` state an transaction that has already been executed: {:?}.",
585        digest
586    )]
587    TransactionAlreadyExecuted { digest: TransactionDigest },
588    #[error("Transaction reject reason not found for transaction {digest:?}")]
589    TransactionRejectReasonNotFound { digest: TransactionDigest },
590    #[error("Object ID did not have the expected type")]
591    BadObjectType { error: String },
592    #[error("Fail to retrieve Object layout for {st}")]
593    FailObjectLayout { st: String },
594
595    #[error("Execution invariant violated")]
596    ExecutionInvariantViolation,
597    #[error("Validator {authority:?} is faulty in a Byzantine manner: {reason:?}")]
598    ByzantineAuthoritySuspicion {
599        authority: AuthorityName,
600        reason: String,
601    },
602    #[allow(non_camel_case_types)]
603    #[serde(rename = "StorageError")]
604    #[error("DEPRECATED")]
605    DEPRECATED_StorageError,
606    #[allow(non_camel_case_types)]
607    #[serde(rename = "GenericStorageError")]
608    #[error("DEPRECATED")]
609    DEPRECATED_GenericStorageError,
610    #[error(
611        "Attempted to access {object} through parent {given_parent}, \
612        but it's actual parent is {actual_owner}"
613    )]
614    InvalidChildObjectAccess {
615        object: ObjectID,
616        given_parent: ObjectID,
617        actual_owner: Owner,
618    },
619
620    #[allow(non_camel_case_types)]
621    #[serde(rename = "StorageMissingFieldError")]
622    #[error("DEPRECATED")]
623    DEPRECATED_StorageMissingFieldError,
624    #[allow(non_camel_case_types)]
625    #[serde(rename = "StorageCorruptedFieldError")]
626    #[error("DEPRECATED")]
627    DEPRECATED_StorageCorruptedFieldError,
628
629    #[error("Authority Error: {error}")]
630    GenericAuthorityError { error: String },
631
632    #[error("Generic Bridge Error: {error}")]
633    GenericBridgeError { error: String },
634
635    #[error("Failed to dispatch subscription: {error}")]
636    FailedToDispatchSubscription { error: String },
637
638    #[error("Failed to serialize Owner: {error}")]
639    OwnerFailedToSerialize { error: String },
640
641    #[error("Failed to deserialize fields into JSON: {error}")]
642    ExtraFieldFailedToDeserialize { error: String },
643
644    #[error("Failed to execute transaction locally by Orchestrator: {error}")]
645    TransactionOrchestratorLocalExecutionError { error: String },
646
647    // Errors returned by authority and client read API's
648    #[error("Failure serializing transaction in the requested format: {error}")]
649    TransactionSerializationError { error: String },
650    #[error("Failure deserializing transaction from the provided format: {error}")]
651    TransactionDeserializationError { error: String },
652    #[error("Failure serializing transaction effects from the provided format: {error}")]
653    TransactionEffectsSerializationError { error: String },
654    #[error("Failure deserializing transaction effects from the provided format: {error}")]
655    TransactionEffectsDeserializationError { error: String },
656    #[error("Failure serializing transaction events from the provided format: {error}")]
657    TransactionEventsSerializationError { error: String },
658    #[error("Failure deserializing transaction events from the provided format: {error}")]
659    TransactionEventsDeserializationError { error: String },
660    #[error("Failure serializing object in the requested format: {error}")]
661    ObjectSerializationError { error: String },
662    #[error("Failure deserializing object in the requested format: {error}")]
663    ObjectDeserializationError { error: String },
664    #[error("Event store component is not active on this node")]
665    NoEventStore,
666
667    // Client side error
668    #[error("Too many authority errors were detected for {}: {:?}", action, errors)]
669    TooManyIncorrectAuthorities {
670        errors: Vec<(AuthorityName, SuiError)>,
671        action: String,
672    },
673    #[error("Invalid transaction range query to the fullnode: {error}")]
674    FullNodeInvalidTxRangeQuery { error: String },
675
676    // Errors related to the authority-consensus interface.
677    #[error("Failed to submit transaction to consensus: {0}")]
678    FailedToSubmitToConsensus(String),
679    #[error("Failed to connect with consensus node: {0}")]
680    ConsensusConnectionBroken(String),
681    #[error("Failed to execute handle_consensus_transaction on Sui: {0}")]
682    HandleConsensusTransactionFailure(String),
683
684    // Cryptography errors.
685    #[error("Signature key generation error: {0}")]
686    SignatureKeyGenError(String),
687    #[error("Key Conversion Error: {0}")]
688    KeyConversionError(String),
689    #[error("Invalid Private Key provided")]
690    InvalidPrivateKey,
691
692    // Unsupported Operations on Fullnode
693    #[error("Fullnode does not support handle_certificate")]
694    FullNodeCantHandleCertificate,
695
696    // Epoch related errors.
697    #[error("Validator temporarily stopped processing transactions due to epoch change")]
698    ValidatorHaltedAtEpochEnd,
699    #[error("Operations for epoch {0} have ended")]
700    EpochEnded(EpochId),
701    #[error("Error when advancing epoch: {error}")]
702    AdvanceEpochError { error: String },
703
704    #[error("Transaction Expired")]
705    TransactionExpired,
706
707    // These are errors that occur when an RPC fails and is simply the utf8 message sent in a
708    // Tonic::Status
709    #[error("{1} - {0}")]
710    RpcError(String, String),
711
712    #[error("Method not allowed")]
713    InvalidRpcMethodError,
714
715    #[error("Use of disabled feature: {error}")]
716    UnsupportedFeatureError { error: String },
717
718    #[error("Unable to communicate with the Quorum Driver channel: {error}")]
719    QuorumDriverCommunicationError { error: String },
720
721    #[error("Operation timed out")]
722    TimeoutError,
723
724    #[error("Error executing {0}")]
725    ExecutionError(String),
726
727    #[error("Invalid committee composition")]
728    InvalidCommittee(String),
729
730    #[error("Missing committee information for epoch {0}")]
731    MissingCommitteeAtEpoch(EpochId),
732
733    #[error("Index store not available on this Fullnode.")]
734    IndexStoreNotAvailable,
735
736    #[error("Failed to read dynamic field from table in the object store: {0}")]
737    DynamicFieldReadError(String),
738
739    #[error("Failed to read or deserialize system state related data structures on-chain: {0}")]
740    SuiSystemStateReadError(String),
741
742    #[error("Failed to read or deserialize bridge related data structures on-chain: {0}")]
743    SuiBridgeReadError(String),
744
745    #[error("Unexpected version error: {0}")]
746    UnexpectedVersion(String),
747
748    #[error("Message version is not supported at the current protocol version: {error}")]
749    WrongMessageVersion { error: String },
750
751    #[error("unknown error: {0}")]
752    Unknown(String),
753
754    #[error("Failed to perform file operation: {0}")]
755    FileIOError(String),
756
757    #[error("Failed to get JWK")]
758    JWKRetrievalError,
759
760    #[error("Storage error: {0}")]
761    Storage(String),
762
763    #[error(
764        "Validator cannot handle the request at the moment. Please retry after at least {retry_after_secs} seconds."
765    )]
766    ValidatorOverloadedRetryAfter { retry_after_secs: u64 },
767
768    #[error("Too many requests")]
769    TooManyRequests,
770
771    #[error("The request did not contain a certificate")]
772    NoCertificateProvidedError,
773
774    #[error("Nitro attestation verify failed: {0}")]
775    NitroAttestationFailedToVerify(String),
776
777    #[error("Failed to serialize {type_info}, error: {error}")]
778    GrpcMessageSerializeError { type_info: String, error: String },
779
780    #[error("Failed to deserialize {type_info}, error: {error}")]
781    GrpcMessageDeserializeError { type_info: String, error: String },
782
783    #[error(
784        "Validator consensus rounds are lagging behind. last committed leader round: {last_committed_round}, requested round: {round}"
785    )]
786    ValidatorConsensusLagging {
787        round: u32,
788        last_committed_round: u32,
789    },
790
791    #[error("Invalid admin request: {0}")]
792    InvalidAdminRequest(String),
793
794    #[error("Invalid request: {0}")]
795    InvalidRequest(String),
796
797    #[error(
798        "The current set of aliases for a required signer changed after the transaction was submitted"
799    )]
800    AliasesChanged,
801
802    // Retriable by client because another validator can create the correct claim.
803    #[error("Object {object_id} not found among input objects.")]
804    ImmutableObjectClaimNotFoundInInput { object_id: ObjectID },
805
806    // Retriable by client because another validator can create the correct claim.
807    #[error("Immutable object {object_id} was not included in immutable claims.")]
808    ImmutableObjectNotClaimed { object_id: ObjectID },
809
810    // Retriable by client because the object can be frozen in the future.
811    #[error(
812        "Claimed object {claimed_object_id} is not immutable. Found object ref: {found_object_ref:?}"
813    )]
814    InvalidImmutableObjectClaim {
815        claimed_object_id: ObjectID,
816        found_object_ref: ObjectRef,
817    },
818}
819
820#[repr(u64)]
821#[allow(non_camel_case_types)]
822#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
823/// Sub-status codes for the `UNKNOWN_VERIFICATION_ERROR` VM Status Code which provides more context
824/// TODO: add more Vm Status errors. We use `UNKNOWN_VERIFICATION_ERROR` as a catchall for now.
825pub enum VMMVerifierErrorSubStatusCode {
826    MULTIPLE_RETURN_VALUES_NOT_ALLOWED = 0,
827    INVALID_OBJECT_CREATION = 1,
828}
829
830#[repr(u64)]
831#[allow(non_camel_case_types)]
832#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
833/// Sub-status codes for the `MEMORY_LIMIT_EXCEEDED` VM Status Code which provides more context
834pub enum VMMemoryLimitExceededSubStatusCode {
835    EVENT_COUNT_LIMIT_EXCEEDED = 0,
836    EVENT_SIZE_LIMIT_EXCEEDED = 1,
837    NEW_ID_COUNT_LIMIT_EXCEEDED = 2,
838    DELETED_ID_COUNT_LIMIT_EXCEEDED = 3,
839    TRANSFER_ID_COUNT_LIMIT_EXCEEDED = 4,
840    OBJECT_RUNTIME_CACHE_LIMIT_EXCEEDED = 5,
841    OBJECT_RUNTIME_STORE_LIMIT_EXCEEDED = 6,
842    TOTAL_EVENT_SIZE_LIMIT_EXCEEDED = 7,
843}
844
845pub type SuiResult<T = ()> = Result<T, SuiError>;
846pub type UserInputResult<T = ()> = Result<T, UserInputError>;
847
848impl From<SuiErrorKind> for SuiError {
849    fn from(error: SuiErrorKind) -> Self {
850        SuiError(Box::new(error))
851    }
852}
853
854impl std::ops::Deref for SuiError {
855    type Target = SuiErrorKind;
856
857    fn deref(&self) -> &Self::Target {
858        &self.0
859    }
860}
861
862impl From<sui_protocol_config::Error> for SuiError {
863    fn from(error: sui_protocol_config::Error) -> Self {
864        SuiErrorKind::WrongMessageVersion { error: error.0 }.into()
865    }
866}
867
868impl From<ExecutionError> for SuiError {
869    fn from(error: ExecutionError) -> Self {
870        SuiErrorKind::ExecutionError(error.to_string()).into()
871    }
872}
873
874impl From<Status> for SuiError {
875    fn from(status: Status) -> Self {
876        if status.message() == "Too many requests" {
877            return SuiErrorKind::TooManyRequests.into();
878        }
879
880        let result = bcs::from_bytes::<SuiError>(status.details());
881        if let Ok(sui_error) = result {
882            sui_error
883        } else {
884            SuiErrorKind::RpcError(
885                status.message().to_owned(),
886                status.code().description().to_owned(),
887            )
888            .into()
889        }
890    }
891}
892
893impl From<TypedStoreError> for SuiError {
894    fn from(e: TypedStoreError) -> Self {
895        SuiErrorKind::Storage(e.to_string()).into()
896    }
897}
898
899impl From<crate::storage::error::Error> for SuiError {
900    fn from(e: crate::storage::error::Error) -> Self {
901        SuiErrorKind::Storage(e.to_string()).into()
902    }
903}
904
905impl From<SuiErrorKind> for Status {
906    fn from(error: SuiErrorKind) -> Self {
907        let bytes = bcs::to_bytes(&error).unwrap();
908        Status::with_details(tonic::Code::Internal, error.to_string(), bytes.into())
909    }
910}
911
912impl From<SuiError> for Status {
913    fn from(error: SuiError) -> Self {
914        Status::from(error.into_inner())
915    }
916}
917
918impl From<ExecutionErrorKind> for SuiError {
919    fn from(kind: ExecutionErrorKind) -> Self {
920        ExecutionError::from_kind(kind).into()
921    }
922}
923
924impl From<&str> for SuiError {
925    fn from(error: &str) -> Self {
926        SuiErrorKind::GenericAuthorityError {
927            error: error.to_string(),
928        }
929        .into()
930    }
931}
932
933impl From<String> for SuiError {
934    fn from(error: String) -> Self {
935        SuiErrorKind::GenericAuthorityError { error }.into()
936    }
937}
938
939impl TryFrom<SuiErrorKind> for UserInputError {
940    type Error = anyhow::Error;
941
942    fn try_from(err: SuiErrorKind) -> Result<Self, Self::Error> {
943        match err {
944            SuiErrorKind::UserInputError { error } => Ok(error),
945            other => anyhow::bail!("error {:?} is not UserInputError", other),
946        }
947    }
948}
949
950impl TryFrom<SuiError> for UserInputError {
951    type Error = anyhow::Error;
952
953    fn try_from(err: SuiError) -> Result<Self, Self::Error> {
954        err.into_inner().try_into()
955    }
956}
957
958impl From<UserInputError> for SuiError {
959    fn from(error: UserInputError) -> Self {
960        SuiErrorKind::UserInputError { error }.into()
961    }
962}
963
964impl From<SuiObjectResponseError> for SuiError {
965    fn from(error: SuiObjectResponseError) -> Self {
966        SuiErrorKind::SuiObjectResponseError { error }.into()
967    }
968}
969
970impl PartialEq<SuiErrorKind> for SuiError {
971    fn eq(&self, other: &SuiErrorKind) -> bool {
972        &*self.0 == other
973    }
974}
975
976impl PartialEq<SuiError> for SuiErrorKind {
977    fn eq(&self, other: &SuiError) -> bool {
978        self == &*other.0
979    }
980}
981
982impl SuiError {
983    pub fn as_inner(&self) -> &SuiErrorKind {
984        &self.0
985    }
986
987    pub fn into_inner(self) -> SuiErrorKind {
988        *self.0
989    }
990}
991
992impl SuiErrorKind {
993    /// Returns the variant name of the error. Sub-variants within UserInputError are unpacked too.
994    pub fn to_variant_name(&self) -> &'static str {
995        match &self {
996            SuiErrorKind::UserInputError { error } => error.into(),
997            _ => self.into(),
998        }
999    }
1000
1001    pub fn individual_error_indicates_epoch_change(&self) -> bool {
1002        matches!(
1003            self,
1004            SuiErrorKind::ValidatorHaltedAtEpochEnd | SuiErrorKind::MissingCommitteeAtEpoch(_)
1005        )
1006    }
1007
1008    /// Returns if the error is retryable and if the error's retryability is
1009    /// explicitly categorized.
1010    /// There should be only a handful of retryable errors. For now we list common
1011    /// non-retryable error below to help us find more retryable errors in logs.
1012    pub fn is_retryable(&self) -> (bool, bool) {
1013        let retryable = match self {
1014            // Network error
1015            SuiErrorKind::RpcError { .. } => true,
1016
1017            // Reconfig error
1018            SuiErrorKind::ValidatorHaltedAtEpochEnd => true,
1019            SuiErrorKind::MissingCommitteeAtEpoch(..) => true,
1020            SuiErrorKind::WrongEpoch { .. } => true,
1021            SuiErrorKind::EpochEnded(..) => true,
1022
1023            SuiErrorKind::UserInputError { error } => {
1024                match error {
1025                    // Only ObjectNotFound and DependentPackageNotFound is potentially retryable
1026                    UserInputError::ObjectNotFound { .. } => true,
1027                    UserInputError::DependentPackageNotFound { .. } => true,
1028                    _ => false,
1029                }
1030            }
1031
1032            SuiErrorKind::PotentiallyTemporarilyInvalidSignature { .. } => true,
1033
1034            // Overload errors
1035            SuiErrorKind::TooManyTransactionsPendingExecution { .. } => true,
1036            SuiErrorKind::TooManyTransactionsPendingOnObject { .. } => true,
1037            SuiErrorKind::TooOldTransactionPendingOnObject { .. } => true,
1038            SuiErrorKind::TooManyTransactionsPendingConsensus => true,
1039            SuiErrorKind::ValidatorOverloadedRetryAfter { .. } => true,
1040
1041            // Non retryable error
1042            SuiErrorKind::ExecutionError(..) => false,
1043            SuiErrorKind::ByzantineAuthoritySuspicion { .. } => false,
1044            SuiErrorKind::QuorumFailedToGetEffectsQuorumWhenProcessingTransaction { .. } => false,
1045            SuiErrorKind::TxAlreadyFinalizedWithDifferentUserSigs => false,
1046            SuiErrorKind::FailedToVerifyTxCertWithExecutedEffects { .. } => false,
1047            SuiErrorKind::ObjectLockConflict { .. } => false,
1048
1049            // NB: This is not an internal overload, but instead an imposed rate
1050            // limit / blocking of a client. It must be non-retryable otherwise
1051            // we will make the threat worse through automatic retries.
1052            SuiErrorKind::TooManyRequests => false,
1053
1054            // For all un-categorized errors, return here with categorized = false.
1055            _ => return (false, false),
1056        };
1057
1058        (retryable, true)
1059    }
1060
1061    pub fn is_object_or_package_not_found(&self) -> bool {
1062        match self {
1063            SuiErrorKind::UserInputError { error } => {
1064                matches!(
1065                    error,
1066                    UserInputError::ObjectNotFound { .. }
1067                        | UserInputError::DependentPackageNotFound { .. }
1068                )
1069            }
1070            _ => false,
1071        }
1072    }
1073
1074    pub fn is_overload(&self) -> bool {
1075        matches!(
1076            self,
1077            SuiErrorKind::TooManyTransactionsPendingExecution { .. }
1078                | SuiErrorKind::TooManyTransactionsPendingOnObject { .. }
1079                | SuiErrorKind::TooOldTransactionPendingOnObject { .. }
1080                | SuiErrorKind::TooManyTransactionsPendingConsensus
1081        )
1082    }
1083
1084    pub fn is_retryable_overload(&self) -> bool {
1085        matches!(self, SuiErrorKind::ValidatorOverloadedRetryAfter { .. })
1086    }
1087
1088    pub fn retry_after_secs(&self) -> u64 {
1089        match self {
1090            SuiErrorKind::ValidatorOverloadedRetryAfter { retry_after_secs } => *retry_after_secs,
1091            _ => 0,
1092        }
1093    }
1094
1095    /// Categorizes SuiError into ErrorCategory.
1096    pub fn categorize(&self) -> ErrorCategory {
1097        match self {
1098            SuiErrorKind::UserInputError { error } => {
1099                match error {
1100                    // ObjectNotFound and DependentPackageNotFound are potentially valid because the missing
1101                    // input can be created by other transactions.
1102                    UserInputError::ObjectNotFound { .. } => ErrorCategory::Aborted,
1103                    UserInputError::DependentPackageNotFound { .. } => ErrorCategory::Aborted,
1104                    // Other UserInputError variants indeed indicate invalid transaction.
1105                    _ => ErrorCategory::InvalidTransaction,
1106                }
1107            }
1108
1109            SuiErrorKind::InvalidSignature { .. }
1110            | SuiErrorKind::SignerSignatureAbsent { .. }
1111            | SuiErrorKind::SignerSignatureNumberMismatch { .. }
1112            | SuiErrorKind::IncorrectSigner { .. }
1113            | SuiErrorKind::UnknownSigner { .. }
1114            | SuiErrorKind::TransactionExpired => ErrorCategory::InvalidTransaction,
1115
1116            SuiErrorKind::ObjectLockConflict { .. } => ErrorCategory::LockConflict,
1117
1118            SuiErrorKind::Unknown { .. }
1119            | SuiErrorKind::GrpcMessageSerializeError { .. }
1120            | SuiErrorKind::GrpcMessageDeserializeError { .. }
1121            | SuiErrorKind::ByzantineAuthoritySuspicion { .. }
1122            | SuiErrorKind::InvalidTxKindInSoftBundle
1123            | SuiErrorKind::UnsupportedFeatureError { .. }
1124            | SuiErrorKind::InvalidRequest { .. } => ErrorCategory::Internal,
1125
1126            SuiErrorKind::TooManyTransactionsPendingExecution { .. }
1127            | SuiErrorKind::TooManyTransactionsPendingOnObject { .. }
1128            | SuiErrorKind::TooOldTransactionPendingOnObject { .. }
1129            | SuiErrorKind::TooManyTransactionsPendingConsensus
1130            | SuiErrorKind::ValidatorOverloadedRetryAfter { .. } => {
1131                ErrorCategory::ValidatorOverloaded
1132            }
1133
1134            SuiErrorKind::TimeoutError => ErrorCategory::Unavailable,
1135
1136            // Other variants are assumed to be retriable with new transaction submissions.
1137            _ => ErrorCategory::Aborted,
1138        }
1139    }
1140}
1141
1142impl Ord for SuiError {
1143    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1144        Ord::cmp(self.as_ref(), other.as_ref())
1145    }
1146}
1147
1148impl PartialOrd for SuiError {
1149    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1150        Some(self.cmp(other))
1151    }
1152}
1153
1154impl std::fmt::Debug for SuiError {
1155    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1156        self.as_inner().fmt(f)
1157    }
1158}
1159
1160type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
1161
1162#[derive(Debug)]
1163pub struct ExecutionError {
1164    inner: Box<ExecutionErrorInner>,
1165}
1166
1167#[derive(Debug)]
1168struct ExecutionErrorInner {
1169    kind: ExecutionErrorKind,
1170    source: Option<BoxError>,
1171    command: Option<CommandIndex>,
1172}
1173
1174impl ExecutionError {
1175    pub fn new(kind: ExecutionErrorKind, source: Option<BoxError>) -> Self {
1176        Self {
1177            inner: Box::new(ExecutionErrorInner {
1178                kind,
1179                source,
1180                command: None,
1181            }),
1182        }
1183    }
1184
1185    pub fn new_with_source<E: Into<BoxError>>(kind: ExecutionErrorKind, source: E) -> Self {
1186        Self::new(kind, Some(source.into()))
1187    }
1188
1189    pub fn invariant_violation<E: Into<BoxError>>(source: E) -> Self {
1190        Self::new_with_source(ExecutionErrorKind::InvariantViolation, source)
1191    }
1192
1193    pub fn with_command_index(mut self, command: CommandIndex) -> Self {
1194        self.inner.command = Some(command);
1195        self
1196    }
1197
1198    pub fn from_kind(kind: ExecutionErrorKind) -> Self {
1199        Self::new(kind, None)
1200    }
1201
1202    pub fn kind(&self) -> &ExecutionErrorKind {
1203        &self.inner.kind
1204    }
1205
1206    pub fn command(&self) -> Option<CommandIndex> {
1207        self.inner.command
1208    }
1209
1210    pub fn source(&self) -> &Option<BoxError> {
1211        &self.inner.source
1212    }
1213
1214    pub fn to_execution_status(&self) -> (ExecutionErrorKind, Option<CommandIndex>) {
1215        (self.kind().clone(), self.command())
1216    }
1217}
1218
1219impl std::fmt::Display for ExecutionError {
1220    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1221        write!(f, "ExecutionError: {:?}", self)
1222    }
1223}
1224
1225impl std::error::Error for ExecutionError {
1226    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1227        self.inner.source.as_ref().map(|e| &**e as _)
1228    }
1229}
1230
1231impl From<ExecutionErrorKind> for ExecutionError {
1232    fn from(kind: ExecutionErrorKind) -> Self {
1233        Self::from_kind(kind)
1234    }
1235}
1236
1237pub fn command_argument_error(e: CommandArgumentError, arg_idx: usize) -> ExecutionError {
1238    ExecutionError::from_kind(ExecutionErrorKind::command_argument_error(
1239        e,
1240        arg_idx as u16,
1241    ))
1242}
1243
1244/// Types of SuiError.
1245#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, IntoStaticStr)]
1246pub enum ErrorCategory {
1247    // A generic error that is retriable with new transaction resubmissions.
1248    Aborted,
1249    // Any validator or full node can check if a transaction is valid.
1250    InvalidTransaction,
1251    // Lock conflict on the transaction input.
1252    LockConflict,
1253    // Unexpected client error, for example generating invalid request or entering into invalid state.
1254    // And unexpected error from the remote peer. The validator may be malicious or there is a software bug.
1255    Internal,
1256    // Validator is overloaded.
1257    ValidatorOverloaded,
1258    // Target validator is down or there are network issues.
1259    Unavailable,
1260}
1261
1262impl ErrorCategory {
1263    // Whether the failure is retriable with new transaction submission.
1264    pub fn is_submission_retriable(&self) -> bool {
1265        matches!(
1266            self,
1267            ErrorCategory::Aborted
1268                | ErrorCategory::ValidatorOverloaded
1269                | ErrorCategory::Unavailable
1270        )
1271    }
1272}