Skip to content

Commit

Permalink
Add reserved gas for system transactions
Browse files Browse the repository at this point in the history
Currently, when collecting transactions to commit to a block, we only check the
accumulated gas with block's gas limit. Later, when finalizing the block, with
system transactions included, the block's gas used may exceed the block's gas
limit. As a result, that block is not mined. We want to avoid this scenario by
introducing a new flag to reserve some gas in gas pool for system transactions.
  • Loading branch information
minh-bq committed Nov 30, 2022
1 parent 8388d71 commit 1c8c44a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ronin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ var (
utils.MinerNotifyFlag,
utils.LegacyMinerGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerGasReserveFlag,
utils.MinerGasPriceFlag,
utils.MinerEtherbaseFlag,
utils.MinerExtraDataFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/ronin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.MinerNotifyFullFlag,
utils.MinerGasPriceFlag,
utils.MinerGasLimitFlag,
utils.MinerGasReserveFlag,
utils.MinerEtherbaseFlag,
utils.MinerExtraDataFlag,
utils.MinerRecommitIntervalFlag,
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ var (
Usage: "Target gas ceiling for mined blocks",
Value: ethconfig.Defaults.Miner.GasCeil,
}
MinerGasReserveFlag = cli.Uint64Flag{
Name: "miner.gasreserve",
Usage: "Reserved gas for system transactions",
}
MinerGasPriceFlag = BigFlag{
Name: "miner.gasprice",
Usage: "Minimum gas price for mining a transaction",
Expand Down Expand Up @@ -1474,6 +1478,9 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
if ctx.GlobalIsSet(MinerGasLimitFlag.Name) {
cfg.GasCeil = ctx.GlobalUint64(MinerGasLimitFlag.Name)
}
if ctx.GlobalIsSet(MinerGasReserveFlag.Name) {
cfg.GasReserve = ctx.GlobalUint64(MinerGasReserveFlag.Name)
}
if ctx.GlobalIsSet(MinerGasPriceFlag.Name) {
cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name)
}
Expand Down
5 changes: 5 additions & 0 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (api *PrivateMinerAPI) SetGasLimit(gasLimit hexutil.Uint64) bool {
return true
}

func (api *PrivateMinerAPI) SetGasReserve(gasReserve hexutil.Uint64) bool {
api.e.Miner().SetGasReserve(uint64(gasReserve))
return true
}

// SetEtherbase sets the etherbase of the miner
func (api *PrivateMinerAPI) SetEtherbase(etherbase common.Address) bool {
api.e.SetEtherbase(etherbase)
Expand Down
6 changes: 6 additions & 0 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
GasFloor uint64 // Target gas floor for mined blocks.
GasCeil uint64 // Target gas ceiling for mined blocks.
GasReserve uint64 // Reserved gas for system transactions
GasPrice *big.Int // Minimum gas price for mining a transaction
Recommit time.Duration // The time interval for miner to re-create mining work.
Noverify bool // Disable remote mining solution verification(only useful in ethash).
Expand Down Expand Up @@ -216,6 +217,11 @@ func (miner *Miner) SetGasCeil(ceil uint64) {
miner.worker.setGasCeil(ceil)
}

// SetGasReserve sets the reserved gas for system transactions
func (miner *Miner) SetGasReserve(reserve uint64) {
miner.worker.setGasReserve(reserve)
}

// EnablePreseal turns on the preseal mining feature. It's enabled by default.
// Note this function shouldn't be exposed to API, it's unnecessary for users
// (miners) to actually know the underlying detail. It's only for outside project
Expand Down
12 changes: 12 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ func (w *worker) setGasCeil(ceil uint64) {
w.config.GasCeil = ceil
}

func (w *worker) setGasReserve(reserve uint64) {
w.mu.Lock()
defer w.mu.Unlock()
w.config.GasReserve = reserve
}

// setExtra sets the content used to initialize the block extra field.
func (w *worker) setExtra(extra []byte) {
w.mu.Lock()
Expand Down Expand Up @@ -813,6 +819,12 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin
gasLimit := w.current.header.GasLimit
if w.current.gasPool == nil {
w.current.gasPool = new(core.GasPool).AddGas(gasLimit)

// If the gas pool is newly created, reserve some gas for system transactions
if err := w.current.gasPool.SubGas(w.config.GasReserve); err != nil {
log.Error("Failed to reserve gas for system transactions", "pool", w.current.gasPool, "reserve", w.config.GasReserve)
return true
}
}

var coalescedLogs []*types.Log
Expand Down

0 comments on commit 1c8c44a

Please sign in to comment.