Skip to content

Commit

Permalink
Add fn to expose max expiration available (#1106)
Browse files Browse the repository at this point in the history
### What
Add fn to expose max expiration available.

### Why
It came up in the Discord meeting today that it would be helpful if
contracts can access the max expiration as they may wish to simply bump
to that. The host fn exists, but it isn't exposed in the SDK.
  • Loading branch information
leighmcculloch authored Oct 5, 2023
1 parent 3db7a43 commit 05d5332
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions soroban-sdk/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ impl Ledger {
.into()
}

/// Returns the maximum ledger sequence number that data can live to.
#[doc(hidden)]
pub fn max_expiration_sequence(&self) -> u32 {
internal::Env::get_max_expiration_ledger(self.env())
.unwrap_infallible()
.into()
}

/// Returns a unix timestamp for when the ledger was closed.
///
/// The timestamp is the number of seconds, excluding leap seconds,
Expand Down
15 changes: 15 additions & 0 deletions soroban-sdk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ impl Storage {
}
}

/// Returns the maximum number of ledgers that an entry can have rent paid
/// for it in one moment.
///
/// When counting the number of ledgers an entry is active for, the current
/// ledger is included. If an entry is created it the current ledger, its
/// maximum expiration duration in ledgers will be the value returned from
/// the function including the current ledger. This means the last ledger
/// that the entry will be accessible will be the current ledger sequence
/// plus the max expiration minus one.
pub fn max_expiration(&self) -> u32 {
let seq = self.env.ledger().sequence();
let max = self.env.ledger().max_expiration_sequence();
max - seq + 1
}

/// Returns if there is a value stored for the given key in the currently
/// executing contracts storage.
#[inline(always)]
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod crypto_keccak256;
mod crypto_secp256k1;
mod crypto_sha256;
mod env;
mod max_expiration;
mod prng;
mod proptest_scval_cmp;
mod proptest_val_cmp;
Expand Down
20 changes: 20 additions & 0 deletions soroban-sdk/src/tests/max_expiration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{self as soroban_sdk, testutils::Ledger as _};
use soroban_sdk::{contract, Env};

#[contract]
pub struct Contract;

#[test]
fn max() {
let e = Env::default();
let contract_id = e.register_contract(None, Contract);

let mut ledger_info = e.ledger().get();
ledger_info.sequence_number = 1;
ledger_info.max_entry_expiration = 5;
e.ledger().set(ledger_info);

e.as_contract(&contract_id, || {
assert_eq!(e.storage().max_expiration(), 5);
});
}

0 comments on commit 05d5332

Please sign in to comment.