Skip to content

Commit

Permalink
Merge branch 'grarco/runtime-gas-meter' (#2838)
Browse files Browse the repository at this point in the history
* origin/grarco/runtime-gas-meter:
  Test that native VPs correctly run out of gas
  Empty loop in out of gas unit tests
  Adds source for wasm gas costs
  Changelog #2838
  Increases gas limits in unit tests
  Increases gas scale
  Gas metering in `PseudoExecutionContext`
  Updates gas costs
  Singlepass compiler for wasm benchmarks
  Iterations and improvements for wasm opcodes benchmarks
  Removes old comment
  Renames vp user benchmark
  Updates beatch size for benchmarks
  Fixes make clippy for benchmarks
  Higher resolution gas metering for masp
  Updates masp vp checks
  Removes testing dependency from the benchmarks crate
  Clippy + fmt
  Removes benchmark for vp validator
  Benchmarks for shielded actions
  Adds wasm opcodes benchmarks
  Changes docstrings for signature verification
  Adds unit tests for out of gas
  Gas for ibc tx and removes unused host fn for whitelisted gas
  Removes whitelisted gas and sets a constant for the runtime gas meter

# Conflicts:
#	crates/apps/src/lib/bench_utils.rs
#	crates/apps/src/lib/node/ledger/shell/finalize_block.rs
#	crates/benches/Cargo.toml
#	crates/benches/native_vps.rs
#	crates/benches/txs.rs
#	crates/core/Cargo.toml
#	crates/macros/src/lib.rs
#	crates/namada/Cargo.toml
#	crates/namada/src/ledger/protocol/mod.rs
#	crates/namada/src/vm/host_env.rs
#	crates/namada/src/vm/wasm/run.rs
#	crates/state/Cargo.toml
#	crates/test_utils/src/lib.rs
#	wasm/wasm_source/src/tx_ibc.rs
#	wasm_for_tests/tx_fail.wasm
#	wasm_for_tests/tx_memory_limit.wasm
#	wasm_for_tests/tx_mint_tokens.wasm
#	wasm_for_tests/tx_no_op.wasm
#	wasm_for_tests/tx_proposal_code.wasm
#	wasm_for_tests/tx_proposal_masp_reward.wasm
#	wasm_for_tests/tx_read_storage_key.wasm
#	wasm_for_tests/tx_write.wasm
#	wasm_for_tests/tx_write_storage_key.wasm
#	wasm_for_tests/vp_always_false.wasm
#	wasm_for_tests/vp_always_true.wasm
#	wasm_for_tests/vp_eval.wasm
#	wasm_for_tests/vp_memory_limit.wasm
#	wasm_for_tests/vp_read_storage_key.wasm
#	wasm_for_tests/wasm_source/Cargo.toml
#	wasm_for_tests/wasm_source/Makefile
  • Loading branch information
tzemanovic committed Apr 11, 2024
2 parents 134be07 + eb302a8 commit 7beccde
Show file tree
Hide file tree
Showing 75 changed files with 2,032 additions and 663 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2838-runtime-gas-meter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Improved the gas metering system to track gas at runtime in wasm.
([\#2838](https://github.com/anoma/namada/pull/2838))
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ tracing-appender = "0.2.2"
tracing-log = "0.1.2"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}
wasmparser = "0.107.0"
wasm-instrument = {version = "0.4.0", features = ["sign_ext"]}
wasmer = {git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b"}
wasmer-compiler-singlepass = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
wasmer-engine-universal = { git = "https://github.com/heliaxdev/wasmer", rev = "255054f7f58b7b4a525f2fee6b9b86422d1ca15b" }
winapi = "0.3.9"
yansi = "0.5.1"
zeroize = { version = "1.5.5", features = ["zeroize_derive"] }
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ check-crates:

clippy-wasm = $(cargo) +$(nightly) clippy --manifest-path $(wasm)/Cargo.toml --all-targets -- -D warnings

# Need a separate command for benchmarks to prevent the "testing" feature flag from being activated
clippy:
$(cargo) +$(nightly) clippy $(jobs) --all-targets -- -D warnings && \
$(cargo) +$(nightly) clippy $(jobs) --all-targets --workspace --exclude namada_benchmarks -- -D warnings && \
$(cargo) +$(nightly) clippy $(jobs) --all-targets --package namada_benchmarks -- -D warnings && \
make -C $(wasms) clippy && \
make -C $(wasms_for_tests) clippy && \
$(foreach wasm,$(wasm_templates),$(clippy-wasm) && ) true
Expand Down Expand Up @@ -262,7 +264,7 @@ clean:
$(cargo) clean

bench:
$(cargo) bench --package namada_benchmarks
$(cargo) bench --package namada_benchmarks

build-doc:
$(cargo) doc --no-deps
Expand Down
2 changes: 1 addition & 1 deletion crates/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mainnet = [
]
# for integration tests and test utilities
testing = ["namada_test_utils"]
benches = ["testing", "namada_test_utils"]
benches = ["namada_test_utils"]
integration = []
jemalloc = ["rocksdb/jemalloc"]
migrations = [
Expand Down
69 changes: 69 additions & 0 deletions crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ impl BenchShell {
.unwrap();

self.inner.commit();
self.inner
.state
.in_mem_mut()
.set_header(get_dummy_header())
.unwrap();
}

// Commit a masp transaction and cache the tx and the changed keys for
Expand Down Expand Up @@ -1094,4 +1099,68 @@ impl BenchShieldedCtx {
};
(ctx, tx)
}

pub fn generate_shielded_action(
self,
amount: Amount,
source: TransferSource,
target: TransferTarget,
) -> (Self, Tx) {
let (ctx, tx) = self.generate_masp_tx(
amount,
source.clone(),
TransferTarget::Address(Address::Internal(InternalAddress::Ibc)),
);

let token = PrefixedCoin {
denom: address::testing::nam().to_string().parse().unwrap(),
amount: amount
.to_string_native()
.split('.')
.next()
.unwrap()
.to_string()
.parse()
.unwrap(),
};
let timeout_height = TimeoutHeight::At(IbcHeight::new(0, 100).unwrap());

#[allow(clippy::disallowed_methods)]
let now: namada::tendermint::Time =
DateTimeUtc::now().try_into().unwrap();
let now: IbcTimestamp = now.into();
let timeout_timestamp =
(now + std::time::Duration::new(3600, 0)).unwrap();
let msg = IbcMsgTransfer {
port_id_on_a: PortId::transfer(),
chan_id_on_a: ChannelId::new(5),
packet_data: PacketData {
token,
sender: source.effective_address().to_string().into(),
receiver: target.effective_address().to_string().into(),
memo: "".parse().unwrap(),
},
timeout_height_on_b: timeout_height,
timeout_timestamp_on_b: timeout_timestamp,
};

let transfer =
Transfer::deserialize(&mut tx.data().unwrap().as_slice()).unwrap();
let masp_tx = tx
.get_section(&transfer.shielded.unwrap())
.unwrap()
.masp_tx()
.unwrap();
let msg = MsgTransfer {
message: msg,
transfer: Some(transfer),
};

let mut ibc_tx = ctx
.shell
.generate_ibc_tx(TX_IBC_WASM, msg.serialize_to_vec());
ibc_tx.add_masp_tx_section(masp_tx);

(ctx, ibc_tx)
}
}
2 changes: 1 addition & 1 deletion crates/apps/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(rustdoc::private_intra_doc_links)]

#[cfg(feature = "testing")]
#[cfg(feature = "benches")]
pub mod bench_utils;
pub mod cli;
pub mod client;
Expand Down
6 changes: 5 additions & 1 deletion crates/apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ impl Shell {
tracing::debug!("Request InitChain");
self.init_chain(
init,
#[cfg(any(test, feature = "testing"))]
#[cfg(any(
test,
feature = "testing",
feature = "benches"
))]
1,
)
.map(Response::InitChain)
Expand Down
12 changes: 6 additions & 6 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ mod test_finalize_block {
FinalizeBlock, ProcessedTx,
};

const GAS_LIMIT_MULTIPLIER: u64 = 100_000_000;
const WRAPPER_GAS_LIMIT: u64 = 20_000;

/// Make a wrapper tx and a processed tx from the wrapped tx that can be
/// added to `FinalizeBlock` request.
Expand All @@ -784,7 +784,7 @@ mod test_finalize_block {
},
keypair.ref_to(),
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
WRAPPER_GAS_LIMIT.into(),
None,
))));
wrapper_tx.header.chain_id = shell.chain_id.clone();
Expand Down Expand Up @@ -2459,7 +2459,7 @@ mod test_finalize_block {
},
keypair.ref_to(),
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
WRAPPER_GAS_LIMIT.into(),
None,
))));
wrapper.header.chain_id = shell.chain_id.clone();
Expand All @@ -2474,7 +2474,7 @@ mod test_finalize_block {
},
keypair_2.ref_to(),
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
WRAPPER_GAS_LIMIT.into(),
None,
))));
new_wrapper.add_section(Section::Signature(Signature::new(
Expand Down Expand Up @@ -2590,7 +2590,7 @@ mod test_finalize_block {
},
keypair.ref_to(),
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
WRAPPER_GAS_LIMIT.into(),
None,
))));
unsigned_wrapper.header.chain_id = shell.chain_id.clone();
Expand Down Expand Up @@ -2816,7 +2816,7 @@ mod test_finalize_block {
},
keypair.ref_to(),
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
WRAPPER_GAS_LIMIT.into(),
None,
))));
wrapper.header.chain_id = shell.chain_id.clone();
Expand Down
3 changes: 2 additions & 1 deletion crates/apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ where
pub fn init_chain(
&mut self,
init: request::InitChain,
#[cfg(any(test, feature = "testing"))] _num_validators: u64,
#[cfg(any(test, feature = "testing", feature = "benches"))]
_num_validators: u64,
) -> Result<response::InitChain> {
let mut response = response::InitChain::default();
let chain_id = self.state.in_mem().chain_id.as_str();
Expand Down
13 changes: 12 additions & 1 deletion crates/benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,28 @@ name = "host_env"
harness = false
path = "host_env.rs"

[[bench]]
name = "wasm_opcodes"
harness = false
path = "wasm_opcodes.rs"

[dependencies]

# NOTE: this crate MUST NOT import any dependency with testing features to prevent benchmarking non-production code
[dev-dependencies]
namada = { path = "../namada", features = ["rand", "testing"] }
namada = { path = "../namada", features = ["rand", "benches"] }
namada_apps = { path = "../apps", features = ["benches"] }
masp_primitives.workspace = true
borsh.workspace = true
borsh-ext.workspace = true
criterion = { version = "0.5", features = ["html_reports"] }
lazy_static.workspace= true
prost.workspace = true
rand_core.workspace = true
rand.workspace = true
tempfile.workspace = true
sha2.workspace = true
wasm-instrument.workspace = true
wasmer-compiler-singlepass.workspace = true
wasmer-engine-universal.workspace = true
wasmer.workspace = true

0 comments on commit 7beccde

Please sign in to comment.