Make block headers and txs public

This commit is contained in:
Jeremy Wei 2024-02-11 18:23:06 -06:00
parent dfcdccd122
commit a4f97f7b3d

View file

@ -191,9 +191,9 @@ type Body struct {
// - We do not copy body data on access because it does not affect the caches, and also // - We do not copy body data on access because it does not affect the caches, and also
// because it would be too expensive. // because it would be too expensive.
type Block struct { type Block struct {
header *Header Header_ *Header
uncles []*Header uncles []*Header
transactions Transactions Txs Transactions
withdrawals Withdrawals withdrawals Withdrawals
// caches // caches
@ -221,28 +221,28 @@ type extblock struct {
// are ignored and set to values derived from the given txs, uncles // are ignored and set to values derived from the given txs, uncles
// and receipts. // and receipts.
func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, hasher TrieHasher) *Block { func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, hasher TrieHasher) *Block {
b := &Block{header: CopyHeader(header)} b := &Block{Header_: CopyHeader(header)}
// TODO: panic if len(txs) != len(receipts) // TODO: panic if len(txs) != len(receipts)
if len(txs) == 0 { if len(txs) == 0 {
b.header.TxHash = EmptyTxsHash b.Header_.TxHash = EmptyTxsHash
} else { } else {
b.header.TxHash = DeriveSha(Transactions(txs), hasher) b.Header_.TxHash = DeriveSha(Transactions(txs), hasher)
b.transactions = make(Transactions, len(txs)) b.Txs = make(Transactions, len(txs))
copy(b.transactions, txs) copy(b.Txs, txs)
} }
if len(receipts) == 0 { if len(receipts) == 0 {
b.header.ReceiptHash = EmptyReceiptsHash b.Header_.ReceiptHash = EmptyReceiptsHash
} else { } else {
b.header.ReceiptHash = DeriveSha(Receipts(receipts), hasher) b.Header_.ReceiptHash = DeriveSha(Receipts(receipts), hasher)
b.header.Bloom = CreateBloom(receipts) b.Header_.Bloom = CreateBloom(receipts)
} }
if len(uncles) == 0 { if len(uncles) == 0 {
b.header.UncleHash = EmptyUncleHash b.Header_.UncleHash = EmptyUncleHash
} else { } else {
b.header.UncleHash = CalcUncleHash(uncles) b.Header_.UncleHash = CalcUncleHash(uncles)
b.uncles = make([]*Header, len(uncles)) b.uncles = make([]*Header, len(uncles))
for i := range uncles { for i := range uncles {
b.uncles[i] = CopyHeader(uncles[i]) b.uncles[i] = CopyHeader(uncles[i])
@ -261,12 +261,12 @@ func NewBlockWithWithdrawals(header *Header, txs []*Transaction, uncles []*Heade
b := NewBlock(header, txs, uncles, receipts, hasher) b := NewBlock(header, txs, uncles, receipts, hasher)
if withdrawals == nil { if withdrawals == nil {
b.header.WithdrawalsHash = nil b.Header_.WithdrawalsHash = nil
} else if len(withdrawals) == 0 { } else if len(withdrawals) == 0 {
b.header.WithdrawalsHash = &EmptyWithdrawalsHash b.Header_.WithdrawalsHash = &EmptyWithdrawalsHash
} else { } else {
h := DeriveSha(Withdrawals(withdrawals), hasher) h := DeriveSha(Withdrawals(withdrawals), hasher)
b.header.WithdrawalsHash = &h b.Header_.WithdrawalsHash = &h
} }
return b.WithWithdrawals(withdrawals) return b.WithWithdrawals(withdrawals)
@ -314,7 +314,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&eb); err != nil { if err := s.Decode(&eb); err != nil {
return err return err
} }
b.header, b.uncles, b.transactions, b.withdrawals = eb.Header, eb.Uncles, eb.Txs, eb.Withdrawals b.Header_, b.uncles, b.Txs, b.withdrawals = eb.Header, eb.Uncles, eb.Txs, eb.Withdrawals
b.size.Store(rlp.ListSize(size)) b.size.Store(rlp.ListSize(size))
return nil return nil
} }
@ -322,8 +322,8 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
// EncodeRLP serializes a block as RLP. // 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_,
Txs: b.transactions, Txs: b.Txs,
Uncles: b.uncles, Uncles: b.uncles,
Withdrawals: b.withdrawals, Withdrawals: b.withdrawals,
}) })
@ -332,18 +332,18 @@ func (b *Block) EncodeRLP(w io.Writer) error {
// Body returns the non-header content of the block. // Body returns the non-header content of the block.
// Note the returned data is not an independent copy. // Note the returned data is not an independent copy.
func (b *Block) Body() *Body { func (b *Block) Body() *Body {
return &Body{b.transactions, b.uncles, b.withdrawals} return &Body{b.Txs, b.uncles, b.withdrawals}
} }
// Accessors for body data. These do not return a copy because the content // 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. // 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.Txs }
func (b *Block) Withdrawals() Withdrawals { return b.withdrawals } func (b *Block) Withdrawals() Withdrawals { return b.withdrawals }
func (b *Block) Transaction(hash common.Hash) *Transaction { func (b *Block) Transaction(hash common.Hash) *Transaction {
for _, transaction := range b.transactions { for _, transaction := range b.Txs {
if transaction.Hash() == hash { if transaction.Hash() == hash {
return transaction return transaction
} }
@ -353,52 +353,52 @@ func (b *Block) Transaction(hash common.Hash) *Transaction {
// Header returns the block header (as a copy). // Header returns the block header (as a copy).
func (b *Block) Header() *Header { func (b *Block) Header() *Header {
return CopyHeader(b.header) return CopyHeader(b.Header_)
} }
// Header value accessors. These do copy! // 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 }
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) } func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.Header_.Difficulty) }
func (b *Block) Time() uint64 { return b.header.Time } func (b *Block) Time() uint64 { return b.Header_.Time }
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() } func (b *Block) NumberU64() uint64 { return b.Header_.Number.Uint64() }
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest } func (b *Block) MixDigest() common.Hash { return b.Header_.MixDigest }
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) } func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.Header_.Nonce[:]) }
func (b *Block) Bloom() Bloom { return b.header.Bloom } func (b *Block) Bloom() Bloom { return b.Header_.Bloom }
func (b *Block) Coinbase() common.Address { return b.header.Coinbase } func (b *Block) Coinbase() common.Address { return b.Header_.Coinbase }
func (b *Block) Root() common.Hash { return b.header.Root } func (b *Block) Root() common.Hash { return b.Header_.Root }
func (b *Block) ParentHash() common.Hash { return b.header.ParentHash } func (b *Block) ParentHash() common.Hash { return b.Header_.ParentHash }
func (b *Block) TxHash() common.Hash { return b.header.TxHash } func (b *Block) TxHash() common.Hash { return b.Header_.TxHash }
func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash } func (b *Block) ReceiptHash() common.Hash { return b.Header_.ReceiptHash }
func (b *Block) UncleHash() common.Hash { return b.header.UncleHash } func (b *Block) UncleHash() common.Hash { return b.Header_.UncleHash }
func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) } func (b *Block) Extra() []byte { return common.CopyBytes(b.Header_.Extra) }
func (b *Block) BaseFee() *big.Int { func (b *Block) BaseFee() *big.Int {
if b.header.BaseFee == nil { if b.Header_.BaseFee == nil {
return nil return nil
} }
return new(big.Int).Set(b.header.BaseFee) return new(big.Int).Set(b.Header_.BaseFee)
} }
func (b *Block) BeaconRoot() *common.Hash { return b.header.ParentBeaconRoot } func (b *Block) BeaconRoot() *common.Hash { return b.Header_.ParentBeaconRoot }
func (b *Block) ExcessBlobGas() *uint64 { func (b *Block) ExcessBlobGas() *uint64 {
var excessBlobGas *uint64 var excessBlobGas *uint64
if b.header.ExcessBlobGas != nil { if b.Header_.ExcessBlobGas != nil {
excessBlobGas = new(uint64) excessBlobGas = new(uint64)
*excessBlobGas = *b.header.ExcessBlobGas *excessBlobGas = *b.Header_.ExcessBlobGas
} }
return excessBlobGas return excessBlobGas
} }
func (b *Block) BlobGasUsed() *uint64 { func (b *Block) BlobGasUsed() *uint64 {
var blobGasUsed *uint64 var blobGasUsed *uint64
if b.header.BlobGasUsed != nil { if b.Header_.BlobGasUsed != nil {
blobGasUsed = new(uint64) blobGasUsed = new(uint64)
*blobGasUsed = *b.header.BlobGasUsed *blobGasUsed = *b.Header_.BlobGasUsed
} }
return blobGasUsed return blobGasUsed
} }
@ -418,7 +418,7 @@ func (b *Block) Size() uint64 {
// 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 {
return b.header.SanityCheck() return b.Header_.SanityCheck()
} }
type writeCounter uint64 type writeCounter uint64
@ -439,15 +439,15 @@ func CalcUncleHash(uncles []*Header) common.Hash {
// header data is copied, changes to header and to the field values // header data is copied, changes to header and to the field values
// will not affect the block. // will not affect the block.
func NewBlockWithHeader(header *Header) *Block { func NewBlockWithHeader(header *Header) *Block {
return &Block{header: CopyHeader(header)} return &Block{Header_: CopyHeader(header)}
} }
// 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 {
return &Block{ return &Block{
header: CopyHeader(header), Header_: CopyHeader(header),
transactions: b.transactions, Txs: b.Txs,
uncles: b.uncles, uncles: b.uncles,
withdrawals: b.withdrawals, withdrawals: b.withdrawals,
} }
@ -456,12 +456,12 @@ func (b *Block) WithSeal(header *Header) *Block {
// WithBody returns a copy of the block with the given transaction and uncle contents. // WithBody returns a copy of the block with the given transaction and uncle contents.
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block { func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
block := &Block{ block := &Block{
header: b.header, Header_: b.Header_,
transactions: make([]*Transaction, len(transactions)), Txs: make([]*Transaction, len(transactions)),
uncles: make([]*Header, len(uncles)), uncles: make([]*Header, len(uncles)),
withdrawals: b.withdrawals, withdrawals: b.withdrawals,
} }
copy(block.transactions, transactions) copy(block.Txs, transactions)
for i := range uncles { for i := range uncles {
block.uncles[i] = CopyHeader(uncles[i]) block.uncles[i] = CopyHeader(uncles[i])
} }
@ -471,8 +471,8 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
// WithWithdrawals returns a copy of the block containing the given withdrawals. // WithWithdrawals returns a copy of the block containing the given withdrawals.
func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block { func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
block := &Block{ block := &Block{
header: b.header, Header_: b.Header_,
transactions: b.transactions, Txs: b.Txs,
uncles: b.uncles, uncles: b.uncles,
} }
if withdrawals != nil { if withdrawals != nil {
@ -488,7 +488,7 @@ func (b *Block) Hash() common.Hash {
if hash := b.hash.Load(); hash != nil { if hash := b.hash.Load(); hash != nil {
return hash.(common.Hash) return hash.(common.Hash)
} }
v := b.header.Hash() v := b.Header_.Hash()
b.hash.Store(v) b.hash.Store(v)
return v return v
} }