casper-node 2.0.3

The Casper blockchain node
Documentation
//! Errors that the contract runtime component may raise.
use derive_more::From;
use std::collections::BTreeMap;

use serde::Serialize;
use thiserror::Error;

use casper_execution_engine::engine_state::Error as EngineStateError;
use casper_storage::{
    data_access_layer::{
        forced_undelegate::ForcedUndelegateError, BlockRewardsError, FeeError, StepError,
    },
    global_state::error::Error as GlobalStateError,
    tracking_copy::TrackingCopyError,
};
use casper_types::{bytesrepr, CLValueError, Digest, EraId, PublicKey, U512};

use crate::{
    components::contract_runtime::ExecutionPreState,
    types::{ChunkingError, ExecutableBlock, InternalEraReport},
};

/// Common state result errors.
#[derive(Debug, Error)]
pub(crate) enum StateResultError {
    /// Invalid state root hash.
    #[error("invalid state root hash")]
    RootNotFound,
    /// Value not found.
    #[error("{0}")]
    ValueNotFound(String),
    /// Failure result.
    #[error("{0}")]
    Failure(TrackingCopyError),
}

/// An error returned from mis-configuring the contract runtime component.
#[derive(Debug, Error)]
pub(crate) enum ConfigError {
    /// Error initializing the LMDB environment.
    #[error("failed to initialize LMDB environment for contract runtime: {0}")]
    GlobalState(#[from] GlobalStateError),
    /// Error initializing metrics.
    #[error("failed to initialize metrics for contract runtime: {0}")]
    Prometheus(#[from] prometheus::Error),
}

/// An enum that represents all possible error conditions of a `contract_runtime` component.
#[derive(Debug, Error, From)]
pub(crate) enum ContractRuntimeError {
    /// The provided serialized id cannot be deserialized properly.
    #[error("error deserializing id: {0}")]
    InvalidSerializedId(#[source] bincode::Error),
    // It was not possible to get trie with the specified id
    #[error("error retrieving trie by id: {0}")]
    FailedToRetrieveTrieById(#[source] GlobalStateError),
    /// Chunking error.
    #[error("failed to chunk the data {0}")]
    ChunkingError(#[source] ChunkingError),
}

/// An error during block execution.
#[derive(Debug, Error, Serialize)]
pub enum BlockExecutionError {
    /// Currently the contract runtime can only execute one commit at a time, so we cannot handle
    /// more than one execution result.
    #[error("more than one execution result")]
    MoreThanOneExecutionResult,
    /// Both the block to be executed and the execution pre-state specify the height of the next
    /// block. These must agree and this error will be thrown if they do not.
    #[error(
        "block's height does not agree with execution pre-state. \
         block: {executable_block:?}, \
         execution pre-state: {execution_pre_state:?}"
    )]
    WrongBlockHeight {
        /// The finalized block the system attempted to execute.
        executable_block: Box<ExecutableBlock>,
        /// The state of the block chain prior to block execution that was to be used.
        execution_pre_state: Box<ExecutionPreState>,
    },
    /// A core error thrown by the execution engine.
    #[error(transparent)]
    EngineState(
        #[from]
        #[serde(skip_serializing)]
        EngineStateError,
    ),
    /// An error that occurred when trying to run the auction contract.
    #[error(transparent)]
    Step(
        #[from]
        #[serde(skip_serializing)]
        StepError,
    ),
    #[error(transparent)]
    DistributeFees(
        #[from]
        #[serde(skip_serializing)]
        FeeError,
    ),
    #[error(transparent)]
    DistributeBlockRewards(
        #[from]
        #[serde(skip_serializing)]
        BlockRewardsError,
    ),
    #[error(transparent)]
    ForcedUndelegate(
        #[from]
        #[serde(skip_serializing)]
        ForcedUndelegateError,
    ),
    /// Failed to compute the approvals checksum.
    #[error("failed to compute approvals checksum: {0}")]
    FailedToComputeApprovalsChecksum(bytesrepr::Error),
    /// Failed to compute the execution results checksum.
    #[error("failed to compute execution results checksum: {0}")]
    FailedToComputeExecutionResultsChecksum(bytesrepr::Error),
    /// Failed to convert the checksum registry to a `CLValue`.
    #[error("failed to convert the checksum registry to a clvalue: {0}")]
    ChecksumRegistryToCLValue(CLValueError),
    /// `EraEnd`s need both an `EraReport` present and a map of the next era validator weights.
    /// If one of them is not present while trying to construct an `EraEnd`, this error is
    /// produced.
    #[error(
        "cannot create era end unless we have both an era report and next era validators. \
         era report: {maybe_era_report:?}, \
         next era validator weights: {maybe_next_era_validator_weights:?}"
    )]
    FailedToCreateEraEnd {
        /// An optional `EraReport` we tried to use to construct an `EraEnd`.
        maybe_era_report: Option<InternalEraReport>,
        /// An optional map of the next era validator weights used to construct an `EraEnd`.
        maybe_next_era_validator_weights: Option<(BTreeMap<PublicKey, U512>, u8)>,
    },
    /// An error that occurred while interacting with lmdb.
    #[error(transparent)]
    Lmdb(
        #[from]
        #[serde(skip_serializing)]
        GlobalStateError,
    ),
    /// An error that occurred while getting era validators.
    #[error(transparent)]
    GetEraValidators(
        #[from]
        #[serde(skip_serializing)]
        TrackingCopyError,
    ),
    /// A root state hash was not found.
    #[error("Root state hash not found in global state.")]
    RootNotFound(Digest),
    /// Missing checksum registry.
    #[error("Missing checksum registry")]
    MissingChecksumRegistry,
    #[error("Failed to get new era gas price when executing switch block")]
    FailedToGetNewEraGasPrice { era_id: EraId },
    // Payment error.
    #[error("Error while trying to set up payment for transaction: {0}")]
    PaymentError(String),
    // Error attempting to set block global data.
    #[error("Error while attempting to store block global data: {0}")]
    BlockGlobal(String),
    #[error("No switch block header available for era: {0}")]
    /// No switch block available
    NoSwitchBlockHash(u64),
    #[error("Unsupported execution kind: {0}")]
    /// Unsupported execution kind
    UnsupportedTransactionKind(u8),
    #[error("Error while converting transaction to internal representation: {0}")]
    TransactionConversion(String),
    /// Invalid gas limit amount.
    #[error("Invalid gas limit amount: {0}")]
    InvalidGasLimit(U512),
    /// Invalid transaction variant.
    #[error("Invalid transaction variant")]
    InvalidTransactionVariant,
    /// Invalid transaction arguments.
    #[error("Invalid transaction arguments")]
    InvalidTransactionArgs,
    #[error("Data Access Layer conflicts with chainspec setting: {0}")]
    InvalidAESetting(bool),
}