@@ -31,132 +31,12 @@ import (
31
31
"encoding/binary"
32
32
"io"
33
33
"math/big"
34
- "reflect"
35
34
"sync/atomic"
36
35
37
36
"github.com/ava-labs/libevm/common"
38
- "github.com/ava-labs/libevm/common/hexutil"
39
37
"github.com/ava-labs/libevm/rlp"
40
38
)
41
39
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
-
160
40
// Body is a simple (mutable, non-safe) data container for storing and moving
161
41
// a block's data contents (transactions and uncles) together.
162
42
type Body struct {
@@ -249,6 +129,12 @@ func NewBlock(
249
129
// CopyHeader creates a deep copy of a block header.
250
130
func CopyHeader (h * Header ) * Header {
251
131
cpy := * h
132
+ hExtra := GetHeaderExtra (h )
133
+ cpyExtra := & HeaderExtra {
134
+ ExtDataHash : hExtra .ExtDataHash ,
135
+ }
136
+ SetHeaderExtra (& cpy , cpyExtra )
137
+
252
138
if cpy .Difficulty = new (big.Int ); h .Difficulty != nil {
253
139
cpy .Difficulty .Set (h .Difficulty )
254
140
}
@@ -258,16 +144,20 @@ func CopyHeader(h *Header) *Header {
258
144
if h .BaseFee != nil {
259
145
cpy .BaseFee = new (big.Int ).Set (h .BaseFee )
260
146
}
261
- if h .ExtDataGasUsed != nil {
262
- cpy .ExtDataGasUsed = new (big.Int ).Set (h .ExtDataGasUsed )
147
+ if hExtra .ExtDataGasUsed != nil {
148
+ cpyExtra .ExtDataGasUsed = new (big.Int ).Set (hExtra .ExtDataGasUsed )
263
149
}
264
- if h .BlockGasCost != nil {
265
- cpy .BlockGasCost = new (big.Int ).Set (h .BlockGasCost )
150
+ if hExtra .BlockGasCost != nil {
151
+ cpyExtra .BlockGasCost = new (big.Int ).Set (hExtra .BlockGasCost )
266
152
}
267
153
if len (h .Extra ) > 0 {
268
154
cpy .Extra = make ([]byte , len (h .Extra ))
269
155
copy (cpy .Extra , h .Extra )
270
156
}
157
+ if h .WithdrawalsHash != nil {
158
+ cpy .WithdrawalsHash = new (common.Hash )
159
+ * cpy .WithdrawalsHash = * h .WithdrawalsHash
160
+ }
271
161
if h .ExcessBlobGas != nil {
272
162
cpy .ExcessBlobGas = new (uint64 )
273
163
* cpy .ExcessBlobGas = * h .ExcessBlobGas
@@ -380,10 +270,11 @@ func (b *Block) BlobGasUsed() *uint64 {
380
270
}
381
271
382
272
func (b * Block ) BlockGasCost () * big.Int {
383
- if b .header .BlockGasCost == nil {
273
+ cost := GetHeaderExtra (b .header ).BlockGasCost
274
+ if cost == nil {
384
275
return nil
385
276
}
386
- return new (big.Int ).Set (b . header . BlockGasCost )
277
+ return new (big.Int ).Set (cost )
387
278
}
388
279
389
280
// Size returns the true RLP encoded storage size of the block, either by encoding
0 commit comments