Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: Reward Voters/Validators of previous block #1873

Merged
merged 31 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9a4d12b
consensus: Pass a list of voters to be rewarded to both EST and VST c…
Jun 20, 2024
9384784
node: Add voters list to both accept and vst methods
Jun 20, 2024
f984978
rusk: Reward voters with cumulative reward
Jun 20, 2024
76f9207
WIP: Generate a list of Voters from previous block
Jun 20, 2024
c603ea4
node: Return a merged set of voters of previous block
Jun 21, 2024
1700ee0
consensus: Add members method
Jun 21, 2024
3316682
consensus: Return a copy of the generated committee
Jun 21, 2024
31a04d6
node: Return both prev_block_voters and tip_block_voters lists
Jun 21, 2024
192be7e
node: Initialize consensus round with tip_block_voters. Execute VM::a…
Jun 21, 2024
e9f7adc
rusk: Update coinbase_value
Jun 21, 2024
ef363b8
consensus: Rename prev_block_voters to att_voters
Jun 25, 2024
f2287e8
node: Provide the voters of tip attestation to consensus engine
Jun 25, 2024
a175d3a
consensus: Fix aggregator unit test
Jun 26, 2024
2b98f97
rusk: Update coinbase_value doc
Jun 26, 2024
4a20d11
node: Update verify_failed_iterations
Jun 26, 2024
f3f7c07
Merge branch 'master' into fix-1832
goshawk-3 Jun 26, 2024
a1b9cd8
rusk: Fix unit tests
Jun 26, 2024
88283f2
rusk: 10/80/10 reward values to Dusk/Generator/Validators
Jun 27, 2024
635e567
Merge branch 'master' into fix-1832
goshawk-3 Jun 27, 2024
fd71f5b
rusk: Trace reward values in log debug
Jun 27, 2024
3f9f688
consensus: Pass proper set of voters to VST call
Jun 28, 2024
b8a8b84
node: Regenerate committees with provisioners_list.prev() on consensu…
Jun 28, 2024
a14437a
rusk: Handle InvalidCreditsCount. Fix credit_reward calc
Jun 28, 2024
f3c94e7
node: Rename get_voters to get_att_voters
Jun 28, 2024
91ff119
rusk: Wrap voters in Option
Jul 2, 2024
4446ac8
node: Wrap voters in Option
Jul 2, 2024
53205ee
consensus: Wrap voters in Option
Jul 2, 2024
4bfdcbf
Merge branch 'master' into fix-1832
goshawk-3 Jul 3, 2024
15579b7
node: Rename verify_block_att to verify_success_att
Jul 3, 2024
76c34cb
node: Address comments
Jul 3, 2024
7a87345
node: Move merge_committees in verify_block_att
Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions consensus/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ mod tests {
secret_key,
&tip_header,
HashMap::new(),
vec![],
);

let msg = crate::build_validation_payload(
Expand Down
8 changes: 8 additions & 0 deletions consensus/src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use thiserror::Error;

use crate::operations::VoterWithCredits;
use execution_core::{BlsSigError, StakeSecretKey};
use node_data::bls::PublicKey;
use node_data::message::{AsyncQueue, Message, Payload};
Expand All @@ -34,6 +35,7 @@ pub struct RoundUpdate {
seed: Seed,
hash: [u8; 32],
att: Attestation,
att_voters: Vec<VoterWithCredits>,
timestamp: u64,

pub base_timeouts: TimeoutSet,
Expand All @@ -45,6 +47,7 @@ impl RoundUpdate {
secret_key: StakeSecretKey,
tip_header: &Header,
base_timeouts: TimeoutSet,
att_voters: Vec<VoterWithCredits>,
) -> Self {
let round = tip_header.height + 1;
RoundUpdate {
Expand All @@ -56,6 +59,7 @@ impl RoundUpdate {
seed: tip_header.seed,
timestamp: tip_header.timestamp,
base_timeouts,
att_voters,
}
}

Expand All @@ -74,6 +78,10 @@ impl RoundUpdate {
pub fn timestamp(&self) -> u64 {
self.timestamp
}

pub fn att_voters(&self) -> &Vec<VoterWithCredits> {
&self.att_voters
}
}

#[derive(Debug, Clone, Copy, Error)]
Expand Down
5 changes: 2 additions & 3 deletions consensus/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Consensus<T: Operations, D: Database> {
future_msgs: Arc<Mutex<MsgRegistry<Message>>>,

/// Reference to the executor of any EST-related call
executor: Arc<Mutex<T>>,
executor: Arc<T>,

// Database
db: Arc<Mutex<D>>,
Expand All @@ -58,7 +58,7 @@ impl<T: Operations + 'static, D: Database + 'static> Consensus<T, D> {
inbound: AsyncQueue<Message>,
outbound: AsyncQueue<Message>,
future_msgs: Arc<Mutex<MsgRegistry<Message>>>,
executor: Arc<Mutex<T>>,
executor: Arc<T>,
db: Arc<Mutex<D>>,
) -> Self {
Self {
Expand Down Expand Up @@ -169,7 +169,6 @@ impl<T: Operations + 'static, D: Database + 'static> Consensus<T, D> {
validation_handler.clone(),
)),
Phase::Ratification(ratification::step::RatificationStep::new(
executor.clone(),
ratification_handler.clone(),
)),
];
Expand Down
8 changes: 3 additions & 5 deletions consensus/src/execution_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct ExecutionCtx<'a, DB: Database, T> {
step: StepName,
step_start_time: Option<Instant>,

pub client: Arc<Mutex<T>>,
pub client: Arc<T>,

pub sv_registry: SafeAttestationInfoRegistry,
quorum_sender: QuorumMsgSender,
Expand All @@ -68,7 +68,7 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {
round_update: RoundUpdate,
iteration: u8,
step: StepName,
client: Arc<Mutex<T>>,
client: Arc<T>,
sv_registry: SafeAttestationInfoRegistry,
quorum_sender: QuorumMsgSender,
) -> Self {
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {

if let Some(committee) = self.iter_ctx.committees.get_committee(step) {
if self.am_member(committee) {
RatificationStep::<T, DB>::try_vote(
RatificationStep::<DB>::try_vote(
&self.round_update,
msg_iteration,
validation,
Expand Down Expand Up @@ -456,8 +456,6 @@ impl<'a, DB: Database, T: Operations + 'static> ExecutionCtx<'a, DB, T> {

let _ = self
.client
.lock()
.await
.add_step_elapsed_time(
self.round_update.round,
self.step_name(),
Expand Down
5 changes: 4 additions & 1 deletion consensus/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use node_data::StepName;

pub type StateRoot = [u8; 32];
pub type EventHash = [u8; 32];
pub type VoterWithCredits = (StakePublicKey, usize);

#[derive(Debug)]
pub enum Error {
Expand All @@ -26,6 +27,7 @@ pub struct CallParams {
pub block_gas_limit: u64,
pub generator_pubkey: node_data::bls::PublicKey,
pub missed_generators: Vec<StakePublicKey>,
pub voters_pubkey: Option<Vec<VoterWithCredits>>,
}

#[derive(Default)]
Expand Down Expand Up @@ -58,11 +60,12 @@ pub trait Operations: Send + Sync {
&self,
candidate_header: &Header,
disable_winning_att_check: bool,
) -> Result<(), Error>;
) -> Result<(u8, Vec<VoterWithCredits>, Vec<VoterWithCredits>), Error>;

async fn verify_state_transition(
&self,
blk: &Block,
voters: &[VoterWithCredits],
) -> Result<VerificationOutput, Error>;

async fn execute_state_transition(
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! await_phase {
pub enum Phase<T: Operations, D: Database> {
Proposal(proposal::step::ProposalStep<T, D>),
Validation(validation::step::ValidationStep<T>),
Ratification(ratification::step::RatificationStep<T, D>),
Ratification(ratification::step::RatificationStep<D>),
}

impl<T: Operations + 'static, D: Database + 'static> Phase<T, D> {
Expand Down
25 changes: 14 additions & 11 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::commons::{get_current_timestamp, RoundUpdate};
use crate::operations::{CallParams, Operations};
use crate::operations::{CallParams, Operations, VoterWithCredits};
use node_data::ledger::{to_str, Attestation, Block, IterationsInfo, Seed};
use std::cmp::max;

Expand All @@ -19,15 +19,14 @@ use node_data::message::payload::Candidate;
use node_data::message::{ConsensusHeader, Message, SignInfo, StepMessage};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::Mutex;
use tracing::{debug, info};

pub struct Generator<T: Operations> {
executor: Arc<Mutex<T>>,
executor: Arc<T>,
}

impl<T: Operations> Generator<T> {
pub fn new(executor: Arc<Mutex<T>>) -> Self {
pub fn new(executor: Arc<T>) -> Self {
Self { executor }
}

Expand All @@ -46,7 +45,13 @@ impl<T: Operations> Generator<T> {
let start = Instant::now();

let candidate = self
.generate_block(ru, Seed::from(seed), iteration, failed_iterations)
.generate_block(
ru,
Seed::from(seed),
iteration,
failed_iterations,
ru.att_voters(),
)
.await?;

info!(
Expand Down Expand Up @@ -81,6 +86,7 @@ impl<T: Operations> Generator<T> {
seed: Seed,
iteration: u8,
failed_iterations: IterationsInfo,
voters: &[VoterWithCredits],
) -> Result<Block, crate::operations::Error> {
let missed_generators = failed_iterations
.to_missed_generators()
Expand All @@ -91,14 +97,11 @@ impl<T: Operations> Generator<T> {
block_gas_limit: config::DEFAULT_BLOCK_GAS_LIMIT,
generator_pubkey: ru.pubkey_bls.clone(),
missed_generators,
voters_pubkey: Some(voters.to_owned()),
};

let result = self
.executor
.lock()
.await
.execute_state_transition(call_params)
.await?;
let result =
self.executor.execute_state_transition(call_params).await?;

let tx_hashes: Vec<_> =
result.txs.iter().map(|t| t.inner.hash()).collect();
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/proposal/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where

impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {
pub fn new(
executor: Arc<Mutex<T>>,
executor: Arc<T>,
_db: Arc<Mutex<D>>,
handler: Arc<Mutex<handler::ProposalHandler<D>>>,
) -> Self {
Expand Down
5 changes: 3 additions & 2 deletions consensus/src/quorum/verifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn verify_step_votes(
committees_set: &RwLock<CommitteeSet<'_>>,
seed: Seed,
step: StepName,
) -> Result<QuorumResult, StepSigError> {
) -> Result<(QuorumResult, Committee), StepSigError> {
let round = header.round;
let iteration = header.iteration;

Expand All @@ -94,7 +94,8 @@ pub async fn verify_step_votes(
let set = committees_set.read().await;
let committee = set.get(&cfg).expect("committee to be created");

verify_votes(header, step, vote, sv, committee)
let quorum_result = verify_votes(header, step, vote, sv, committee)?;
Ok((quorum_result, committee.clone()))
}

#[derive(Default)]
Expand Down
13 changes: 4 additions & 9 deletions consensus/src/ratification/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ use tokio::sync::Mutex;

use tracing::{error, info, Instrument};

pub struct RatificationStep<T, DB> {
pub struct RatificationStep<DB> {
handler: Arc<Mutex<handler::RatificationHandler>>,

_executor: Arc<Mutex<T>>,

marker: PhantomData<DB>,
}

impl<T: Operations + 'static, DB: Database> RatificationStep<T, DB> {
impl<DB: Database> RatificationStep<DB> {
pub async fn try_vote(
ru: &RoundUpdate,
iteration: u8,
Expand Down Expand Up @@ -77,14 +74,12 @@ pub fn build_ratification_payload(
ratification
}

impl<T: Operations + 'static, DB: Database> RatificationStep<T, DB> {
impl<DB: Database> RatificationStep<DB> {
pub(crate) fn new(
executor: Arc<Mutex<T>>,
handler: Arc<Mutex<handler::RatificationHandler>>,
) -> Self {
Self {
handler,
_executor: executor,
marker: PhantomData,
}
}
Expand Down Expand Up @@ -119,7 +114,7 @@ impl<T: Operations + 'static, DB: Database> RatificationStep<T, DB> {
)
}

pub async fn run(
pub async fn run<T: Operations + 'static>(
&mut self,
mut ctx: ExecutionCtx<'_, DB, T>,
) -> Result<Message, ConsensusError> {
Expand Down
5 changes: 5 additions & 0 deletions consensus/src/user/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ impl Committee {
self.members.get(pubkey_bls).copied()
}

pub fn members(&self) -> &BTreeMap<PublicKey, usize> {
&self.members
}

// get_occurrences returns values in a vec
pub fn get_occurrences(&self) -> Vec<usize> {
self.members.values().copied().collect()
Expand Down Expand Up @@ -145,6 +149,7 @@ impl fmt::Display for &Committee {
}

/// Implements a cache of generated committees so that they can be reused.
#[derive(Clone)]
pub struct CommitteeSet<'p> {
committees: HashMap<sortition::Config, Committee>,
provisioners: &'p Provisioners,
Expand Down
Loading
Loading