From 87e92b2c3654b3c491e1c4b561ea9e7b6e407799 Mon Sep 17 00:00:00 2001
From: Maximilian Langenfeld <15726643+ezdac@users.noreply.github.com>
Date: Tue, 4 Jun 2024 10:27:27 +0200
Subject: [PATCH] Port the block limit per fee currency (#119)
* Port the block limit per fee currency feature
Closes #65
This implements the block-limit per fee-currency feature.
Some parts of this have been directly ported from celo-blockchain
(https://github.com/celo-org/celo-blockchain/commit/dc45bdc00).
* Copy MultiGasPool in miner environment
* Add USDT and USDC to block-limit per currency defaults
* Initialize MultiGasPool with currency whitelist
---
cmd/geth/main.go | 2 +
cmd/utils/flags.go | 46 +++++++++
core/celo_multi_gaspool.go | 71 ++++++++++++++
core/celo_multi_gaspool_test.go | 138 +++++++++++++++++++++++++++
core/txpool/legacypool/legacypool.go | 3 +
core/txpool/subpool.go | 3 +
eth/handler_test.go | 15 +--
miner/celo_defaults.go | 29 ++++++
miner/miner.go | 6 ++
miner/worker.go | 55 ++++++++++-
10 files changed, 360 insertions(+), 8 deletions(-)
create mode 100644 core/celo_multi_gaspool.go
create mode 100644 core/celo_multi_gaspool_test.go
create mode 100644 miner/celo_defaults.go
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index f8db209d0e..c698b12f4e 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -132,6 +132,8 @@ var (
utils.MinerRecommitIntervalFlag,
utils.MinerPendingFeeRecipientFlag,
utils.MinerNewPayloadTimeoutFlag, // deprecated
+ utils.CeloFeeCurrencyDefault,
+ utils.CeloFeeCurrencyLimits,
utils.NATFlag,
utils.NoDiscoverFlag,
utils.DiscoveryV4Flag,
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 427da783b8..e935772b62 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -558,6 +558,17 @@ var (
Usage: "0x prefixed public address for the pending block producer (not used for actual block production)",
Category: flags.MinerCategory,
}
+ CeloFeeCurrencyDefault = &cli.Float64Flag{
+ Name: "celo.feecurrency.default",
+ Usage: "Default fraction of block gas limit available for TXs paid with a whitelisted alternative currency",
+ Value: ethconfig.Defaults.Miner.FeeCurrencyDefault,
+ Category: flags.MinerCategory,
+ }
+ CeloFeeCurrencyLimits = &cli.StringFlag{
+ Name: "celo.feecurrency.limits",
+ Usage: "Comma separated currency address-to-block percentage mappings (
=)",
+ Category: flags.MinerCategory,
+ }
// Account settings
UnlockedAccountFlag = &cli.StringFlag{
@@ -1729,6 +1740,39 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
}
}
+func setCeloMiner(ctx *cli.Context, cfg *miner.Config, networkId uint64) {
+ cfg.FeeCurrencyDefault = ctx.Float64(CeloFeeCurrencyDefault.Name)
+
+ defaultLimits, ok := miner.DefaultFeeCurrencyLimits[networkId]
+ if !ok {
+ defaultLimits = make(map[common.Address]float64)
+ }
+
+ cfg.FeeCurrencyLimits = defaultLimits
+
+ if ctx.IsSet(CeloFeeCurrencyLimits.Name) {
+ feeCurrencyLimits := ctx.String(CeloFeeCurrencyLimits.Name)
+
+ for _, entry := range strings.Split(feeCurrencyLimits, ",") {
+ parts := strings.Split(entry, "=")
+ if len(parts) != 2 {
+ Fatalf("Invalid fee currency limits entry: %s", entry)
+ }
+ var address common.Address
+ if err := address.UnmarshalText([]byte(parts[0])); err != nil {
+ Fatalf("Invalid fee currency address hash %s: %v", parts[0], err)
+ }
+
+ fraction, err := strconv.ParseFloat(parts[1], 64)
+ if err != nil {
+ Fatalf("Invalid block limit fraction %s: %v", parts[1], err)
+ }
+
+ cfg.FeeCurrencyLimits[address] = fraction
+ }
+ }
+}
+
func setRequiredBlocks(ctx *cli.Context, cfg *ethconfig.Config) {
requiredBlocks := ctx.String(EthRequiredBlocksFlag.Name)
if requiredBlocks == "" {
@@ -2099,6 +2143,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.VMTraceJsonConfig = config
}
}
+
+ setCeloMiner(ctx, &cfg.Miner, cfg.NetworkId)
}
// SetDNSDiscoveryDefaults configures DNS discovery with the given URL if
diff --git a/core/celo_multi_gaspool.go b/core/celo_multi_gaspool.go
new file mode 100644
index 0000000000..9b128afb5d
--- /dev/null
+++ b/core/celo_multi_gaspool.go
@@ -0,0 +1,71 @@
+package core
+
+import (
+ "github.com/ethereum/go-ethereum/common"
+)
+
+type FeeCurrency = common.Address
+
+// MultiGasPool tracks the amount of gas available during execution
+// of the transactions in a block per fee currency. The zero value is a pool
+// with zero gas available.
+type MultiGasPool struct {
+ pools map[FeeCurrency]*GasPool
+ defaultPool *GasPool
+}
+
+type FeeCurrencyLimitMapping = map[FeeCurrency]float64
+
+// NewMultiGasPool creates a multi-fee currency gas pool and a default fallback
+// pool for CELO
+func NewMultiGasPool(
+ blockGasLimit uint64,
+ allowlist []FeeCurrency,
+ defaultLimit float64,
+ limitsMapping FeeCurrencyLimitMapping,
+) *MultiGasPool {
+ pools := make(map[FeeCurrency]*GasPool, len(allowlist))
+
+ for i := range allowlist {
+ currency := allowlist[i]
+ fraction, ok := limitsMapping[currency]
+ if !ok {
+ fraction = defaultLimit
+ }
+
+ pools[currency] = new(GasPool).AddGas(
+ uint64(float64(blockGasLimit) * fraction),
+ )
+ }
+
+ // A special case for CELO which doesn't have a limit
+ celoPool := new(GasPool).AddGas(blockGasLimit)
+
+ return &MultiGasPool{
+ pools: pools,
+ defaultPool: celoPool,
+ }
+}
+
+// PoolFor returns a configured pool for the given fee currency or the default
+// one otherwise
+func (mgp MultiGasPool) PoolFor(feeCurrency *FeeCurrency) *GasPool {
+ if feeCurrency == nil || mgp.pools[*feeCurrency] == nil {
+ return mgp.defaultPool
+ }
+
+ return mgp.pools[*feeCurrency]
+}
+
+func (mgp MultiGasPool) Copy() *MultiGasPool {
+ pools := make(map[FeeCurrency]*GasPool, len(mgp.pools))
+ for fc, gp := range mgp.pools {
+ gpCpy := *gp
+ pools[fc] = &gpCpy
+ }
+ gpCpy := *mgp.defaultPool
+ return &MultiGasPool{
+ pools: pools,
+ defaultPool: &gpCpy,
+ }
+}
diff --git a/core/celo_multi_gaspool_test.go b/core/celo_multi_gaspool_test.go
new file mode 100644
index 0000000000..70f60573a5
--- /dev/null
+++ b/core/celo_multi_gaspool_test.go
@@ -0,0 +1,138 @@
+package core
+
+import (
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+func TestMultiCurrencyGasPool(t *testing.T) {
+ blockGasLimit := uint64(1_000)
+ subGasAmount := 100
+
+ cUSDToken := common.HexToAddress("0x765DE816845861e75A25fCA122bb6898B8B1282a")
+ cEURToken := common.HexToAddress("0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73")
+
+ testCases := []struct {
+ name string
+ feeCurrency *FeeCurrency
+ allowlist []FeeCurrency
+ defaultLimit float64
+ limits FeeCurrencyLimitMapping
+ defaultPoolExpected bool
+ expectedValue uint64
+ }{
+ {
+ name: "Empty allowlist, empty mapping, CELO uses default pool",
+ feeCurrency: nil,
+ allowlist: []FeeCurrency{},
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{},
+ defaultPoolExpected: true,
+ expectedValue: 900, // blockGasLimit - subGasAmount
+ },
+ {
+ name: "Non-empty allowlist, non-empty mapping, CELO uses default pool",
+ feeCurrency: nil,
+ allowlist: []FeeCurrency{
+ cUSDToken,
+ },
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{
+ cUSDToken: 0.5,
+ },
+ defaultPoolExpected: true,
+ expectedValue: 900, // blockGasLimit - subGasAmount
+ },
+ {
+ name: "Empty allowlist, empty mapping, non-registered currency fallbacks to the default pool",
+ feeCurrency: &cUSDToken,
+ allowlist: []FeeCurrency{},
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{},
+ defaultPoolExpected: true,
+ expectedValue: 900, // blockGasLimit - subGasAmount
+ },
+ {
+ name: "Non-empty allowlist, non-empty mapping, non-registered currency uses default pool",
+ feeCurrency: &cEURToken,
+ allowlist: []FeeCurrency{
+ cUSDToken,
+ },
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{
+ cUSDToken: 0.5,
+ },
+ defaultPoolExpected: true,
+ expectedValue: 900, // blockGasLimit - subGasAmount
+ },
+ {
+ name: "Non-empty allowlist, empty mapping, registered currency uses default limit",
+ feeCurrency: &cUSDToken,
+ allowlist: []FeeCurrency{
+ cUSDToken,
+ },
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{},
+ defaultPoolExpected: false,
+ expectedValue: 800, // blockGasLimit * defaultLimit - subGasAmount
+ },
+ {
+ name: "Non-empty allowlist, non-empty mapping, configured registered currency uses configured limits",
+ feeCurrency: &cUSDToken,
+ allowlist: []FeeCurrency{
+ cUSDToken,
+ },
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{
+ cUSDToken: 0.5,
+ },
+ defaultPoolExpected: false,
+ expectedValue: 400, // blockGasLimit * 0.5 - subGasAmount
+ },
+ {
+ name: "Non-empty allowlist, non-empty mapping, unconfigured registered currency uses default limit",
+ feeCurrency: &cEURToken,
+ allowlist: []FeeCurrency{
+ cUSDToken,
+ cEURToken,
+ },
+ defaultLimit: 0.9,
+ limits: map[FeeCurrency]float64{
+ cUSDToken: 0.5,
+ },
+ defaultPoolExpected: false,
+ expectedValue: 800, // blockGasLimit * 0.5 - subGasAmount
+ },
+ }
+
+ for _, c := range testCases {
+ t.Run(c.name, func(t *testing.T) {
+ mgp := NewMultiGasPool(
+ blockGasLimit,
+ c.allowlist,
+ c.defaultLimit,
+ c.limits,
+ )
+
+ pool := mgp.PoolFor(c.feeCurrency)
+ pool.SubGas(uint64(subGasAmount))
+
+ if c.defaultPoolExpected {
+ result := mgp.PoolFor(nil).Gas()
+ if result != c.expectedValue {
+ t.Error("Default pool expected", c.expectedValue, "got", result)
+ }
+ } else {
+ result := mgp.PoolFor(c.feeCurrency).Gas()
+
+ if result != c.expectedValue {
+ t.Error(
+ "Expected pool", c.feeCurrency, "value", c.expectedValue,
+ "got", result,
+ )
+ }
+ }
+ })
+ }
+}
diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go
index fcc6e7602c..42e1bd5313 100644
--- a/core/txpool/legacypool/legacypool.go
+++ b/core/txpool/legacypool/legacypool.go
@@ -595,6 +595,9 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) map[common.Address]
Gas: txs[i].Gas(),
BlobGas: txs[i].BlobGas(),
DABytes: daBytes,
+
+ // Celo specific
+ FeeCurrency: txs[i].FeeCurrency(),
}
}
pending[addr] = lazies
diff --git a/core/txpool/subpool.go b/core/txpool/subpool.go
index 4e2a0133f3..aa02faaa8d 100644
--- a/core/txpool/subpool.go
+++ b/core/txpool/subpool.go
@@ -43,6 +43,9 @@ type LazyTransaction struct {
BlobGas uint64 // Amount of blob gas required by the transaction
DABytes *big.Int // Amount of data availability bytes this transaction may require if this is a rollup
+
+ // Celo
+ FeeCurrency *common.Address
}
// Resolve retrieves the full transaction belonging to a lazy handle if it is still
diff --git a/eth/handler_test.go b/eth/handler_test.go
index 7b250df2e9..aa52f43ced 100644
--- a/eth/handler_test.go
+++ b/eth/handler_test.go
@@ -108,13 +108,14 @@ func (p *testTxPool) Pending(filter txpool.PendingFilter) map[common.Address][]*
for addr, batch := range batches {
for _, tx := range batch {
pending[addr] = append(pending[addr], &txpool.LazyTransaction{
- Hash: tx.Hash(),
- Tx: tx,
- Time: tx.Time(),
- GasFeeCap: uint256.MustFromBig(tx.GasFeeCap()),
- GasTipCap: uint256.MustFromBig(tx.GasTipCap()),
- Gas: tx.Gas(),
- BlobGas: tx.BlobGas(),
+ Hash: tx.Hash(),
+ Tx: tx,
+ Time: tx.Time(),
+ GasFeeCap: uint256.MustFromBig(tx.GasFeeCap()),
+ GasTipCap: uint256.MustFromBig(tx.GasTipCap()),
+ Gas: tx.Gas(),
+ BlobGas: tx.BlobGas(),
+ FeeCurrency: tx.FeeCurrency(),
})
}
}
diff --git a/miner/celo_defaults.go b/miner/celo_defaults.go
new file mode 100644
index 0000000000..be843d1bc9
--- /dev/null
+++ b/miner/celo_defaults.go
@@ -0,0 +1,29 @@
+package miner
+
+import (
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+// cStables addresses on mainnet
+var (
+ cUSD_TOKEN = common.HexToAddress("0x765DE816845861e75A25fCA122bb6898B8B1282a")
+ cEUR_TOKEN = common.HexToAddress("0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73")
+ cREAL_TOKEN = common.HexToAddress("0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787")
+ USDC_TOKEN = common.HexToAddress("0xcebA9300f2b948710d2653dD7B07f33A8B32118C")
+ USDT_TOKEN = common.HexToAddress("0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e")
+)
+
+// default limits default fraction
+const DefaultFeeCurrencyLimit = 0.5
+
+// default limits configuration
+var DefaultFeeCurrencyLimits = map[uint64]map[common.Address]float64{
+ params.CeloMainnetChainID: {
+ cUSD_TOKEN: 0.9,
+ USDT_TOKEN: 0.9,
+ USDC_TOKEN: 0.9,
+ cEUR_TOKEN: 0.5,
+ cREAL_TOKEN: 0.5,
+ },
+}
diff --git a/miner/miner.go b/miner/miner.go
index 447cf4ccbd..f2590776ff 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -66,6 +66,10 @@ type Config struct {
EffectiveGasCeil uint64 // if non-zero, a gas ceiling to apply independent of the header's gaslimit value
MaxDATxSize *big.Int `toml:",omitempty"` // if non-nil, don't include any txs with data availability size larger than this in any built block
MaxDABlockSize *big.Int `toml:",omitempty"` // if non-nil, then don't build a block requiring more than this amount of total data availability
+
+ // Celo:
+ FeeCurrencyDefault float64 // Default fraction of block gas limit
+ FeeCurrencyLimits map[common.Address]float64 // Fee currency-to-limit fraction mapping
}
// DefaultConfig contains default settings for miner.
@@ -78,6 +82,8 @@ var DefaultConfig = Config{
// for payload generation. It should be enough for Geth to
// run 3 rounds.
Recommit: 2 * time.Second,
+
+ FeeCurrencyDefault: DefaultFeeCurrencyLimit,
}
// Miner is the main object which takes care of submitting new work to consensus
diff --git a/miner/worker.go b/miner/worker.go
index a4f297c6c6..a12fbfe8e2 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -81,6 +81,10 @@ type environment struct {
noTxs bool // true if we are reproducing a block, and do not have to check interop txs
rpcCtx context.Context // context to control block-building RPC work. No RPC allowed if nil.
+
+ // Celo specific
+ multiGasPool *core.MultiGasPool // available per-fee-currency gas used to pack transactions
+ feeCurrencyAllowlist []common.Address
}
const (
@@ -142,6 +146,14 @@ func (miner *Miner) generateWork(params *generateParams, witness bool) *newPaylo
}
work.gasPool = new(core.GasPool).AddGas(gasLimit)
}
+ if work.multiGasPool == nil {
+ work.multiGasPool = core.NewMultiGasPool(
+ work.header.GasLimit,
+ work.feeCurrencyAllowlist,
+ miner.config.FeeCurrencyDefault,
+ miner.config.FeeCurrencyLimits,
+ )
+ }
misc.EnsureCreate2Deployer(miner.chainConfig, work.header.Time, work.state)
@@ -152,6 +164,10 @@ func (miner *Miner) generateWork(params *generateParams, witness bool) *newPaylo
if err != nil {
return &newPayloadResult{err: fmt.Errorf("failed to force-include tx: %s type: %d sender: %s nonce: %d, err: %w", tx.Hash(), tx.Type(), from, tx.Nonce(), err)}
}
+ // the non-fee currency pool in the multipool is not used, but for consistency
+ // subtract the gas. Don't check the error either, this has been checked already
+ // with the work.gasPool.
+ work.multiGasPool.PoolFor(nil).SubGas(tx.Gas())
}
if !params.noTxs {
// use shared interrupt if present
@@ -301,8 +317,9 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir
return nil, err
}
env.noTxs = genParams.noTxs
+ context := core.NewEVMBlockContext(header, miner.chain, nil, miner.chainConfig, env.state)
+ env.feeCurrencyAllowlist = common.CurrencyAllowlist(context.ExchangeRates)
if header.ParentBeaconRoot != nil {
- context := core.NewEVMBlockContext(header, miner.chain, nil, miner.chainConfig, env.state)
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, miner.chainConfig, vm.Config{})
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state)
}
@@ -485,6 +502,14 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
env.gasPool = new(core.GasPool).AddGas(gasLimit)
}
blockDABytes := new(big.Int)
+ if env.multiGasPool == nil {
+ env.multiGasPool = core.NewMultiGasPool(
+ env.header.GasLimit,
+ env.feeCurrencyAllowlist,
+ miner.config.FeeCurrencyDefault,
+ miner.config.FeeCurrencyLimits,
+ )
+ }
for {
// Check interruption signal and abort building if it's fired.
if interrupt != nil {
@@ -548,6 +573,15 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
continue
}
}
+ if left := env.multiGasPool.PoolFor(ltx.FeeCurrency).Gas(); left < ltx.Gas {
+ log.Trace(
+ "Not enough specific fee-currency gas left for transaction",
+ "currency", ltx.FeeCurrency, "hash", ltx.Hash,
+ "left", left, "needed", ltx.Gas,
+ )
+ txs.Pop()
+ continue
+ }
// Transaction seems to fit, pull it up from the pool
tx := ltx.Resolve()
if tx == nil {
@@ -569,7 +603,9 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
// Start executing the transaction
env.state.SetTxContext(tx.Hash(), env.tcount)
+ availableGas := env.gasPool.Gas()
err := miner.commitTransaction(env, tx)
+ gasUsed := availableGas - env.gasPool.Gas()
switch {
case errors.Is(err, core.ErrNonceTooLow):
// New head notification data race between the transaction pool and miner, shift
@@ -594,6 +630,23 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
txs.Pop()
case errors.Is(err, nil):
+ err := env.multiGasPool.PoolFor(tx.FeeCurrency()).SubGas(gasUsed)
+ if err != nil {
+ // Should never happen as we check it above
+ log.Warn(
+ "Unexpectedly reached limit for fee currency, but tx will not be skipped",
+ "hash", tx.Hash(), "gas", env.multiGasPool.PoolFor(tx.FeeCurrency()).Gas(),
+ "tx gas used", gasUsed,
+ )
+ // If we reach this codepath, we want to still include the transaction,
+ // since the "global" gasPool in the commitTransaction accepted it and we
+ // would have to roll the transaction back now, introducing unnecessary
+ // complexity.
+ // Since we shouldn't reach this point anyways and the
+ // block gas limit per fee currency is enforced voluntarily and not
+ // included in the consensus this is fine.
+ }
+
// Everything ok, collect the logs and shift in the next transaction from the same account
blockDABytes = daBytesAfter
txs.Shift()