Skip to content

Commit

Permalink
Merge #4921
Browse files Browse the repository at this point in the history
4921: Nesting wasm config into "v1" field so that we can have clear separat… r=zajko a=zajko

…ion of configs between VMs in the future. Promoting "storage_costs" property in chainspec to root since it's not a wasm-specific property


Co-authored-by: Jakub Zajkowski <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and Jakub Zajkowski authored Oct 21, 2024
2 parents 7750e81 + 9bc7270 commit 7971492
Show file tree
Hide file tree
Showing 40 changed files with 543 additions and 375 deletions.
24 changes: 20 additions & 4 deletions execution_engine/src/engine_state/engine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use num_rational::Ratio;
use num_traits::One;

use casper_types::{
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, SystemConfig,
TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, StorageCosts,
SystemConfig, TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
DEFAULT_REFUND_HANDLING,
};

Expand Down Expand Up @@ -86,6 +86,7 @@ pub struct EngineConfig {
pub(crate) fee_handling: FeeHandling,
/// Compute auction rewards.
pub(crate) compute_rewards: bool,
storage_costs: StorageCosts,
}

impl Default for EngineConfig {
Expand All @@ -108,6 +109,7 @@ impl Default for EngineConfig {
fee_handling: DEFAULT_FEE_HANDLING,
compute_rewards: DEFAULT_COMPUTE_REWARDS,
protocol_version: DEFAULT_PROTOCOL_VERSION,
storage_costs: Default::default(),
}
}
}
Expand Down Expand Up @@ -198,6 +200,11 @@ impl EngineConfig {
self.fee_handling
}

/// Returns the engine config's storage_costs.
pub fn storage_costs(&self) -> &StorageCosts {
&self.storage_costs
}

/// Returns the engine config's compute rewards flag.
pub fn compute_rewards(&self) -> bool {
self.compute_rewards
Expand All @@ -215,7 +222,7 @@ impl EngineConfig {
/// Sets the `wasm_config.max_memory` to `new_value`.
#[cfg(feature = "test-support")]
pub fn set_max_memory(&mut self, new_value: u32) {
self.wasm_config.max_memory = new_value;
*self.wasm_config.v1_mut().max_memory_mut() = new_value;
}
}

Expand Down Expand Up @@ -244,6 +251,7 @@ pub struct EngineConfigBuilder {
fee_handling: Option<FeeHandling>,
compute_rewards: Option<bool>,
balance_hold_interval: Option<TimeDiff>,
storage_costs: Option<StorageCosts>,
}

impl EngineConfigBuilder {
Expand Down Expand Up @@ -312,7 +320,7 @@ impl EngineConfigBuilder {
/// Sets the maximum wasm stack height config option.
pub fn with_wasm_max_stack_height(mut self, wasm_stack_height: u32) -> Self {
let wasm_config = self.wasm_config.get_or_insert_with(WasmConfig::default);
wasm_config.max_stack_height = wasm_stack_height;
*wasm_config.v1_mut().max_stack_height_mut() = wasm_stack_height;
self
}

Expand Down Expand Up @@ -391,6 +399,12 @@ impl EngineConfigBuilder {
self
}

/// Sets the storage_costs config option.
pub fn with_storage_costs(mut self, storage_costs: StorageCosts) -> Self {
self.storage_costs = Some(storage_costs);
self
}

/// Builds a new [`EngineConfig`] object.
pub fn build(self) -> EngineConfig {
let max_associated_keys = self
Expand Down Expand Up @@ -437,6 +451,7 @@ impl EngineConfigBuilder {
.max_delegators_per_validator
.unwrap_or(DEFAULT_MAX_DELEGATORS_PER_VALIDATOR);
let compute_rewards = self.compute_rewards.unwrap_or(DEFAULT_COMPUTE_REWARDS);
let storage_costs = self.storage_costs.unwrap_or_default();

EngineConfig {
max_associated_keys,
Expand All @@ -456,6 +471,7 @@ impl EngineConfigBuilder {
vesting_schedule_period_millis,
max_delegators_per_validator,
compute_rewards,
storage_costs,
}
}
}
2 changes: 1 addition & 1 deletion execution_engine/src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn create_module_resolver(
// TODO: revisit how protocol_version check here is meant to combine with upgrade
if protocol_version >= ProtocolVersion::V1_0_0 {
return Ok(v1_resolver::RuntimeModuleImportResolver::new(
engine_config.wasm_config().max_memory,
engine_config.wasm_config().v1().max_memory(),
));
}
Err(ResolverError::UnknownProtocolVersion(protocol_version))
Expand Down
7 changes: 2 additions & 5 deletions execution_engine/src/runtime/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ where
) -> Result<Option<RuntimeValue>, Trap> {
let func = FunctionIndex::try_from(index).expect("unknown function index");

let host_function_costs = self
.context
.engine_config()
.wasm_config()
.take_host_function_costs();
let host_function_costs =
(*self.context.engine_config().wasm_config().v1()).take_host_function_costs();

match func {
FunctionIndex::ReadFuncIndex => {
Expand Down
8 changes: 6 additions & 2 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ where
let engine_config = self.context.engine_config();
let wasm_config = engine_config.wasm_config();
#[cfg(feature = "test-support")]
let max_stack_height = wasm_config.max_stack_height;
let max_stack_height = wasm_config.v1().max_stack_height();
let module = wasm_prep::preprocess(*wasm_config, module_bytes)?;
let (instance, memory) =
utils::instance_and_memory(module.clone(), protocol_version, engine_config)?;
Expand Down Expand Up @@ -1683,7 +1683,11 @@ where
#[cfg(feature = "test-support")]
dump_runtime_stack_info(
instance,
self.context.engine_config().wasm_config().max_stack_height,
self.context
.engine_config()
.wasm_config()
.v1()
.max_stack_height(),
);
if let Some(host_error) = error.as_host_error() {
// If the "error" was in fact a trap caused by calling `ret` then this is normal
Expand Down
6 changes: 3 additions & 3 deletions execution_engine/src/runtime/wasm_prep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,11 @@ pub(crate) fn preprocess(
ensure_parameter_limit(&module, DEFAULT_MAX_PARAMETER_COUNT)?;
ensure_valid_imports(&module)?;

let costs = RuledOpcodeCosts(wasm_config.opcode_costs());
let module = casper_wasm_utils::externalize_mem(module, None, wasm_config.max_memory);
let costs = RuledOpcodeCosts(wasm_config.v1().opcode_costs());
let module = casper_wasm_utils::externalize_mem(module, None, wasm_config.v1().max_memory());
let module = casper_wasm_utils::inject_gas_counter(module, &costs, DEFAULT_GAS_MODULE_NAME)
.map_err(|_| PreprocessingError::OperationForbiddenByGasRules)?;
let module = stack_height::inject_limiter(module, wasm_config.max_stack_height)
let module = stack_height::inject_limiter(module, wasm_config.v1().max_stack_height())
.map_err(|_| PreprocessingError::StackLimiter)?;
Ok(module)
}
Expand Down
5 changes: 2 additions & 3 deletions execution_engine/src/runtime_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ where
entry_point_type: EntryPointType,
calling_add_contract_version: CallingAddContractVersion,
) -> Self {
let emit_message_cost = engine_config
.wasm_config()
let emit_message_cost = (*engine_config.wasm_config().v1())
.take_host_function_costs()
.emit_message
.cost()
Expand Down Expand Up @@ -842,7 +841,7 @@ where
}
}

let storage_costs = self.engine_config.wasm_config().storage_costs();
let storage_costs = self.engine_config.storage_costs();

let gas_cost = storage_costs.calculate_gas_cost(bytes_count);

Expand Down
2 changes: 0 additions & 2 deletions execution_engine/src/runtime_context/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ fn should_meter_for_gas_storage_write() {

let value = StoredValue::CLValue(CLValue::from_t(43_i32).unwrap());
let expected_write_cost = test_engine_config()
.wasm_config()
.storage_costs()
.calculate_gas_cost(value.serialized_length());

Expand Down Expand Up @@ -1025,7 +1024,6 @@ fn should_meter_for_gas_storage_add() {

let value = StoredValue::CLValue(CLValue::from_t(43_i32).unwrap());
let expected_add_cost = test_engine_config()
.wasm_config()
.storage_costs()
.calculate_gas_cost(value.serialized_length());

Expand Down
10 changes: 8 additions & 2 deletions execution_engine_testing/test_support/src/chainspec_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
system::auction::VESTING_SCHEDULE_LENGTH_MILLIS, CoreConfig, FeeHandling, GenesisAccount,
GenesisConfig, GenesisConfigBuilder, MintCosts, PricingHandling, ProtocolVersion,
RefundHandling, SystemConfig, TimeDiff, WasmConfig,
RefundHandling, StorageCosts, SystemConfig, TimeDiff, WasmConfig,
};

use crate::{
Expand Down Expand Up @@ -58,6 +58,8 @@ pub struct ChainspecConfig {
/// SystemConfig
#[serde(rename = "system_costs")]
pub system_costs_config: SystemConfig,
/// Storage costs.
pub storage_costs: StorageCosts,
}

impl ChainspecConfig {
Expand Down Expand Up @@ -121,6 +123,7 @@ impl ChainspecConfig {
core_config,
wasm_config,
system_costs_config,
storage_costs,
} = self;
let CoreConfig {
validator_slots,
Expand All @@ -141,6 +144,7 @@ impl ChainspecConfig {
.with_round_seigniorage_rate(*round_seigniorage_rate)
.with_unbonding_delay(*unbonding_delay)
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
.with_storage_costs(*storage_costs)
.build();

Ok(GenesisRequest::new(
Expand Down Expand Up @@ -207,7 +211,7 @@ impl ChainspecConfig {

/// Sets wasm max stack height.
pub fn with_wasm_max_stack_height(mut self, max_stack_height: u32) -> Self {
self.wasm_config.max_stack_height = max_stack_height;
*self.wasm_config.v1_mut().max_stack_height_mut() = max_stack_height;
self
}

Expand Down Expand Up @@ -251,6 +255,7 @@ impl ChainspecConfig {
.with_allow_unrestricted_transfers(self.core_config.allow_unrestricted_transfers)
.with_refund_handling(self.core_config.refund_handling)
.with_fee_handling(self.core_config.fee_handling)
.with_storage_costs(self.storage_costs)
.build()
}
}
Expand Down Expand Up @@ -296,6 +301,7 @@ impl TryFrom<ChainspecConfig> for GenesisConfig {
.with_round_seigniorage_rate(chainspec_config.core_config.round_seigniorage_rate)
.with_unbonding_delay(chainspec_config.core_config.unbonding_delay)
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
.with_storage_costs(chainspec_config.storage_costs)
.build())
}
}
Expand Down
7 changes: 6 additions & 1 deletion execution_engine_testing/test_support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use casper_storage::data_access_layer::GenesisRequest;
use casper_types::{
account::AccountHash, testing::TestRng, ChainspecRegistry, Digest, GenesisAccount,
GenesisConfig, GenesisConfigBuilder, Motes, ProtocolVersion, PublicKey, SecretKey,
SystemConfig, WasmConfig, U512,
StorageCosts, SystemConfig, WasmConfig, WasmV1Config, U512,
};

pub use chainspec_config::{ChainspecConfig, CHAINSPEC_SYMLINK};
Expand Down Expand Up @@ -142,8 +142,12 @@ pub const DEFAULT_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::V2_0_0;
pub static DEFAULT_PAYMENT: Lazy<U512> = Lazy::new(|| U512::from(10_000_000_000_000u64));
/// Default [`WasmConfig`].
pub static DEFAULT_WASM_CONFIG: Lazy<WasmConfig> = Lazy::new(WasmConfig::default);
/// Default [`WasmV1Config`].
pub static DEFAULT_WASM_V1_CONFIG: Lazy<WasmV1Config> = Lazy::new(WasmV1Config::default);
/// Default [`SystemConfig`].
pub static DEFAULT_SYSTEM_CONFIG: Lazy<SystemConfig> = Lazy::new(SystemConfig::default);
/// Default [`StorageConfig`].
pub static DEFAULT_STORAGE_COSTS: Lazy<StorageCosts> = Lazy::new(StorageCosts::default);

/// Default [`GenesisConfig`].
pub static DEFAULT_EXEC_CONFIG: Lazy<GenesisConfig> = Lazy::new(|| {
Expand All @@ -157,6 +161,7 @@ pub static DEFAULT_EXEC_CONFIG: Lazy<GenesisConfig> = Lazy::new(|| {
.with_round_seigniorage_rate(DEFAULT_ROUND_SEIGNIORAGE_RATE)
.with_unbonding_delay(DEFAULT_UNBONDING_DELAY)
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
.with_storage_costs(*DEFAULT_STORAGE_COSTS)
.build()
});

Expand Down
4 changes: 3 additions & 1 deletion execution_engine_testing/test_support/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{DEFAULT_ROUND_SEIGNIORAGE_RATE, DEFAULT_SYSTEM_CONFIG, DEFAULT_UNBON
use crate::{
DEFAULT_AUCTION_DELAY, DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_GENESIS_CONFIG_HASH,
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROTOCOL_VERSION,
DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
DEFAULT_STORAGE_COSTS, DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
};

static RUST_WORKSPACE_PATH: Lazy<PathBuf> = Lazy::new(|| {
Expand Down Expand Up @@ -133,6 +133,7 @@ pub fn create_genesis_config(accounts: Vec<GenesisAccount>) -> GenesisConfig {
let round_seigniorage_rate = DEFAULT_ROUND_SEIGNIORAGE_RATE;
let unbonding_delay = DEFAULT_UNBONDING_DELAY;
let genesis_timestamp_millis = DEFAULT_GENESIS_TIMESTAMP_MILLIS;
let storage_costs = *DEFAULT_STORAGE_COSTS;

GenesisConfigBuilder::default()
.with_accounts(accounts)
Expand All @@ -144,6 +145,7 @@ pub fn create_genesis_config(accounts: Vec<GenesisAccount>) -> GenesisConfig {
.with_round_seigniorage_rate(round_seigniorage_rate)
.with_unbonding_delay(unbonding_delay)
.with_genesis_timestamp_millis(genesis_timestamp_millis)
.with_storage_costs(storage_costs)
.build()
}

Expand Down
Loading

0 comments on commit 7971492

Please sign in to comment.