@@ -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
7581var (
@@ -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
0 commit comments