Skip to content

Commit

Permalink
Initialize and validate gas table
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Mar 28, 2023
1 parent e0c1bf4 commit 2466237
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
19 changes: 19 additions & 0 deletions apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 19 additions & 2 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub mod genesis_config {
/// Fix wrapper tx fees
pub wrapper_tx_fees: Option<token::Amount>,
/// Gas table
pub gas_table: BTreeMap<String, u64>,
pub gas_table: Option<BTreeMap<String, u64>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions apps/src/lib/wasm_loader/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -327,9 +327,11 @@ async fn download_wasm(url: String) -> Result<Vec<u8>, 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<Path>) -> HashMap<String, u64> {
pub fn read_gas_file(
wasm_directory: impl AsRef<Path>,
) -> BTreeMap<String, u64> {
let gas_path = wasm_directory.as_ref().join("gas.json");

match fs::File::open(&gas_path) {
Expand Down

0 comments on commit 2466237

Please sign in to comment.