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

Expose invocation metering in the SDK. #1411

Merged
merged 15 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions soroban-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ derive_arbitrary = { version = "~1.3.0" }
proptest = "1.2.0"
proptest-arbitrary-interop = "0.1.0"
libfuzzer-sys = "0.4.7"
expect-test = "1.4.1"
dmkozh marked this conversation as resolved.
Show resolved Hide resolved

[features]
alloc = []
Expand Down
29 changes: 29 additions & 0 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ impl Env {
}
}

#[cfg(any(test, feature = "testutils"))]
use crate::testutils::cost_estimate::CostEstimate;
#[cfg(any(test, feature = "testutils"))]
use crate::{
auth,
Expand Down Expand Up @@ -560,6 +562,7 @@ impl Env {
}
})))
.unwrap();
env_impl.enable_invocation_metering();

let env = Env {
env_impl,
Expand All @@ -576,6 +579,31 @@ impl Env {
env
}

/// Returns the resources metered during the last top level contract
/// invocation.
///
/// In order to get non-`None` results, `enable_invocation_metering` has to
/// be called and at least one invocation has to happen after that.
///
/// Take the return value with a grain of salt. The returned resources mostly
/// correspond only to the operations that have happened during the host
/// invocation, i.e. this won't try to simulate the work that happens in
/// production scenarios (e.g. certain XDR rountrips). This also doesn't try
/// to model resources related to the transaction size.
///
/// The returned value is as useful as the preceding setup, e.g. if a test
/// contract is used instead of a Wasm contract, all the costs related to
/// VM instantiation and execution, as well as Wasm reads/rent bumps will be
/// missed.
///
/// While the resource metering may be useful for contract optimization,
/// keep in mind that resource and fee estimation may be imprecise. Use
/// simulation with RPC in order to get the exact resources for submitting
/// the transactions to the network.
pub fn cost_estimate(&self) -> CostEstimate {
CostEstimate::new(self.clone())
}

/// Register a contract with the [Env] for testing.
///
/// Pass the contract type when the contract is defined in the current crate
Expand Down Expand Up @@ -1611,6 +1639,7 @@ impl Env {
}

/// Get the budget that tracks the resources consumed for the environment.
#[deprecated(note = "use cost_estimate().detailed_metering()")]
pub fn budget(&self) -> Budget {
Budget::new(self.env_impl.budget_cloned())
}
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod address;
mod auth;
mod budget;
mod bytes_alloc_vec;
mod bytes_buffer;
mod contract_add_i32;
Expand All @@ -25,6 +24,7 @@ mod contract_udt_struct;
mod contract_udt_struct_tuple;
mod contractimport;
mod contractimport_with_error;
mod cost_estimate;
mod crypto_bls12_381;
mod crypto_ed25519;
mod crypto_keccak256;
Expand Down
38 changes: 0 additions & 38 deletions soroban-sdk/src/tests/budget.rs

This file was deleted.

Loading
Loading