Skip to content

Commit

Permalink
Merge pull request casper-network#4540 from EdHastingsCasperAssociati…
Browse files Browse the repository at this point in the history
…on/feat-2.0-tc-transfer

initial native transfer wireup
  • Loading branch information
EdHastingsCasperAssociation authored Feb 16, 2024
2 parents a483a0e + 6e11a31 commit c52231d
Show file tree
Hide file tree
Showing 57 changed files with 2,381 additions and 2,114 deletions.
7 changes: 6 additions & 1 deletion execution_engine/src/engine_state/deploy_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl DeployItem {
deploy_hash,
}
}

/// Is this a native transfer?
pub fn is_native_transfer(&self) -> bool {
matches!(self.session, ExecutableDeployItem::Transfer { .. })
}
}

impl From<Deploy> for DeployItem {
Expand All @@ -62,7 +67,7 @@ impl From<Deploy> for DeployItem {
deploy.payment().clone(),
deploy.header().gas_price(),
authorization_keys,
casper_types::DeployHash::new(*deploy.hash().inner()),
DeployHash::new(*deploy.hash().inner()),
)
}
}
13 changes: 12 additions & 1 deletion execution_engine/src/engine_state/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use thiserror::Error;

use casper_storage::{
global_state::{self, state::CommitError},
system::{genesis::GenesisError, protocol_upgrade::ProtocolUpgradeError},
system::{
genesis::GenesisError, protocol_upgrade::ProtocolUpgradeError, transfer::TransferError,
},
tracking_copy::TrackingCopyError,
};
use casper_types::{
Expand Down Expand Up @@ -120,6 +122,9 @@ pub enum Error {
/// Storage error.
#[error("Tracking copy error: {0}")]
TrackingCopy(TrackingCopyError),
/// Native transfer error.
#[error("Transfer error: {0}")]
Transfer(TransferError),
}

impl Error {
Expand All @@ -133,6 +138,12 @@ impl Error {
}
}

impl From<TransferError> for Error {
fn from(err: TransferError) -> Self {
Error::Transfer(err)
}
}

impl From<execution::Error> for Error {
fn from(error: execution::Error) -> Self {
match error {
Expand Down
61 changes: 46 additions & 15 deletions execution_engine/src/engine_state/execution_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
use std::collections::VecDeque;

use casper_storage::data_access_layer::TransferResult;
use casper_types::{
bytesrepr::FromBytes,
contract_messages::Messages,
execution::{Effects, ExecutionResultV2 as TypesExecutionResult, Transform, TransformKind},
CLTyped, CLValue, Gas, Key, Motes, StoredValue, TransferAddr,
};

use super::error;
use super::Error;
use crate::execution::Error as ExecError;

/// Represents the result of an execution specified by
Expand All @@ -19,7 +20,7 @@ pub enum ExecutionResult {
/// An error condition that happened during execution
Failure {
/// Error causing this `Failure` variant.
error: error::Error,
error: Error,
/// List of transfers that happened during execution up to the point of the failure.
transfers: Vec<TransferAddr>,
/// Gas consumed up to the point of the failure.
Expand Down Expand Up @@ -59,7 +60,7 @@ impl ExecutionResult {
/// Constructs [ExecutionResult::Failure] that has 0 cost and no effects.
/// This is the case for failures that we can't (or don't want to) charge
/// for, like `PreprocessingError` or `InvalidNonce`.
pub fn precondition_failure(error: error::Error) -> ExecutionResult {
pub fn precondition_failure(error: Error) -> ExecutionResult {
ExecutionResult::Failure {
error,
transfers: Vec::default(),
Expand Down Expand Up @@ -224,18 +225,19 @@ impl ExecutionResult {

/// Returns error value, if possible.
///
/// Returns a reference to a wrapped [`error::Error`] instance if the object is a failure
/// variant.
pub fn as_error(&self) -> Option<&error::Error> {
/// Returns a reference to a wrapped [`super::Error`]
/// instance if the object is a failure variant.
pub fn as_error(&self) -> Option<&Error> {
match self {
ExecutionResult::Failure { error, .. } => Some(error),
ExecutionResult::Success { .. } => None,
}
}

/// Consumes [`ExecutionResult`] instance and optionally returns [`error::Error`] instance for
/// Consumes [`ExecutionResult`] instance and optionally returns
/// [`super::Error`] instance for
/// [`ExecutionResult::Failure`] variant.
pub fn take_error(self) -> Option<error::Error> {
pub fn take_error(self) -> Option<Error> {
match self {
ExecutionResult::Failure { error, .. } => Some(error),
ExecutionResult::Success { .. } => None,
Expand Down Expand Up @@ -288,16 +290,16 @@ impl ExecutionResult {
/// The effects that are produced as part of this process would subract `max_payment_cost` from
/// account's main purse, and add `max_payment_cost` to proposer account's balance.
pub fn new_payment_code_error(
error: error::Error,
error: Error,
max_payment_cost: Motes,
account_main_purse_balance: Motes,
gas_cost: Gas,
account_main_purse_balance_key: Key,
proposer_main_purse_balance_key: Key,
) -> Result<ExecutionResult, error::Error> {
) -> Result<ExecutionResult, Error> {
let new_balance = account_main_purse_balance
.checked_sub(max_payment_cost)
.ok_or(error::Error::InsufficientPayment)?;
.ok_or(Error::InsufficientPayment)?;
let new_balance_value =
StoredValue::CLValue(CLValue::from_t(new_balance.value()).map_err(ExecError::from)?);
let mut effects = Effects::new();
Expand Down Expand Up @@ -328,6 +330,37 @@ impl ExecutionResult {
pub(crate) fn take_without_ret<T: FromBytes + CLTyped>(self) -> (Option<T>, Self) {
(None, self)
}

/// A temporary measure to keep things functioning mid-refactor.
#[allow(clippy::result_unit_err)]
pub fn from_transfer_result(transfer_result: TransferResult, cost: Gas) -> Result<Self, ()> {
match transfer_result {
TransferResult::RootNotFound => {
Err(())
}
// native transfer is auto-commit...but execution results does not currently allow
// for a post state hash to be returned.
TransferResult::Success {
transfers, effects, .. // post_state_hash
} => {
Ok(ExecutionResult::Success {
transfers,
cost,
effects,
messages: Messages::default(),
})
}
TransferResult::Failure(te) => {
Ok(ExecutionResult::Failure {
error: Error::Transfer(te),
transfers: vec![],
cost,
effects: Effects::default(), // currently not returning effects on failure
messages: Messages::default(),
})
}
}
}
}

/// A versioned execution result and the messages produced by that execution.
Expand Down Expand Up @@ -459,7 +492,7 @@ impl ExecutionResultBuilder {
/// Builds a final [`ExecutionResult`] based on session result, payment result and a
/// finalization result.
pub fn build(self) -> Result<ExecutionResult, ExecutionResultBuilderError> {
let mut error: Option<error::Error> = None;
let mut error: Option<Error> = None;
let mut transfers = self.transfers();
let cost = self.total_cost();

Expand Down Expand Up @@ -497,9 +530,7 @@ impl ExecutionResultBuilder {
match self.finalize_execution_result {
Some(ExecutionResult::Failure { .. }) => {
// payment_code_spec_5_a: Finalization Error should only ever be raised here
return Ok(ExecutionResult::precondition_failure(
error::Error::Finalization,
));
return Ok(ExecutionResult::precondition_failure(Error::Finalization));
}
Some(ExecutionResult::Success {
effects, messages, ..
Expand Down
Loading

0 comments on commit c52231d

Please sign in to comment.