Skip to content

Commit

Permalink
Merge pull request #2160 from dusk-network/2100-unify-majority-and-su…
Browse files Browse the repository at this point in the history
…permajority

Re-define Majority and Supermajority
  • Loading branch information
fed-franz authored Aug 20, 2024
2 parents e5f6395 + 09e4846 commit b4aa394
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
61 changes: 38 additions & 23 deletions consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@ use std::time::Duration;
/// Maximum number of iterations Consensus runs per a single round.
pub const CONSENSUS_MAX_ITER: u8 = 50;

/// Percentage number that determines quorums.
pub const SUPERMAJORITY_THRESHOLD: f64 = 0.67;
pub const MAJORITY_THRESHOLD: f64 = 0.5;

/// Total credits of steps committees
pub const PROPOSAL_COMMITTEE_CREDITS: usize = 1;
pub const VALIDATION_COMMITTEE_CREDITS: usize = 64;
pub const VALIDATION_COMMITTEE_QUORUM: f64 =
VALIDATION_COMMITTEE_CREDITS as f64 * SUPERMAJORITY_THRESHOLD;

pub const RATIFICATION_COMMITTEE_CREDITS: usize = 64;
pub const RATIFICATION_COMMITTEE_QUORUM: f64 =
RATIFICATION_COMMITTEE_CREDITS as f64 * SUPERMAJORITY_THRESHOLD;

pub const DEFAULT_BLOCK_GAS_LIMIT: u64 = 5 * 1_000_000_000;

Expand All @@ -35,32 +26,56 @@ pub const MAX_STEP_TIMEOUT: Duration = Duration::from_secs(40);
pub const TIMEOUT_INCREASE: Duration = Duration::from_secs(2);
pub const MINIMUM_BLOCK_TIME: u64 = 10;

/// Returns delta between full quorum and super_majority
pub fn validation_extra() -> usize {
VALIDATION_COMMITTEE_CREDITS - validation_committee_quorum()
// Returns `floor(value/2) + 1`
pub fn majority(value: usize) -> usize {
value / 2 + 1
}

pub fn ratification_extra() -> usize {
RATIFICATION_COMMITTEE_CREDITS - ratification_committee_quorum()
// Returns `ceil( value/3*2 )`
pub fn supermajority(value: usize) -> usize {
let sm = value as f32 / 3.0 * 2.0;
sm.ceil() as usize
}

/// Returns the quorum of a Ratification committee
pub fn ratification_quorum() -> usize {
supermajority(RATIFICATION_COMMITTEE_CREDITS)
}

/// Returns the quorum of a Validation committee
pub fn validation_quorum() -> usize {
supermajority(VALIDATION_COMMITTEE_CREDITS)
}

/// Returns ceil of RATIFICATION_COMMITTEE_QUORUM
pub fn ratification_committee_quorum() -> usize {
RATIFICATION_COMMITTEE_QUORUM.ceil() as usize
/// Returns the number of credits beyond the quorum for a Validation committee
pub fn validation_extra() -> usize {
VALIDATION_COMMITTEE_CREDITS - validation_quorum()
}

/// Returns ceil of VALIDATION_COMMITTEE_QUORUM
pub fn validation_committee_quorum() -> usize {
VALIDATION_COMMITTEE_QUORUM.ceil() as usize
/// Returns the number of credits beyond the quorum for a Ratification committee
pub fn ratification_extra() -> usize {
RATIFICATION_COMMITTEE_CREDITS - ratification_quorum()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_majorities() {
assert_eq!(majority(4), 3);
assert_eq!(majority(11), 6);
assert_eq!(majority(99), 50);
assert_eq!(supermajority(3), 2);
assert_eq!(supermajority(9), 6);
assert_eq!(supermajority(51), 34);
}

#[test]
fn test_quorum_consts() {
assert_eq!(validation_committee_quorum(), 43);
assert_eq!(ratification_committee_quorum(), 43);
fn test_quorums() {
assert_eq!(majority(VALIDATION_COMMITTEE_CREDITS), 33);
assert_eq!(validation_quorum(), 43);
assert_eq!(ratification_quorum(), 43);
assert_eq!(validation_extra(), 21);
assert_eq!(ratification_extra(), 21);
}
Expand Down
11 changes: 4 additions & 7 deletions consensus/src/user/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::user::provisioners::Provisioners;
use crate::user::sortition;

use super::cluster::Cluster;
use crate::config;
use crate::config::{majority, supermajority};
use node_data::bls::{PublicKey, PublicKeyBytes};
use std::collections::{BTreeMap, HashMap};
use std::fmt;
Expand Down Expand Up @@ -36,13 +36,10 @@ impl Committee {
pub fn new(provisioners: &Provisioners, cfg: &sortition::Config) -> Self {
// Generate committee using deterministic sortition.
let extracted = provisioners.create_committee(cfg);
let committee_credits = cfg.committee_credits() as f64;
let committee_credits = cfg.committee_credits();

let super_majority = (committee_credits
* config::SUPERMAJORITY_THRESHOLD)
.ceil() as usize;
let majority =
(committee_credits * config::MAJORITY_THRESHOLD) as usize + 1;
let majority = majority(committee_credits);
let super_majority = supermajority(committee_credits);

// Turn the raw vector into a hashmap where we map a pubkey to its
// occurrences.
Expand Down
8 changes: 4 additions & 4 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use tracing::{debug, info, warn};

use dusk_bytes::{DeserializableSlice, Serializable};
use dusk_consensus::config::{
ratification_committee_quorum, ratification_extra,
validation_committee_quorum, validation_extra,
RATIFICATION_COMMITTEE_CREDITS, VALIDATION_COMMITTEE_CREDITS,
ratification_extra, ratification_quorum, validation_extra,
validation_quorum, RATIFICATION_COMMITTEE_CREDITS,
VALIDATION_COMMITTEE_CREDITS,
};
use dusk_consensus::operations::{CallParams, VerificationOutput, Voter};
use execution_core::{
Expand Down Expand Up @@ -773,7 +773,7 @@ fn calc_generator_extra_reward(
let reward_per_quota = generator_extra_reward
/ (validation_extra() + ratification_extra()) as u64;

let sum = ratification_committee_quorum() + validation_committee_quorum();
let sum = ratification_quorum() + validation_quorum();
credits.saturating_sub(sum as u64) * reward_per_quota
}

Expand Down

0 comments on commit b4aa394

Please sign in to comment.