Skip to content

Commit

Permalink
Extend cost params in simulation in order to match the hardcoded valu…
Browse files Browse the repository at this point in the history
…e. (#1405)

### What

Extend cost params in simulation in order to match the hardcoded value.

### Why

Env expects cost params to have the same size as defined in XDR, even if
protocol doesn't use these params yet. This is a temporary workaround
until #1388 is resolved.

### Known limitations

N/A
  • Loading branch information
dmkozh authored Apr 17, 2024
1 parent 80a3a9c commit 27897f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
32 changes: 28 additions & 4 deletions soroban-simulation/src/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use soroban_env_host::fees::{
compute_write_fee_per_1kb, FeeConfiguration, RentFeeConfiguration, WriteFeeConfiguration,
};
use soroban_env_host::xdr::{
ConfigSettingEntry, ConfigSettingId, ContractCostParams, LedgerEntry, LedgerEntryData,
LedgerKey, LedgerKeyConfigSetting,
ConfigSettingEntry, ConfigSettingId, ContractCostParamEntry, ContractCostParams,
ContractCostType, ExtensionPoint, LedgerEntry, LedgerEntryData, LedgerKey,
LedgerKeyConfigSetting,
};
use soroban_env_host::LedgerInfo;
use std::rc::Rc;
Expand Down Expand Up @@ -49,6 +50,24 @@ fn load_configuration_setting(
}
}

fn extend_cost_params(params: &mut ContractCostParams) -> Result<()> {
let target_size = ContractCostType::variants().len();
if params.0.len() >= target_size {
return Ok(());
}
let mut v = params.to_vec();
v.resize(
target_size,
ContractCostParamEntry {
ext: ExtensionPoint::V0,
const_term: 0,
linear_term: 0,
},
);
params.0 = v.try_into().context("too many contract cost params")?;
Ok(())
}

macro_rules! load_setting {
($snapshot:ident, $enum_variant:ident) => {
match load_configuration_setting($snapshot, ConfigSettingId::$enum_variant)? {
Expand Down Expand Up @@ -76,8 +95,13 @@ impl NetworkConfig {
let events = load_setting!(snapshot, ContractEventsV0);
let bandwidth = load_setting!(snapshot, ContractBandwidthV0);
let state_archival = load_setting!(snapshot, StateArchival);
let cpu_cost_params = load_setting!(snapshot, ContractCostParamsCpuInstructions);
let memory_cost_params = load_setting!(snapshot, ContractCostParamsMemoryBytes);
let mut cpu_cost_params = load_setting!(snapshot, ContractCostParamsCpuInstructions);
let mut memory_cost_params = load_setting!(snapshot, ContractCostParamsMemoryBytes);
// This is a hack to get around env using hardcoded cost param length
// instead of the protocol-defined length:
// https://github.com/stellar/rs-soroban-env/issues/1388.
extend_cost_params(&mut cpu_cost_params)?;
extend_cost_params(&mut memory_cost_params)?;

let write_fee_configuration = WriteFeeConfiguration {
bucket_list_target_size_bytes: ledger_cost.bucket_list_target_size_bytes,
Expand Down
18 changes: 16 additions & 2 deletions soroban-simulation/src/test/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ fn test_load_config_from_snapshot() {
NetworkConfig::load_from_snapshot(&snapshot_source, 150_000_000_000_000).unwrap();
// From tests/resources `test_compute_write_fee`
let write_fee = 1_000_000_000 + 50 * (1_000_000_000_i64 - 1_000_000) / 2;

let empty_entry = ContractCostParamEntry {
ext: ExtensionPoint::V0,
const_term: 0,
linear_term: 0,
};
let mut expected_cpu_cost_params_vec = cpu_cost_params.to_vec();
expected_cpu_cost_params_vec.resize(ContractCostType::variants().len(), empty_entry.clone());
let expected_cpu_cost_params =
ContractCostParams(expected_cpu_cost_params_vec.try_into().unwrap());
let mut expected_mem_cost_params_vec = memory_cost_params.to_vec();
expected_mem_cost_params_vec.resize(ContractCostType::variants().len(), empty_entry.clone());
let expected_mem_cost_params =
ContractCostParams(expected_mem_cost_params_vec.try_into().unwrap());
assert_eq!(
network_config,
NetworkConfig {
Expand All @@ -154,8 +168,8 @@ fn test_load_config_from_snapshot() {
},
tx_max_instructions: 2,
tx_memory_limit: 4,
cpu_cost_params,
memory_cost_params,
cpu_cost_params: expected_cpu_cost_params,
memory_cost_params: expected_mem_cost_params,
min_temp_entry_ttl: 27,
min_persistent_entry_ttl: 28,
max_entry_ttl: 26,
Expand Down

0 comments on commit 27897f6

Please sign in to comment.