-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d45ef86
commit 2f4ee4b
Showing
4 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
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,60 @@ | ||
use crate::bank_state::BankCheckpoint; | ||
use fnv::FnvHasher; | ||
use solana_sdk::pubkey::Pubkey; | ||
use solana_sdk::vote_program; | ||
use std::hash::Hasher; | ||
|
||
pub const REFRESH_RATE: u64 = 1000; | ||
|
||
struct Sched { | ||
min_slot: u64, | ||
ranks: Vec<(Pubkey, u64)>, | ||
} | ||
|
||
impl Sched { | ||
fn should_regenerate(prev_root: u64, new_root: u64) -> bool { | ||
prev_root / REFRESH_RATE != new_root / REFRESH_RATE | ||
} | ||
|
||
/// ranked leaders | ||
fn new_schedule(root: &BankCheckpoint) -> Sched { | ||
let accounts = root.accounts.accounts_db.read().unwrap(); | ||
let leaders: Vec<(Pubkey, u64)> = accounts | ||
.accounts | ||
.iter() | ||
.filter_map(|(id, account)| { | ||
if vote_program::check_id(&account.owner) { | ||
return Some((id, account.tokens)); | ||
} | ||
None | ||
}) | ||
.collect(); | ||
let start = (Pubkey::default(), 0); | ||
let ranks = leaders | ||
.into_iter() | ||
.scan(start, |z, x| Some((x.0, z.1 + x.1))) | ||
.collect(); | ||
let min_slot = ((root.fork_id() + REFRESH_RATE) / REFRESH_RATE) * REFRESH_RATE; | ||
Sched { ranks, min_slot } | ||
} | ||
|
||
fn compute_node(&self, slot: u64) -> Option<Pubkey> { | ||
if slot < self.min_slot { | ||
return None; | ||
} | ||
let total = self.ranks.last().unwrap().1; | ||
// use a stable known hasher because this MUST be the same across the entire network | ||
let mut hasher = FnvHasher::with_key(self.min_slot); | ||
hasher.write(&slot.to_le_bytes()); | ||
let random = hasher.finish(); | ||
let val = random % total; | ||
Some( | ||
self.ranks | ||
.iter() | ||
.skip_while(|l| val < l.1) | ||
.nth(0) | ||
.unwrap() | ||
.0, | ||
) | ||
} | ||
} |