From 5017dcb92a371695ae91f2bfa7ee8207adcb04da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Wed, 3 May 2023 17:45:17 +0200 Subject: [PATCH] Add block payload size limit (#309) add block payload size limit --- miner/worker.go | 19 +++++++++++++------ params/config.go | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index 581df046b0..942deae0f5 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -82,12 +82,13 @@ const ( type environment struct { signer types.Signer - state *state.StateDB // apply state changes here - ancestors mapset.Set // ancestor set (used for checking uncle parent validity) - family mapset.Set // family set (used for checking uncle invalidity) - uncles mapset.Set // uncle set - tcount int // tx count in cycle - gasPool *core.GasPool // available gas used to pack transactions + state *state.StateDB // apply state changes here + ancestors mapset.Set // ancestor set (used for checking uncle parent validity) + family mapset.Set // family set (used for checking uncle invalidity) + uncles mapset.Set // uncle set + tcount int // tx count in cycle + blockSize common.StorageSize // approximate size of tx payload in bytes + gasPool *core.GasPool // available gas used to pack transactions header *types.Header txs []*types.Transaction @@ -707,6 +708,7 @@ func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error { } // Keep track of transactions which return errors so they can be removed env.tcount = 0 + env.blockSize = 0 // Swap out the old work with the new one, terminating any leftover prefetcher // processes in the mean time and starting a new one. @@ -833,6 +835,10 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin if tx == nil { break } + if !w.chainConfig.Scroll.IsValidBlockSize(w.current.blockSize + tx.Size()) { + log.Trace("Block size limit reached", "have", w.current.blockSize, "want", w.chainConfig.Scroll.MaxTxPayloadBytesPerBlock, "tx", tx.Size()) + break + } // Error may be ignored here. The error has already been checked // during transaction acceptance is the transaction pool. // @@ -870,6 +876,7 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin // Everything ok, collect the logs and shift in the next transaction from the same account coalescedLogs = append(coalescedLogs, logs...) w.current.tcount++ + w.current.blockSize += tx.Size() txs.Shift() case errors.Is(err, core.ErrTxTypeNotSupported): diff --git a/params/config.go b/params/config.go index 958d797895..687d840002 100644 --- a/params/config.go +++ b/params/config.go @@ -255,8 +255,9 @@ var ( } // ScrollAlphaChainConfig contains the chain parameters to run a node on the Scroll Alpha test network. - ScrollFeeVaultAddress = common.HexToAddress("0x5300000000000000000000000000000000000005") - ScrollMaxTxPerBlock = 44 + ScrollFeeVaultAddress = common.HexToAddress("0x5300000000000000000000000000000000000005") + ScrollMaxTxPerBlock = 44 + ScrollMaxTxPayloadBytesPerBlock = 120 * 1024 ScrollAlphaChainConfig = &ChainConfig{ ChainID: big.NewInt(534353), @@ -279,11 +280,12 @@ var ( Epoch: 30000, }, Scroll: ScrollConfig{ - UseZktrie: true, - MaxTxPerBlock: &ScrollMaxTxPerBlock, - FeeVaultAddress: &ScrollFeeVaultAddress, - EnableEIP2718: false, - EnableEIP1559: false, + UseZktrie: true, + MaxTxPerBlock: &ScrollMaxTxPerBlock, + MaxTxPayloadBytesPerBlock: &ScrollMaxTxPayloadBytesPerBlock, + FeeVaultAddress: &ScrollFeeVaultAddress, + EnableEIP2718: false, + EnableEIP1559: false, }, } @@ -431,6 +433,9 @@ type ScrollConfig struct { // Maximum number of transactions per block [optional] MaxTxPerBlock *int `json:"maxTxPerBlock,omitempty"` + // Maximum tx payload size of blocks that we produce [optional] + MaxTxPayloadBytesPerBlock *int `json:"maxTxPayloadBytesPerBlock,omitempty"` + // Transaction fee vault address [optional] FeeVaultAddress *common.Address `json:"feeVaultAddress,omitempty"` @@ -468,6 +473,11 @@ func (s ScrollConfig) IsValidTxCount(count int) bool { return s.MaxTxPerBlock == nil || count <= *s.MaxTxPerBlock } +// IsValidBlockSize returns whether the given block's transaction payload size is below the limit. +func (s ScrollConfig) IsValidBlockSize(size common.StorageSize) bool { + return s.MaxTxPayloadBytesPerBlock == nil || size <= common.StorageSize(*s.MaxTxPayloadBytesPerBlock) +} + // EthashConfig is the consensus engine configs for proof-of-work based sealing. type EthashConfig struct{}