Skip to content

Commit

Permalink
core,miner: DRY for goat gas fee
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlee42 committed Nov 6, 2024
1 parent 100717c commit ae37590
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 59 deletions.
32 changes: 4 additions & 28 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,41 +355,17 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse

var requests [][]byte
if config.Goat != nil {
var allLogs []*types.Log
var blockLogs []*types.Log
for _, r := range b.receipts {
allLogs = append(allLogs, r.Logs...)
}
// calculate the reward
burntFees := new(big.Int)
if b.header.BaseFee != nil && b.header.GasUsed > 0 {
gasUsed := new(big.Int).SetUint64(b.header.GasUsed)
burntFees.Mul(b.header.BaseFee, gasUsed)
}

if b.header.ExcessBlobGas != nil && b.header.BlobGasUsed != nil && *b.header.BlobGasUsed > 0 {
blobBaseFee := eip4844.CalcBlobFee(*b.header.ExcessBlobGas)
blobUsed := new(big.Int).SetUint64(*b.header.BlobGasUsed)
burntFees.Add(burntFees, blobUsed.Mul(blobUsed, blobBaseFee))
}

var gasFees = new(big.Int)
gasFees.Add(gasFees, burntFees)
for i, tx := range b.txs {
gasUsed := b.receipts[i].GasUsed
if gasUsed == 0 { // It's the goat tx
continue
}
minerFee, _ := tx.EffectiveGasTip(b.header.BaseFee)
gasFees.Add(gasFees, new(big.Int).Mul(new(big.Int).SetUint64(gasUsed), minerFee))
blockLogs = append(blockLogs, r.Logs...)
}
gasRevenue := ProcessGoatGasFee(statedb, gasFees)
goatRequests, err := ProcessGoatRequests(b.Number().Uint64(), gasRevenue, allLogs)
gasRevenue := AllocateGoatGasFee(statedb, CalculateGoatGasFees(b.header, b.txs, b.receipts))
goatRequests, err := ProcessGoatRequests(b.Number().Uint64(), gasRevenue, blockLogs)
if err != nil {
panic(fmt.Sprintf("failed to parse goat logs: %v", err))
}
requests = goatRequests
}

if config.Goat == nil && config.IsPrague(b.header.Number, b.header.Time) {
requests = [][]byte{}
// EIP-6110 deposits
Expand Down
10 changes: 5 additions & 5 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
allLogs []*types.Log
gp = new(GasPool).AddGas(block.GasLimit())

// gas reward to validators and delegators
gasReward = new(big.Int)
// gas reward to validators and goat foundation
goatGasFees = new(big.Int)
)

// Mutate the block and state according to any hard-fork specs
Expand Down Expand Up @@ -109,7 +109,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
if receipt.GasUsed > 0 { // non-goatTx case
tipFee := new(big.Int).SetUint64(receipt.GasUsed)
tipFee.Mul(tipFee, tx.EffectiveGasTipValue(context.BaseFee))
gasReward.Add(gasReward, tipFee)
goatGasFees.Add(goatGasFees, tipFee)
}
}
// Read requests if Prague is enabled.
Expand All @@ -123,8 +123,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
blobUsed := new(big.Int).SetUint64(*gasUsed)
burntFees.Add(burntFees, blobUsed.Mul(blobUsed, context.BlobBaseFee))
}
gasReward.Add(gasReward, burntFees)
reward := ProcessGoatGasFee(statedb, gasReward)
goatGasFees.Add(goatGasFees, burntFees)
reward := AllocateGoatGasFee(statedb, goatGasFees)
goatRequests, err := ProcessGoatRequests(block.NumberU64(), reward, allLogs)
if err != nil {
return nil, err
Expand Down
32 changes: 29 additions & 3 deletions core/state_processor_goat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"math/big"

"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -15,7 +16,7 @@ var (
gfMaxBasePoint = big.NewInt(1e4)
)

func ProcessGoatGasFee(statedb *state.StateDB, gasFees *big.Int) *big.Int {
func AllocateGoatGasFee(statedb *state.StateDB, gasFees *big.Int) *big.Int {
if gasFees.BitLen() == 0 {
return new(big.Int)
}
Expand All @@ -24,15 +25,15 @@ func ProcessGoatGasFee(statedb *state.StateDB, gasFees *big.Int) *big.Int {
tax := new(big.Int).Mul(gasFees, gfBasePoint)
tax.Div(tax, gfMaxBasePoint)

if tax.BitLen() != 0 {
if tax.Sign() > 0 {
f, _ := uint256.FromBig(tax)
statedb.AddBalance(goattypes.GoatFoundationContract, f, tracing.BalanceIncreaseRewardTransactionFee)
}

// add gas revenue to locking contract
// if the validator withdraws the gas reward, we will subtract it from locking contract then
gas := new(big.Int).Sub(gasFees, tax)
if gas.BitLen() != 0 {
if gas.Sign() > 0 {
f, _ := uint256.FromBig(gas)
statedb.AddBalance(goattypes.LockingContract, f, tracing.BalanceIncreaseRewardTransactionFee)
}
Expand Down Expand Up @@ -150,3 +151,28 @@ func ProcessGoatRequests(height uint64, reward *big.Int, allLogs []*types.Log) (
requests = append(requests, relayerRequests.Encode()...)
return requests, nil
}

// CalculateGoatGasFees gets all fees in the block
func CalculateGoatGasFees(header *types.Header, txs []*types.Transaction, receipts []*types.Receipt) *big.Int {
gasFees := new(big.Int)
if header.BaseFee != nil && header.GasUsed > 0 {
gasUsed := new(big.Int).SetUint64(header.GasUsed)
gasFees.Mul(header.BaseFee, gasUsed)
}

if header.ExcessBlobGas != nil && header.BlobGasUsed != nil && *header.BlobGasUsed > 0 {
blobBaseFee := eip4844.CalcBlobFee(*header.ExcessBlobGas)
blobUsed := new(big.Int).SetUint64(*header.BlobGasUsed)
gasFees.Add(gasFees, blobUsed.Mul(blobUsed, blobBaseFee))
}

for i, tx := range txs {
gasUsed := receipts[i].GasUsed
if gasUsed == 0 { // It's the goat tx
continue
}
minerFee := tx.EffectiveGasTipValue(header.BaseFee)
gasFees.Add(gasFees, new(big.Int).Mul(new(big.Int).SetUint64(gasUsed), minerFee))
}
return gasFees
}
25 changes: 2 additions & 23 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,8 @@ func (miner *Miner) generateWork(params *generateParams, witness bool) *newPaylo
// Collect consensus-layer requests if Prague is enabled.
var requests [][]byte
if miner.chainConfig.Goat != nil {
// calculate the reward
burntFees := new(big.Int)
if work.header.BaseFee != nil && work.header.GasUsed > 0 {
gasUsed := new(big.Int).SetUint64(work.header.GasUsed)
burntFees.Mul(work.header.BaseFee, gasUsed)
}

if work.header.ExcessBlobGas != nil && work.header.BlobGasUsed != nil && *work.header.BlobGasUsed > 0 {
blobBaseFee := eip4844.CalcBlobFee(*work.header.ExcessBlobGas)
blobUsed := new(big.Int).SetUint64(*work.header.BlobGasUsed)
burntFees.Add(burntFees, blobUsed.Mul(blobUsed, blobBaseFee))
}

gasFees.Add(gasFees, burntFees)
for i, tx := range work.txs {
gasUsed := work.receipts[i].GasUsed
if gasUsed == 0 { // It's the goat tx
continue
}
minerFee := tx.EffectiveGasTipValue(work.header.BaseFee)
gasFees.Add(gasFees, new(big.Int).Mul(new(big.Int).SetUint64(gasUsed), minerFee))
}
gasRevenue := core.ProcessGoatGasFee(work.state, gasFees)
gasFees = core.CalculateGoatGasFees(work.header, work.txs, work.receipts)
gasRevenue := core.AllocateGoatGasFee(work.state, gasFees)
goatRequests, err := core.ProcessGoatRequests(work.header.Number.Uint64(), gasRevenue, allLogs)
if err != nil {
return &newPayloadResult{err: err}
Expand Down

0 comments on commit ae37590

Please sign in to comment.