Skip to content

Commit

Permalink
Merge branch 'grarco/gas-minor-adjustments' (#2982)
Browse files Browse the repository at this point in the history
* origin/grarco/gas-minor-adjustments:
  Changelog #2982
  Simplify gas metering in code fetch
  • Loading branch information
tzemanovic committed Apr 3, 2024
2 parents fcb91cc + 88a6457 commit d53b460
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Simplified gas metering for code compilation and validation.
([\#2982](https://github.com/anoma/namada/pull/2982))
68 changes: 30 additions & 38 deletions crates/namada/src/vm/wasm/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,64 +535,56 @@ where
{
match code_or_hash {
Commitment::Hash(code_hash) => {
let (module, store, tx_len) = match wasm_cache.fetch(code_hash)? {
Some((module, store)) => {
// Gas accounting even if the compiled module is in cache
let key = Key::wasm_code_len(code_hash);
let tx_len = state
.read::<u64>(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code length failed from storage: \
key {}, error {}",
key, e
))
})?
.ok_or_else(|| {
Error::LoadWasmCode(format!(
"No wasm code length in storage: key {}",
key
))
})?;
let code_len_key = Key::wasm_code_len(code_hash);
let tx_len = state
.read::<u64>(&code_len_key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code length failed: key {code_len_key}, \
error {e}"
))
})?
.ok_or_else(|| {
Error::LoadWasmCode(format!(
"No wasm code length in storage: key {code_len_key}"
))
})?;

// Gas accounting in any case, even if the compiled module is in
// cache
gas_meter
.borrow_mut()
.add_wasm_load_from_storage_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
gas_meter
.borrow_mut()
.add_compiling_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;

(module, store, tx_len)
}
let (module, store) = match wasm_cache.fetch(code_hash)? {
Some((module, store)) => (module, store),
None => {
let key = Key::wasm_code(code_hash);
let code = state
.read_bytes(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code failed from storage: key {}, \
error {}",
key, e
"Read wasm code failed: key {key}, error {e}"
))
})?
.ok_or_else(|| {
Error::LoadWasmCode(format!(
"No wasm code in storage: key {}",
key
"No wasm code in storage: key {key}"
))
})?;

let tx_len = u64::try_from(code.len())
.map_err(|e| Error::ConversionError(e.to_string()))?;

match wasm_cache.compile_or_fetch(code)? {
Some((module, store)) => (module, store, tx_len),
Some((module, store)) => (module, store),
None => return Err(Error::NoCompiledWasmCode),
}
}
};

gas_meter
.borrow_mut()
.add_wasm_load_from_storage_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
gas_meter
.borrow_mut()
.add_compiling_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
Ok((module, store))
}
Commitment::Id(code) => {
Expand Down

0 comments on commit d53b460

Please sign in to comment.