Skip to content

Commit 17233f7

Browse files
committed
use header type from libevm
1 parent 1755289 commit 17233f7

27 files changed

+422
-336
lines changed

accounts/abi/bind/bind_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,11 @@ func golangBindings(t *testing.T, overload bool) {
21792179
if out, err := replacer.CombinedOutput(); err != nil {
21802180
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
21812181
}
2182+
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-require", "github.com/ava-labs/[email protected]", "-replace", "github.com/ava-labs/libevm=github.com/ava-labs/[email protected]")
2183+
replacer.Dir = pkg
2184+
if out, err := replacer.CombinedOutput(); err != nil {
2185+
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
2186+
}
21822187
tidier := exec.Command(gocmd, "mod", "tidy", "-compat=1.22")
21832188
tidier.Dir = pkg
21842189
if out, err := tidier.CombinedOutput(); err != nil {

consensus/dummy/consensus.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ func (eng *DummyEngine) verifyHeaderGasFields(config *params.ChainConfig, header
168168
}
169169

170170
// Verify BlockGasCost, ExtDataGasUsed not present before AP4
171+
headerExtra := types.HeaderExtras(header)
171172
if !configExtra.IsApricotPhase4(header.Time) {
172-
if header.BlockGasCost != nil {
173-
return fmt.Errorf("invalid blockGasCost before fork: have %d, want <nil>", header.BlockGasCost)
173+
if headerExtra.BlockGasCost != nil {
174+
return fmt.Errorf("invalid blockGasCost before fork: have %d, want <nil>", headerExtra.BlockGasCost)
174175
}
175-
if header.ExtDataGasUsed != nil {
176-
return fmt.Errorf("invalid extDataGasUsed before fork: have %d, want <nil>", header.ExtDataGasUsed)
176+
if headerExtra.ExtDataGasUsed != nil {
177+
return fmt.Errorf("invalid extDataGasUsed before fork: have %d, want <nil>", headerExtra.ExtDataGasUsed)
177178
}
178179
return nil
179180
}
@@ -188,24 +189,24 @@ func (eng *DummyEngine) verifyHeaderGasFields(config *params.ChainConfig, header
188189
ApricotPhase4MinBlockGasCost,
189190
ApricotPhase4MaxBlockGasCost,
190191
blockGasCostStep,
191-
parent.BlockGasCost,
192+
types.HeaderExtras(parent).BlockGasCost,
192193
parent.Time, header.Time,
193194
)
194-
if header.BlockGasCost == nil {
195+
if headerExtra.BlockGasCost == nil {
195196
return errBlockGasCostNil
196197
}
197-
if !header.BlockGasCost.IsUint64() {
198+
if !headerExtra.BlockGasCost.IsUint64() {
198199
return errBlockGasCostTooLarge
199200
}
200-
if header.BlockGasCost.Cmp(expectedBlockGasCost) != 0 {
201-
return fmt.Errorf("invalid block gas cost: have %d, want %d", header.BlockGasCost, expectedBlockGasCost)
201+
if headerExtra.BlockGasCost.Cmp(expectedBlockGasCost) != 0 {
202+
return fmt.Errorf("invalid block gas cost: have %d, want %d", headerExtra.BlockGasCost, expectedBlockGasCost)
202203
}
203204
// ExtDataGasUsed correctness is checked during block validation
204205
// (when the validator has access to the block contents)
205-
if header.ExtDataGasUsed == nil {
206+
if headerExtra.ExtDataGasUsed == nil {
206207
return errExtDataGasUsedNil
207208
}
208-
if !header.ExtDataGasUsed.IsUint64() {
209+
if !headerExtra.ExtDataGasUsed.IsUint64() {
209210
return errExtDataGasUsedTooLarge
210211
}
211212
return nil
@@ -417,7 +418,7 @@ func (eng *DummyEngine) Finalize(chain consensus.ChainHeaderReader, block *types
417418
ApricotPhase4MinBlockGasCost,
418419
ApricotPhase4MaxBlockGasCost,
419420
blockGasCostStep,
420-
parent.BlockGasCost,
421+
types.HeaderExtras(parent).BlockGasCost,
421422
parent.Time, block.Time(),
422423
)
423424
// Verify the BlockGasCost set in the header matches the calculated value.
@@ -454,28 +455,29 @@ func (eng *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, h
454455
}
455456
}
456457
chainConfigExtra := params.GetExtra(chain.Config())
458+
headerExtra := types.HeaderExtras(header)
457459
if chainConfigExtra.IsApricotPhase4(header.Time) {
458-
header.ExtDataGasUsed = extDataGasUsed
459-
if header.ExtDataGasUsed == nil {
460-
header.ExtDataGasUsed = new(big.Int).Set(common.Big0)
460+
headerExtra.ExtDataGasUsed = extDataGasUsed
461+
if headerExtra.ExtDataGasUsed == nil {
462+
headerExtra.ExtDataGasUsed = new(big.Int).Set(common.Big0)
461463
}
462464
blockGasCostStep := ApricotPhase4BlockGasCostStep
463465
if chainConfigExtra.IsApricotPhase5(header.Time) {
464466
blockGasCostStep = ApricotPhase5BlockGasCostStep
465467
}
466468
// Calculate the required block gas cost for this block.
467-
header.BlockGasCost = calcBlockGasCost(
469+
headerExtra.BlockGasCost = calcBlockGasCost(
468470
ApricotPhase4TargetBlockRate,
469471
ApricotPhase4MinBlockGasCost,
470472
ApricotPhase4MaxBlockGasCost,
471473
blockGasCostStep,
472-
parent.BlockGasCost,
474+
types.HeaderExtras(parent).BlockGasCost,
473475
parent.Time, header.Time,
474476
)
475477
// Verify that this block covers the block fee.
476478
if err := eng.verifyBlockFee(
477479
header.BaseFee,
478-
header.BlockGasCost,
480+
headerExtra.BlockGasCost,
479481
txs,
480482
receipts,
481483
contribution,

consensus/dummy/dynamic_fees.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header, timestamp uin
9090
// gas in.
9191
if roll < rollupWindow {
9292
var blockGasCost, parentExtraStateGasUsed uint64
93+
parentExtra := types.HeaderExtras(parent)
9394
switch {
9495
case isApricotPhase5:
9596
// [blockGasCost] has been removed in AP5, so it is left as 0.
9697

9798
// At the start of a new network, the parent
9899
// may not have a populated [ExtDataGasUsed].
99-
if parent.ExtDataGasUsed != nil {
100-
parentExtraStateGasUsed = parent.ExtDataGasUsed.Uint64()
100+
if parentExtra.ExtDataGasUsed != nil {
101+
parentExtraStateGasUsed = parentExtra.ExtDataGasUsed.Uint64()
101102
}
102103
case isApricotPhase4:
103104
// The [blockGasCost] is paid by the effective tips in the block using
@@ -107,14 +108,14 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header, timestamp uin
107108
ApricotPhase4MinBlockGasCost,
108109
ApricotPhase4MaxBlockGasCost,
109110
ApricotPhase4BlockGasCostStep,
110-
parent.BlockGasCost,
111+
parentExtra.BlockGasCost,
111112
parent.Time, timestamp,
112113
).Uint64()
113114

114115
// On the boundary of AP3 and AP4 or at the start of a new network, the parent
115116
// may not have a populated [ExtDataGasUsed].
116-
if parent.ExtDataGasUsed != nil {
117-
parentExtraStateGasUsed = parent.ExtDataGasUsed.Uint64()
117+
if parentExtra.ExtDataGasUsed != nil {
118+
parentExtraStateGasUsed = parentExtra.ExtDataGasUsed.Uint64()
118119
}
119120
default:
120121
blockGasCost = ApricotPhase3BlockGasFee
@@ -339,21 +340,22 @@ func MinRequiredTip(config *params.ChainConfig, header *types.Header) (*big.Int,
339340
if header.BaseFee == nil {
340341
return nil, errBaseFeeNil
341342
}
342-
if header.BlockGasCost == nil {
343+
headerExtra := types.HeaderExtras(header)
344+
if headerExtra.BlockGasCost == nil {
343345
return nil, errBlockGasCostNil
344346
}
345-
if header.ExtDataGasUsed == nil {
347+
if headerExtra.ExtDataGasUsed == nil {
346348
return nil, errExtDataGasUsedNil
347349
}
348350

349351
// minTip = requiredBlockFee/blockGasUsage
350352
requiredBlockFee := new(big.Int).Mul(
351-
header.BlockGasCost,
353+
headerExtra.BlockGasCost,
352354
header.BaseFee,
353355
)
354356
blockGasUsage := new(big.Int).Add(
355357
new(big.Int).SetUint64(header.GasUsed),
356-
header.ExtDataGasUsed,
358+
headerExtra.ExtDataGasUsed,
357359
)
358360
return new(big.Int).Div(requiredBlockFee, blockGasUsage), nil
359361
}

consensus/dummy/dynamic_fees_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,15 @@ func TestCalcBaseFeeAP4(t *testing.T) {
425425
nextExtraData, nextBaseFee, err = CalcBaseFee(params.TestApricotPhase4Config, extDataHeader, block.timestamp)
426426
assert.NoError(t, err)
427427
log.Info("Update", "baseFee (w/extData)", nextBaseFee)
428-
extDataHeader = &types.Header{
429-
Time: block.timestamp,
430-
GasUsed: block.gasUsed,
431-
Number: big.NewInt(int64(index) + 1),
432-
BaseFee: nextBaseFee,
433-
Extra: nextExtraData,
428+
extDataHeader = types.WithHeaderExtras(&types.Header{
429+
Time: block.timestamp,
430+
GasUsed: block.gasUsed,
431+
Number: big.NewInt(int64(index) + 1),
432+
BaseFee: nextBaseFee,
433+
Extra: nextExtraData,
434+
}, &types.HeaderExtra{
434435
ExtDataGasUsed: block.extDataGasUsed,
435-
}
436+
})
436437

437438
assert.Equal(t, event.extDataFeeGreater, extDataHeader.BaseFee.Cmp(header.BaseFee) == 1, "unexpected cmp for index %d", index)
438439
}

core/state_processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
376376
header.Extra, header.BaseFee, _ = dummy.CalcBaseFee(config, parent.Header(), header.Time)
377377
}
378378
if params.GetExtra(config).IsApricotPhase4(header.Time) {
379-
header.BlockGasCost = big.NewInt(0)
380-
header.ExtDataGasUsed = big.NewInt(0)
379+
types.HeaderExtras(header).BlockGasCost = big.NewInt(0)
380+
types.HeaderExtras(header).ExtDataGasUsed = big.NewInt(0)
381381
}
382382
var receipts []*types.Receipt
383383
// The post-state result doesn't need to be correct (this is a bad block), but we do need something there

core/types/block.go

Lines changed: 11 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
16242
type Body struct {
@@ -249,6 +129,10 @@ func NewBlock(
249129
// CopyHeader creates a deep copy of a block header.
250130
func 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

383268
func (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

core/types/block_ext.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (b *Block) setExtData(data []byte, recalc bool) {
2828
b.extdata = &_data
2929
copy(*b.extdata, data)
3030
if recalc {
31-
b.header.ExtDataHash = CalcExtDataHash(*b.extdata)
31+
HeaderExtras(b.header).ExtDataHash = CalcExtDataHash(*b.extdata)
3232
}
3333
}
3434

@@ -44,10 +44,10 @@ func (b *Block) Version() uint32 {
4444
}
4545

4646
func (b *Block) ExtDataGasUsed() *big.Int {
47-
if b.header.ExtDataGasUsed == nil {
47+
if HeaderExtras(b.header).ExtDataGasUsed == nil {
4848
return nil
4949
}
50-
return new(big.Int).Set(b.header.ExtDataGasUsed)
50+
return new(big.Int).Set(HeaderExtras(b.header).ExtDataGasUsed)
5151
}
5252

5353
func CalcExtDataHash(extdata []byte) common.Hash {

0 commit comments

Comments
 (0)