@ -23,6 +23,7 @@ import (
"io"
"io"
"math/big"
"math/big"
"reflect"
"reflect"
"slices"
"sync/atomic"
"sync/atomic"
"time"
"time"
@ -217,13 +218,19 @@ type extblock struct {
// NewBlock creates a new block. The input data is copied, changes to header and to the
// NewBlock creates a new block. The input data is copied, changes to header and to the
// field values will not affect the block.
// field values will not affect the block.
//
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
// The body elements and the receipts are used to recompute and overwrite the
// are ignored and set to values derived from the given txs, uncles
// relevant portions of the header.
// and receipts.
func NewBlock ( header * Header , body * Body , receipts [ ] * Receipt , hasher TrieHasher ) * Block {
func NewBlock ( header * Header , txs [ ] * Transaction , uncles [ ] * Header , receipts [ ] * Receipt , hasher TrieHasher ) * Block {
if body == nil {
b := & Block { header : CopyHeader ( header ) }
body = & Body { }
}
var (
b = NewBlockWithHeader ( header )
txs = body . Transactions
uncles = body . Uncles
withdrawals = body . Withdrawals
)
// 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 {
@ -249,27 +256,18 @@ func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*
}
}
}
}
return b
}
// NewBlockWithWithdrawals creates a new block with withdrawals. The input data is copied,
// changes to header and to the field values will not affect the block.
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header are ignored and set to
// values derived from the given txs, uncles and receipts.
func NewBlockWithWithdrawals ( header * Header , txs [ ] * Transaction , uncles [ ] * Header , receipts [ ] * Receipt , withdrawals [ ] * Withdrawal , hasher TrieHasher ) * Block {
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
b . withdrawals = Withdrawals { }
} else {
} else {
h := DeriveSha ( Withdrawals ( withdrawals ) , hasher )
hash := DeriveSha ( Withdrawals ( withdrawals ) , hasher )
b . header . WithdrawalsHash = & h
b . header . WithdrawalsHash = & hash
b . withdrawals = slices . Clone ( withdrawals )
}
}
return b . WithWithdrawals ( withdrawals )
return b
}
}
// CopyHeader creates a deep copy of a block header.
// CopyHeader creates a deep copy of a block header.
@ -453,31 +451,17 @@ func (b *Block) WithSeal(header *Header) *Block {
}
}
}
}
// WithBody returns a copy of the block with the given transaction and uncle contents.
// WithBody returns a new block with the original header and a deep copy of the
func ( b * Block ) WithBody ( transactions [ ] * Transaction , uncles [ ] * Header ) * Block {
// provided body.
block := & Block {
func ( b * Block ) WithBody ( body Body ) * Block {
header : b . header ,
transactions : make ( [ ] * Transaction , len ( transactions ) ) ,
uncles : make ( [ ] * Header , len ( uncles ) ) ,
withdrawals : b . withdrawals ,
}
copy ( block . transactions , transactions )
for i := range uncles {
block . uncles [ i ] = CopyHeader ( uncles [ i ] )
}
return block
}
// WithWithdrawals returns a copy of the block containing the given withdrawals.
func ( b * Block ) WithWithdrawals ( withdrawals [ ] * Withdrawal ) * Block {
block := & Block {
block := & Block {
header : b . header ,
header : b . header ,
transactions : b . transactions ,
transactions : slices . Clone ( body . Transactions ) ,
uncles : b . uncles ,
uncles : make ( [ ] * Header , len ( body . Uncles ) ) ,
withdrawals : slices . Clone ( body . Withdrawals ) ,
}
}
if withdrawals != nil {
for i := range body . Uncles {
block . withdrawals = make ( [ ] * Withdrawal , len ( withdrawals ) )
block . uncles [ i ] = CopyHeader ( body . Uncles [ i ] )
copy ( block . withdrawals , withdrawals )
}
}
return block
return block
}
}