From 37c2fa1d8dcad0f67a66bbb56860dfcb21800fbd Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Fri, 7 Jun 2019 11:41:34 -0700 Subject: [PATCH] add current to bank syscalls (#4581) --- runtime/src/bank.rs | 15 +++++++++++++++ runtime/src/epoch_schedule.rs | 5 +++++ sdk/src/syscall/current.rs | 7 +++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 525a7b04e..d8ec0ea84 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -29,6 +29,7 @@ use solana_sdk::hash::{extend_and_hash, Hash}; use solana_sdk::native_loader; use solana_sdk::pubkey::Pubkey; use solana_sdk::signature::{Keypair, Signature}; +use solana_sdk::syscall::current::{self, Current}; use solana_sdk::syscall::fees::{self, Fees}; use solana_sdk::syscall::slot_hashes::{self, SlotHashes}; use solana_sdk::syscall::tick_height::{self, TickHeight}; @@ -275,6 +276,7 @@ impl Bank { bank.epoch_stakes.insert(i, stakes.clone()); } } + bank.update_current(); bank } @@ -331,6 +333,7 @@ impl Bank { bank.parents().iter().enumerate().for_each(|(i, p)| { bank.ancestors.insert(p.slot(), i + 1); }); + bank.update_current(); bank } @@ -355,6 +358,18 @@ impl Bank { *self.hash.read().unwrap() != Hash::default() } + fn update_current(&self) { + let mut account = current::create_account(1); + let current = Current { + slot: self.slot, + epoch: self.epoch_schedule.get_epoch(self.slot), + stakers_epoch: self.epoch_schedule.get_stakers_epoch(self.slot), + }; + current.to(&mut account).unwrap(); + + self.store(¤t::id(), &account); + } + fn update_slot_hashes(&self) { let mut account = self .get_account(&slot_hashes::id()) diff --git a/runtime/src/epoch_schedule.rs b/runtime/src/epoch_schedule.rs index 1c578c0c1..cc6fd725e 100644 --- a/runtime/src/epoch_schedule.rs +++ b/runtime/src/epoch_schedule.rs @@ -62,6 +62,11 @@ impl EpochSchedule { } } + /// get epoch for the given slot + pub fn get_epoch(&self, slot: u64) -> u64 { + self.get_epoch_and_slot_index(slot).0 + } + /// get epoch and offset into the epoch for the given slot pub fn get_epoch_and_slot_index(&self, slot: u64) -> (u64, u64) { if slot < self.first_normal_slot { diff --git a/sdk/src/syscall/current.rs b/sdk/src/syscall/current.rs index 27399e398..8755cc604 100644 --- a/sdk/src/syscall/current.rs +++ b/sdk/src/syscall/current.rs @@ -14,10 +14,9 @@ const ID: [u8; 32] = [ #[repr(C)] #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] pub struct Current { - slot: u64, - block: u64, - epoch: u64, - stakers_epoch: u64, + pub slot: u64, + pub epoch: u64, + pub stakers_epoch: u64, } impl Current {