Skip to content

Commit 756eb95

Browse files
committed
core: miner: apply envelope amount limit to miner and block processor
1 parent 4c52135 commit 756eb95

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

core/error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ var (
5959
// by a transaction is higher than what's left in the block.
6060
ErrGasLimitReached = errors.New("gas limit reached")
6161

62+
// ErrEnvelopeNumberLimitReached is returned if the number of envelope transactions
63+
// in the block has reached the limit and is going to break the limit.
64+
ErrEnvelopeNumberLimitReached = errors.New("envelope number limit reached")
65+
6266
// ErrInsufficientFundsForTransfer is returned if the transaction sender doesn't
6367
// have enough funds for transfer(topmost call only).
6468
ErrInsufficientFundsForTransfer = errors.New("insufficient funds for transfer")

core/state_processor.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"math/big"
2323

24+
"github.com/ethereum/go-ethereum/antimev"
2425
"github.com/ethereum/go-ethereum/common"
2526
"github.com/ethereum/go-ethereum/consensus"
2627
"github.com/ethereum/go-ethereum/consensus/misc"
@@ -60,22 +61,24 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
6061
// transactions failed to execute due to insufficient gas it will return an error.
6162
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
6263
var (
63-
receipts types.Receipts
64-
usedGas = new(uint64)
65-
header = block.Header()
66-
blockHash = block.Hash()
67-
blockNumber = block.Number()
68-
allLogs []*types.Log
69-
gp = new(GasPool).AddGas(block.GasLimit())
64+
receipts types.Receipts
65+
usedGas = new(uint64)
66+
envelopeCount = uint64(0)
67+
header = block.Header()
68+
blockHash = block.Hash()
69+
blockNumber = block.Number()
70+
allLogs []*types.Log
71+
gp = new(GasPool).AddGas(block.GasLimit())
7072
)
7173
// Mutate the block and state according to any hard-fork specs
7274
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
7375
misc.ApplyDAOHardFork(statedb)
7476
}
7577
var (
76-
context = NewEVMBlockContext(header, p.bc, nil)
77-
vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg)
78-
signer = types.MakeSigner(p.config, header.Number, header.Time)
78+
context = NewEVMBlockContext(header, p.bc, nil)
79+
vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg)
80+
signer = types.MakeSigner(p.config, header.Number, header.Time)
81+
envelopeNumberLimit = statedb.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMaxEnvelopesPerBlockStateHash()).Big().Uint64()
7982
)
8083
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
8184
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
@@ -88,6 +91,14 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
8891
}
8992
// Iterate over and process the individual transactions
9093
for i, tx := range block.Transactions() {
94+
// Check against envelope number policy
95+
if p.bc.chainConfig.IsNeoXAMEV(blockNumber) && antimev.IsEnvelope(tx) {
96+
if envelopeCount >= envelopeNumberLimit {
97+
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w, envelope limit %v", i, tx.Hash().Hex(),
98+
ErrEnvelopeNumberLimitReached, envelopeNumberLimit)
99+
}
100+
envelopeCount++
101+
}
91102
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
92103
if err != nil {
93104
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)

core/systemcontracts/contracts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const blackListSlotIndex = 1
2525
const minGasTipCapSlotIndex = 2
2626
const baseFeeSlotIndex = 3
2727
const envelopeFeeSlotIndex = 5
28+
const maxEnvelopesPerBlockSlotIndex = 6
2829
const maxEnvelopeGasLimitSlotIndex = 7
2930

3031
// A set of genesis contract hashes.
@@ -104,6 +105,13 @@ func GetEnvelopeFeeStateHash() common.Hash {
104105
return common.BytesToHash([]byte{envelopeFeeSlotIndex})
105106
}
106107

108+
// GetMaxEnvelopesPerBlockStateHash computes and returns the storage key
109+
// of maxEnvelopesPerBlock in policy contract, for reading corresponding
110+
// values from statedb.
111+
func GetMaxEnvelopesPerBlockStateHash() common.Hash {
112+
return common.BytesToHash([]byte{maxEnvelopesPerBlockSlotIndex})
113+
}
114+
107115
// GetMaxEnvelopeGasLimitStateHash computes and returns the storage key
108116
// of maxEnvelopeGasLimit in policy contract, for reading corresponding
109117
// values from statedb.

miner/worker.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ import (
2424
"sync/atomic"
2525
"time"
2626

27+
"github.com/ethereum/go-ethereum/antimev"
2728
"github.com/ethereum/go-ethereum/common"
2829
"github.com/ethereum/go-ethereum/consensus"
2930
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3031
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
3132
"github.com/ethereum/go-ethereum/core"
3233
"github.com/ethereum/go-ethereum/core/state"
34+
"github.com/ethereum/go-ethereum/core/systemcontracts"
3335
"github.com/ethereum/go-ethereum/core/txpool"
3436
"github.com/ethereum/go-ethereum/core/types"
3537
"github.com/ethereum/go-ethereum/core/vm"
@@ -86,6 +88,7 @@ type environment struct {
8688
signer types.Signer
8789
state *state.StateDB // apply state changes here
8890
tcount int // tx count in cycle
91+
ecount int // envelope tx count in cycle
8992
gasPool *core.GasPool // available gas used to pack transactions
9093
coinbase common.Address
9194

@@ -102,6 +105,7 @@ func (env *environment) copy() *environment {
102105
signer: env.signer,
103106
state: env.state.Copy(),
104107
tcount: env.tcount,
108+
ecount: env.ecount,
105109
coinbase: env.coinbase,
106110
header: types.CopyHeader(env.header),
107111
receipts: copyReceipts(env.receipts),
@@ -744,6 +748,7 @@ func (w *worker) makeEnv(parent *types.Header, header *types.Header, coinbase co
744748
}
745749
// Keep track of transactions which return errors so they can be removed
746750
env.tcount = 0
751+
env.ecount = 0
747752
return env, nil
748753
}
749754

@@ -892,6 +897,17 @@ func (w *worker) commitTransactions(env *environment, plainTxs, blobTxs *transac
892897
txs.Pop()
893898
continue
894899
}
900+
// Check whether the tx is an Envelope. If it is an Envelope and
901+
// policy doesn't allow any more Envelopes in this block, ignore.
902+
if w.chainConfig.IsNeoXAMEV(env.header.Number) && antimev.IsEnvelope(tx) {
903+
envelopNumberLimit := env.state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetMaxEnvelopesPerBlockStateHash()).Big()
904+
if new(big.Int).SetUint64(uint64(env.ecount)).Cmp(envelopNumberLimit) >= 0 {
905+
log.Trace("Ignoring envelope transaction more than limit", "hash", ltx.Hash, "limit", envelopNumberLimit)
906+
txs.Pop()
907+
continue
908+
}
909+
env.ecount++
910+
}
895911
// Start executing the transaction
896912
env.state.SetTxContext(tx.Hash(), env.tcount)
897913

0 commit comments

Comments
 (0)