From 0049ab69fb111c533836e875a0ea231aadd3f2f8 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 26 Oct 2020 14:07:06 -0600 Subject: [PATCH] Repurpose unused as Clock::epoch_start_timestamp; add gated update --- runtime/src/bank.rs | 29 +++++++++++++++++++++-------- sdk/program/src/clock.rs | 4 ++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index fa1aa477fe..471686cc9a 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -741,7 +741,7 @@ impl Bank { } bank.update_stake_history(None); } - bank.update_clock(); + bank.update_clock(None); bank.update_rent(); bank.update_epoch_schedule(); bank.update_recent_blockhashes(); @@ -844,7 +844,7 @@ impl Bank { new.update_slot_hashes(); new.update_rewards(parent.epoch()); new.update_stake_history(Some(parent.epoch())); - new.update_clock(); + new.update_clock(Some(parent.epoch())); new.update_fees(); if !new.fix_recent_blockhashes_sysvar_delay() { new.update_recent_blockhashes(); @@ -1073,7 +1073,7 @@ impl Bank { .unwrap_or_default() } - fn update_clock(&self) { + fn update_clock(&self, parent_epoch: Option) { let mut unix_timestamp = self.unix_timestamp_from_genesis(); if self .feature_set @@ -1090,9 +1090,22 @@ impl Bank { } } } + let epoch_start_timestamp = if self + .feature_set + .is_active(&feature_set::timestamp_bounding::id()) + { + // On epoch boundaries, update epoch_start_timestamp + if parent_epoch.is_some() && parent_epoch.unwrap() != self.epoch() { + unix_timestamp + } else { + self.clock().epoch_start_timestamp + } + } else { + Self::get_unused_from_slot(self.slot, self.unused) as i64 + }; let clock = sysvar::clock::Clock { slot: self.slot, - unused: Self::get_unused_from_slot(self.slot, self.unused), + epoch_start_timestamp, epoch: self.epoch_schedule.get_epoch(self.slot), leader_schedule_epoch: self.epoch_schedule.get_leader_schedule_epoch(self.slot), unix_timestamp, @@ -9757,7 +9770,7 @@ mod tests { bank.unix_timestamp_from_genesis() ); - bank.update_clock(); + bank.update_clock(None); assert_eq!( bank.clock().unix_timestamp, bank.unix_timestamp_from_genesis() @@ -9771,7 +9784,7 @@ mod tests { &bank, &voting_keypair.pubkey(), ); - bank.update_clock(); + bank.update_clock(None); assert_eq!( bank.clock().unix_timestamp, bank.unix_timestamp_from_genesis() @@ -9785,7 +9798,7 @@ mod tests { &bank, &voting_keypair.pubkey(), ); - bank.update_clock(); + bank.update_clock(None); assert_eq!( bank.clock().unix_timestamp, bank.unix_timestamp_from_genesis() @@ -9799,7 +9812,7 @@ mod tests { &bank, &voting_keypair.pubkey(), ); - bank.update_clock(); + bank.update_clock(None); assert_eq!( bank.clock().unix_timestamp, bank.unix_timestamp_from_genesis() + 1 diff --git a/sdk/program/src/clock.rs b/sdk/program/src/clock.rs index f576a9bb61..26244aa1c2 100644 --- a/sdk/program/src/clock.rs +++ b/sdk/program/src/clock.rs @@ -79,8 +79,8 @@ pub type UnixTimestamp = i64; pub struct Clock { /// the current network/bank Slot pub slot: Slot, - /// unused - pub unused: u64, + /// the timestamp of the first Slot in this Epoch + pub epoch_start_timestamp: UnixTimestamp, /// the bank Epoch pub epoch: Epoch, /// the future Epoch for which the leader schedule has