Skip to content

Commit

Permalink
app/ledger/finalize_block: log block rewards before recording slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jul 17, 2023
1 parent ca02d26 commit 2afb27c
Showing 1 changed file with 50 additions and 36 deletions.
86 changes: 50 additions & 36 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ where
)?;
}

// Invariant: Has to be applied before `record_slashes_from_evidence`
// because it potentially needs to be able to read validator state from
// previous epoch and jailing validator removes the historical state
self.log_block_rewards(&req.votes, height, current_epoch, new_epoch)?;
if new_epoch {
self.apply_inflation(current_epoch)?;
}

// Invariant: This has to be applied after
// `copy_validator_sets_and_positions` and before `self.update_epoch`.
self.record_slashes_from_evidence();
Expand Down Expand Up @@ -530,42 +538,6 @@ where
self.update_eth_oracle();
}

// Read the block proposer of the previously committed block in storage
// (n-1 if we are in the process of finalizing n right now).
match read_last_block_proposer_address(&self.wl_storage)? {
Some(proposer_address) => {
tracing::debug!(
"Found last block proposer: {proposer_address}"
);
let votes = pos_votes_from_abci(&self.wl_storage, &req.votes);
namada_proof_of_stake::log_block_rewards(
&mut self.wl_storage,
if new_epoch {
current_epoch.prev()
} else {
current_epoch
},
&proposer_address,
votes,
)?;
}
None => {
if height > BlockHeight::default().next_height() {
tracing::error!(
"Can't find the last block proposer at height {height}"
);
} else {
tracing::debug!(
"No last block proposer at height {height}"
);
}
}
}

if new_epoch {
self.apply_inflation(current_epoch)?;
}

if !req.proposer_address.is_empty() {
let tm_raw_hash_string =
tm_raw_hash_to_string(req.proposer_address);
Expand Down Expand Up @@ -887,6 +859,48 @@ where

Ok(())
}

// Process the proposer and votes in the block to assign their PoS rewards.
fn log_block_rewards(
&mut self,
votes: &[VoteInfo],
height: BlockHeight,
current_epoch: Epoch,
new_epoch: bool,
) -> Result<()> {
// Read the block proposer of the previously committed block in storage
// (n-1 if we are in the process of finalizing n right now).
match read_last_block_proposer_address(&self.wl_storage)? {
Some(proposer_address) => {
tracing::debug!(
"Found last block proposer: {proposer_address}"
);
let votes = pos_votes_from_abci(&self.wl_storage, votes);
namada_proof_of_stake::log_block_rewards(
&mut self.wl_storage,
if new_epoch {
current_epoch.prev()
} else {
current_epoch
},
&proposer_address,
votes,
)?;
}
None => {
if height > BlockHeight::default().next_height() {
tracing::error!(
"Can't find the last block proposer at height {height}"
);
} else {
tracing::debug!(
"No last block proposer at height {height}"
);
}
}
}
Ok(())
}
}

/// Convert ABCI vote info to PoS vote info. Any info which fails the conversion
Expand Down

0 comments on commit 2afb27c

Please sign in to comment.