diff --git a/core/chain_makers.go b/core/chain_makers.go index 58f84146946f2..7174a9ae5b8aa 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -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 diff --git a/core/state_processor.go b/core/state_processor.go index 4cb8ff3567c25..744c4e3ba7f1a 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -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 @@ -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. @@ -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 diff --git a/core/state_processor_goat.go b/core/state_processor_goat.go index 5fb30a2b8d027..948489c332f0e 100644 --- a/core/state_processor_goat.go +++ b/core/state_processor_goat.go @@ -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" @@ -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) } @@ -24,7 +25,7 @@ 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) } @@ -32,7 +33,7 @@ func ProcessGoatGasFee(statedb *state.StateDB, gasFees *big.Int) *big.Int { // 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) } @@ -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 +} diff --git a/miner/worker.go b/miner/worker.go index 0a964310728ac..0b8533f7590f2 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -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}