Skip to content

Commit 4dfda24

Browse files
authored
Fix upload simulation (#1671)
### What Fixes simulation due to #1663
1 parent b351f88 commit 4dfda24

3 files changed

Lines changed: 22 additions & 38 deletions

File tree

soroban-env-host/src/host/frame.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -859,26 +859,22 @@ impl Host {
859859
if self.in_storage_recording_mode()? {
860860
if let Some((parsed_module, wasmi_linker)) =
861861
self.budget_ref().with_observable_shadow_mode(|| {
862-
use crate::vm::ParsedModule;
862+
use crate::vm::{ParsedModule, VersionedContractCodeCostInputs};
863863
let wasm_key = self.contract_code_ledger_key(wasm_hash)?;
864864
let is_key_live_in_snapshot = self
865865
.try_borrow_storage_mut()?
866866
.is_key_live_in_snapshot(self, &wasm_key)?;
867867
if is_key_live_in_snapshot {
868868
let (code, _costs) = self.retrieve_wasm_from_storage(&wasm_hash)?;
869-
// Currently only v0 costs are used by the inter-ledger
870-
// module cache. Note, that when key is not in the live
871-
// snapshot, the inter-ledger cache won't be used
872-
// either, so we'll end up in the "cache miss" case
873-
// below and then correctly charge the instantiation
874-
// costs in `Vm::new_with_cost_inputs`.
875-
let costs_v0 = crate::vm::VersionedContractCodeCostInputs::V0 {
876-
wasm_bytes: code.len(),
877-
};
869+
// Always extract and use v1 cost inputs, matching what
870+
// the real module cache does in enforcement mode via
871+
// `parse_and_cache_module_simple`.
872+
let cost_inputs =
873+
ParsedModule::extract_refined_contract_cost_inputs(self, &code)?;
878874
let parsed_module = ParsedModule::new_with_isolated_engine(
879875
self,
880876
code.as_slice(),
881-
costs_v0,
877+
VersionedContractCodeCostInputs::V1(cost_inputs),
882878
)?;
883879
let wasmi_linker = parsed_module.make_wasmi_linker(self)?;
884880
Ok(Some((parsed_module, wasmi_linker)))

soroban-env-host/src/test/e2e_tests.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::e2e_testutils::{
55
upload_wasm_host_fn,
66
};
77
use crate::testutils::simple_account_sign_fn;
8-
use crate::vm::VersionedContractCodeCostInputs;
98
use crate::{
109
budget::{AsBudget, Budget},
1110
builtin_contracts::testutils::TestSigner,
@@ -394,18 +393,7 @@ fn build_module_cache_for_entries(
394393
if restored_contracts.contains(&contract_id) {
395394
continue;
396395
}
397-
// Currently module cache always uses V0 cost inputs, so the parsed
398-
// module will also use them during instantiation.
399-
let code_cost_inputs = VersionedContractCodeCostInputs::V0 {
400-
wasm_bytes: cd.code.len(),
401-
};
402-
cache.parse_and_cache_module(
403-
&ctx,
404-
ledger_info.protocol_version,
405-
&contract_id,
406-
&cd.code,
407-
code_cost_inputs,
408-
)?;
396+
cache.parse_and_cache_module_simple(&ctx, ledger_info.protocol_version, &cd.code)?;
409397
}
410398
}
411399
Ok(cache)
@@ -1397,7 +1385,7 @@ fn test_create_contract_success_in_recording_mode() {
13971385
]
13981386
);
13991387
assert_eq!(res.auth, vec![cd.auth_entry]);
1400-
expect!["639648"].assert_eq(&res.resources.instructions.to_string());
1388+
expect!["663629"].assert_eq(&res.resources.instructions.to_string());
14011389
expect!["104"].assert_eq(&res.resources.write_bytes.to_string());
14021390
assert_eq!(
14031391
res.resources,
@@ -1535,7 +1523,7 @@ fn test_create_contract_success_in_recording_mode_with_custom_account() {
15351523
]
15361524
);
15371525
assert_eq!(res.auth, vec![cd.auth_entry]);
1538-
expect!["1046806"].assert_eq(&res.resources.instructions.to_string());
1526+
expect!["1070787"].assert_eq(&res.resources.instructions.to_string());
15391527
expect!["176"].assert_eq(&res.resources.write_bytes.to_string());
15401528
assert_eq!(
15411529
res.resources,
@@ -1606,7 +1594,7 @@ fn test_create_contract_success_in_recording_mode_with_enforced_auth() {
16061594
]
16071595
);
16081596
assert_eq!(res.auth, vec![cd.auth_entry]);
1609-
expect!["641135"].assert_eq(&res.resources.instructions.to_string());
1597+
expect!["665116"].assert_eq(&res.resources.instructions.to_string());
16101598
expect!["104"].assert_eq(&res.resources.write_bytes.to_string());
16111599
assert_eq!(
16121600
res.resources,
@@ -2045,7 +2033,7 @@ fn test_invoke_contract_with_storage_ops_success_in_recording_mode() {
20452033
]
20462034
);
20472035
assert!(res.restored_rw_entry_ids.is_empty());
2048-
expect!["791047"].assert_eq(&res.resources.instructions.to_string());
2036+
expect!["898006"].assert_eq(&res.resources.instructions.to_string());
20492037
expect!["80"].assert_eq(&res.resources.write_bytes.to_string());
20502038
assert_eq!(
20512039
res.resources,
@@ -2112,7 +2100,7 @@ fn test_invoke_contract_with_storage_ops_success_in_recording_mode() {
21122100
wasm_entry_change.clone()
21132101
]
21142102
);
2115-
expect!["902901"].assert_eq(&extend_res.resources.instructions.to_string());
2103+
expect!["1009860"].assert_eq(&extend_res.resources.instructions.to_string());
21162104
assert_eq!(
21172105
extend_res.resources,
21182106
SorobanResources {
@@ -2555,7 +2543,7 @@ fn test_auto_restore_with_overwrite_in_recording_mode() {
25552543
]
25562544
);
25572545

2558-
expect!["921385"].assert_eq(&res.resources.instructions.to_string());
2546+
expect!["1028344"].assert_eq(&res.resources.instructions.to_string());
25592547
assert_eq!(
25602548
res.resources,
25612549
SorobanResources {

soroban-simulation/src/test/simulation.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ fn test_simulate_create_contract() {
340340
);
341341
assert!(res.contract_events.is_empty());
342342
assert!(res.diagnostic_events.is_empty());
343-
expect!["2377272"].assert_eq(&res.simulated_instructions.to_string());
343+
expect!["2742756"].assert_eq(&res.simulated_instructions.to_string());
344344
expect!["104"].assert_eq(
345345
&res.transaction_data
346346
.as_ref()
@@ -349,7 +349,7 @@ fn test_simulate_create_contract() {
349349
.write_bytes
350350
.to_string(),
351351
);
352-
expect!["12931"].assert_eq(
352+
expect!["13296"].assert_eq(
353353
&res.transaction_data
354354
.as_ref()
355355
.unwrap()
@@ -372,7 +372,7 @@ fn test_simulate_create_contract() {
372372
resource_fee: res.transaction_data.as_ref().unwrap().resource_fee,
373373
})
374374
);
375-
expect!["1188635"].assert_eq(&res.simulated_memory.to_string());
375+
expect!["1371376"].assert_eq(&res.simulated_memory.to_string());
376376
assert_eq!(
377377
res.modified_entries,
378378
vec![LedgerEntryDiff {
@@ -497,7 +497,7 @@ fn test_simulate_invoke_contract_with_auth() {
497497
assert!(res.contract_events.is_empty());
498498
assert!(!res.diagnostic_events.is_empty());
499499

500-
expect!["39120150"].assert_eq(&res.simulated_instructions.to_string());
500+
expect!["41453957"].assert_eq(&res.simulated_instructions.to_string());
501501
expect!["144"].assert_eq(
502502
&res.transaction_data
503503
.as_ref()
@@ -514,7 +514,7 @@ fn test_simulate_invoke_contract_with_auth() {
514514
.write_bytes
515515
.to_string(),
516516
);
517-
expect!["114064"].assert_eq(
517+
expect!["116397"].assert_eq(
518518
&res.transaction_data
519519
.as_ref()
520520
.unwrap()
@@ -557,7 +557,7 @@ fn test_simulate_invoke_contract_with_auth() {
557557
resource_fee: res.transaction_data.as_ref().unwrap().resource_fee,
558558
})
559559
);
560-
expect!["19560059"].assert_eq(&res.simulated_memory.to_string());
560+
expect!["20726952"].assert_eq(&res.simulated_memory.to_string());
561561
assert_eq!(
562562
res.modified_entries,
563563
vec![LedgerEntryDiff {
@@ -1435,15 +1435,15 @@ fn test_simulate_unsuccessful_sac_call_with_try_call() {
14351435
issuer: AccountId(PublicKey::PublicKeyTypeEd25519(Uint256([0; 32]))),
14361436
}),
14371437
});
1438-
expect!["5227433"].assert_eq(
1438+
expect!["5592955"].assert_eq(
14391439
&res.transaction_data
14401440
.as_ref()
14411441
.unwrap()
14421442
.resources
14431443
.instructions
14441444
.to_string(),
14451445
);
1446-
expect!["5576"].assert_eq(
1446+
expect!["5941"].assert_eq(
14471447
&res.transaction_data
14481448
.as_ref()
14491449
.unwrap()

0 commit comments

Comments
 (0)