Skip to content

Commit

Permalink
consensus/parlia: estimate gas reserved for systemTxs (bnb-chain#2896)
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 authored Feb 17, 2025
1 parent 9e24b24 commit b5ed41b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ func startNode(ctx *cli.Context, stack *node.Node, backend ethapi.Backend, isCon
// Start auxiliary services if enabled
ethBackend, ok := backend.(*eth.EthAPIBackend)
gasCeil := ethBackend.Miner().GasCeil()
if gasCeil > params.SystemTxsGas {
ethBackend.TxPool().SetMaxGas(gasCeil - params.SystemTxsGas)
if gasCeil > params.SystemTxsGasSoftLimit {
ethBackend.TxPool().SetMaxGas(gasCeil - params.SystemTxsGasSoftLimit)
}
if ctx.Bool(utils.MiningEnabledFlag.Name) {
// Mining only makes sense if a full Ethereum node is running
Expand Down
38 changes: 38 additions & 0 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,44 @@ func (p *Parlia) distributeFinalityReward(chain consensus.ChainHeaderReader, sta
return p.applyTransaction(msg, state, header, cx, txs, receipts, systemTxs, usedGas, mining, tracer)
}

func (p *Parlia) EstimateGasReservedForSystemTxs(chain consensus.ChainHeaderReader, header *types.Header) uint64 {
parent := chain.GetHeaderByHash(header.ParentHash)
if parent != nil {
// Mainnet and Chapel have both passed Feynman. Now, simplify the logic before and during the Feynman hard fork.
if p.chainConfig.IsFeynman(header.Number, header.Time) &&
!p.chainConfig.IsOnFeynman(header.Number, parent.Time, header.Time) {
// const (
// the following values represent the maximum values found in the most recent blocks on the mainnet
// depositTxGas = uint64(60_000)
// slashTxGas = uint64(140_000)
// finalityRewardTxGas = uint64(350_000)
// updateValidatorTxGas = uint64(12_160_000)
// )
// suggestReservedGas := depositTxGas
// if header.Difficulty.Cmp(diffInTurn) != 0 {
// snap, err := p.snapshot(chain, header.Number.Uint64()-1, header.ParentHash, nil)
// if err != nil || !snap.SignRecently(snap.inturnValidator()) {
// suggestReservedGas += slashTxGas
// }
// }
// if header.Number.Uint64()%p.config.Epoch == 0 {
// suggestReservedGas += finalityRewardTxGas
// }
// if isBreatheBlock(parent.Time, header.Time) {
// suggestReservedGas += updateValidatorTxGas
// }
// return suggestReservedGas * 150 / 100
if !isBreatheBlock(parent.Time, header.Time) {
// params.SystemTxsGasSoftLimit > (depositTxGas+slashTxGas+finalityRewardTxGas)*150/100
return params.SystemTxsGasSoftLimit
}
}
}

// params.SystemTxsGasHardLimit > (depositTxGas+slashTxGas+finalityRewardTxGas+updateValidatorTxGas)*150/100
return params.SystemTxsGasHardLimit
}

// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given.
func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, txs *[]*types.Transaction,
Expand Down
4 changes: 2 additions & 2 deletions eth/api_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (api *MinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
// SetGasLimit sets the gaslimit to target towards during mining.
func (api *MinerAPI) SetGasLimit(gasLimit hexutil.Uint64) bool {
api.e.Miner().SetGasCeil(uint64(gasLimit))
if uint64(gasLimit) > params.SystemTxsGas {
api.e.TxPool().SetMaxGas(uint64(gasLimit) - params.SystemTxsGas)
if uint64(gasLimit) > params.SystemTxsGasSoftLimit {
api.e.TxPool().SetMaxGas(uint64(gasLimit) - params.SystemTxsGasSoftLimit)
}
return true
}
Expand Down
5 changes: 4 additions & 1 deletion miner/bid_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/bidutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/parlia"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -582,7 +583,9 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
gasLimit := bidRuntime.env.header.GasLimit
if bidRuntime.env.gasPool == nil {
bidRuntime.env.gasPool = new(core.GasPool).AddGas(gasLimit)
bidRuntime.env.gasPool.SubGas(params.SystemTxsGas)
if p, ok := b.engine.(*parlia.Parlia); ok {
bidRuntime.env.gasPool.SubGas(p.EstimateGasReservedForSystemTxs(b.chain, bidRuntime.env.header))
}
bidRuntime.env.gasPool.SubGas(params.PayBidTxGasLimit)
}

Expand Down
6 changes: 5 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,11 @@ func (w *worker) commitTransactions(env *environment, plainTxs, blobTxs *transac
gasLimit := env.header.GasLimit
if env.gasPool == nil {
env.gasPool = new(core.GasPool).AddGas(gasLimit)
env.gasPool.SubGas(params.SystemTxsGas)
if p, ok := w.engine.(*parlia.Parlia); ok {
gasReserved := p.EstimateGasReservedForSystemTxs(w.chain, env.header)
env.gasPool.SubGas(gasReserved)
log.Debug("commitTransactions", "number", env.header.Number.Uint64(), "time", env.header.Time, "EstimateGasReservedForSystemTxs", gasReserved)
}
}

var coalescedLogs []*types.Log
Expand Down
3 changes: 2 additions & 1 deletion params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (
CallValueTransferGas uint64 = 9000 // Paid for CALL when the value transfer is non-zero.
CallNewAccountGas uint64 = 25000 // Paid for CALL when the destination address didn't exist prior.
TxGas uint64 = 21000 // Per transaction not creating a contract. NOTE: Not payable on data of calls between transactions.
SystemTxsGas uint64 = 20000000 // The gas reserved for system txs; only for parlia consensus
SystemTxsGasHardLimit uint64 = 20000000 // Maximum gas reserved for system transactions (Parlia consensus only)
SystemTxsGasSoftLimit uint64 = 1000000 // Maximum gas reserved for system transactions, excluding validator update transactions (Parlia consensus only)
TxGasContractCreation uint64 = 53000 // Per transaction that creates a contract. NOTE: Not payable on data of calls between transactions.
TxDataZeroGas uint64 = 4 // Per byte of data attached to a transaction that equals zero. NOTE: Not payable on data of calls between transactions.
QuadCoeffDiv uint64 = 512 // Divisor for the quadratic particle of the memory cost equation.
Expand Down

0 comments on commit b5ed41b

Please sign in to comment.