Skip to content

Commit 0a96e8d

Browse files
committed
✨ Initialize svm with current time
1 parent 26bef93 commit 0a96e8d

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/accounts_db.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::HashMap;
2-
use std::time::UNIX_EPOCH;
32

43
use serde::de::DeserializeOwned;
54

@@ -10,6 +9,8 @@ use solana_sdk::pubkey::Pubkey;
109
use solana_sdk::sysvar::Sysvar;
1110
use solana_sdk::sysvar::SysvarId;
1211

12+
use crate::utils::get_current_timestamp;
13+
1314
#[derive(Default)]
1415
pub struct AccountsDB {
1516
accounts: HashMap<Pubkey, AccountSharedData>,
@@ -83,10 +84,7 @@ impl AccountsDB {
8384
let mut clock: Clock =
8485
bincode::deserialize(self.sysvars.get(&Clock::id()).unwrap().data()).unwrap();
8586

86-
let current_timestamp = std::time::SystemTime::now()
87-
.duration_since(UNIX_EPOCH)
88-
.expect("Time went backwards!")
89-
.as_secs();
87+
let current_timestamp = get_current_timestamp();
9088

9189
// current time is always greater than last clock update
9290
let time_since_last_update =
@@ -126,10 +124,7 @@ pub struct SysvarTracker {
126124

127125
impl SysvarTracker {
128126
pub fn refresh_last_clock_update(&mut self) {
129-
self.last_clock_update = std::time::SystemTime::now()
130-
.duration_since(UNIX_EPOCH)
131-
.expect("Time went backwards!")
132-
.as_secs();
127+
self.last_clock_update = get_current_timestamp();
133128
}
134129
}
135130

src/trident_svm_methods.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use crate::log::turn_off_solana_logging;
4949
use crate::native::BUILTINS;
5050
use crate::trident_fork_graphs::TridentForkGraph;
5151
use crate::trident_svm::TridentSVM;
52+
use crate::utils::get_current_timestamp;
5253
use crate::utils::ProgramEntrypoint;
5354
use crate::utils::SBFTarget;
5455
use crate::utils::TridentAccountSharedData;
@@ -97,7 +98,11 @@ impl TridentSVM {
9798
self
9899
}
99100
fn with_sysvars(mut self) -> Self {
100-
self.set_sysvar(&Clock::default());
101+
let clock = Clock {
102+
unix_timestamp: get_current_timestamp() as i64,
103+
..Default::default()
104+
};
105+
self.set_sysvar(&clock);
101106
self.set_sysvar(&EpochRewards::default());
102107
self.set_sysvar(&EpochSchedule::default());
103108
#[allow(deprecated)]

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::UNIX_EPOCH;
2+
13
use solana_program_runtime::invoke_context::BuiltinFunctionWithContext;
24

35
use solana_sdk::account::AccountSharedData;
@@ -46,3 +48,10 @@ impl TridentAccountSharedData {
4648
Self { address, account }
4749
}
4850
}
51+
52+
pub(crate) fn get_current_timestamp() -> u64 {
53+
std::time::SystemTime::now()
54+
.duration_since(UNIX_EPOCH)
55+
.expect("Time went backwards!")
56+
.as_secs()
57+
}

0 commit comments

Comments
 (0)