-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make threshold configurable for TreeOverlay
- Loading branch information
1 parent
f57bfc8
commit b58e35b
Showing
6 changed files
with
78 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use fraction::{Fraction, GenericFraction, ToPrimitive}; | ||
|
||
const LEADER_SUPER_MAJORITY_THRESHOLD_NUM: u64 = 2; | ||
const LEADER_SUPER_MAJORITY_THRESHOLD_DEN: u64 = 3; | ||
|
||
pub fn default_leader_super_majority_threshold() -> GenericFraction<u64> { | ||
Fraction::new( | ||
LEADER_SUPER_MAJORITY_THRESHOLD_NUM, | ||
LEADER_SUPER_MAJORITY_THRESHOLD_DEN, | ||
) | ||
} | ||
|
||
pub fn apply_threshold(size: usize, threshold: GenericFraction<u64>) -> usize { | ||
// `threshold` is a tuple of (num, den) where `num/den` is the super majority threshold | ||
(Fraction::from(size) * threshold) | ||
.floor() | ||
.to_usize() | ||
.unwrap() | ||
} | ||
|
||
pub mod deser_fraction { | ||
use fraction::Fraction; | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
use std::str::FromStr; | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Fraction>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
<Option<String>>::deserialize(deserializer)? | ||
.map(|s| FromStr::from_str(&s).map_err(de::Error::custom)) | ||
.transpose() | ||
} | ||
|
||
pub fn serialize<S>(value: &Option<Fraction>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
value.map(|v| v.to_string()).serialize(serializer) | ||
} | ||
} |
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