@@ -4,8 +4,8 @@ use soroban_sdk::{
44
55use crate :: timelock:: {
66 emit_min_delay_changed, emit_operation_cancelled, emit_operation_executed,
7- emit_operation_scheduled, TimelockError , DONE_TIMESTAMP , TIMELOCK_EXTEND_AMOUNT ,
8- TIMELOCK_TTL_THRESHOLD , UNSET_TIMESTAMP ,
7+ emit_operation_scheduled, TimelockError , DONE_LEDGER , TIMELOCK_EXTEND_AMOUNT ,
8+ TIMELOCK_TTL_THRESHOLD , UNSET_LEDGER ,
99} ;
1010
1111// ################## TYPES ##################
@@ -50,25 +50,25 @@ pub enum OperationState {
5050#[ derive( Clone ) ]
5151#[ contracttype]
5252pub enum TimelockStorageKey {
53- /// Minimum delay in seconds for operations
53+ /// Minimum delay in ledgers for operations
5454 MinDelay ,
55- /// Maps operation ID to the timestamp when it will be in a
55+ /// Maps operation ID to the ledger sequence number when it will be in a
5656 /// [`OperationState::Ready`] state (Note: value is 0 for
57- /// [`OperationState::Unset`], 1 for [`OperationState:Done`]).
58- Timestamp ( BytesN < 32 > ) ,
57+ /// [`OperationState::Unset`], 1 for [`OperationState:: Done`]).
58+ OperationLedger ( BytesN < 32 > ) ,
5959}
6060
6161// ################## QUERY STATE ##################
6262
63- /// Returns the minimum delay in seconds required for operations.
63+ /// Returns the minimum delay in ledgers required for operations.
6464///
6565/// # Arguments
6666///
6767/// * `e` - Access to Soroban environment.
6868///
6969/// # Returns
7070///
71- /// The minimum delay in seconds .
71+ /// The minimum delay in ledgers .
7272///
7373/// # Errors
7474///
@@ -80,7 +80,7 @@ pub fn get_min_delay(e: &Env) -> u32 {
8080 . unwrap_or_else ( || panic_with_error ! ( e, TimelockError :: MinDelayNotSet ) )
8181}
8282
83- /// Returns the timestamp at which an operation becomes ready.
83+ /// Returns the ledger sequence number at which an operation becomes ready.
8484///
8585/// # Arguments
8686///
@@ -89,16 +89,17 @@ pub fn get_min_delay(e: &Env) -> u32 {
8989///
9090/// # Returns
9191///
92- /// - `UNSET_TIMESTAMP` for unset operations
93- /// - `DONE_TIMESTAMP` for done operations
94- /// - Unix timestamp when the operation becomes ready for scheduled operations
95- pub fn get_timestamp ( e : & Env , operation_id : & BytesN < 32 > ) -> u64 {
96- let key = TimelockStorageKey :: Timestamp ( operation_id. clone ( ) ) ;
97- if let Some ( timestamp) = e. storage ( ) . persistent ( ) . get :: < _ , u64 > ( & key) {
92+ /// - `UNSET_LEDGER` for unset operations
93+ /// - `DONE_LEDGER` for done operations
94+ /// - Ledger sequence number when the operation becomes ready for scheduled
95+ /// operations
96+ pub fn get_operation_ledger ( e : & Env , operation_id : & BytesN < 32 > ) -> u32 {
97+ let key = TimelockStorageKey :: OperationLedger ( operation_id. clone ( ) ) ;
98+ if let Some ( ready_ledger) = e. storage ( ) . persistent ( ) . get :: < _ , u32 > ( & key) {
9899 e. storage ( ) . persistent ( ) . extend_ttl ( & key, TIMELOCK_TTL_THRESHOLD , TIMELOCK_EXTEND_AMOUNT ) ;
99- timestamp
100+ ready_ledger
100101 } else {
101- UNSET_TIMESTAMP
102+ UNSET_LEDGER
102103 }
103104}
104105
@@ -113,13 +114,13 @@ pub fn get_timestamp(e: &Env, operation_id: &BytesN<32>) -> u64 {
113114///
114115/// The current [`OperationState`] of the operation.
115116pub fn get_operation_state ( e : & Env , operation_id : & BytesN < 32 > ) -> OperationState {
116- let ready_timestamp = get_timestamp ( e, operation_id) ;
117- let current_timestamp = e. ledger ( ) . timestamp ( ) ;
117+ let ready_ledger = get_operation_ledger ( e, operation_id) ;
118+ let current_ledger = e. ledger ( ) . sequence ( ) ;
118119
119- match ready_timestamp {
120- UNSET_TIMESTAMP => OperationState :: Unset ,
121- DONE_TIMESTAMP => OperationState :: Done ,
122- ready if ready > current_timestamp => OperationState :: Waiting ,
120+ match ready_ledger {
121+ UNSET_LEDGER => OperationState :: Unset ,
122+ DONE_LEDGER => OperationState :: Done ,
123+ ready if ready > current_ledger => OperationState :: Waiting ,
123124 _ => OperationState :: Ready ,
124125 }
125126}
@@ -172,7 +173,7 @@ pub fn is_operation_done(e: &Env, operation_id: &BytesN<32>) -> bool {
172173/// # Arguments
173174///
174175/// * `e` - Access to Soroban environment.
175- /// * `min_delay` - The new minimum delay in seconds .
176+ /// * `min_delay` - The new minimum delay in ledgers .
176177///
177178/// # Events
178179///
@@ -196,7 +197,7 @@ pub fn set_min_delay(e: &Env, min_delay: u32) {
196197///
197198/// * `e` - Access to Soroban environment.
198199/// * `operation` - The operation to schedule.
199- /// * `delay` - The delay in seconds before the operation can be executed.
200+ /// * `delay` - The delay in ledgers before the operation can be executed.
200201///
201202/// # Returns
202203///
@@ -235,11 +236,11 @@ pub fn schedule_operation(e: &Env, operation: &Operation, delay: u32) -> BytesN<
235236 panic_with_error ! ( e, TimelockError :: InsufficientDelay ) ;
236237 }
237238
238- let current_timestamp = e. ledger ( ) . timestamp ( ) ;
239- let ready_timestamp = current_timestamp + ( delay as u64 ) ;
239+ let current_ledger = e. ledger ( ) . sequence ( ) ;
240+ let ready_ledger = current_ledger . saturating_add ( delay) ;
240241
241- let key = TimelockStorageKey :: Timestamp ( id. clone ( ) ) ;
242- e. storage ( ) . persistent ( ) . set ( & key, & ready_timestamp ) ;
242+ let key = TimelockStorageKey :: OperationLedger ( id. clone ( ) ) ;
243+ e. storage ( ) . persistent ( ) . set ( & key, & ready_ledger ) ;
243244
244245 emit_operation_scheduled (
245246 e,
@@ -337,8 +338,8 @@ pub fn set_execute_operation(e: &Env, operation: &Operation) {
337338 panic_with_error ! ( e, TimelockError :: UnexecutedPredecessor ) ;
338339 }
339340
340- let key = TimelockStorageKey :: Timestamp ( id. clone ( ) ) ;
341- e. storage ( ) . persistent ( ) . set ( & key, & DONE_TIMESTAMP ) ;
341+ let key = TimelockStorageKey :: OperationLedger ( id. clone ( ) ) ;
342+ e. storage ( ) . persistent ( ) . set ( & key, & DONE_LEDGER ) ;
342343
343344 emit_operation_executed (
344345 e,
@@ -377,7 +378,7 @@ pub fn cancel_operation(e: &Env, operation_id: &BytesN<32>) {
377378 panic_with_error ! ( e, TimelockError :: InvalidOperationState ) ;
378379 }
379380
380- let key = TimelockStorageKey :: Timestamp ( operation_id. clone ( ) ) ;
381+ let key = TimelockStorageKey :: OperationLedger ( operation_id. clone ( ) ) ;
381382 e. storage ( ) . persistent ( ) . remove ( & key) ;
382383
383384 emit_operation_cancelled ( e, operation_id) ;
0 commit comments