Skip to content

Commit

Permalink
Moves gas cost to checksums file
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Mar 31, 2023
1 parent 9353f2a commit 8103c26
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 96 deletions.
41 changes: 2 additions & 39 deletions apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use crate::config::genesis::genesis_config::{
self, GenesisConfig, HexString, ValidatorPreGenesisConfig,
};
use crate::config::global::GlobalConfig;
use crate::config::{
self, Config, TendermintMode, DEFAULT_WASM_CHECKSUMS_GAS_FILE,
};
use crate::config::{self, Config, TendermintMode};
use crate::facade::tendermint::node::Id as TendermintNodeId;
use crate::facade::tendermint_config::net::Address as TendermintAddress;
use crate::node::ledger::tendermint_node;
Expand Down Expand Up @@ -425,7 +423,7 @@ pub fn init_network(
// Find the sha256 from checksums.json
let name = format!("{}.wasm", name);
// Full name in format `{name}.{sha256}.wasm`
let full_name = checksums.0.get(&name).unwrap();
let full_name = checksums.0.get(&name).unwrap().get("hash").unwrap();
let hash = full_name
.split_once('.')
.unwrap()
Expand Down Expand Up @@ -668,7 +666,6 @@ pub fn init_network(
fs::rename(&temp_dir, &chain_dir).unwrap();

// Copy the WASM checksums
//FIXME: hange wasm_checksums_path to wasm_path?
let wasm_dir_full = chain_dir.join(config::DEFAULT_WASM_DIR);
fs::create_dir_all(&wasm_dir_full).unwrap();
fs::copy(
Expand All @@ -677,16 +674,6 @@ pub fn init_network(
)
.unwrap();

// Copy the gas checksums
fs::copy(
&wasm_checksums_path
.parent()
.unwrap()
.join(DEFAULT_WASM_CHECKSUMS_GAS_FILE),
wasm_dir_full.join(config::DEFAULT_WASM_CHECKSUMS_GAS_FILE),
)
.unwrap();

config.validator.iter().for_each(|(name, _config)| {
let validator_dir = global_args
.base_dir
Expand All @@ -711,16 +698,6 @@ pub fn init_network(
)
.unwrap();

// Copy the gas checksums
fs::copy(
&wasm_checksums_path
.parent()
.unwrap()
.join(DEFAULT_WASM_CHECKSUMS_GAS_FILE),
wasm_dir_full.join(config::DEFAULT_WASM_CHECKSUMS_GAS_FILE),
)
.unwrap();

// Write the genesis and global config into validator sub-dirs
genesis_config::write_genesis_config(
&config,
Expand Down Expand Up @@ -862,20 +839,6 @@ pub fn init_network(
release_wasm_checksums_path,
)
.unwrap();
let release_wasm_checksums_gas_path =
PathBuf::from(config::DEFAULT_BASE_DIR)
.join(chain_id.as_str())
.join(config::DEFAULT_WASM_DIR)
.join(DEFAULT_WASM_CHECKSUMS_GAS_FILE);
release
.append_path_with_name(
&wasm_checksums_path
.parent()
.unwrap()
.join(DEFAULT_WASM_CHECKSUMS_GAS_FILE),
release_wasm_checksums_gas_path,
)
.unwrap();

// Gzip tar release and write to file
let release_file = archive_dir
Expand Down
3 changes: 0 additions & 3 deletions apps/src/lib/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ pub const DEFAULT_WASM_DIR: &str = "wasm";
/// The WASM checksums file contains the hashes of built WASMs. It is inside the
/// WASM dir.
pub const DEFAULT_WASM_CHECKSUMS_FILE: &str = "checksums.json";
/// The WASM checksums gas file contains the gas cost of built WASMs. it is inside
/// the WASM dir.
pub const DEFAULT_WASM_CHECKSUMS_GAS_FILE: &str = "gas_checksums.json";
/// Chain-specific Namada configuration. Nested in chain dirs.
pub const FILENAME: &str = "config.toml";
/// Chain-specific Tendermint configuration. Nested in chain dirs.
Expand Down
11 changes: 7 additions & 4 deletions apps/src/lib/wasm_loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum Error {
/// including SHA256 hash
#[derive(Debug, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Checksums(pub HashMap<String, String>);
pub struct Checksums(pub HashMap<String, HashMap<String, String>>);

const S3_URL: &str = "https://namada-wasm-master.s3.eu-west-1.amazonaws.com";

Expand Down Expand Up @@ -132,8 +132,9 @@ pub async fn pre_fetch_wasm(wasm_directory: impl AsRef<Path>) {
// load json with wasm hashes
let checksums = Checksums::read_checksums_async(&wasm_directory).await;

join_all(checksums.0.into_iter().map(|(name, full_name)| {
join_all(checksums.0.into_iter().map(|(name, map)| {
let wasm_directory = wasm_directory.as_ref().to_owned();
let full_name = map.get("hash").unwrap().to_owned();

// Async check and download (if needed) each file
tokio::spawn(async move {
Expand Down Expand Up @@ -267,8 +268,10 @@ pub fn read_wasm(
if let Some(os_name) = file_path.as_ref().file_name() {
if let Some(name) = os_name.to_str() {
let wasm_path = match checksums.0.get(name) {
Some(wasm_filename) => {
wasm_directory.as_ref().join(wasm_filename)
Some(map) => {
wasm_directory.as_ref().join(map.get("hash").ok_or_else(
|| eyre!("Missing hash field in checksum"),
)?)
}
None => {
if !file_path.as_ref().is_absolute() {
Expand Down
28 changes: 15 additions & 13 deletions tests/src/e2e/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +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));
genesis.parameters.gas_table = Some(get_all_wasms_gas(&working_dir));

// Run the provided function on it
let genesis = update_genesis(genesis);
Expand Down Expand Up @@ -918,15 +918,16 @@ pub fn get_all_wasms_hashes(
) -> Vec<String> {
let checksums_path = working_dir.join("wasm/checksums.json");
let checksums_content = fs::read_to_string(checksums_path).unwrap();
let checksums: HashMap<String, String> =
let mut checksums: HashMap<String, HashMap<String, String>> =
serde_json::from_str(&checksums_content).unwrap();
let filter_prefix = filter.unwrap_or_default();
checksums
.values()
.values_mut()
.filter_map(|wasm| {
if wasm.contains(filter_prefix) {
let hash = wasm.get_mut("hash").expect("Missing hash in checksum");
if hash.contains(filter_prefix) {
Some(
wasm.split('.').collect::<Vec<&str>>()[1]
hash.split('.').collect::<Vec<&str>>()[1]
.to_owned()
.to_lowercase(),
)
Expand All @@ -937,19 +938,20 @@ 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");
let gas: BTreeMap<String, u64> =
serde_json::from_reader(fs::File::open(gas_checksums_path).unwrap())
pub fn get_all_wasms_gas(working_dir: &Path) -> BTreeMap<String, u64> {
let checksums_path = working_dir.join("wasm/checksums.json");
let checksums: HashMap<String, HashMap<String, String>> =
serde_json::from_reader(fs::File::open(checksums_path).unwrap())
.unwrap();

gas.into_iter()
.map(|(full_name, gas)| {
checksums
.values()
.map(|map| {
(
full_name.split('.').collect::<Vec<&str>>()[1]
map.get("hash").unwrap().split('.').collect::<Vec<&str>>()[1]
.to_owned()
.to_lowercase(),
gas,
map.get("gas").unwrap().parse::<u64>().unwrap(),
)
})
.collect()
Expand Down
23 changes: 6 additions & 17 deletions wasm/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os

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

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

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

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

# Prune unused gas entries if needed (in case of a tx/vp removal)
for k in list(gas.keys()):
if k not in checksums:
del gas[k]
extended_file_name = "{}.{}.wasm".format(
file_name, hashlib.sha256(open(wasm, "rb").read()).hexdigest()
)
checksums[file_key] = {"hash": extended_file_name, "gas": str(gas[file_key])}

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)
os.rename(wasm, "wasm/{}".format(checksums[file_key]["hash"]))

updated_wasms = list(checksums.values())
updated_wasms = [value["hash"] for value in checksums.values()]

for wasm in sorted(glob.glob("wasm/*.wasm")):
basename = os.path.basename(wasm)
Expand Down
20 changes: 0 additions & 20 deletions wasm/gas_checksums.json

This file was deleted.

0 comments on commit 8103c26

Please sign in to comment.