-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Introduce a EpochStakesService #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2bb4fff
wip
akhi3030 b2473a6
fix minor test issue
akhi3030 e31abba
use root bank cache instead
akhi3030 ce39acc
use subscribers
akhi3030 0cf884f
send bank over the channel
akhi3030 293e6c8
exit the loop if receiver fails
akhi3030 d224e88
some rename
akhi3030 9458a78
.
akhi3030 82ccb41
pass in initial state
akhi3030 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| use { | ||
| crate::{ | ||
| epoch_stakes::{BLSPubkeyToRankMap, EpochStakes}, | ||
| root_bank_cache::RootBankCache, | ||
| }, | ||
| crossbeam_channel::Receiver, | ||
| parking_lot::RwLock as PlRwLock, | ||
| solana_sdk::{ | ||
| clock::{Epoch, Slot}, | ||
| epoch_schedule::EpochSchedule, | ||
| }, | ||
| std::{collections::HashMap, sync::Arc, thread}, | ||
| }; | ||
|
|
||
| struct State { | ||
| stakes: HashMap<Epoch, EpochStakes>, | ||
| epoch_schedule: EpochSchedule, | ||
| } | ||
|
|
||
| /// Updates the epoch stakes state from the bank forks. | ||
| /// | ||
| /// If new state was found, returns the corresponding root epoch and the state. | ||
| fn update_state(root_bank_cache: &mut RootBankCache, epoch: Epoch) -> Option<(Epoch, State)> { | ||
| let root_bank = root_bank_cache.root_bank(); | ||
| let root_epoch = root_bank.epoch(); | ||
| (root_epoch > epoch).then(|| { | ||
| ( | ||
| root_epoch, | ||
| State { | ||
| stakes: root_bank.epoch_stakes_map().clone(), | ||
| epoch_schedule: root_bank.epoch_schedule().clone(), | ||
| }, | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// A service that regularly updates the epoch stakes state from the bank forks | ||
| /// and exposes various methods to access the state. | ||
| pub struct EpochStakesService { | ||
| state: Arc<PlRwLock<State>>, | ||
| } | ||
|
|
||
| impl EpochStakesService { | ||
| pub fn new(new_epoch_receiver: Receiver<()>, mut root_bank_cache: RootBankCache) -> Self { | ||
| let mut prev_epoch = Epoch::default(); | ||
| let state = Arc::new(PlRwLock::new(State { | ||
| stakes: HashMap::new(), | ||
| epoch_schedule: EpochSchedule::default(), | ||
| })); | ||
|
|
||
| { | ||
| let state = state.clone(); | ||
| thread::spawn(move || loop { | ||
| let () = new_epoch_receiver.recv().unwrap(); | ||
AshwinSekar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if let Some((new_epoch, update)) = update_state(&mut root_bank_cache, prev_epoch) { | ||
| prev_epoch = new_epoch; | ||
| *state.write() = update; | ||
| } | ||
| }); | ||
| } | ||
| Self { state } | ||
| } | ||
|
|
||
| pub fn get_key_to_rank_map(&self, slot: Slot) -> Option<Arc<BLSPubkeyToRankMap>> { | ||
| let guard = self.state.read(); | ||
| let epoch = guard.epoch_schedule.get_epoch(slot); | ||
| guard | ||
| .stakes | ||
| .get(&epoch) | ||
| .map(|stake| Arc::clone(stake.bls_pubkey_to_rank_map())) | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.