Skip to content

Commit

Permalink
Error if bumping past max_entry_expiration and and host function to r…
Browse files Browse the repository at this point in the history
…etrieve max_entry_expiration (#985)

* Error if bumping past max_entry_expiration

* Bump meta

* Expose max_expiration_ledger instead of max_entry_expiration

* Fixes
  • Loading branch information
sisuresh authored Aug 8, 2023
1 parent 2069b9c commit 4867132
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 12 deletions.
7 changes: 7 additions & 0 deletions soroban-env-common/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
"args": [],
"return": "AddressObject",
"docs": "Get the Address object for the current contract."
},
{
"export": "a",
"name": "get_max_expiration_ledger",
"args": [],
"return": "U32Val",
"docs": "Returns the max ledger sequence that an entry can live to (inclusive)."
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const ENV_META_V0_SECTION_NAME: &str = "contractenvmetav0";

soroban_env_macros::generate_env_meta_consts!(
ledger_protocol_version: 20,
pre_release_version: 52,
pre_release_version: 53,
);

pub fn get_ledger_protocol_version(interface_version: u64) -> u32 {
Expand Down
7 changes: 7 additions & 0 deletions soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2836,6 +2836,13 @@ impl VmCallerEnv for Host {
self.with_ledger_info(|li| Ok(self.add_host_object(li.timestamp)?.into()))
}

fn get_max_expiration_ledger(
&self,
_vmcaller: &mut VmCaller<Host>,
) -> Result<U32Val, Self::Error> {
Ok(self.max_expiration_ledger()?.into())
}

fn get_ledger_network_id(
&self,
_vmcaller: &mut VmCaller<Host>,
Expand Down
16 changes: 13 additions & 3 deletions soroban-env-host/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,19 @@ impl Storage {
bump_by_ledgers: u32,
) -> Result<(), HostError> {
let _span = tracy_span!("bump key");
let new_expiration = host
.with_ledger_info(|li| Ok(li.sequence_number.saturating_add(bump_by_ledgers)))?
.min(host.max_expiration_ledger()?);

let new_expiration =
host.with_ledger_info(|li| Ok(li.sequence_number.saturating_add(bump_by_ledgers)))?;

if new_expiration > host.max_expiration_ledger()? {
return Err(host.err(
ScErrorType::Storage,
ScErrorCode::InvalidAction,
"trying to bump past max expiration ledger",
&[new_expiration.into()],
));
}

// Bumping deleted/non-existing/out-of-footprint entries will result in
// an error.
let old_entry = self.get(&key, host.budget_ref())?;
Expand Down
23 changes: 21 additions & 2 deletions soroban-env-host/src/test/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,38 @@ fn test_storage(host: &Host, contract_id: AddressObject, storage: &str) {
true
);

let max_expiration_ledger: u32 = host.max_expiration_ledger().unwrap().into();
let ledger_seq: u32 = host.get_ledger_sequence().unwrap().into();
let max_bump = max_expiration_ledger - ledger_seq;

// Smoke test bump
let bump_args = if storage == "instance" {
host_vec![host, 6000_u32]
host_vec![host, max_bump]
} else {
host_vec![host, key_1, 6000_u32]
host_vec![host, key_1, max_bump]
};

host.call(
contract_id,
storage_fn_name(host, "bump", storage),
bump_args.into(),
)
.unwrap();

let bump_args_past_max = if storage == "instance" {
host_vec![host, max_bump + 1]
} else {
host_vec![host, key_1, max_bump + 1]
};

assert!(host
.call(
contract_id,
storage_fn_name(host, "bump", storage),
bump_args_past_max.into(),
)
.is_err());

// Put another key and verify it's there
host.call(
contract_id,
Expand Down
4 changes: 2 additions & 2 deletions soroban-env-host/src/test/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl TokenTest {
base_reserve: 5_000_000,
min_persistent_entry_expiration: 4096,
min_temp_entry_expiration: 16,
max_entry_expiration: 10000,
max_entry_expiration: 6_312_000,
autobump_ledgers: 0,
})
.unwrap();
Expand Down Expand Up @@ -1747,7 +1747,7 @@ fn test_auth_rejected_for_incorrect_nonce() {
&token.address,
"mint",
args.clone(),
Some((12345, 123 + 10000)),
Some((12345, 123 + 6_312_000)),
);
assert!(test
.host
Expand Down
15 changes: 13 additions & 2 deletions soroban-env-host/src/test/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
budget::{AsBudget, Budget, COST_MODEL_LIN_TERM_SCALE_BITS},
host::error::TryBorrowOrErr,
storage::{test_storage::MockSnapshotSource, Storage},
xdr, Host, HostError,
xdr, Host, HostError, LedgerInfo,
};

use soroban_bench_utils::HostTracker;
Expand Down Expand Up @@ -63,7 +63,18 @@ impl Host {
let snapshot_source = Rc::<MockSnapshotSource>::new(MockSnapshotSource::new());
let storage = Storage::with_recording_footprint(snapshot_source);
let host = Host::with_storage_and_budget(storage, Budget::default());
host.set_ledger_info(Default::default()).unwrap();
host.set_ledger_info(LedgerInfo {
protocol_version: 20,
sequence_number: 0,
timestamp: 0,
network_id: [0; 32],
base_reserve: 0,
min_persistent_entry_expiration: 4096,
min_temp_entry_expiration: 16,
max_entry_expiration: 6_312_000,
autobump_ledgers: 0,
})
.unwrap();
host
}

Expand Down
Binary file modified soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_alloc.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_complex.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_err.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fib.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ impl WriteBytesContract {
hash
}
}

mod test;

0 comments on commit 4867132

Please sign in to comment.