Skip to content

Commit

Permalink
miner, txpool: add a reserved gas constant for system transactions (#364
Browse files Browse the repository at this point in the history
)

* Revert "Add reserved gas for system transactions (#187)"

This partially reverts commit a1f3645 to make
the reserved gas a constant not a provided configuration from user. This still
keeps the created flag in that commit for compatibility.

* miner, txpool: add a reserved gas constant for system transactions

This commit adds the reserved gas for system transaction, which is previously a
user provided flag, as a constant.

In the miner part, the miner stops commit normal transactions to block when
block gas used is over the block's gas limit - reserved gas. So when finalizing
and including system transactions into block, the block's gas used does not exceed
the block's gas limit.

In the txpool part, when validating a transaction before putting it into the
pool, previously, we only check if the transaction's gas limit is not over the
block's gas limit. However, if the transaction's gas limit is over the block's
gas limit - reserved gas, it is never included into the block but still always
stay in pending transaction pool. This commit fixes the check to reject the
transaction with gas limit over the block's gas limit - reserved gas.
  • Loading branch information
minh-bq authored Oct 4, 2023
1 parent 9589f03 commit fa1dc18
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
3 changes: 0 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,6 @@ 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
6 changes: 5 additions & 1 deletion core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
return ErrNegativeValue
}
// Ensure the transaction doesn't exceed the current block limit gas.
if pool.currentMaxGas < tx.Gas() {
var reservedGas uint64 = 0
if pool.chainconfig.Consortium != nil {
reservedGas = params.ReservedGasForSystemTransactions
}
if pool.currentMaxGas-reservedGas < tx.Gas() {
return ErrGasLimit
}
// Sanity check for extremely large numbers
Expand Down
5 changes: 0 additions & 5 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ 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: 0 additions & 6 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ 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 @@ -219,11 +218,6 @@ 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)
}

func (miner *Miner) SetBlockProducerLeftover(interval time.Duration) {
miner.worker.setBlockProducerLeftover(interval)
}
Expand Down
19 changes: 10 additions & 9 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,6 @@ 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 @@ -883,9 +877,16 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin
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, "error", err)
return true
if w.chainConfig.Consortium != nil {
if err := w.current.gasPool.SubGas(params.ReservedGasForSystemTransactions); err != nil {
log.Error(
"Failed to reserve gas for system transactions",
"pool", w.current.gasPool,
"reserve", params.ReservedGasForSystemTransactions,
"error", err,
)
return true
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package params
import "math/big"

const (
ReservedGasForSystemTransactions uint64 = 10_000_000 // The reserved gas for system transactions in each block.

GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
ConsortiumGasLimitBoundDivisor uint64 = 256
MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be.
Expand Down

0 comments on commit fa1dc18

Please sign in to comment.