Time is an important aspect to test in contracts but it's actually non-trival to advance time currently. Adding a env.ledger().sequence().increase(n: u32) method to progress the ledger would be quite nice. Behind the scenes I would imagine this would clone the existing env, advance the sequence by n and replace the existing env. Or some such utility
#Request for a method to easily advance env.ledger().sequence()
11 messages · Page 1 of 1 (latest)
There is a LedgerInfo in the testutils you can import that allows you to modify this on the existing env.
I normally write a helper method that goes something like:
use soroban_sdk::testutils::{Ledger, LedgerInfo};
...
pub fn jump_with_sequence(env: &Env, time: u64) {
let blocks = time / 5;
env.ledger().set(LedgerInfo {
timestamp: env.ledger().timestamp().saturating_add(time),
protocol_version: 20,
sequence_number:
env.ledger().sequence().saturating_add(blocks as u32),
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 999999,
min_persistent_entry_expiration: 999999,
max_entry_expiration: u32::MAX,
});
}
Yeah I'm using this
fn advance_sequence(env: &mut Env) {
let mut snapshot = env.to_snapshot();
snapshot.sequence_number += 1;
snapshot.timestamp = snapshot.timestamp.saturating_add(5);
*env = Env::from_snapshot(snapshot);
env.budget().reset_default();
}
Then use it like:
let mut env = Env::default();
...
advance_sequence(&mut env);
Which works fine but would be nice to have an official utility since this is such a common action
Yeah I agree, also it seems the current approach is not straightforward for those who are new as I've seen a couple of folks asking for this
I guess though that the function should also have the time for every ledger to be specified
futurenet and testnet ledgers definitely close before 5 seconds
there are also situations where passing too many sequence numbers is not good for testing edge cases, like with a fuzzer that jumps an arbitrary amount of blocks.
Reason being is things will expire and cause "bad" errors.
I guess though that the function should also have the time for every ledger to be specified
I don't think you need to optimize for testnet (and in general you probably should be advancing either sequence or time - these are correlated, but there are no guarantees on when exactly certain ledger is applied)
FWIW this is a good thing to just open a feature request for rs-soroban-sdk OR just do a quick PR - that should be just one or two lines with env.ledger().with_mut(|li| li.sequence_number += 1;)
FWIW with_mut is probably much better for anything else ledger-related (you don't need to create snapshots for this)