diff --git a/apps/src/lib/client/utils.rs b/apps/src/lib/client/utils.rs index 21fed46c116..ae0211b677f 100644 --- a/apps/src/lib/client/utils.rs +++ b/apps/src/lib/client/utils.rs @@ -422,6 +422,25 @@ pub fn init_network( config.sha256 = Some(genesis_config::HexString(hash.to_owned())); }); + // Read gas file + let mut gas_table = wasm_loader::read_gas_file(&wasm_checksums_path); + + // Check that the gas table length is equal to the whitelisted tx + vp lengths + if gas_table.len() != checksums.0.len() { + panic!("Gas table contains a different number of elements then the sum of the whitelisted txs and vps"); + } + + // Write gas cost in parameters + for (name, hash) in checksums.0 { + // Substitute txs and vps names with their checksums in gas table + let gas = gas_table + .remove(&name) + .expect(&format!("Missing gas cost for {}", name)); + gas_table.insert(hash.to_owned(), gas); + } + + config.parameters.gas_table = Some(gas_table); + // The `temp_chain_id` gets renamed after we have chain ID. let temp_chain_id = chain_id_prefix.temp_chain_id(); let temp_dir = global_args.base_dir.join(temp_chain_id.as_str()); diff --git a/apps/src/lib/config/genesis.rs b/apps/src/lib/config/genesis.rs index 74b1d71e79c..820f305ac19 100644 --- a/apps/src/lib/config/genesis.rs +++ b/apps/src/lib/config/genesis.rs @@ -273,7 +273,7 @@ pub mod genesis_config { /// Fix wrapper tx fees pub wrapper_tx_fees: Option, /// Gas table - pub gas_table: BTreeMap, + pub gas_table: Option>, } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -597,6 +597,7 @@ pub mod genesis_config { let min_duration: i64 = 60 * 60 * 24 * 365 / (parameters.epochs_per_year as i64); + let parameters = Parameters { epoch_duration: EpochDuration { min_num_of_blocks: parameters.min_num_of_blocks, @@ -622,9 +623,25 @@ pub mod genesis_config { staked_ratio: Decimal::ZERO, pos_inflation_amount: 0, wrapper_tx_fees: parameters.wrapper_tx_fees, - gas_table: parameters.gas_table, + gas_table: parameters.gas_table.unwrap_or_default(), }; + // Check validity of whitelists and gas table + if parameters.gas_table.len() + != parameters.tx_whitelist.len() + parameters.vp_whitelist.len() + { + panic!("Mismatching length of gas table and txs/vps whitelists"); + } + for hash in parameters + .tx_whitelist + .iter() + .chain(parameters.vp_whitelist.iter()) + { + if !parameters.gas_table.contains_key(hash) { + panic!("Missing gas cost for {}", hash); + } + } + let GovernanceParamsConfig { min_proposal_fund, max_proposal_code_size, diff --git a/apps/src/lib/wasm_loader/mod.rs b/apps/src/lib/wasm_loader/mod.rs index 7456c9eec50..a0e8964785b 100644 --- a/apps/src/lib/wasm_loader/mod.rs +++ b/apps/src/lib/wasm_loader/mod.rs @@ -1,6 +1,6 @@ //! A module for loading WASM files and downloading pre-built WASMs. use core::borrow::Borrow; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::fs; use std::path::Path; @@ -327,9 +327,11 @@ async fn download_wasm(url: String) -> Result, Error> { } } -/// Read the json file containing the gas costs for the whitelisted vps and txsi +/// Read the json file containing the gas costs for the whitelisted vps and txs /// from the default "gas.json" file in the given directory -pub fn read_gas_file(wasm_directory: impl AsRef) -> HashMap { +pub fn read_gas_file( + wasm_directory: impl AsRef, +) -> BTreeMap { let gas_path = wasm_directory.as_ref().join("gas.json"); match fs::File::open(&gas_path) {