Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile gas adjustments #2982

Merged
merged 27 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e6ef593
Removes whitelisted gas and sets a constant for the runtime gas meter
grarco Mar 7, 2024
592981c
Gas for ibc tx and removes unused host fn for whitelisted gas
grarco Mar 7, 2024
4571652
Adds unit tests for out of gas
grarco Mar 7, 2024
bbec083
Changes docstrings for signature verification
grarco Mar 7, 2024
e46164b
Adds wasm opcodes benchmarks
grarco Mar 8, 2024
a1a76ec
Benchmarks for shielded actions
grarco Mar 9, 2024
3d979c8
Removes benchmark for vp validator
grarco Mar 9, 2024
feed8ee
Clippy + fmt
grarco Mar 9, 2024
e811ead
Removes testing dependency from the benchmarks crate
grarco Mar 9, 2024
d256359
Updates masp vp checks
grarco Mar 10, 2024
84eee96
Higher resolution gas metering for masp
grarco Mar 10, 2024
aecad82
Fixes make clippy for benchmarks
grarco Mar 10, 2024
e71add1
Updates beatch size for benchmarks
grarco Mar 12, 2024
fc2d769
Renames vp user benchmark
grarco Mar 12, 2024
ec2f97b
Removes old comment
grarco Mar 14, 2024
217c354
Iterations and improvements for wasm opcodes benchmarks
grarco Mar 14, 2024
dff2cb1
Singlepass compiler for wasm benchmarks
grarco Mar 17, 2024
75859ff
Updates gas costs
grarco Mar 17, 2024
bdf62dd
Gas metering in `PseudoExecutionContext`
grarco Mar 18, 2024
571dba2
Increases gas scale
grarco Mar 18, 2024
e225cb5
Increases gas limits in unit tests
grarco Mar 18, 2024
d591d3c
Changelog #2838
grarco Mar 18, 2024
937682f
Adds source for wasm gas costs
grarco Mar 20, 2024
57cb950
Empty loop in out of gas unit tests
grarco Mar 25, 2024
eb302a8
Test that native VPs correctly run out of gas
sug0 Mar 21, 2024
cc9324f
Simplify gas metering in code fetch
grarco Apr 2, 2024
88a6457
Changelog #2982
grarco Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
110 changes: 41 additions & 69 deletions crates/namada/src/vm/wasm/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::num::NonZeroU32;
use borsh::BorshDeserialize;
use namada_core::validity_predicate::VpSentinel;
use namada_gas::{GasMetering, TxGasMeter, WASM_MEMORY_PAGE_GAS};
use namada_state::write_log::StorageModification;
use namada_state::{DBIter, State, StateRead, StorageHasher, DB};
use namada_tx::data::TxSentinel;
use namada_tx::{Commitment, Section, Tx};
Expand Down Expand Up @@ -505,83 +504,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 = match state.write_log().read(&key).0 {
Some(StorageModification::Write { value }) => {
u64::try_from_slice(value).map_err(|e| {
Error::ConversionError(e.to_string())
})
}
_ => match state
.db_read(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code length failed from \
storage: key {}, error {}",
key, e
))
})?
.0
{
Some(v) => u64::try_from_slice(&v).map_err(|e| {
Error::ConversionError(e.to_string())
}),
None => Err(Error::LoadWasmCode(format!(
"No wasm code length in storage: key {}",
key
))),
},
}?;

(module, store, tx_len)
}
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()))?;

let (module, store) = match wasm_cache.fetch(code_hash)? {
Some((module, store)) => (module, store),
None => {
let key = Key::wasm_code(code_hash);
let code = match state.write_log().read(&key).0 {
Some(StorageModification::Write { value }) => {
value.clone()
}
_ => match state
.db_read(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code failed from storage: key \
{}, error {}",
key, e
))
})?
.0
{
Some(v) => v,
None => {
return Err(Error::LoadWasmCode(format!(
"No wasm code in storage: key {}",
key
)));
}
},
};
let tx_len = u64::try_from(code.len())
.map_err(|e| Error::ConversionError(e.to_string()))?;
let code = state
.read_bytes(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code failed: key {key}, error {e}"
))
})?
.ok_or_else(|| {
Error::LoadWasmCode(format!(
"No wasm code in storage: key {key}"
))
})?;

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
Loading