Skip to content

Commit

Permalink
Add comments to block fee handling (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald authored Jul 19, 2022
1 parent e44b348 commit 50e7dfd
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,27 @@ func (self *DummyEngine) verifyBlockFee(
totalBlockFee.Add(totalBlockFee, extraStateChangeContribution)
}

// Calculate the total excess over the base fee that was paid towards the block fee
// Calculate the total excess (denominated in AVAX) over the base fee that was paid towards the block fee
for i, receipt := range receipts {
// Each transaction contributes the excess over the baseFee towards the totalBlockFee
// This should be equivalent to the sum of the "priority fees" within EIP-1559.
txFeePremium, err := txs[i].EffectiveGasTip(baseFee)
if err != nil {
return err
}
// Multiply the [txFeePremium] by the gasUsed in the transaction since this gives the total AVAX that was paid
// above the amount required if the transaction had simply paid the minimum base fee for the block.
//
// Ex. LegacyTx paying a gas price of 100 gwei for 1M gas in a block with a base fee of 10 gwei.
// Total Fee = 100 gwei * 1M gas
// Minimum Fee = 10 gwei * 1M gas (minimum fee that would have been accepted for this transaction)
// Fee Premium = 90 gwei
// Total Overpaid = 90 gwei * 1M gas
blockFeeContribution.Mul(txFeePremium, gasUsed.SetUint64(receipt.GasUsed))
totalBlockFee.Add(totalBlockFee, blockFeeContribution)
}
// Calculate how much gas the [totalBlockFee] would purchase at the price level
// set by this block.
// set by the base fee of this block.
blockGas := new(big.Int).Div(totalBlockFee, baseFee)

// Require that the amount of gas purchased by the effective tips within the block, [blockGas],
Expand Down Expand Up @@ -343,6 +351,8 @@ func (self *DummyEngine) Finalize(chain consensus.ChainHeaderReader, block *type
if chain.Config().IsApricotPhase5(new(big.Int).SetUint64(block.Time())) {
blockGasCostStep = ApricotPhase5BlockGasCostStep
}
// Calculate the expected blockGasCost for this block.
// Note: this is a deterministic transtion that defines an exact block fee for this block.
blockGasCost := calcBlockGasCost(
ApricotPhase4TargetBlockRate,
ApricotPhase4MinBlockGasCost,
Expand All @@ -351,9 +361,11 @@ func (self *DummyEngine) Finalize(chain consensus.ChainHeaderReader, block *type
parent.BlockGasCost,
parent.Time, block.Time(),
)
// Verify the BlockGasCost set in the header matches the calculated value.
if blockBlockGasCost := block.BlockGasCost(); blockBlockGasCost == nil || !blockBlockGasCost.IsUint64() || blockBlockGasCost.Cmp(blockGasCost) != 0 {
return fmt.Errorf("invalid blockGasCost: have %d, want %d", blockBlockGasCost, blockGasCost)
}
// Verify the block fee was paid.
if err := self.verifyBlockFee(
block.BaseFee(),
block.BlockGasCost(),
Expand Down Expand Up @@ -390,6 +402,7 @@ func (self *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader,
if chain.Config().IsApricotPhase5(new(big.Int).SetUint64(header.Time)) {
blockGasCostStep = ApricotPhase5BlockGasCostStep
}
// Calculate the required block gas cost for this block.
header.BlockGasCost = calcBlockGasCost(
ApricotPhase4TargetBlockRate,
ApricotPhase4MinBlockGasCost,
Expand All @@ -398,6 +411,7 @@ func (self *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader,
parent.BlockGasCost,
parent.Time, header.Time,
)
// Verify that this block covers the block fee.
if err := self.verifyBlockFee(
header.BaseFee,
header.BlockGasCost,
Expand Down

0 comments on commit 50e7dfd

Please sign in to comment.