Skip to content

Commit

Permalink
Merge pull request #1929 from dusk-network/fix-1833
Browse files Browse the repository at this point in the history
Split Block Generator reward into fixed reward and extra reward
  • Loading branch information
goshawk-3 authored Jul 10, 2024
2 parents c1c39c5 + 71b8cec commit 88b35bb
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 124 deletions.
2 changes: 1 addition & 1 deletion consensus/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mod tests {
}

// Execute sortition with specific config
let cfg = Config::raw(Seed::from([4u8; 48]), round, 1, 10, None);
let cfg = Config::raw(Seed::from([4u8; 48]), round, 1, 10, vec![]);
let c = Committee::new(&p, &cfg);

let target_quorum = 7;
Expand Down
36 changes: 36 additions & 0 deletions consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ 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 @@ -29,3 +34,34 @@ pub const MIN_STEP_TIMEOUT: Duration = Duration::from_secs(7);
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()
}

pub fn ratification_extra() -> usize {
RATIFICATION_COMMITTEE_CREDITS - ratification_committee_quorum()
}

/// Returns ceil of RATIFICATION_COMMITTEE_QUORUM
pub fn ratification_committee_quorum() -> usize {
RATIFICATION_COMMITTEE_QUORUM.ceil() as usize
}

/// Returns ceil of VALIDATION_COMMITTEE_QUORUM
pub fn validation_committee_quorum() -> usize {
VALIDATION_COMMITTEE_QUORUM.ceil() as usize
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quorum_consts() {
assert_eq!(validation_committee_quorum(), 43);
assert_eq!(ratification_committee_quorum(), 43);
assert_eq!(validation_extra(), 21);
assert_eq!(ratification_extra(), 21);
}
}
20 changes: 15 additions & 5 deletions consensus/src/execution_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
committee.is_member(&self.round_update.pubkey_bls)
}

pub(crate) fn save_committee(&mut self, committee: Committee) {
self.iter_ctx.committees.insert(self.step(), committee);
pub(crate) fn save_committee(&mut self, step: u16, committee: Committee) {
self.iter_ctx.committees.insert(step, committee);
}

pub(crate) fn get_current_committee(&self) -> Option<&Committee> {
Expand Down Expand Up @@ -276,6 +276,9 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
let committee = self
.get_current_committee()
.expect("committee to be created before run");

let generator = self.get_curr_generator();

// Check if message is valid in the context of current step
let valid = phase.lock().await.is_valid(
&msg,
Expand Down Expand Up @@ -335,7 +338,7 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
let collected = phase
.lock()
.await
.collect(msg, &self.round_update, committee)
.collect(msg, &self.round_update, committee, generator)
.await;

match collected {
Expand Down Expand Up @@ -380,6 +383,9 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
let committee = self
.get_current_committee()
.expect("committee to be created before run");

let generator = self.get_curr_generator();

if let Some(messages) = self
.future_msgs
.lock()
Expand Down Expand Up @@ -421,7 +427,7 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
if let Ok(HandleMsgOutput::Ready(msg)) = phase
.lock()
.await
.collect(msg, &self.round_update, committee)
.collect(msg, &self.round_update, committee, generator)
.await
{
return Some(msg);
Expand All @@ -435,7 +441,7 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {

pub fn get_sortition_config(
&self,
exclusion: Option<PublicKeyBytes>,
exclusion: Vec<PublicKeyBytes>,
) -> sortition::Config {
sortition::Config::new(
self.round_update.seed(),
Expand Down Expand Up @@ -463,4 +469,8 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
)
.await;
}

pub(crate) fn get_curr_generator(&self) -> Option<PublicKeyBytes> {
self.iter_ctx.get_generator(self.iteration)
}
}
16 changes: 11 additions & 5 deletions consensus/src/iteration_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,29 @@ impl<D: Database> IterationCtx<D> {
msg: Message,
) -> Option<Message> {
let committee = self.committees.get_committee(msg.get_step())?;
let generator = self.get_generator(msg.header.iteration);

match msg.topic() {
node_data::message::Topics::Candidate => {
let mut handler = self.proposal_handler.lock().await;
_ = handler.collect_from_past(msg, ru, committee).await;
_ = handler
.collect_from_past(msg, ru, committee, generator)
.await;
}
node_data::message::Topics::Validation => {
let mut handler = self.validation_handler.lock().await;
if let Ok(HandleMsgOutput::Ready(m)) =
handler.collect_from_past(msg, ru, committee).await
if let Ok(HandleMsgOutput::Ready(m)) = handler
.collect_from_past(msg, ru, committee, generator)
.await
{
return Some(m);
}
}
node_data::message::Topics::Ratification => {
let mut handler = self.ratification_handler.lock().await;
if let Ok(HandleMsgOutput::Ready(m)) =
handler.collect_from_past(msg, ru, committee).await
if let Ok(HandleMsgOutput::Ready(m)) = handler
.collect_from_past(msg, ru, committee, generator)
.await
{
return Some(m);
}
Expand Down
3 changes: 3 additions & 0 deletions consensus/src/msg_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::commons::{ConsensusError, RoundUpdate};
use crate::iteration_ctx::RoundCommittees;
use crate::user::committee::Committee;
use async_trait::async_trait;
use node_data::bls::PublicKeyBytes;
use node_data::message::{Message, Status};
use node_data::StepName;
use tracing::{debug, trace};
Expand Down Expand Up @@ -83,6 +84,7 @@ pub trait MsgHandler {
msg: Message,
ru: &RoundUpdate,
committee: &Committee,
generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError>;

/// collect allows each Phase to process a verified message from a former
Expand All @@ -92,6 +94,7 @@ pub trait MsgHandler {
msg: Message,
ru: &RoundUpdate,
committee: &Committee,
generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError>;

/// handle_timeout allows each Phase to handle a timeout event.
Expand Down
33 changes: 30 additions & 3 deletions consensus/src/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::commons::{ConsensusError, Database};
use crate::config::CONSENSUS_MAX_ITER;
use crate::execution_ctx::ExecutionCtx;
use crate::operations::Operations;
use crate::user::committee::Committee;
Expand Down Expand Up @@ -62,13 +63,26 @@ impl<T: Operations + 'static, D: Database + 'static> Phase<T, D> {
debug!(event = "execute_step", ?timeout);

let exclusion = match step_name {
StepName::Proposal => None,
StepName::Proposal => vec![],
_ => {
let mut exclusion_list = vec![];
let generator = ctx
.iter_ctx
.get_generator(ctx.iteration)
.expect("Proposal committee to be already generated");
Some(generator)

exclusion_list.push(generator);

if ctx.iteration < CONSENSUS_MAX_ITER {
let next_generator =
ctx.iter_ctx.get_generator(ctx.iteration + 1).expect(
"Next Proposal committee to be already generated",
);

exclusion_list.push(next_generator);
}

exclusion_list
}
};

Expand All @@ -82,12 +96,25 @@ impl<T: Operations + 'static, D: Database + 'static> Phase<T, D> {
&ctx.get_sortition_config(exclusion),
);

if let StepName::Proposal = step_name {
if ctx.iteration < CONSENSUS_MAX_ITER {
let mut cfg_next_iteration = ctx.get_sortition_config(vec![]);
cfg_next_iteration.step =
StepName::Proposal.to_step(ctx.iteration + 1);

ctx.save_committee(
cfg_next_iteration.step,
Committee::new(ctx.provisioners, &cfg_next_iteration),
);
}
}

debug!(
event = "committee_generated",
members = format!("{}", &step_committee)
);

ctx.save_committee(step_committee);
ctx.save_committee(ctx.step(), step_committee);

// Execute step
await_phase!(self, run(ctx))
Expand Down
2 changes: 2 additions & 0 deletions consensus/src/proposal/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<D: Database> MsgHandler for ProposalHandler<D> {
msg: Message,
_ru: &RoundUpdate,
_committee: &Committee,
_generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError> {
// store candidate block
let p = Self::unwrap_msg(&msg)?;
Expand All @@ -60,6 +61,7 @@ impl<D: Database> MsgHandler for ProposalHandler<D> {
_msg: Message,
_ru: &RoundUpdate,
_committee: &Committee,
_generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError> {
Ok(HandleMsgOutput::Pending)
}
Expand Down
3 changes: 2 additions & 1 deletion consensus/src/proposal/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {
let committee = ctx
.get_current_committee()
.expect("committee to be created before run");

if ctx.am_member(committee) {
let iteration =
cmp::min(config::RELAX_ITERATION_THRESHOLD, ctx.iteration);
Expand Down Expand Up @@ -84,7 +85,7 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {
.handler
.lock()
.await
.collect(msg, &ctx.round_update, committee)
.collect(msg, &ctx.round_update, committee, None)
.await
{
Ok(HandleMsgOutput::Ready(msg)) => return Ok(msg),
Expand Down
16 changes: 15 additions & 1 deletion consensus/src/quorum/verifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::user::cluster::Cluster;
use crate::user::committee::{Committee, CommitteeSet};
use crate::user::sortition;

use crate::config::CONSENSUS_MAX_ITER;
use dusk_bytes::Serializable as BytesSerializable;
use execution_core::{StakeAggPublicKey, StakeSignature};
use tokio::sync::RwLock;
Expand Down Expand Up @@ -78,14 +79,27 @@ pub async fn verify_step_votes(
let round = header.round;
let iteration = header.iteration;

let mut exclusion_list = vec![];
let generator = committees_set
.read()
.await
.provisioners()
.get_generator(iteration, seed, round);

exclusion_list.push(generator);

if iteration < CONSENSUS_MAX_ITER {
let next_generator = committees_set
.read()
.await
.provisioners()
.get_generator(iteration + 1, seed, round);

exclusion_list.push(next_generator);
}

let cfg =
sortition::Config::new(seed, round, iteration, step, Some(generator));
sortition::Config::new(seed, round, iteration, step, exclusion_list);

if committees_set.read().await.get(&cfg).is_none() {
let _ = committees_set.write().await.get_or_create(&cfg);
Expand Down
7 changes: 5 additions & 2 deletions consensus/src/ratification/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::commons::{ConsensusError, RoundUpdate};
use crate::msg_handler::{HandleMsgOutput, MsgHandler};
use crate::step_votes_reg::SafeAttestationInfoRegistry;
use async_trait::async_trait;
use node_data::bls::PublicKeyBytes;
use node_data::ledger::Attestation;
use node_data::{ledger, StepName};
use tracing::{error, warn};
Expand Down Expand Up @@ -71,6 +72,7 @@ impl MsgHandler for RatificationHandler {
msg: Message,
ru: &RoundUpdate,
committee: &Committee,
generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError> {
let p = Self::unwrap_msg(msg)?;
let iteration = p.header().iteration;
Expand Down Expand Up @@ -104,7 +106,7 @@ impl MsgHandler for RatificationHandler {
sv,
StepName::Ratification,
quorum_reached,
committee.excluded().expect("Generator to be excluded"),
&generator.expect("There must be a valid generator"),
);

if quorum_reached {
Expand All @@ -126,6 +128,7 @@ impl MsgHandler for RatificationHandler {
msg: Message,
_ru: &RoundUpdate,
committee: &Committee,
generator: Option<PublicKeyBytes>,
) -> Result<HandleMsgOutput, ConsensusError> {
let p = Self::unwrap_msg(msg)?;

Expand All @@ -142,7 +145,7 @@ impl MsgHandler for RatificationHandler {
sv,
StepName::Ratification,
quorum_reached,
committee.excluded().expect("Generator to be excluded"),
&generator.expect("There must be a valid generator"),
)
{
return Ok(HandleMsgOutput::Ready(quorum_msg));
Expand Down
4 changes: 3 additions & 1 deletion consensus/src/ratification/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ impl<DB: Database> RatificationStep<DB> {
.get_current_committee()
.expect("committee to be created before run");

let generator = ctx.get_curr_generator();

if ctx.am_member(committee) {
let mut handler = self.handler.lock().await;
let vote = handler.validation_result().vote();
Expand All @@ -137,7 +139,7 @@ impl<DB: Database> RatificationStep<DB> {

// Collect my own vote
let res = handler
.collect(vote_msg, &ctx.round_update, committee)
.collect(vote_msg, &ctx.round_update, committee, generator)
.await?;
if let HandleMsgOutput::Ready(m) = res {
return Ok(m);
Expand Down
Loading

0 comments on commit 88b35bb

Please sign in to comment.