Skip to content

Commit

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

Remove state from execution engine
  • Loading branch information
EdHastingsCasperAssociation authored Mar 6, 2024
2 parents 005d2cb + 0156463 commit 6223147
Show file tree
Hide file tree
Showing 96 changed files with 2,351 additions and 2,548 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ lint-smart-contracts:

.PHONY: audit-rs
audit-rs:
$(CARGO) audit --ignore RUSTSEC-2024-0006 --ignore RUSTSEC-2024-0003
$(CARGO) audit --ignore RUSTSEC-2024-0006 --ignore RUSTSEC-2024-0003 --ignore RUSTSEC-2024-0019

.PHONY: audit-as
audit-as:
Expand Down
63 changes: 13 additions & 50 deletions execution_engine/src/engine_state/engine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const DAY_MILLIS: usize = 24 * 60 * 60 * 1000;
/// Default length of total vesting schedule period expressed in days.
pub const DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS: u64 =
VESTING_SCHEDULE_LENGTH_DAYS as u64 * DAY_MILLIS as u64;
/// Default maximum number of delegators per validator.
pub const DEFAULT_MAX_DELEGATORS_PER_VALIDATOR: u32 = 1200;
/// Default value for allowing auction bids.
pub const DEFAULT_ALLOW_AUCTION_BIDS: bool = true;
/// Default value for allowing unrestricted transfers.
Expand All @@ -47,8 +49,6 @@ pub const DEFAULT_COMPUTE_REWARDS: bool = true;
/// The runtime configuration of the execution engine
#[derive(Debug, Clone)]
pub struct EngineConfig {
/// Max query depth of the engine.
pub(crate) max_query_depth: u64,
/// Maximum number of associated keys (i.e. map of
/// [`AccountHash`](casper_types::account::AccountHash)s to
/// [`Weight`](casper_types::account::Weight)s) for a single account.
Expand All @@ -59,7 +59,7 @@ pub struct EngineConfig {
strict_argument_checking: bool,
/// Vesting schedule period in milliseconds.
vesting_schedule_period_millis: u64,
max_delegators_per_validator: Option<u32>,
max_delegators_per_validator: u32,
wasm_config: WasmConfig,
system_config: SystemConfig,
/// A private network specifies a list of administrative accounts.
Expand All @@ -84,13 +84,12 @@ pub struct EngineConfig {
impl Default for EngineConfig {
fn default() -> Self {
EngineConfig {
max_query_depth: DEFAULT_MAX_QUERY_DEPTH,
max_associated_keys: DEFAULT_MAX_ASSOCIATED_KEYS,
max_runtime_call_stack_height: DEFAULT_MAX_RUNTIME_CALL_STACK_HEIGHT,
minimum_delegation_amount: DEFAULT_MINIMUM_DELEGATION_AMOUNT,
strict_argument_checking: DEFAULT_STRICT_ARGUMENT_CHECKING,
vesting_schedule_period_millis: DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS,
max_delegators_per_validator: None,
max_delegators_per_validator: DEFAULT_MAX_DELEGATORS_PER_VALIDATOR,
wasm_config: WasmConfig::default(),
system_config: SystemConfig::default(),
administrative_accounts: Default::default(),
Expand All @@ -104,45 +103,6 @@ impl Default for EngineConfig {
}

impl EngineConfig {
/// Creates a new `EngineConfig` instance.
///
/// New code should use [`EngineConfigBuilder`] instead as some config options will otherwise be
/// defaulted.
#[deprecated(
since = "3.0.0",
note = "prefer to use EngineConfigBuilder to construct an EngineConfig"
)]
#[allow(clippy::too_many_arguments)]
pub fn new(
max_query_depth: u64,
max_associated_keys: u32,
max_runtime_call_stack_height: u32,
minimum_delegation_amount: u64,
strict_argument_checking: bool,
vesting_schedule_period_millis: u64,
max_delegators_per_validator: Option<u32>,
wasm_config: WasmConfig,
system_config: SystemConfig,
) -> EngineConfig {
Self {
max_query_depth,
max_associated_keys,
max_runtime_call_stack_height,
minimum_delegation_amount,
strict_argument_checking,
vesting_schedule_period_millis,
max_delegators_per_validator,
wasm_config,
system_config,
administrative_accounts: Default::default(),
allow_auction_bids: DEFAULT_ALLOW_AUCTION_BIDS,
allow_unrestricted_transfers: DEFAULT_ALLOW_UNRESTRICTED_TRANSFERS,
refund_handling: DEFAULT_REFUND_HANDLING,
fee_handling: DEFAULT_FEE_HANDLING,
compute_rewards: DEFAULT_COMPUTE_REWARDS,
}
}

/// Returns the current max associated keys config.
pub fn max_associated_keys(&self) -> u32 {
self.max_associated_keys
Expand Down Expand Up @@ -179,7 +139,7 @@ impl EngineConfig {
}

/// Get the max delegators per validator
pub fn max_delegators_per_validator(&self) -> Option<u32> {
pub fn max_delegators_per_validator(&self) -> u32 {
self.max_delegators_per_validator
}

Expand Down Expand Up @@ -282,8 +242,8 @@ impl EngineConfigBuilder {
}

/// Sets the max delegators per validator config option.
pub fn with_max_delegators_per_validator(mut self, value: Option<u32>) -> Self {
self.max_delegators_per_validator = value;
pub fn with_max_delegators_per_validator(mut self, value: u32) -> Self {
self.max_delegators_per_validator = Some(value);
self
}

Expand Down Expand Up @@ -342,6 +302,9 @@ impl EngineConfigBuilder {
"refund ratio should be in the range of [0, 1]"
);
}
RefundHandling::None => {
//noop
}
}

self.refund_handling = Some(refund_handling);
Expand All @@ -362,7 +325,6 @@ impl EngineConfigBuilder {

/// Builds a new [`EngineConfig`] object.
pub fn build(self) -> EngineConfig {
let max_query_depth = self.max_query_depth.unwrap_or(DEFAULT_MAX_QUERY_DEPTH);
let max_associated_keys = self
.max_associated_keys
.unwrap_or(DEFAULT_MAX_ASSOCIATED_KEYS);
Expand Down Expand Up @@ -396,11 +358,12 @@ impl EngineConfigBuilder {
let vesting_schedule_period_millis = self
.vesting_schedule_period_millis
.unwrap_or(DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS);
let max_delegators_per_validator = self.max_delegators_per_validator;
let max_delegators_per_validator = self
.max_delegators_per_validator
.unwrap_or(DEFAULT_MAX_DELEGATORS_PER_VALIDATOR);
let compute_rewards = self.compute_rewards.unwrap_or(DEFAULT_COMPUTE_REWARDS);

EngineConfig {
max_query_depth,
max_associated_keys,
max_runtime_call_stack_height,
minimum_delegation_amount,
Expand Down
3 changes: 3 additions & 0 deletions execution_engine/src/engine_state/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub enum Error {
/// Native transfer error.
#[error("Transfer error: {0}")]
Transfer(TransferError),
/// Deprecated functionality.
#[error("Deprecated: {0}")]
Deprecated(String),
}

impl Error {
Expand Down
44 changes: 28 additions & 16 deletions execution_engine/src/engine_state/execution_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,25 @@ impl ExecutionKind {
let maybe_version_key =
version.map(|ver| EntityVersionKey::new(protocol_version.value().major, ver));

let contract_version_key = maybe_version_key
let entity_version_key = maybe_version_key
.or_else(|| package.current_entity_version())
.ok_or(Error::Exec(ExecError::NoActiveEntityVersions(package_hash)))?;

if !package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(ExecError::InvalidEntityVersion(
contract_version_key,
if package.is_version_missing(entity_version_key) {
return Err(Error::Exec(ExecError::MissingEntityVersion(
entity_version_key,
)));
}
if !package.is_version_enabled(entity_version_key) {
return Err(Error::Exec(ExecError::DisabledEntityVersion(
entity_version_key,
)));
}

let looked_up_entity_hash: AddressableEntityHash = package
.lookup_entity_hash(contract_version_key)
.ok_or(Error::Exec(ExecError::InvalidEntityVersion(
contract_version_key,
.lookup_entity_hash(entity_version_key)
.ok_or(Error::Exec(ExecError::MissingEntityVersion(
entity_version_key,
)))?
.to_owned();

Expand All @@ -153,21 +158,28 @@ impl ExecutionKind {
let maybe_version_key =
version.map(|ver| EntityVersionKey::new(protocol_version.value().major, ver));

let contract_version_key = maybe_version_key
let entity_version_key = maybe_version_key
.or_else(|| package.current_entity_version())
.ok_or(Error::Exec(ExecError::NoActiveEntityVersions(package_hash)))?;

if !package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(ExecError::InvalidEntityVersion(
contract_version_key,
if package.is_version_missing(entity_version_key) {
return Err(Error::Exec(ExecError::MissingEntityVersion(
entity_version_key,
)));
}

if !package.is_version_enabled(entity_version_key) {
return Err(Error::Exec(ExecError::DisabledEntityVersion(
entity_version_key,
)));
}

let looked_up_entity_hash = *package
.lookup_entity_hash(contract_version_key)
.ok_or(Error::Exec(ExecError::InvalidEntityVersion(
contract_version_key,
)))?;
let looked_up_entity_hash =
*package
.lookup_entity_hash(entity_version_key)
.ok_or(Error::Exec(ExecError::MissingEntityVersion(
entity_version_key,
)))?;

Ok(ExecutionKind::new_addressable_entity(
looked_up_entity_hash,
Expand Down
87 changes: 74 additions & 13 deletions execution_engine/src/engine_state/execution_result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Outcome of an `ExecutionRequest`.
use std::collections::VecDeque;
use tracing::{debug, trace};

use casper_storage::data_access_layer::TransferResult;
use casper_types::{
Expand Down Expand Up @@ -43,19 +44,6 @@ pub enum ExecutionResult {
},
}

/// A type alias that represents multiple execution results.
pub type ExecutionResults = VecDeque<ExecutionResult>;

/// Indicates the outcome of a transfer payment check.
pub enum ForcedTransferResult {
/// Payment code ran out of gas during execution
InsufficientPayment,
/// Gas conversion overflow
GasConversionOverflow,
/// Payment code execution resulted in an error
PaymentFailure,
}

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
Expand Down Expand Up @@ -361,6 +349,79 @@ impl ExecutionResult {
}
}
}

/// Should charge for wasm errors?
pub(crate) fn should_charge_for_errors_in_wasm(&self) -> bool {
match self {
ExecutionResult::Failure {
error,
transfers: _,
cost: _,
effects: _,
messages: _,
} => match error {
Error::Exec(err) => matches!(
err,
ExecError::WasmPreprocessing(_) | ExecError::UnsupportedWasmStart
),
Error::WasmPreprocessing(_) | Error::WasmSerialization(_) => true,
_ => false,
},
ExecutionResult::Success { .. } => false,
}
}

/// Logs execution results.
pub fn log_execution_result(&self, preamble: &'static str) {
trace!("{}: {:?}", preamble, self);
match self {
ExecutionResult::Success {
transfers,
cost,
effects,
messages,
} => {
debug!(
%cost,
transfer_count = %transfers.len(),
transforms_count = %effects.len(),
messages_count = %messages.len(),
"{}: execution success",
preamble
);
}
ExecutionResult::Failure {
error,
transfers,
cost,
effects,
messages,
} => {
debug!(
%error,
%cost,
transfer_count = %transfers.len(),
transforms_count = %effects.len(),
messages_count = %messages.len(),
"{}: execution failure",
preamble
);
}
}
}
}

/// A type alias that represents multiple execution results.
pub type ExecutionResults = VecDeque<ExecutionResult>;

/// Indicates the outcome of a transfer payment check.
pub enum ForcedTransferResult {
/// Payment code ran out of gas during execution
InsufficientPayment,
/// Gas conversion overflow
GasConversionOverflow,
/// Payment code execution resulted in an error
PaymentFailure,
}

/// A versioned execution result and the messages produced by that execution.
Expand Down
Loading

0 comments on commit 6223147

Please sign in to comment.