Skip to content

Commit

Permalink
Fixe issue with fees (#4734)
Browse files Browse the repository at this point in the history
* fix fees

* Update test_rewards execution test

* Update test with updated reward formula

* Update ci.yml

* revert test change

* Rm dbg and create const value

* fmt

---------

Co-authored-by: Leo-Besancon <[email protected]>
  • Loading branch information
damip and Leo-Besancon authored Aug 1, 2024
1 parent 1f2c6be commit 2d6e1ff
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 100 deletions.
44 changes: 31 additions & 13 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,9 @@ impl ExecutionState {
// Set remaining block gas
let mut remaining_block_gas = self.config.max_gas_per_block;

// Set block credits
// Block credits count every operation fee, denunciation slash and endorsement reward.
// We initialize the block credits with the block reward to stimulate block production
// even in the absence of operations and denunciations.
let mut block_credits = self.config.block_reward;

// Try executing the operations of this block in the order in which they appear in the block.
Expand Down Expand Up @@ -1485,22 +1487,36 @@ impl ExecutionState {
// Update speculative rolls state production stats
context.update_production_stats(&block_creator_addr, *slot, Some(*block_id));

// Credit endorsement producers and endorsed block producers
let mut remaining_credit = block_credits;
// Divide the total block credits into parts + remainder
let block_credit_part_count = 3 * (1 + self.config.endorsement_count);
let block_credit_part = block_credits
.checked_div_u64(3 * (1 + (self.config.endorsement_count)))
.checked_div_u64(block_credit_part_count)
.expect("critical: block_credits checked_div factor is 0");
let remainder = block_credits
.checked_rem_u64(block_credit_part_count)
.expect("critical: block_credits checked_rem factor is 0");

// Give 3 parts + remainder to the block producer to stimulate block production
// even in the absence of endorsements.
let mut block_producer_credit = block_credit_part
.saturating_mul_u64(3)
.saturating_add(remainder);

for endorsement_creator in endorsement_creators {
// credit creator of the endorsement with coins
// Credit the creator of the block with 1 part to stimulate endorsement inclusion of endorsements,
// and dissuade from emitting the block too early (before the endorsements have propageted).
block_producer_credit = block_producer_credit.saturating_add(block_credit_part);

// Credit creator of the endorsement with 1 part to stimulate the production of endorsements.
// This also motivates endorsers to not publish their endorsements too early (will not endorse the right block),
// and to not publish too late (will not be included in the block).
match context.transfer_coins(
None,
Some(endorsement_creator),
block_credit_part,
false,
) {
Ok(_) => {
remaining_credit = remaining_credit.saturating_sub(block_credit_part);

#[cfg(feature = "execution-info")]
exec_info
.endorsement_creator_rewards
Expand All @@ -1514,15 +1530,16 @@ impl ExecutionState {
}
}

// credit creator of the endorsed block with coins
// Credit the creator of the endorsed block with 1 part.
// This is done to incentivize block producers to be endorsed,
// typically by not publishing their blocks too late.
match context.transfer_coins(
None,
Some(endorsement_target_creator),
block_credit_part,
false,
) {
Ok(_) => {
remaining_credit = remaining_credit.saturating_sub(block_credit_part);
#[cfg(feature = "execution-info")]
{
exec_info.endorsement_target_reward =
Expand All @@ -1538,18 +1555,19 @@ impl ExecutionState {
}
}

// Credit block creator with remaining_credit
// Credit block producer
if let Err(err) =
context.transfer_coins(None, Some(block_creator_addr), remaining_credit, false)
context.transfer_coins(None, Some(block_creator_addr), block_producer_credit, false)
{
debug!(
"failed to credit {} coins to block creator {} on block execution: {}",
remaining_credit, block_creator_addr, err
block_producer_credit, block_creator_addr, err
)
} else {
#[cfg(feature = "execution-info")]
{
exec_info.block_producer_reward = Some((block_creator_addr, remaining_credit));
exec_info.block_producer_reward =
Some((block_creator_addr, block_producer_credit));
}
}
} else {
Expand Down
Loading

0 comments on commit 2d6e1ff

Please sign in to comment.