Skip to content

Commit

Permalink
Blocklist malfunctioning fee-currencies in miner
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac committed Sep 10, 2024
1 parent d4fd756 commit 14f108c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type Miner struct {
pendingMu sync.Mutex // Lock protects the pending block

backend Backend

feeCurrencyBlocklist *AddressBlocklist
}

// New creates a new miner with provided config.
Expand All @@ -102,6 +104,8 @@ func New(eth Backend, config Config, engine consensus.Engine) *Miner {
txpool: eth.TxPool(),
chain: eth.BlockChain(),
pending: &pending{},

feeCurrencyBlocklist: NewAddressBlocklist(),
}
}

Expand Down
43 changes: 42 additions & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/txpool"
Expand Down Expand Up @@ -261,7 +262,17 @@ func (miner *Miner) prepareWork(genParams *generateParams) (*environment, error)
return nil, err
}
context := core.NewEVMBlockContext(header, miner.chain, nil, miner.chainConfig, env.state)
env.feeCurrencyAllowlist = common.CurrencyAllowlist(context.FeeCurrencyContext.ExchangeRates)
if evicted := miner.feeCurrencyBlocklist.Evict(parent); len(evicted) > 0 {
log.Warn(
"Evicted temporarily blocked fee-currencies from local block-list",
"evicted-fee-currencies", evicted,
"eviction-timeout-seconds", evictionTimeoutSeconds,
)
}
env.feeCurrencyAllowlist = miner.feeCurrencyBlocklist.FilterAllowlist(
common.CurrencyAllowlist(context.FeeCurrencyContext.ExchangeRates),
header,
)
if header.ParentBeaconRoot != nil {
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, miner.chainConfig, vm.Config{})
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state)
Expand Down Expand Up @@ -304,6 +315,9 @@ func (miner *Miner) commitTransaction(env *environment, tx *types.Transaction) e
}
receipt, err := miner.applyTransaction(env, tx)
if err != nil {
if errors.Is(err, contracts.ErrFeeCurrencyEVMCall) {
miner.blockFeeCurrency(env, tx, err)
}
return err
}
env.txs = append(env.txs, tx)
Expand Down Expand Up @@ -406,6 +420,13 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
if ltx == nil {
break
}
if ltx.FeeCurrency != nil {
if _, ok := env.feeCurrencyAllowlist[*ltx.FeeCurrency]; !ok {
log.Trace("Fee-currency not in local allowlist", "hash", ltx.Hash, "fee-currency", ltx.FeeCurrency)
txs.Pop()
continue
}
}
// If we don't have enough space for the next transaction, skip the account.
if env.gasPool.Gas() < ltx.Gas {
log.Trace("Not enough gas left for transaction", "hash", ltx.Hash, "left", env.gasPool.Gas(), "needed", ltx.Gas)
Expand Down Expand Up @@ -603,3 +624,23 @@ func (miner *Miner) validateParams(genParams *generateParams) (time.Duration, er
}
return time.Duration(blockTime) * time.Second, nil
}

func (miner *Miner) blockFeeCurrency(env *environment, tx *types.Transaction, err error) {
if tx.FeeCurrency() != nil {
log.Warn(
"fee-currency EVM execution error, temporarily blocking fee-currency in local txpools",
"tx-hash", tx.Hash(),
"fee-currency", tx.FeeCurrency(),
"error", err.Error(),
)
// the fee-currency is still in the allowlist of this environment,
// so set the fee-currency block gas limit to 0 to prevent other
// transactions.
pool := env.multiGasPool.PoolFor(tx.FeeCurrency())
pool.SetGas(0)
// also add the fee-currency to a worker-wide blocklist,
// so that they are not allowlisted in the following blocks
// (only locally in the txpool, not consensus-critical)
miner.feeCurrencyBlocklist.Add(*tx.FeeCurrency(), *env.header)
}
}

0 comments on commit 14f108c

Please sign in to comment.