forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slot_history for slashing (solana-labs#7589)
* Add slot_history for slashing * fixup * fixup
- Loading branch information
1 parent
352a367
commit 120c8f2
Showing
15 changed files
with
209 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
use solana_sdk::{slot_history::SlotHistory, sysvar::Sysvar}; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn bench_to_from_account(b: &mut Bencher) { | ||
let mut slot_history = SlotHistory::default(); | ||
|
||
b.iter(|| { | ||
let account = slot_history.create_account(0); | ||
slot_history = SlotHistory::from_account(&account).unwrap(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//! | ||
//! slot history | ||
//! | ||
pub use crate::clock::Slot; | ||
use bv::BitVec; | ||
|
||
#[repr(C)] | ||
#[derive(Serialize, Deserialize, PartialEq, Debug)] | ||
pub struct SlotHistory { | ||
pub bits: BitVec<u64>, | ||
pub next_slot: Slot, | ||
} | ||
|
||
impl Default for SlotHistory { | ||
fn default() -> Self { | ||
let mut bits = BitVec::new_fill(false, MAX_ENTRIES); | ||
bits.set(0, true); | ||
Self { bits, next_slot: 1 } | ||
} | ||
} | ||
|
||
pub const MAX_ENTRIES: u64 = 1024 * 1024; // 1 million slots is about 5 days | ||
|
||
#[derive(PartialEq, Debug)] | ||
pub enum Check { | ||
Future, | ||
TooOld, | ||
Found, | ||
NotFound, | ||
} | ||
|
||
impl SlotHistory { | ||
pub fn add(&mut self, slot: Slot) { | ||
for skipped in self.next_slot..slot { | ||
self.bits.set(skipped % MAX_ENTRIES, false); | ||
} | ||
self.bits.set(slot % MAX_ENTRIES, true); | ||
self.next_slot = slot + 1; | ||
} | ||
|
||
pub fn check(&self, slot: Slot) -> Check { | ||
if slot >= self.next_slot { | ||
Check::Future | ||
} else if self.next_slot - slot > MAX_ENTRIES { | ||
Check::TooOld | ||
} else if self.bits.get(slot % MAX_ENTRIES) { | ||
Check::Found | ||
} else { | ||
Check::NotFound | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test() { | ||
let mut slot_history = SlotHistory::default(); | ||
slot_history.add(2); | ||
assert_eq!(slot_history.check(0), Check::Found); | ||
assert_eq!(slot_history.check(1), Check::NotFound); | ||
for i in 3..MAX_ENTRIES { | ||
assert_eq!(slot_history.check(i), Check::Future); | ||
} | ||
slot_history.add(MAX_ENTRIES); | ||
assert_eq!(slot_history.check(0), Check::TooOld); | ||
assert_eq!(slot_history.check(1), Check::NotFound); | ||
assert_eq!(slot_history.check(2), Check::Found); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! named accounts for synthesized data accounts for bank state, etc. | ||
//! | ||
//! this account carries a bitvector of slots present over the past | ||
//! epoch | ||
//! | ||
pub use crate::slot_history::SlotHistory; | ||
|
||
use crate::sysvar::Sysvar; | ||
|
||
crate::declare_sysvar_id!("SysvarS1otHistory11111111111111111111111111", SlotHistory); | ||
|
||
impl Sysvar for SlotHistory { | ||
// override | ||
fn size_of() -> usize { | ||
// hard-coded so that we don't have to construct an empty | ||
131_097 // golden, update if MAX_ENTRIES changes | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn test_size_of() { | ||
assert_eq!( | ||
SlotHistory::size_of(), | ||
bincode::serialized_size(&SlotHistory::default()).unwrap() as usize | ||
); | ||
} | ||
} |
Oops, something went wrong.