Skip to content

Commit 5e62a01

Browse files
authored
upstreams introducing CertificateType::limits_and_vote_types() (#10702)
1 parent d66c33a commit 5e62a01

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

core/src/consensus.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use {
2020
tower_vote_state::TowerVoteState,
2121
},
2222
crate::{consensus::progress_map::LockoutInterval, replay_stage::DUPLICATE_THRESHOLD},
23-
agave_votor_messages::migration::GENESIS_VOTE_THRESHOLD,
23+
agave_votor_messages::{fraction::Fraction, migration::GENESIS_VOTE_THRESHOLD},
2424
chrono::prelude::*,
2525
solana_clock::{Slot, UnixTimestamp},
2626
solana_hash::Hash,
@@ -42,6 +42,7 @@ use {
4242
std::{
4343
cmp::Ordering,
4444
collections::{HashMap, HashSet},
45+
num::NonZeroU64,
4546
ops::Deref,
4647
},
4748
thiserror::Error,
@@ -542,7 +543,8 @@ impl Tower {
542543

543544
debug_assert!(total_stake > 0);
544545
let parent_is_super_oc = bank_slot == parent_slot + 1
545-
&& super_oc_stake as f64 / total_stake as f64 > GENESIS_VOTE_THRESHOLD;
546+
&& Fraction::new(super_oc_stake, NonZeroU64::new(total_stake).unwrap())
547+
> GENESIS_VOTE_THRESHOLD;
546548

547549
// TODO: populate_ancestor_voted_stakes only adds zeros. Comment why
548550
// that is necessary (if so).

votor-messages/src/consensus_message.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Put Alpenglow consensus messages here so all clients can agree on the format.
22
use {
3-
crate::vote::Vote,
3+
crate::{
4+
fraction::Fraction,
5+
migration::GENESIS_VOTE_THRESHOLD,
6+
vote::{Vote, VoteType},
7+
},
48
serde::{Deserialize, Serialize},
59
solana_bls_signatures::Signature as BLSSignature,
610
solana_clock::Slot,
@@ -161,6 +165,31 @@ impl CertificateType {
161165
_ => None,
162166
}
163167
}
168+
169+
/// Returns the stake fraction required for certificate completion and the
170+
/// `VoteType`s that contribute to this certificate.
171+
///
172+
/// Must be in sync with `Vote::to_cert_types`
173+
pub const fn limits_and_vote_types(&self) -> (Fraction, &'static [VoteType]) {
174+
match self {
175+
CertificateType::Notarize(_, _) => {
176+
(Fraction::from_percentage(60), &[VoteType::Notarize])
177+
}
178+
CertificateType::NotarizeFallback(_, _) => (
179+
Fraction::from_percentage(60),
180+
&[VoteType::Notarize, VoteType::NotarizeFallback],
181+
),
182+
CertificateType::FinalizeFast(_, _) => {
183+
(Fraction::from_percentage(80), &[VoteType::Notarize])
184+
}
185+
CertificateType::Finalize(_) => (Fraction::from_percentage(60), &[VoteType::Finalize]),
186+
CertificateType::Skip(_) => (
187+
Fraction::from_percentage(60),
188+
&[VoteType::Skip, VoteType::SkipFallback],
189+
),
190+
CertificateType::Genesis(_, _) => (GENESIS_VOTE_THRESHOLD, &[VoteType::Genesis]),
191+
}
192+
}
164193
}
165194

166195
/// The actual certificate with the aggregate signature and bitmap for which validators are included in the aggregate.

votor-messages/src/migration.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
//! TowerBFT slots pre alpenglow genesis in order to help other cluster participants catchup.
4444
//! - When in `FullAlpenglowEpoch` we completely shutdown these TowerBFT threads (AncestorHashesService and ClusterSlotsService)
4545
use {
46-
crate::consensus_message::{Block, Certificate, CertificateType},
46+
crate::{
47+
consensus_message::{Block, Certificate, CertificateType},
48+
fraction::Fraction,
49+
},
4750
log::*,
4851
solana_address::Address,
4952
solana_clock::{Epoch, Slot},
@@ -77,7 +80,7 @@ pub const MIGRATION_MALICIOUS_THRESHOLD: f64 = 20.0 / 100.0;
7780
/// `SWITCH_FORK_THRESHOLD` - (1 - `GENESIS_VOTE_THRESHOLD`) = `MIGRATION_MALICIOUS_THRESHOLD` malicious stake.
7881
///
7982
/// Using 38% as the `SWITCH_FORK_THRESHOLD` gives us 82% for `GENESIS_VOTE_THRESHOLD`.
80-
pub const GENESIS_VOTE_THRESHOLD: f64 = 82.0 / 100.0;
83+
pub const GENESIS_VOTE_THRESHOLD: Fraction = Fraction::from_percentage(82);
8184

8285
/// The interval at which we refresh our genesis vote
8386
pub const GENESIS_VOTE_REFRESH: Duration = Duration::from_millis(400);

votor/src/common.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
22
agave_votor_messages::{
33
consensus_message::CertificateType,
4-
migration::GENESIS_VOTE_THRESHOLD,
54
vote::{Vote, VoteType},
65
},
76
std::time::Duration,
@@ -31,25 +30,6 @@ pub const fn conflicting_types(vote_type: VoteType) -> &'static [VoteType] {
3130
}
3231
}
3332

34-
/// Lookup from `CertificateId` to the `VoteType`s that contribute,
35-
/// as well as the stake fraction required for certificate completion.
36-
///
37-
/// Must be in sync with `vote_to_cert_types`
38-
pub(crate) const fn certificate_limits_and_vote_types(
39-
cert_type: &CertificateType,
40-
) -> (f64, &'static [VoteType]) {
41-
match cert_type {
42-
CertificateType::Notarize(_, _) => (0.6, &[VoteType::Notarize]),
43-
CertificateType::NotarizeFallback(_, _) => {
44-
(0.6, &[VoteType::Notarize, VoteType::NotarizeFallback])
45-
}
46-
CertificateType::FinalizeFast(_, _) => (0.8, &[VoteType::Notarize]),
47-
CertificateType::Finalize(_) => (0.6, &[VoteType::Finalize]),
48-
CertificateType::Skip(_) => (0.6, &[VoteType::Skip, VoteType::SkipFallback]),
49-
CertificateType::Genesis(_, _) => (GENESIS_VOTE_THRESHOLD, &[VoteType::Genesis]),
50-
}
51-
}
52-
5333
/// Lookup from `Vote` to the `CertificateId`s the vote accounts for
5434
///
5535
/// Must be in sync with `certificate_limits_and_vote_types` and `VoteType::get_type`

votor/src/consensus_pool.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use {
33
crate::{
44
commitment::CommitmentError,
55
common::{
6-
certificate_limits_and_vote_types, conflicting_types, vote_to_cert_types, Stake,
7-
MAX_ENTRIES_PER_PUBKEY_FOR_NOTARIZE_LITE, MAX_ENTRIES_PER_PUBKEY_FOR_OTHER_TYPES,
6+
conflicting_types, vote_to_cert_types, Stake, MAX_ENTRIES_PER_PUBKEY_FOR_NOTARIZE_LITE,
7+
MAX_ENTRIES_PER_PUBKEY_FOR_OTHER_TYPES,
88
},
99
consensus_pool::{
1010
parent_ready_tracker::ParentReadyTracker,
@@ -16,6 +16,7 @@ use {
1616
},
1717
agave_votor_messages::{
1818
consensus_message::{Block, Certificate, CertificateType, ConsensusMessage, VoteMessage},
19+
fraction::Fraction,
1920
migration::MigrationStatus,
2021
vote::{Vote, VoteType},
2122
},
@@ -29,6 +30,7 @@ use {
2930
std::{
3031
cmp::Ordering,
3132
collections::{BTreeMap, HashMap},
33+
num::NonZeroU64,
3234
sync::Arc,
3335
},
3436
thiserror::Error,
@@ -208,7 +210,7 @@ impl ConsensusPool {
208210
continue;
209211
}
210212
// Otherwise check whether the certificate is complete
211-
let (limit, vote_types) = certificate_limits_and_vote_types(&cert_type);
213+
let (limit, vote_types) = cert_type.limits_and_vote_types();
212214
let accumulated_stake = vote_types
213215
.iter()
214216
.filter_map(|vote_type| {
@@ -223,7 +225,8 @@ impl ConsensusPool {
223225
})
224226
})
225227
.sum::<Stake>();
226-
if accumulated_stake as f64 / (total_stake as f64) < limit {
228+
let total_stake = NonZeroU64::new(total_stake).unwrap();
229+
if Fraction::new(accumulated_stake, total_stake) < limit {
227230
continue;
228231
}
229232
let mut cert_builder = CertificateBuilder::new(cert_type);

votor/src/consensus_pool/certificate_builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use {
2-
crate::common::certificate_limits_and_vote_types,
32
agave_votor_messages::consensus_message::{Certificate, CertificateType, VoteMessage},
43
bitvec::prelude::*,
54
solana_bls_signatures::{BlsError, SignatureProjective},
@@ -143,7 +142,7 @@ impl BuilderType {
143142
cert_type: &CertificateType,
144143
msgs: &[VoteMessage],
145144
) -> Result<(), AggregateError> {
146-
let vote_types = certificate_limits_and_vote_types(cert_type).1;
145+
let vote_types = cert_type.limits_and_vote_types().1;
147146
match self {
148147
Self::Skip {
149148
signature0,

0 commit comments

Comments
 (0)