Skip to content

Commit 7971492

Browse files
casperlabs-bors-ng[bot]Jakub Zajkowski
and
Jakub Zajkowski
authored
Merge #4921
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]>
2 parents 7750e81 + 9bc7270 commit 7971492

40 files changed

+543
-375
lines changed

execution_engine/src/engine_state/engine_config.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use num_rational::Ratio;
77
use num_traits::One;
88

99
use casper_types::{
10-
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, SystemConfig,
11-
TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
10+
account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, StorageCosts,
11+
SystemConfig, TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
1212
DEFAULT_REFUND_HANDLING,
1313
};
1414

@@ -86,6 +86,7 @@ pub struct EngineConfig {
8686
pub(crate) fee_handling: FeeHandling,
8787
/// Compute auction rewards.
8888
pub(crate) compute_rewards: bool,
89+
storage_costs: StorageCosts,
8990
}
9091

9192
impl Default for EngineConfig {
@@ -108,6 +109,7 @@ impl Default for EngineConfig {
108109
fee_handling: DEFAULT_FEE_HANDLING,
109110
compute_rewards: DEFAULT_COMPUTE_REWARDS,
110111
protocol_version: DEFAULT_PROTOCOL_VERSION,
112+
storage_costs: Default::default(),
111113
}
112114
}
113115
}
@@ -198,6 +200,11 @@ impl EngineConfig {
198200
self.fee_handling
199201
}
200202

203+
/// Returns the engine config's storage_costs.
204+
pub fn storage_costs(&self) -> &StorageCosts {
205+
&self.storage_costs
206+
}
207+
201208
/// Returns the engine config's compute rewards flag.
202209
pub fn compute_rewards(&self) -> bool {
203210
self.compute_rewards
@@ -215,7 +222,7 @@ impl EngineConfig {
215222
/// Sets the `wasm_config.max_memory` to `new_value`.
216223
#[cfg(feature = "test-support")]
217224
pub fn set_max_memory(&mut self, new_value: u32) {
218-
self.wasm_config.max_memory = new_value;
225+
*self.wasm_config.v1_mut().max_memory_mut() = new_value;
219226
}
220227
}
221228

@@ -244,6 +251,7 @@ pub struct EngineConfigBuilder {
244251
fee_handling: Option<FeeHandling>,
245252
compute_rewards: Option<bool>,
246253
balance_hold_interval: Option<TimeDiff>,
254+
storage_costs: Option<StorageCosts>,
247255
}
248256

249257
impl EngineConfigBuilder {
@@ -312,7 +320,7 @@ impl EngineConfigBuilder {
312320
/// Sets the maximum wasm stack height config option.
313321
pub fn with_wasm_max_stack_height(mut self, wasm_stack_height: u32) -> Self {
314322
let wasm_config = self.wasm_config.get_or_insert_with(WasmConfig::default);
315-
wasm_config.max_stack_height = wasm_stack_height;
323+
*wasm_config.v1_mut().max_stack_height_mut() = wasm_stack_height;
316324
self
317325
}
318326

@@ -391,6 +399,12 @@ impl EngineConfigBuilder {
391399
self
392400
}
393401

402+
/// Sets the storage_costs config option.
403+
pub fn with_storage_costs(mut self, storage_costs: StorageCosts) -> Self {
404+
self.storage_costs = Some(storage_costs);
405+
self
406+
}
407+
394408
/// Builds a new [`EngineConfig`] object.
395409
pub fn build(self) -> EngineConfig {
396410
let max_associated_keys = self
@@ -437,6 +451,7 @@ impl EngineConfigBuilder {
437451
.max_delegators_per_validator
438452
.unwrap_or(DEFAULT_MAX_DELEGATORS_PER_VALIDATOR);
439453
let compute_rewards = self.compute_rewards.unwrap_or(DEFAULT_COMPUTE_REWARDS);
454+
let storage_costs = self.storage_costs.unwrap_or_default();
440455

441456
EngineConfig {
442457
max_associated_keys,
@@ -456,6 +471,7 @@ impl EngineConfigBuilder {
456471
vesting_schedule_period_millis,
457472
max_delegators_per_validator,
458473
compute_rewards,
474+
storage_costs,
459475
}
460476
}
461477
}

execution_engine/src/resolvers/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn create_module_resolver(
2222
// TODO: revisit how protocol_version check here is meant to combine with upgrade
2323
if protocol_version >= ProtocolVersion::V1_0_0 {
2424
return Ok(v1_resolver::RuntimeModuleImportResolver::new(
25-
engine_config.wasm_config().max_memory,
25+
engine_config.wasm_config().v1().max_memory(),
2626
));
2727
}
2828
Err(ResolverError::UnknownProtocolVersion(protocol_version))

execution_engine/src/runtime/externals.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ where
3232
) -> Result<Option<RuntimeValue>, Trap> {
3333
let func = FunctionIndex::try_from(index).expect("unknown function index");
3434

35-
let host_function_costs = self
36-
.context
37-
.engine_config()
38-
.wasm_config()
39-
.take_host_function_costs();
35+
let host_function_costs =
36+
(*self.context.engine_config().wasm_config().v1()).take_host_function_costs();
4037

4138
match func {
4239
FunctionIndex::ReadFuncIndex => {

execution_engine/src/runtime/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ where
12111211
let engine_config = self.context.engine_config();
12121212
let wasm_config = engine_config.wasm_config();
12131213
#[cfg(feature = "test-support")]
1214-
let max_stack_height = wasm_config.max_stack_height;
1214+
let max_stack_height = wasm_config.v1().max_stack_height();
12151215
let module = wasm_prep::preprocess(*wasm_config, module_bytes)?;
12161216
let (instance, memory) =
12171217
utils::instance_and_memory(module.clone(), protocol_version, engine_config)?;
@@ -1683,7 +1683,11 @@ where
16831683
#[cfg(feature = "test-support")]
16841684
dump_runtime_stack_info(
16851685
instance,
1686-
self.context.engine_config().wasm_config().max_stack_height,
1686+
self.context
1687+
.engine_config()
1688+
.wasm_config()
1689+
.v1()
1690+
.max_stack_height(),
16871691
);
16881692
if let Some(host_error) = error.as_host_error() {
16891693
// If the "error" was in fact a trap caused by calling `ret` then this is normal

execution_engine/src/runtime/wasm_prep.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ pub(crate) fn preprocess(
402402
ensure_parameter_limit(&module, DEFAULT_MAX_PARAMETER_COUNT)?;
403403
ensure_valid_imports(&module)?;
404404

405-
let costs = RuledOpcodeCosts(wasm_config.opcode_costs());
406-
let module = casper_wasm_utils::externalize_mem(module, None, wasm_config.max_memory);
405+
let costs = RuledOpcodeCosts(wasm_config.v1().opcode_costs());
406+
let module = casper_wasm_utils::externalize_mem(module, None, wasm_config.v1().max_memory());
407407
let module = casper_wasm_utils::inject_gas_counter(module, &costs, DEFAULT_GAS_MODULE_NAME)
408408
.map_err(|_| PreprocessingError::OperationForbiddenByGasRules)?;
409-
let module = stack_height::inject_limiter(module, wasm_config.max_stack_height)
409+
let module = stack_height::inject_limiter(module, wasm_config.v1().max_stack_height())
410410
.map_err(|_| PreprocessingError::StackLimiter)?;
411411
Ok(module)
412412
}

execution_engine/src/runtime_context/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ where
119119
entry_point_type: EntryPointType,
120120
calling_add_contract_version: CallingAddContractVersion,
121121
) -> Self {
122-
let emit_message_cost = engine_config
123-
.wasm_config()
122+
let emit_message_cost = (*engine_config.wasm_config().v1())
124123
.take_host_function_costs()
125124
.emit_message
126125
.cost()
@@ -842,7 +841,7 @@ where
842841
}
843842
}
844843

845-
let storage_costs = self.engine_config.wasm_config().storage_costs();
844+
let storage_costs = self.engine_config.storage_costs();
846845

847846
let gas_cost = storage_costs.calculate_gas_cost(bytes_count);
848847

execution_engine/src/runtime_context/tests.rs

-2
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,6 @@ fn should_meter_for_gas_storage_write() {
987987

988988
let value = StoredValue::CLValue(CLValue::from_t(43_i32).unwrap());
989989
let expected_write_cost = test_engine_config()
990-
.wasm_config()
991990
.storage_costs()
992991
.calculate_gas_cost(value.serialized_length());
993992

@@ -1025,7 +1024,6 @@ fn should_meter_for_gas_storage_add() {
10251024

10261025
let value = StoredValue::CLValue(CLValue::from_t(43_i32).unwrap());
10271026
let expected_add_cost = test_engine_config()
1028-
.wasm_config()
10291027
.storage_costs()
10301028
.calculate_gas_cost(value.serialized_length());
10311029

execution_engine_testing/test_support/src/chainspec_config.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use casper_storage::data_access_layer::GenesisRequest;
1313
use casper_types::{
1414
system::auction::VESTING_SCHEDULE_LENGTH_MILLIS, CoreConfig, FeeHandling, GenesisAccount,
1515
GenesisConfig, GenesisConfigBuilder, MintCosts, PricingHandling, ProtocolVersion,
16-
RefundHandling, SystemConfig, TimeDiff, WasmConfig,
16+
RefundHandling, StorageCosts, SystemConfig, TimeDiff, WasmConfig,
1717
};
1818

1919
use crate::{
@@ -58,6 +58,8 @@ pub struct ChainspecConfig {
5858
/// SystemConfig
5959
#[serde(rename = "system_costs")]
6060
pub system_costs_config: SystemConfig,
61+
/// Storage costs.
62+
pub storage_costs: StorageCosts,
6163
}
6264

6365
impl ChainspecConfig {
@@ -121,6 +123,7 @@ impl ChainspecConfig {
121123
core_config,
122124
wasm_config,
123125
system_costs_config,
126+
storage_costs,
124127
} = self;
125128
let CoreConfig {
126129
validator_slots,
@@ -141,6 +144,7 @@ impl ChainspecConfig {
141144
.with_round_seigniorage_rate(*round_seigniorage_rate)
142145
.with_unbonding_delay(*unbonding_delay)
143146
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
147+
.with_storage_costs(*storage_costs)
144148
.build();
145149

146150
Ok(GenesisRequest::new(
@@ -207,7 +211,7 @@ impl ChainspecConfig {
207211

208212
/// Sets wasm max stack height.
209213
pub fn with_wasm_max_stack_height(mut self, max_stack_height: u32) -> Self {
210-
self.wasm_config.max_stack_height = max_stack_height;
214+
*self.wasm_config.v1_mut().max_stack_height_mut() = max_stack_height;
211215
self
212216
}
213217

@@ -251,6 +255,7 @@ impl ChainspecConfig {
251255
.with_allow_unrestricted_transfers(self.core_config.allow_unrestricted_transfers)
252256
.with_refund_handling(self.core_config.refund_handling)
253257
.with_fee_handling(self.core_config.fee_handling)
258+
.with_storage_costs(self.storage_costs)
254259
.build()
255260
}
256261
}
@@ -296,6 +301,7 @@ impl TryFrom<ChainspecConfig> for GenesisConfig {
296301
.with_round_seigniorage_rate(chainspec_config.core_config.round_seigniorage_rate)
297302
.with_unbonding_delay(chainspec_config.core_config.unbonding_delay)
298303
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
304+
.with_storage_costs(chainspec_config.storage_costs)
299305
.build())
300306
}
301307
}

execution_engine_testing/test_support/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use casper_storage::data_access_layer::GenesisRequest;
2525
use casper_types::{
2626
account::AccountHash, testing::TestRng, ChainspecRegistry, Digest, GenesisAccount,
2727
GenesisConfig, GenesisConfigBuilder, Motes, ProtocolVersion, PublicKey, SecretKey,
28-
SystemConfig, WasmConfig, U512,
28+
StorageCosts, SystemConfig, WasmConfig, WasmV1Config, U512,
2929
};
3030

3131
pub use chainspec_config::{ChainspecConfig, CHAINSPEC_SYMLINK};
@@ -142,8 +142,12 @@ pub const DEFAULT_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::V2_0_0;
142142
pub static DEFAULT_PAYMENT: Lazy<U512> = Lazy::new(|| U512::from(10_000_000_000_000u64));
143143
/// Default [`WasmConfig`].
144144
pub static DEFAULT_WASM_CONFIG: Lazy<WasmConfig> = Lazy::new(WasmConfig::default);
145+
/// Default [`WasmV1Config`].
146+
pub static DEFAULT_WASM_V1_CONFIG: Lazy<WasmV1Config> = Lazy::new(WasmV1Config::default);
145147
/// Default [`SystemConfig`].
146148
pub static DEFAULT_SYSTEM_CONFIG: Lazy<SystemConfig> = Lazy::new(SystemConfig::default);
149+
/// Default [`StorageConfig`].
150+
pub static DEFAULT_STORAGE_COSTS: Lazy<StorageCosts> = Lazy::new(StorageCosts::default);
147151

148152
/// Default [`GenesisConfig`].
149153
pub static DEFAULT_EXEC_CONFIG: Lazy<GenesisConfig> = Lazy::new(|| {
@@ -157,6 +161,7 @@ pub static DEFAULT_EXEC_CONFIG: Lazy<GenesisConfig> = Lazy::new(|| {
157161
.with_round_seigniorage_rate(DEFAULT_ROUND_SEIGNIORAGE_RATE)
158162
.with_unbonding_delay(DEFAULT_UNBONDING_DELAY)
159163
.with_genesis_timestamp_millis(DEFAULT_GENESIS_TIMESTAMP_MILLIS)
164+
.with_storage_costs(*DEFAULT_STORAGE_COSTS)
160165
.build()
161166
});
162167

execution_engine_testing/test_support/src/utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::{DEFAULT_ROUND_SEIGNIORAGE_RATE, DEFAULT_SYSTEM_CONFIG, DEFAULT_UNBON
1515
use crate::{
1616
DEFAULT_AUCTION_DELAY, DEFAULT_CHAINSPEC_REGISTRY, DEFAULT_GENESIS_CONFIG_HASH,
1717
DEFAULT_GENESIS_TIMESTAMP_MILLIS, DEFAULT_LOCKED_FUNDS_PERIOD_MILLIS, DEFAULT_PROTOCOL_VERSION,
18-
DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
18+
DEFAULT_STORAGE_COSTS, DEFAULT_VALIDATOR_SLOTS, DEFAULT_WASM_CONFIG,
1919
};
2020

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

137138
GenesisConfigBuilder::default()
138139
.with_accounts(accounts)
@@ -144,6 +145,7 @@ pub fn create_genesis_config(accounts: Vec<GenesisAccount>) -> GenesisConfig {
144145
.with_round_seigniorage_rate(round_seigniorage_rate)
145146
.with_unbonding_delay(unbonding_delay)
146147
.with_genesis_timestamp_millis(genesis_timestamp_millis)
148+
.with_storage_costs(storage_costs)
147149
.build()
148150
}
149151

0 commit comments

Comments
 (0)