Skip to main content

WeightedQuorumSet

Struct WeightedQuorumSet 

Source
pub struct WeightedQuorumSet<ID> { /* private fields */ }
Expand description

A quorum rule that accepts an ID set whose IDs weigh at least threshold together.

Each ID of the rule has a positive weight. A set counts the weight of each of its IDs once, and an ID that the rule does not weigh adds nothing. A sum that would exceed u64::MAX exceeds every threshold, so is_quorum() saturates instead of overflowing. A threshold above the total weight gives a rule that accepts no set.

Two disjoint sets together weigh at most the total weight. So two rules with the same weights have quorum intersection when their thresholds sum to more than the total weight. This condition is sufficient but not necessary, because a set adds all of an ID’s weight or none of it. With weights {1:2, 2:2}, thresholds 3 and 1 sum to only the total weight 4, yet the only quorum of threshold 3, {1,2}, meets every quorum of threshold 1.

§Examples

use std::collections::BTreeMap;

use quorum_set::{QuorumIntersection, QuorumSet, WeightedQuorumSet};

// ID 1 weighs as much as IDs 2 and 3 together; the total weight is 4.
let weights = BTreeMap::from([(1, 2), (2, 1), (3, 1)]);
let read = WeightedQuorumSet::new(3, weights.clone()).unwrap();
let write = WeightedQuorumSet::new(2, weights).unwrap();

let write_accepts_one = write.is_quorum([1].iter());
assert!(write_accepts_one);
let read_accepts_two_three = read.is_quorum([2, 3].iter());
assert!(!read_accepts_two_three);

// 3 + 2 > 4, so every read quorum intersects every write quorum.
let intersects = read.intersects_with(&write);
assert_eq!(Some(true), intersects);

Implementations§

Source§

impl<ID> WeightedQuorumSet<ID>
where ID: Ord + Clone,

Source

pub fn new( threshold: u64, weights: BTreeMap<ID, u64>, ) -> Result<Self, WeightedQuorumSetError<ID>>

Builds a rule that accepts an ID set whose IDs weigh at least threshold together, with the weight of each ID in weights.

§Errors
Source

pub fn weights(&self) -> &BTreeMap<ID, u64>

Returns the weight of each ID. QuorumSet::ids() yields its IDs.

Source

pub fn threshold(&self) -> u64

Returns the weight that an accepted set must reach.

Trait Implementations§

Source§

impl<ID: Clone> Clone for WeightedQuorumSet<ID>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<ID: Debug> Debug for WeightedQuorumSet<ID>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<ID: Eq> Eq for WeightedQuorumSet<ID>

Source§

impl<ID: PartialEq> PartialEq for WeightedQuorumSet<ID>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<ID> QuorumIntersection<WeightedQuorumSet<ID>> for WeightedQuorumSet<ID>
where ID: Ord + Clone,

Sufficient quorum intersection for two weighted rules.

Two disjoint sets together weigh at most the total weight. When two rules hold the same weights and their thresholds sum to more than the total weight, a quorum of one and a quorum of the other are therefore never disjoint. The check runs in O(n) for n IDs.

Source§

fn intersects_with(&self, other: &WeightedQuorumSet<ID>) -> Option<bool>

Return Some(true) when both rules hold the same weights and their thresholds sum to more than the total weight, and None otherwise, including for every pair of rules with different weights. This check never returns Some(false).

The sums are computed in u128, so they never overflow.

Source§

impl<ID> QuorumSet for WeightedQuorumSet<ID>
where ID: Ord + Clone,

Source§

type Id = ID

Node ID type in this quorum set.
Source§

type Iter = IntoIter<ID>

Iterator over every voter ID tracked by this quorum set. Read more
Source§

fn is_quorum<'a, I>(&self, ids: I) -> bool
where ID: 'a, I: Iterator<Item = &'a ID> + Clone,

Return true if the candidate IDs satisfy this quorum rule. Read more
Source§

fn ids(&self) -> Self::Iter

Return all voter IDs in this quorum set.
Source§

impl<ID: PartialEq> StructuralPartialEq for WeightedQuorumSet<ID>

Auto Trait Implementations§

§

impl<ID> Forget for WeightedQuorumSet<ID>

§

impl<ID> Freeze for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: Freeze,

§

impl<ID> RefUnwindSafe for WeightedQuorumSet<ID>

§

impl<ID> Send for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: Send,

§

impl<ID> Sync for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: Sync,

§

impl<ID> Unpin for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: Unpin,

§

impl<ID> UnsafeUnpin for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: UnsafeUnpin,

§

impl<ID> UnwindSafe for WeightedQuorumSet<ID>
where BTreeMap<ID, u64>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.