feat(block_validator): check payload size during block validation (#322)

* added block size check in validate body

* added payload method to block

* Update core/types/block.go

Co-authored-by: Péter Garamvölgyi <th307q@gmail.com>

* fixed test

* fix

* bump version

* fix test

---------

Co-authored-by: Péter Garamvölgyi <th307q@gmail.com>
Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
This commit is contained in:
Richord 2023-05-24 06:23:57 -07:00 committed by GitHub
parent 31b754e14b
commit ad43d0ea3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 1 deletions

View file

@ -57,6 +57,10 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
if !v.config.Scroll.IsValidTxCount(len(block.Transactions())) { if !v.config.Scroll.IsValidTxCount(len(block.Transactions())) {
return consensus.ErrInvalidTxCount return consensus.ErrInvalidTxCount
} }
// Check if block payload size is smaller than the max size
if !v.config.Scroll.IsValidBlockSize(block.PayloadSize()) {
return ErrInvalidBlockPayloadSize
}
// Header validity is known at this point, check the uncles and transactions // Header validity is known at this point, check the uncles and transactions
header := block.Header() header := block.Header()
if err := v.engine.VerifyUncles(v.bc, block); err != nil { if err := v.engine.VerifyUncles(v.bc, block); err != nil {

View file

@ -3239,6 +3239,67 @@ func TestTransactionCountLimit(t *testing.T) {
} }
} }
func TestBlockPayloadSizeLimit(t *testing.T) {
// Create config that allows at most 150 bytes per block payload
config := params.TestChainConfig
config.Scroll.MaxTxPayloadBytesPerBlock = new(int)
*config.Scroll.MaxTxPayloadBytesPerBlock = 150
config.Scroll.MaxTxPerBlock = nil
var (
engine = ethash.NewFaker()
db = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
address = crypto.PubkeyToAddress(key.PublicKey)
funds = big.NewInt(1000000000000000)
gspec = &Genesis{Config: config, Alloc: GenesisAlloc{address: {Balance: funds}}}
genesis = gspec.MustCommit(db)
)
addTx := func(b *BlockGen) {
tx := types.NewTransaction(b.TxNonce(address), address, big.NewInt(0), 50000, b.header.BaseFee, nil)
signed, _ := types.SignTx(tx, types.HomesteadSigner{}, key)
b.AddTx(signed)
}
// Initialize blockchain
blockchain, err := NewBlockChain(db, nil, config, engine, vm.Config{}, nil, nil)
if err != nil {
t.Fatalf("failed to create new chain manager: %v", err)
}
defer blockchain.Stop()
// Insert empty block
block1, _ := GenerateChain(config, genesis, ethash.NewFaker(), db, 1, func(i int, b *BlockGen) {
// empty
})
if _, err := blockchain.InsertChain(block1); err != nil {
t.Fatalf("failed to insert chain: %v", err)
}
// Insert block with 1 transaction
block2, _ := GenerateChain(config, genesis, ethash.NewFaker(), db, 1, func(i int, b *BlockGen) {
addTx(b)
})
if _, err := blockchain.InsertChain(block2); err != nil {
t.Fatalf("failed to insert chain: %v", err)
}
// Insert block with 2 transactions
block3, _ := GenerateChain(config, genesis, ethash.NewFaker(), db, 1, func(i int, b *BlockGen) {
addTx(b)
addTx(b)
})
_, err = blockchain.InsertChain(block3)
if !errors.Is(err, ErrInvalidBlockPayloadSize) {
t.Fatalf("error mismatch: have: %v, want: %v", err, ErrInvalidBlockPayloadSize)
}
}
func TestEIP3651(t *testing.T) { func TestEIP3651(t *testing.T) {
var ( var (
addraa = common.HexToAddress("0x000000000000000000000000000000000000aaaa") addraa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")

View file

@ -26,6 +26,9 @@ var (
// ErrKnownBlock is returned when a block to import is already known locally. // ErrKnownBlock is returned when a block to import is already known locally.
ErrKnownBlock = errors.New("block already known") ErrKnownBlock = errors.New("block already known")
// ErrInvalidBlockPayloadSize is returned when a block to import has an oversized payload.
ErrInvalidBlockPayloadSize = errors.New("invalid block payload size")
// ErrBannedHash is returned if a block to import is on the banned list. // ErrBannedHash is returned if a block to import is on the banned list.
ErrBannedHash = errors.New("banned hash") ErrBannedHash = errors.New("banned hash")

View file

@ -325,6 +325,16 @@ func (b *Block) Size() common.StorageSize {
return common.StorageSize(c) return common.StorageSize(c)
} }
// PayloadSize returns the sum of all transactions in a block.
func (b *Block) PayloadSize() common.StorageSize {
// add up all txs sizes
var totalSize common.StorageSize
for _, tx := range b.transactions {
totalSize += tx.Size()
}
return totalSize
}
// SanityCheck can be used to prevent that unbounded fields are // SanityCheck can be used to prevent that unbounded fields are
// stuffed with junk data to add processing overhead // stuffed with junk data to add processing overhead
func (b *Block) SanityCheck() error { func (b *Block) SanityCheck() error {

View file

@ -24,7 +24,7 @@ import (
const ( const (
VersionMajor = 4 // Major version component of the current release VersionMajor = 4 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release VersionMinor = 0 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release VersionPatch = 1 // Patch version component of the current release
VersionMeta = "sepolia" // Version metadata to append to the version string VersionMeta = "sepolia" // Version metadata to append to the version string
) )