Skip to content

Commit a28262b

Browse files
authored
miner: limit block size to eth protocol msg size (#2696)
1 parent 7de27ca commit a28262b

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

eth/protocols/eth/protocol.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/forkid"
2727
"github.com/ethereum/go-ethereum/core/types"
28+
"github.com/ethereum/go-ethereum/params"
2829
"github.com/ethereum/go-ethereum/rlp"
2930
)
3031

@@ -46,7 +47,7 @@ var ProtocolVersions = []uint{ETH68}
4647
var protocolLengths = map[uint]uint64{ETH68: 17}
4748

4849
// maxMessageSize is the maximum cap on the size of a protocol message.
49-
const maxMessageSize = 10 * 1024 * 1024
50+
var maxMessageSize = params.MaxMessageSize
5051

5152
const (
5253
StatusMsg = 0x00

miner/bid_simulator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,14 @@ func (b *bidSimulator) simBid(interruptCh chan int32, bidRuntime *BidRuntime) {
692692
return
693693
}
694694

695+
// check bid size
696+
if bidRuntime.env.size+blockReserveSize > params.MaxMessageSize {
697+
log.Error("BidSimulator: failed to check bid size", "builder", bidRuntime.bid.Builder,
698+
"bidHash", bidRuntime.bid.Hash(), "env.size", bidRuntime.env.size)
699+
err = errors.New("invalid bid size")
700+
return
701+
}
702+
695703
bestBid := b.GetBestBid(parentHash)
696704
if bestBid == nil {
697705
log.Info("[BID RESULT]", "win", "true[first]", "builder", bidRuntime.bid.Builder, "hash", bidRuntime.bid.Hash().TerminalString())
@@ -858,6 +866,7 @@ func (r *BidRuntime) commitTransaction(chain *core.BlockChain, chainConfig *para
858866
}
859867

860868
r.env.tcount++
869+
r.env.size += uint32(tx.Size())
861870

862871
return nil
863872
}

miner/worker.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ const (
7070

7171
// the default to wait for the mev miner to finish
7272
waitMEVMinerEndTimeLimit = 50 * time.Millisecond
73+
74+
// Reserve block size for the following 3 components:
75+
// a. System transactions at the end of the block
76+
// b. Seal in the block header
77+
// c. Overhead from RLP encoding
78+
blockReserveSize = 100 * 1024
7379
)
7480

7581
var (
@@ -89,6 +95,7 @@ type environment struct {
8995
signer types.Signer
9096
state *state.StateDB // apply state changes here
9197
tcount int // tx count in cycle
98+
size uint32 // almost accurate block size,
9299
gasPool *core.GasPool // available gas used to pack transactions
93100
coinbase common.Address
94101

@@ -105,6 +112,7 @@ func (env *environment) copy() *environment {
105112
signer: env.signer,
106113
state: env.state.Copy(),
107114
tcount: env.tcount,
115+
size: env.size,
108116
coinbase: env.coinbase,
109117
header: types.CopyHeader(env.header),
110118
receipts: copyReceipts(env.receipts),
@@ -895,6 +903,13 @@ LOOP:
895903
txs.Pop()
896904
continue
897905
}
906+
// If we don't have enough size left for the next transaction, skip it.
907+
if env.size+uint32(tx.Size())+blockReserveSize > params.MaxMessageSize {
908+
log.Trace("Not enough size left for transaction", "hash", ltx.Hash,
909+
"env.size", env.size, "needed", uint32(tx.Size()))
910+
txs.Pop()
911+
continue
912+
}
898913
// Error may be ignored here. The error has already been checked
899914
// during transaction acceptance is the transaction pool.
900915
from, _ := types.Sender(env.signer, tx)
@@ -920,6 +935,7 @@ LOOP:
920935
// Everything ok, collect the logs and shift in the next transaction from the same account
921936
coalescedLogs = append(coalescedLogs, logs...)
922937
env.tcount++
938+
env.size += uint32(tx.Size()) // size of BlobTxSidecar included
923939
txs.Shift()
924940

925941
default:
@@ -1055,6 +1071,9 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) {
10551071
vmenv := vm.NewEVM(context, vm.TxContext{}, env.state, w.chainConfig, vm.Config{})
10561072
core.ProcessBeaconBlockRoot(*header.ParentBeaconRoot, vmenv, env.state)
10571073
}
1074+
1075+
env.size = uint32(env.header.Size())
1076+
10581077
return env, nil
10591078
}
10601079

params/protocol_params.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929
GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block.
3030
PayBidTxGasLimit uint64 = 25000 // Gas limit of the PayBidTx in the types.BidArgs.
3131

32+
MaxMessageSize uint32 = 10 * 1024 * 1024 // MaxMessageSize is the maximum cap on the size of a eth protocol message.
33+
3234
MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
3335
ForkIDSize uint64 = 4 // The length of fork id
3436
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.

0 commit comments

Comments
 (0)