forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Approvals usage collection #3
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
Open
EclesioMeloJunior
wants to merge
12
commits into
master
Choose a base branch
from
eclesio-approval-rewards
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f94eae2
WIP
haikoschol 21b7b26
wip
EclesioMeloJunior 3438319
implement compute_approvals_tallies()
haikoschol 13566cb
chore: include session idx on approvals tallies
EclesioMeloJunior 4470f2c
chore: update to use candidate approvals entries
EclesioMeloJunior d820dd8
chore: consider only finalized blocks
EclesioMeloJunior 8831255
chore
EclesioMeloJunior 363426a
progress the lest session index
EclesioMeloJunior c5949ed
chore: move to a specific function
EclesioMeloJunior 7537185
chore: receive signed approvals tallies
EclesioMeloJunior c9474b0
feat: implement medians calculation
EclesioMeloJunior d98eb4c
Merge pull request #4 from ChainSafe/eclesio-distrib-usages
EclesioMeloJunior 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
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 |
|---|---|---|
|
|
@@ -85,7 +85,6 @@ use std::{ | |
| sync::Arc, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use schnellru::{ByLength, LruMap}; | ||
|
|
||
| use approval_checking::RequiredTranches; | ||
|
|
@@ -891,15 +890,32 @@ struct State { | |
| keystore: Arc<LocalKeystore>, | ||
| slot_duration_millis: u64, | ||
| clock: Arc<dyn Clock + Send + Sync>, | ||
| last_session_index: Option<SessionIndex>, | ||
| assignment_criteria: Box<dyn AssignmentCriteria + Send + Sync>, | ||
| // Per block, candidate records about how long we take until we gather enough | ||
| // assignments, this is relevant because it gives us a good idea about how many | ||
| // tranches we trigger and why. | ||
| per_block_assignments_gathering_times: | ||
| LruMap<BlockNumber, HashMap<(Hash, CandidateHash), AssignmentGatheringRecord>>, | ||
| no_show_stats: NoShowStats, | ||
|
|
||
| candidates_per_session: HashMap<SessionIndex, Vec<CandidateHash>>, | ||
|
|
||
| // amount of approvals usage per epoch per validator index | ||
| // where the ith index in the vector corresponds to the | ||
| approvals_usage: HashMap<SessionIndex, Vec<ApprovalTallyLine>>, | ||
| } | ||
|
|
||
| /// Our subjective record of what we used from some other validator on the finalized chain | ||
| #[derive(Clone, Debug, Default, PartialEq)] | ||
| pub struct ApprovalTallyLine { | ||
| /// Approvals by this validator which our approvals gadget used in marking candidates approved. | ||
| approval_usages: u32, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| struct ApprovalsTally((SessionIndex, Vec<ApprovalTallyLine>)); | ||
|
|
||
| // Regularly dump the no-show stats at this block number frequency. | ||
| const NO_SHOW_DUMP_FREQUENCY: BlockNumber = 50; | ||
| // The maximum number of validators we record no-shows for, per candidate. | ||
|
|
@@ -982,7 +998,7 @@ impl State { | |
| block_entry.parent_hash(), | ||
| block_entry.session(), | ||
| ) | ||
| .await | ||
| .await | ||
| { | ||
| Some(s) => s, | ||
| None => return None, | ||
|
|
@@ -1252,11 +1268,14 @@ where | |
| keystore: subsystem.keystore, | ||
| slot_duration_millis: subsystem.slot_duration_millis, | ||
| clock: subsystem.clock, | ||
| last_session_index: None, | ||
| assignment_criteria, | ||
| per_block_assignments_gathering_times: LruMap::new(ByLength::new( | ||
| MAX_BLOCKS_WITH_ASSIGNMENT_TIMESTAMPS, | ||
| )), | ||
| no_show_stats: NoShowStats::default(), | ||
| candidates_per_session: Default::default(), | ||
| approvals_usage: Default::default(), | ||
| }; | ||
|
|
||
| let mut last_finalized_height: Option<BlockNumber> = { | ||
|
|
@@ -2053,6 +2072,11 @@ async fn handle_from_overseer< | |
| for (c_hash, c_entry) in block_batch.imported_candidates { | ||
| metrics.on_candidate_imported(); | ||
|
|
||
| state.candidates_per_session | ||
| .entry(c_entry.session) | ||
| .and_modify(|candidates| { candidates.push(c_hash.clone()) }) | ||
| .or_insert_with(|| vec![c_hash.clone()]); | ||
|
|
||
| let our_tranche = c_entry | ||
| .approval_entry(&block_batch.block_hash) | ||
| .and_then(|a| a.our_assignment().map(|a| a.tranche())); | ||
|
|
@@ -2088,8 +2112,56 @@ async fn handle_from_overseer< | |
| }, | ||
| FromOrchestra::Signal(OverseerSignal::BlockFinalized(block_hash, block_number)) => { | ||
| gum::debug!(target: LOG_TARGET, ?block_hash, ?block_number, "Block finalized"); | ||
| *last_finalized_height = Some(block_number); | ||
| let finalized_tip = db.load_block_entry(&block_hash)?.unwrap(); | ||
|
|
||
| let is_new_session = match state.last_session_index { | ||
| Some(last_session) if finalized_tip.session() > last_session => true, | ||
| Some(_) => false, | ||
| None => true, | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is possible while finalizing to finalize more than one session? |
||
|
|
||
| if is_new_session { | ||
| let retrieve_size: usize = last_finalized_height | ||
| .clone() | ||
| .map_or(block_number, |b| block_number - (b as u32)) as usize; | ||
|
|
||
| let finalized_hashes: HashSet<Hash> = fetch_ancestry( | ||
| sender, | ||
| block_hash, | ||
| retrieve_size, | ||
| ).await?.into_iter().collect(); | ||
|
|
||
| let mut prev_session_approvals: HashMap<usize, u32> = HashMap::new(); | ||
| let prev = (finalized_tip.session() as u32).saturating_sub(1) as SessionIndex; | ||
| let candidates = match state.candidates_per_session.remove(&prev) { | ||
| Some(candidates) => candidates, | ||
| _ => vec![], | ||
| }; | ||
|
|
||
| for c_hash in candidates { | ||
| match db.load_candidate_entry(&c_hash)? { | ||
| Some(candidate) => { | ||
| let on_finalized_block = candidate.block_assignments | ||
| .keys() | ||
| .any(|b_hash| finalized_hashes.contains(b_hash)); | ||
|
|
||
| if on_finalized_block { | ||
| for idx in candidate.approvals.iter_ones() { | ||
| prev_session_approvals | ||
| .entry(idx as usize) | ||
| .and_modify(|e| *e += 1) | ||
| .or_insert(0); | ||
| } | ||
| } | ||
| }, | ||
| _ => {}, | ||
| } | ||
| }; | ||
|
|
||
| state.last_session_index = Some(finalized_tip.session()); | ||
| }; | ||
|
|
||
| *last_finalized_height = Some(block_number); | ||
| crate::ops::canonicalize(db, block_number, block_hash) | ||
| .map_err(|e| SubsystemError::with_origin("db", e))?; | ||
|
|
||
|
|
@@ -2986,6 +3058,15 @@ where | |
| actions.extend(new_actions); | ||
| } | ||
|
|
||
| let block_entry = match db.load_block_entry(&approval.block_hash)? { | ||
| Some(b) => b, | ||
| None => { | ||
| respond_early!(ApprovalCheckResult::Bad(ApprovalCheckError::UnknownBlock( | ||
| approval.block_hash | ||
| ),)) | ||
| }, | ||
| }; | ||
|
|
||
| // importing the approval can be heavy as it may trigger acceptance for a series of blocks. | ||
| Ok((actions, ApprovalCheckResult::Accepted)) | ||
| } | ||
|
|
@@ -3971,6 +4052,33 @@ async fn maybe_create_signature< | |
| Ok(None) | ||
| } | ||
|
|
||
| // Fetch ancestors in descending order, up to the amount requested. | ||
| #[overseer::contextbounds(ApprovalVoting, prefix = self::overseer)] | ||
| async fn fetch_ancestry<Sender: SubsystemSender<ChainApiMessage>>( | ||
| sender: &mut Sender, | ||
| relay_hash: Hash, | ||
| ancestors: usize, | ||
| ) -> SubsystemResult<Vec<Hash>> { | ||
| if ancestors == 0 { | ||
| return Ok(Vec::new()) | ||
| } | ||
|
|
||
| let (tx, rx) = oneshot::channel(); | ||
| sender.send_message(ChainApiMessage::Ancestors { | ||
| hash: relay_hash, | ||
| k: ancestors, | ||
| response_channel: tx, | ||
| }).await; | ||
|
|
||
| let hashes = match rx.await { | ||
| Ok(Ok(hashes)) => hashes, | ||
| Ok(Err(e)) => return Err(SubsystemError::with_origin("chain-api", e)), | ||
| Err(e) => return Err(SubsystemError::with_origin("chain-api", e)), | ||
| }; | ||
|
|
||
| Ok(hashes) | ||
| } | ||
|
|
||
| // Sign an approval vote. Fails if the key isn't present in the store. | ||
| fn sign_approval( | ||
| keystore: &LocalKeystore, | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see this used anywhere