Skip to content

Commit

Permalink
Merge pull request #22 from ambrosus/fix-fees-calculation
Browse files Browse the repository at this point in the history
Fix fees calculation
  • Loading branch information
SigismundSchlomo authored Mar 1, 2024
2 parents 1297067 + 5a792b7 commit db42433
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
26 changes: 17 additions & 9 deletions crates/ethcore/src/engines/authority_round/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,19 +2006,27 @@ impl Engine<EthereumMachine> for AuthorityRound {
beneficiaries.push((author, RewardKind::Author));

if let Some(params) = self.get_fees_params(block) {
let (author_fees, governance_fees) = block.transactions.iter()
.filter_map(|tx| tx.get_fees(params.governance_part))
.fold((U256::zero(),U256::zero()), |(acc_author,acc_governance), (author, governance)| {
(acc_author.saturating_add(author), acc_governance.saturating_add(governance))
});
let (author_fees, governance_fees) = block.receipts.iter().enumerate()
.filter_map(|(i, receipt)| {
let tx = &block.transactions[i];
tx.gas_price().map(|gas_price| (receipt, gas_price))
})
.fold((U256::zero(), U256::zero()), |(acc_author, acc_governance), (receipt, gas_price)| {
match receipt.get_fees(params.governance_part, gas_price) {
Some((auth, goven)) => (acc_author.saturating_add(auth), acc_governance.saturating_add(goven)),
None => (acc_author, acc_governance),
}
});

let author_addr = *block.header.author();
if let Tracing::Enabled(ref mut traces) = *block.traces_mut() {
let mut tracer = ExecutiveTracer::default();

tracer.trace_reward(author_addr, author_fees, RewardType::Uncle.into());
tracer.trace_reward(params.address, governance_fees, RewardType::EmptyStep.into());

if author_fees != U256::zero() {
tracer.trace_reward(author_addr, author_fees, RewardType::Uncle.into());
}
if governance_fees != U256::zero() {
tracer.trace_reward(params.address, governance_fees, RewardType::EmptyStep.into());
}
traces.push(tracer.drain().into());
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/ethcore/types/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ impl TypedReceipt {
}
}
}

pub fn get_fees(&self, part: U256, gas_price: U256) -> Option<(U256, U256)> {
match self {
Self::Legacy(receipt) => {
//Assume that transaction is checked already
let (fees_value, _) = receipt.gas_used.overflowing_mul(gas_price);
let governance_part = fees_value.saturating_mul(part) / U256::from(1_000_000);
let validator_part = fees_value.saturating_sub(part);
Some((validator_part, governance_part))
},
_ => None,
}
}
}

impl Deref for TypedReceipt {
Expand Down
10 changes: 3 additions & 7 deletions crates/ethcore/types/src/transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,15 +978,11 @@ impl SignedTransaction {
self.transaction.is_unsigned()
}

// Returns the author's part of fee and the governence part
pub fn get_fees(&self, part: U256) -> Option<(U256, U256)> {
/// Returns the gas price is the type of tx is Legacy
pub fn gas_price(&self) -> Option<U256> {
match &self.transaction.unsigned {
TypedTransaction::Legacy(tx) => {
//Assume that transaction is checked already
let (fees_value, _ ) = tx.gas.overflowing_mul(tx.gas_price);
let governance_part = fees_value.saturating_mul(part) / U256::from(1_000_000);
let validator_part = fees_value.saturating_sub(part);
Some((validator_part, governance_part))
Some(tx.gas_price)
},
_ => None,
}
Expand Down

0 comments on commit db42433

Please sign in to comment.