Skip to content

Commit

Permalink
Implemented the limit per fee currency
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Rodygin committed Nov 2, 2023
1 parent 864096b commit c9cbca2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
50 changes: 44 additions & 6 deletions miner/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ import (
type blockState struct {
signer types.Signer

state *state.StateDB // apply state changes here
tcount int // tx count in cycle
gasPool *core.GasPool // available gas used to pack transactions
bytesBlock *core.BytesBlock // available bytes used to pack transactions
gasLimit uint64
sysCtx *core.SysContractCallCtx
state *state.StateDB // apply state changes here
tcount int // tx count in cycle
gasPool *core.GasPool // available gas used to pack transactions
bytesBlock *core.BytesBlock // available bytes used to pack transactions
multiGasPool core.MultiGasPool // available gas to pay for with currency
gasLimit uint64
sysCtx *core.SysContractCallCtx

header *types.Header
txs []*types.Transaction
Expand Down Expand Up @@ -112,6 +113,20 @@ func prepareBlock(w *worker) (*blockState, error) {
txFeeRecipient: txFeeRecipient,
}
b.gasPool = new(core.GasPool).AddGas(b.gasLimit)

whitelist, err := currency.CurrencyWhitelist(vmRunner)
if err != nil {
log.Warn("Can't fetch currency whitelist", "error", err, "block", header.Number.Uint64())
whitelist = []common.Address{}
}

b.multiGasPool = core.NewMultiGasPool(
b.gasLimit,
whitelist,
w.config.FeeCurrencyDefault,
w.config.FeeCurrencyLimits,
)

if w.chainConfig.IsGingerbread(header.Number) {
header.GasLimit = b.gasLimit
header.Difficulty = big.NewInt(0)
Expand Down Expand Up @@ -250,6 +265,17 @@ loop:
if tx == nil {
break
}
// Short-circuit if the transaction is using more gas allocated for the
// given fee currency.
if b.multiGasPool.PoolFor(tx.FeeCurrency()).Gas() < tx.Gas() {
log.Trace(
"Skipping transaction which requires more gas than is left in the pool",
"hash", tx.Hash(), "gas", b.multiGasPool.PoolFor(tx.FeeCurrency()).Gas(),
"txgas", tx.Gas(),
)
txs.Pop()
continue
}
// Short-circuit if the transaction requires more gas than we have in the pool.
// If we didn't short-circuit here, we would get core.ErrGasLimitReached below.
// Short-circuiting here saves us the trouble of checking the GPM and so on when the tx can't be included
Expand Down Expand Up @@ -321,6 +347,18 @@ loop:
return err
}
}

err = b.multiGasPool.PoolFor(tx.FeeCurrency()).SubGas(tx.Gas())
// Should never happen as we check it above
if err != nil {
log.Warn(
"Unexpectedly reached limit for fee currency",
"hash", tx.Hash(), "gas", b.multiGasPool.PoolFor(tx.FeeCurrency()).Gas(),
"txgas", tx.Gas(),
)
return err
}

txs.Shift()

default:
Expand Down
6 changes: 4 additions & 2 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type Backend interface {

// Config is the configuration parameters of mining.
type Config struct {
Validator common.Address `toml:",omitempty"` // Public address for block signing and randomness (default = first account)
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
Validator common.Address `toml:",omitempty"` // Public address for block signing and randomness (default = first account)
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
FeeCurrencyDefault float64 // Default fraction of block gas limit
FeeCurrencyLimits map[common.Address]float64 // Fee currency-to-limit fraction mapping
}

// Miner creates blocks and searches for proof-of-work values.
Expand Down

0 comments on commit c9cbca2

Please sign in to comment.