core/types: fix immutability guarantees in Block #27844, close XFN-82 (#1727)

This commit is contained in:
Daniel Liu 2025-11-16 13:53:22 +08:00 committed by benjamin202410
parent 01f65ffe8e
commit 247eb0d464

View file

@ -160,7 +160,23 @@ type Body struct {
Uncles []*Header Uncles []*Header
} }
// Block represents an entire block in the Ethereum blockchain. // Block represents an Ethereum block.
//
// Note the Block type tries to be 'immutable', and contains certain caches that rely
// on that. The rules around block immutability are as follows:
//
// - We copy all data when the block is constructed. This makes references held inside
// the block independent of whatever value was passed in.
//
// - We copy all header data on access. This is because any change to the header would mess
// up the cached hash and size values in the block. Calling code is expected to take
// advantage of this to avoid over-allocating!
//
// - When new body data is attached to the block, a shallow copy of the block is returned.
// This ensures block modifications are race-free.
//
// - We do not copy body data on access because it does not affect the caches, and also
// because it would be too expensive.
type Block struct { type Block struct {
header *Header header *Header
uncles []*Header uncles []*Header
@ -255,8 +271,7 @@ func NewBlockWithHeader(header *Header) *Block {
return &Block{header: CopyHeader(header)} return &Block{header: CopyHeader(header)}
} }
// CopyHeader creates a deep copy of a block header to prevent side effects from // CopyHeader creates a deep copy of a block header.
// modifying a header variable.
func CopyHeader(h *Header) *Header { func CopyHeader(h *Header) *Header {
cpy := *h cpy := *h
if cpy.Time = new(big.Int); h.Time != nil { if cpy.Time = new(big.Int); h.Time != nil {
@ -282,7 +297,7 @@ func CopyHeader(h *Header) *Header {
return &cpy return &cpy
} }
// DecodeRLP decodes the Ethereum // DecodeRLP decodes a block from RLP.
func (b *Block) DecodeRLP(s *rlp.Stream) error { func (b *Block) DecodeRLP(s *rlp.Stream) error {
var eb extblock var eb extblock
_, size, _ := s.Kind() _, size, _ := s.Kind()
@ -294,7 +309,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
return nil return nil
} }
// EncodeRLP serializes b into the Ethereum RLP block format. // EncodeRLP serializes a block as RLP.
func (b *Block) EncodeRLP(w io.Writer) error { func (b *Block) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, extblock{ return rlp.Encode(w, extblock{
Header: b.header, Header: b.header,
@ -313,7 +328,14 @@ func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
return nil return nil
} }
// TODO: copies // Body returns the non-header content of the block.
// Note the returned data is not an independent copy.
func (b *Block) Body() *Body {
return &Body{b.transactions, b.uncles}
}
// Accessors for body data. These do not return a copy because the content
// of the body slices does not affect the cached hash/size in block.
func (b *Block) Uncles() []*Header { return b.uncles } func (b *Block) Uncles() []*Header { return b.uncles }
func (b *Block) Transactions() Transactions { return b.transactions } func (b *Block) Transactions() Transactions { return b.transactions }
@ -327,6 +349,13 @@ func (b *Block) Transaction(hash common.Hash) *Transaction {
return nil return nil
} }
// Header returns the block header (as a copy).
func (b *Block) Header() *Header {
return CopyHeader(b.header)
}
// Header value accessors. These do copy!
func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) } func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
func (b *Block) GasLimit() uint64 { return b.header.GasLimit } func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
func (b *Block) GasUsed() uint64 { return b.header.GasUsed } func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
@ -354,11 +383,6 @@ func (b *Block) BaseFee() *big.Int {
return new(big.Int).Set(b.header.BaseFee) return new(big.Int).Set(b.header.BaseFee)
} }
func (b *Block) Header() *Header { return CopyHeader(b.header) }
// Body returns the non-header content of the block.
func (b *Block) Body() *Body { return &Body{b.transactions, b.uncles} }
func (b *Block) HashNoNonce() common.Hash { func (b *Block) HashNoNonce() common.Hash {
return b.header.HashNoNonce() return b.header.HashNoNonce()
} }
@ -392,10 +416,8 @@ func CalcUncleHash(uncles []*Header) common.Hash {
// WithSeal returns a new block with the data from b but the header replaced with // WithSeal returns a new block with the data from b but the header replaced with
// the sealed one. // the sealed one.
func (b *Block) WithSeal(header *Header) *Block { func (b *Block) WithSeal(header *Header) *Block {
cpy := *header
return &Block{ return &Block{
header: &cpy, header: CopyHeader(header),
transactions: b.transactions, transactions: b.transactions,
uncles: b.uncles, uncles: b.uncles,
} }