Skip to content

Commit

Permalink
Ensure Bank timestamp always increases, and make test less brittle
Browse files Browse the repository at this point in the history
  • Loading branch information
CriesofCarrots committed Oct 6, 2020
1 parent b236b35 commit 1bd59f4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ use solana_stake_program::stake_state::{self, Delegation, PointValue};
use solana_vote_program::{vote_instruction::VoteInstruction, vote_state::VoteState};
use std::{
cell::RefCell,
cmp::max,
collections::{HashMap, HashSet},
convert::{TryFrom, TryInto},
mem,
Expand Down Expand Up @@ -1385,7 +1386,7 @@ impl Bank {

EpochTimestamps {
first_slot: self.slot,
timestamp: epoch_timestamp,
timestamp: max(epoch_timestamp, self.unix_timestamp_from_genesis()),
..EpochTimestamps::default()
}
.create_account(self.inherit_sysvar_account_balance(account))
Expand Down Expand Up @@ -9507,11 +9508,16 @@ mod tests {
genesis_config.epoch_schedule = EpochSchedule::new(32);
let mut bank = Arc::new(Bank::new(&genesis_config));
let recent_timestamp: UnixTimestamp = bank.unix_timestamp_from_genesis();
let slot_duration = Duration::from_nanos(bank.ns_per_slot as u64);

let timestamp_slot_12 = recent_timestamp + (12 * slot_duration).as_secs() as i64;
let timestamp_slot_22 = recent_timestamp + (22 * slot_duration).as_secs() as i64;
let slowdown_secs = 4;
let timestamp_slot_22 = timestamp_slot_22 + slowdown_secs;
bank.update_sysvar_account(&sysvar::epoch_timestamps::id(), |account| {
let mut samples = HashMap::new();
samples.insert(12, Some(recent_timestamp));
samples.insert(22, Some(recent_timestamp + 6));
samples.insert(12, Some(timestamp_slot_12));
samples.insert(22, Some(timestamp_slot_22));

EpochTimestamps {
first_slot: bank.slot,
Expand All @@ -9528,7 +9534,10 @@ mod tests {
)
.unwrap();
assert_eq!(epoch_timestamps.first_slot, bank.slot());
assert_eq!(epoch_timestamps.timestamp, recent_timestamp + 9);
assert_eq!(
epoch_timestamps.timestamp,
bank.unix_timestamp_from_genesis() + (slowdown_secs / 2)
);
assert!(epoch_timestamps.samples.is_empty());
}

Expand Down

0 comments on commit 1bd59f4

Please sign in to comment.