@@ -31,132 +31,12 @@ import (
3131 "encoding/binary"
3232 "io"
3333 "math/big"
34- "reflect"
3534 "sync/atomic"
3635
3736 "github.com/ava-labs/libevm/common"
38- "github.com/ava-labs/libevm/common/hexutil"
3937 "github.com/ava-labs/libevm/rlp"
4038)
4139
42- // A BlockNonce is a 64-bit hash which proves (combined with the
43- // mix-hash) that a sufficient amount of computation has been carried
44- // out on a block.
45- type BlockNonce [8 ]byte
46-
47- // EncodeNonce converts the given integer to a block nonce.
48- func EncodeNonce (i uint64 ) BlockNonce {
49- var n BlockNonce
50- binary .BigEndian .PutUint64 (n [:], i )
51- return n
52- }
53-
54- // Uint64 returns the integer value of a block nonce.
55- func (n BlockNonce ) Uint64 () uint64 {
56- return binary .BigEndian .Uint64 (n [:])
57- }
58-
59- // MarshalText encodes n as a hex string with 0x prefix.
60- func (n BlockNonce ) MarshalText () ([]byte , error ) {
61- return hexutil .Bytes (n [:]).MarshalText ()
62- }
63-
64- // UnmarshalText implements encoding.TextUnmarshaler.
65- func (n * BlockNonce ) UnmarshalText (input []byte ) error {
66- return hexutil .UnmarshalFixedText ("BlockNonce" , input , n [:])
67- }
68-
69- //go:generate go run github.com/fjl/gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
70- //go:generate go run github.com/ava-labs/libevm/rlp/rlpgen -type Header -out gen_header_rlp.go
71-
72- // Header represents a block header in the Ethereum blockchain.
73- type Header struct {
74- ParentHash common.Hash `json:"parentHash" gencodec:"required"`
75- UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
76- Coinbase common.Address `json:"miner" gencodec:"required"`
77- Root common.Hash `json:"stateRoot" gencodec:"required"`
78- TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
79- ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
80- Bloom Bloom `json:"logsBloom" gencodec:"required"`
81- Difficulty * big.Int `json:"difficulty" gencodec:"required"`
82- Number * big.Int `json:"number" gencodec:"required"`
83- GasLimit uint64 `json:"gasLimit" gencodec:"required"`
84- GasUsed uint64 `json:"gasUsed" gencodec:"required"`
85- Time uint64 `json:"timestamp" gencodec:"required"`
86- Extra []byte `json:"extraData" gencodec:"required"`
87- MixDigest common.Hash `json:"mixHash"`
88- Nonce BlockNonce `json:"nonce"`
89- ExtDataHash common.Hash `json:"extDataHash" gencodec:"required"`
90-
91- // BaseFee was added by EIP-1559 and is ignored in legacy headers.
92- BaseFee * big.Int `json:"baseFeePerGas" rlp:"optional"`
93-
94- // ExtDataGasUsed was added by Apricot Phase 4 and is ignored in legacy
95- // headers.
96- //
97- // It is not a uint64 like GasLimit or GasUsed because it is not possible to
98- // correctly encode this field optionally with uint64.
99- ExtDataGasUsed * big.Int `json:"extDataGasUsed" rlp:"optional"`
100-
101- // BlockGasCost was added by Apricot Phase 4 and is ignored in legacy
102- // headers.
103- BlockGasCost * big.Int `json:"blockGasCost" rlp:"optional"`
104-
105- // BlobGasUsed was added by EIP-4844 and is ignored in legacy headers.
106- BlobGasUsed * uint64 `json:"blobGasUsed" rlp:"optional"`
107-
108- // ExcessBlobGas was added by EIP-4844 and is ignored in legacy headers.
109- ExcessBlobGas * uint64 `json:"excessBlobGas" rlp:"optional"`
110-
111- // ParentBeaconRoot was added by EIP-4788 and is ignored in legacy headers.
112- ParentBeaconRoot * common.Hash `json:"parentBeaconBlockRoot" rlp:"optional"`
113- }
114-
115- // field type overrides for gencodec
116- type headerMarshaling struct {
117- Difficulty * hexutil.Big
118- Number * hexutil.Big
119- GasLimit hexutil.Uint64
120- GasUsed hexutil.Uint64
121- Time hexutil.Uint64
122- Extra hexutil.Bytes
123- BaseFee * hexutil.Big
124- ExtDataGasUsed * hexutil.Big
125- BlockGasCost * hexutil.Big
126- Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
127- BlobGasUsed * hexutil.Uint64
128- ExcessBlobGas * hexutil.Uint64
129- }
130-
131- // Hash returns the block hash of the header, which is simply the keccak256 hash of its
132- // RLP encoding.
133- func (h * Header ) Hash () common.Hash {
134- return rlpHash (h )
135- }
136-
137- var headerSize = common .StorageSize (reflect .TypeOf (Header {}).Size ())
138-
139- // Size returns the approximate memory used by all internal contents. It is used
140- // to approximate and limit the memory consumption of various caches.
141- func (h * Header ) Size () common.StorageSize {
142- var baseFeeBits int
143- if h .BaseFee != nil {
144- baseFeeBits = h .BaseFee .BitLen ()
145- }
146- return headerSize + common .StorageSize (len (h .Extra )+ (h .Difficulty .BitLen ()+ h .Number .BitLen ()+ baseFeeBits )/ 8 )
147- }
148-
149- // EmptyBody returns true if there is no additional 'body' to complete the header
150- // that is: no transactions and no uncles.
151- func (h * Header ) EmptyBody () bool {
152- return h .TxHash == EmptyTxsHash && h .UncleHash == EmptyUncleHash
153- }
154-
155- // EmptyReceipts returns true if there are no receipts for this header/block.
156- func (h * Header ) EmptyReceipts () bool {
157- return h .ReceiptHash == EmptyReceiptsHash
158- }
159-
16040// Body is a simple (mutable, non-safe) data container for storing and moving
16141// a block's data contents (transactions and uncles) together.
16242type Body struct {
@@ -249,6 +129,10 @@ func NewBlock(
249129// CopyHeader creates a deep copy of a block header.
250130func CopyHeader (h * Header ) * Header {
251131 cpy := * h
132+ extras .Header .Set (& cpy , & HeaderExtra {})
133+ cpyExtra := HeaderExtras (& cpy )
134+ * cpyExtra = * HeaderExtras (h )
135+
252136 if cpy .Difficulty = new (big.Int ); h .Difficulty != nil {
253137 cpy .Difficulty .Set (h .Difficulty )
254138 }
@@ -258,11 +142,12 @@ func CopyHeader(h *Header) *Header {
258142 if h .BaseFee != nil {
259143 cpy .BaseFee = new (big.Int ).Set (h .BaseFee )
260144 }
261- if h .ExtDataGasUsed != nil {
262- cpy .ExtDataGasUsed = new (big.Int ).Set (h .ExtDataGasUsed )
145+ hExtra := HeaderExtras (h )
146+ if hExtra .ExtDataGasUsed != nil {
147+ cpyExtra .ExtDataGasUsed = new (big.Int ).Set (hExtra .ExtDataGasUsed )
263148 }
264- if h .BlockGasCost != nil {
265- cpy .BlockGasCost = new (big.Int ).Set (h .BlockGasCost )
149+ if hExtra .BlockGasCost != nil {
150+ cpyExtra .BlockGasCost = new (big.Int ).Set (hExtra .BlockGasCost )
266151 }
267152 if len (h .Extra ) > 0 {
268153 cpy .Extra = make ([]byte , len (h .Extra ))
@@ -381,10 +266,10 @@ func (b *Block) BlobGasUsed() *uint64 {
381266}
382267
383268func (b * Block ) BlockGasCost () * big.Int {
384- if b .header .BlockGasCost == nil {
269+ if HeaderExtras ( b .header ) .BlockGasCost == nil {
385270 return nil
386271 }
387- return new (big.Int ).Set (b .header .BlockGasCost )
272+ return new (big.Int ).Set (HeaderExtras ( b .header ) .BlockGasCost )
388273}
389274
390275// Size returns the true RLP encoded storage size of the block, either by encoding
0 commit comments