Skip to content

Commit

Permalink
Enable invocation metering by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkozh committed Dec 17, 2024
1 parent 0c44618 commit 9c4658d
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 209 deletions.
8 changes: 7 additions & 1 deletion soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl Env {
}
})))
.unwrap();
env_impl.enable_invocation_metering();

let env = Env {
env_impl,
Expand Down Expand Up @@ -593,7 +594,12 @@ impl Env {
/// 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.
/// 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())
}
Expand Down
1 change: 0 additions & 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 Down
38 changes: 0 additions & 38 deletions soroban-sdk/src/tests/budget.rs

This file was deleted.

119 changes: 118 additions & 1 deletion soroban-sdk/src/tests/cost_estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate as soroban_sdk;
use expect_test::expect;
use soroban_sdk::Env;
use soroban_sdk_macros::symbol_short;
use stellar_xdr::curr::ContractCostType;

mod contract_data {
use crate as soroban_sdk;
Expand All @@ -12,7 +13,6 @@ mod contract_data {
#[test]
fn test_cost_estimate_with_storage() {
let e = Env::default();
e.cost_estimate().enable();

let contract_id = e.register(contract_data::WASM, ());
let client = contract_data::Client::new(&e, &contract_id);
Expand Down Expand Up @@ -144,3 +144,120 @@ fn test_cost_estimate_with_storage() {
}"#]]
.assert_eq(format!("{:#?}", e.cost_estimate().fee()).as_str());
}

#[test]
fn test_cost_estimate_budget() {
let e = Env::default();

let contract_id = e.register(contract_data::WASM, ());
let client = contract_data::Client::new(&e, &contract_id);

client.put(&symbol_short!("k1"), &symbol_short!("v1"));

// Budget breakdown corresponds to the last invocation only.
expect![[r#"
===============================================================================================================================================================================
Cpu limit: 100000000; used: 455853
Mem limit: 41943040; used: 1162241
===============================================================================================================================================================================
CostType iterations input cpu_insns mem_bytes const_term_cpu lin_term_cpu const_term_mem lin_term_mem
WasmInsnExec 284 None 1136 0 4 0 0 0
MemAlloc 27 Some(1052425) 143269 1052857 434 16 16 128
MemCpy 95 Some(9665) 5186 0 42 16 0 0
MemCmp 43 Some(1049) 2012 0 44 16 0 0
DispatchHostFunction 1 None 310 0 310 0 0 0
VisitObject 2 None 122 0 61 0 0 0
ValSer 0 Some(0) 0 0 230 29 242 384
ValDeser 0 Some(0) 0 0 59052 4001 0 384
ComputeSha256Hash 1 Some(0) 3738 0 3738 7012 0 0
ComputeEd25519PubKey 0 None 0 0 40253 0 0 0
VerifyEd25519Sig 0 Some(0) 0 0 377524 4068 0 0
VmInstantiation 0 Some(0) 0 0 451626 45405 130065 5064
VmCachedInstantiation 0 Some(0) 0 0 41142 634 69472 1217
InvokeVmFunction 1 None 1948 14 1948 0 14 0
ComputeKeccak256Hash 0 Some(0) 0 0 3766 5969 0 0
DecodeEcdsaCurve256Sig 0 None 0 0 710 0 0 0
RecoverEcdsaSecp256k1Key 0 None 0 0 2315295 0 181 0
Int256AddSub 0 None 0 0 4404 0 99 0
Int256Mul 0 None 0 0 4947 0 99 0
Int256Div 0 None 0 0 4911 0 99 0
Int256Pow 0 None 0 0 4286 0 99 0
Int256Shift 0 None 0 0 913 0 99 0
ChaCha20DrawBytes 0 Some(0) 0 0 1058 501 0 0
ParseWasmInstructions 1 Some(137) 100273 24475 73077 25410 17564 6457
ParseWasmFunctions 1 Some(5) 21123 1854 0 540752 0 47464
ParseWasmGlobals 1 Some(3) 4133 314 0 176363 0 13420
ParseWasmTableEntries 1 Some(0) 0 0 0 29989 0 6285
ParseWasmTypes 1 Some(5) 41462 2526 0 1061449 0 64670
ParseWasmDataSegments 1 Some(0) 0 0 0 237336 0 29074
ParseWasmElemSegments 1 Some(0) 0 0 0 328476 0 48095
ParseWasmImports 1 Some(4) 21932 3225 0 701845 0 103229
ParseWasmExports 1 Some(7) 23481 1990 0 429383 0 36394
ParseWasmDataSegmentBytes 1 Some(0) 0 0 0 28 0 257
InstantiateWasmInstructions 1 None 43030 70704 43030 0 70704 0
InstantiateWasmFunctions 1 Some(5) 295 570 0 7556 0 14613
InstantiateWasmGlobals 1 Some(3) 251 160 0 10711 0 6833
InstantiateWasmTableEntries 1 Some(0) 0 0 0 3300 0 1025
InstantiateWasmTypes 1 None 0 0 0 0 0 0
InstantiateWasmDataSegments 1 Some(0) 0 0 0 23038 0 129632
InstantiateWasmElemSegments 1 Some(0) 0 0 0 42488 0 13665
InstantiateWasmImports 1 Some(4) 25905 3051 0 828974 0 97637
InstantiateWasmExports 1 Some(7) 16247 501 0 297100 0 9176
InstantiateWasmDataSegmentBytes 1 Some(0) 0 0 0 14 0 126
Sec1DecodePointUncompressed 0 None 0 0 1882 0 0 0
VerifyEcdsaSecp256r1Sig 0 None 0 0 3000906 0 0 0
Bls12381EncodeFp 0 None 0 0 661 0 0 0
Bls12381DecodeFp 0 None 0 0 985 0 0 0
Bls12381G1CheckPointOnCurve 0 None 0 0 1934 0 0 0
Bls12381G1CheckPointInSubgroup 0 None 0 0 730510 0 0 0
Bls12381G2CheckPointOnCurve 0 None 0 0 5921 0 0 0
Bls12381G2CheckPointInSubgroup 0 None 0 0 1057822 0 0 0
Bls12381G1ProjectiveToAffine 0 None 0 0 92642 0 0 0
Bls12381G2ProjectiveToAffine 0 None 0 0 100742 0 0 0
Bls12381G1Add 0 None 0 0 7689 0 0 0
Bls12381G1Mul 0 None 0 0 2458985 0 0 0
Bls12381G1Msm 0 Some(0) 0 0 2426722 96397671 109494 354667
Bls12381MapFpToG1 0 None 0 0 1541554 0 5552 0
Bls12381HashToG1 0 Some(0) 0 0 3211191 6713 9424 0
Bls12381G2Add 0 None 0 0 25207 0 0 0
Bls12381G2Mul 0 None 0 0 7873219 0 0 0
Bls12381G2Msm 0 Some(0) 0 0 8035968 309667335 219654 354667
Bls12381MapFp2ToG2 0 None 0 0 2420202 0 3344 0
Bls12381HashToG2 0 Some(0) 0 0 7050564 6797 6816 0
Bls12381Pairing 0 Some(0) 0 0 10558948 632860943 2204 9340474
Bls12381FrFromU256 0 None 0 0 1994 0 0 0
Bls12381FrToU256 0 None 0 0 1155 0 248 0
Bls12381FrAddSub 0 None 0 0 74 0 0 0
Bls12381FrMul 0 None 0 0 332 0 0 0
Bls12381FrPow 0 Some(0) 0 0 691 74558 0 128
Bls12381FrInv 0 None 0 0 35421 0 0 0
===============================================================================================================================================================================
Internal details (diagnostics info, does not affect fees)
Total # times meter was called: 192
Shadow cpu limit: 100000000; used: 34332
Shadow mem limit: 41943040; used: 27725
===============================================================================================================================================================================
"#]]
.assert_eq(format!("{:#?}", e.cost_estimate().budget()).as_str());

// Data for a specific cost type (one row in the budget table above) can be
// obtained as well via `tracker` function.
expect![[r#"
CostTracker {
iterations: 284,
inputs: None,
cpu: 1136,
mem: 0,
}"#]]
.assert_eq(
format!(
"{:#?}",
e.cost_estimate()
.budget()
.tracker(ContractCostType::WasmInsnExec)
)
.as_str(),
);
}
9 changes: 4 additions & 5 deletions soroban-sdk/src/tests/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ fn default_and_from_snapshot_same_settings() {

c1client.test();
c2client.test();
let logs1 = env1.logs().all();
let logs2 = env2.logs().all();
assert_eq!(logs1, &["[Diagnostic Event] contract:CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM, topics:[log], data:\"test\""]);
assert_eq!(logs2, &["[Diagnostic Event] contract:CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM, topics:[log], data:\"test\""]);

let c1addr2 = Address::generate(&env1);
let c2addr2 = Address::generate(&env2);
Expand All @@ -71,11 +75,6 @@ fn default_and_from_snapshot_same_settings() {
ScErrorCode::InvalidAction
)))
);

let logs1 = env1.logs().all();
let logs2 = env2.logs().all();
assert_eq!(logs1, &["[Diagnostic Event] contract:CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM, topics:[log], data:\"test\""]);
assert_eq!(logs2, &["[Diagnostic Event] contract:CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM, topics:[log], data:\"test\""]);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/proptest_scval_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ proptest! {
prop_assert_eq!(scval_partial_eq, rawval_cmp_is_eq);

// Compare<ScVal> for Budget
let budget = env.budget().0;
let budget = env.cost_estimate().budget().0;
let scval_budget_cmp = budget.compare(&scval_1, &scval_2).expect("cmp");

if scval_budget_cmp != scval_cmp {
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/proptest_val_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ proptest! {
rawval_proto_2 in arb::<<Val as SorobanArbitrary>::Prototype>(),
) {
let env = &Env::default();
let budget = env.budget().0;
let budget = env.cost_estimate().budget().0;

let rawval_1 = Val::from_val(env, &rawval_proto_1);
let rawval_2 = Val::from_val(env, &rawval_proto_2);
Expand Down
4 changes: 2 additions & 2 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ pub mod budget {
/// # #[cfg(feature = "testutils")]
/// # fn main() {
/// # let env = Env::default();
/// env.budget().reset_default();
/// env.cost_estimate().budget().reset_default();
/// // ...
/// println!("{}", env.budget());
/// println!("{}", env.cost_estimate().budget());
/// # }
/// # #[cfg(not(feature = "testutils"))]
/// # fn main() { }
Expand Down
Loading

0 comments on commit 9c4658d

Please sign in to comment.