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 29, 2023
1 parent a762282 commit dff51dd
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ pub fn init_network(
allow_duplicate_ip,
dont_archive,
archive_dir,
}: args::InitNetwork,
}: args::InitNetwork;,
) {
let mut config = genesis_config::open_genesis_config(genesis_path).unwrap();

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 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.to_lowercase()) {
panic!("Missing gas cost for hash {}", hash);
}
}

let GovernanceParamsConfig {
min_proposal_fund,
max_proposal_code_size,
Expand Down
22 changes: 1 addition & 21 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 @@ -326,23 +326,3 @@ async fn download_wasm(url: String) -> Result<Vec<u8>, Error> {
Err(e) => Err(Error::Download(url, e)),
}
}

/// Read the json file containing the gas costs for the whitelisted vps and txsi
/// from the default "gas.json" file in the given directory
pub fn read_gas_file(wasm_directory: impl AsRef<Path>) -> HashMap<String, u64> {
let gas_path = wasm_directory.as_ref().join("gas.json");

match fs::File::open(&gas_path) {
Ok(file) => match serde_json::from_reader(file) {
Ok(result) => result,
Err(_) => {
eprintln!("Can't read gas from {}", gas_path.to_string_lossy());
safe_exit(1);
}
},
Err(_) => {
eprintln!("Can't find gas at {}", gas_path.to_string_lossy());
safe_exit(1);
}
}
}
4 changes: 4 additions & 0 deletions core/src/ledger/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl Parameters {

// write gas table
let gas_table_key = storage::get_gas_table_storage_key();
let gas_table = gas_table
.iter()
.map(|(k, v)| (k.to_lowercase(), *v))
.collect::<BTreeMap<String, u64>>();
storage.write(&gas_table_key, gas_table)?;

// write vp whitelist parameter
Expand Down
9 changes: 8 additions & 1 deletion tests/src/e2e/setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::ffi::OsStr;
use std::fmt::Display;
use std::fs::{File, OpenOptions};
Expand Down Expand Up @@ -138,6 +138,7 @@ pub fn network(
Some(get_all_wasms_hashes(&working_dir, Some("vp_")));
genesis.parameters.tx_whitelist =
Some(get_all_wasms_hashes(&working_dir, Some("tx_")));
genesis.parameters.gas_table = Some(get_gas_checksums(&working_dir));

// Run the provided function on it
let genesis = update_genesis(genesis);
Expand Down Expand Up @@ -935,3 +936,9 @@ pub fn get_all_wasms_hashes(
})
.collect()
}

pub fn get_gas_checksums(working_dir: &Path) -> BTreeMap<String, u64> {
let gas_checksums_path = working_dir.join("wasm/gas_checksums.json");
serde_json::from_reader(fs::File::open(gas_checksums_path).unwrap())
.unwrap()
}
10 changes: 7 additions & 3 deletions wasm/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

gas = json.load(open("wasm/gas.json"))
gas_checksums = {}
checksums = {}

for wasm in sorted(glob.glob("wasm/*.wasm")):
Expand All @@ -15,15 +16,17 @@
else os.path.splitext(basename)[0].split(".")[0]
)
file_key = "{}.wasm".format(file_name)
checksums[file_key] = "{}.{}.wasm".format(
file_name, hashlib.sha256(open(wasm, "rb").read()).hexdigest()
)
file_hash = hashlib.sha256(open(wasm, "rb").read()).hexdigest()
checksums[file_key] = "{}.{}.wasm".format(file_name, file_hash)

# Check gas in whitelist
if file_key not in gas:
print("{} doesn't have an associated gas cost in gas.json".format(file_key))
sys.exit(1)

# Add gas to checksum gas
gas_checksums[file_hash] = gas[file_key]

os.rename(wasm, "wasm/{}".format(checksums[file_key]))

# Prune unused gas entries if needed (in case of a tx/vp removal)
Expand All @@ -32,6 +35,7 @@
del gas[k]

json.dump(gas, open("wasm/gas.json", "w"), indent=4, sort_keys=True)
json.dump(gas_checksums, open("wasm/gas_checksums.json", "w"), indent=4, sort_keys=True)

updated_wasms = list(checksums.values())

Expand Down
20 changes: 20 additions & 0 deletions wasm/gas_checksums.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"2b543ec54932a8a5a9f402913f857406dd1e52255d692515f70bc1c393bc85e2": 1,
"462af49196f257d271f75d613e063750c934df1133b4cf3eb44d46c4ce5be668": 1,
"50444d2a47159773cbbe01c26b9a54732de8bbab4133e61a3937f0a1b8723dd4": 0,
"5574a272dbac031e4e14109fabcd864ab3069a0d21ae3b782cce2d91893d1999": 1,
"69f92cb21b49b6b0a2fec8a3e6b24ceb4016b37fe064e913f6fcfe9eb719381b": 1,
"70175441c32efc3cdc0bf33790857ab0a56aabc3309dc39b0e8d5c331a7ad7fe": 1,
"7bc74c63fe03776ed26aa5b07eac4416ac36840f2dd39336c42ae1619babe911": 1,
"7f8357d4554450ba52dc5c7aa6a2ef3ce40cc7c92a6eba79bcd163715e96eee1": 1,
"885702764693e40408baa6fa82530d4b0693d63880187eef2fe8abfd27497e7b": 1,
"8faa70660152a6fc4998a845ac310f92c04cb89692c5ad1940f031ab7e935470": 1,
"94bc8a4bf27f12f8c0cd102ee1a63be0320f5e1d2f540c0dfd510c9324dc3842": 1,
"cd261fd4b5d67db72ace5eb281d6a9e58f1b00cde6c96cd0b0a8bcf5f38eaf41": 1,
"d014ccb0df538af3ffba99bcde354d2117a95fa6906aa96b1873ca29628e20c5": 1,
"d0b03343e585666300d7f01ada1cc51bb5a74fc25005b9b26daef213ca73e00f": 1,
"d2e59b592e11f4c25cb5035fdf69089c84a968702e6e325775c1a9bf23db7c89": 1,
"db724ffa2345002de0dea3b3dc219e98c6f27f9a980002e93f6cef2c892d34ca": 1,
"e208af69e7c52beb29ce53334015dc0ce84167d3d943667755bad7c6bc2f2535": 1,
"f22219a40cc0208ac13f4529ebbf75f55ef3113ae3384ccb16c7c1e646f86462": 1
}

0 comments on commit dff51dd

Please sign in to comment.