Skip to content

Commit

Permalink
Header copy hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 21, 2025
1 parent 8838ae0 commit 2e8acfd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
54 changes: 54 additions & 0 deletions core/types/header_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,60 @@ func (h *HeaderExtra) UnmarshalJSON(eth *ethtypes.Header, input []byte) error {
return nil
}

func (h *HeaderExtra) Copy(header *Header) *Header {
extraCopy := &HeaderExtra{
ExtDataHash: h.ExtDataHash,
}

if h.BlockGasCost != nil {
extraCopy.BlockGasCost = big.NewInt(0)
extraCopy.BlockGasCost.SetBytes(h.BlockGasCost.Bytes())
}

if h.ExtDataGasUsed != nil {
extraCopy.ExtDataGasUsed = big.NewInt(0)
extraCopy.ExtDataGasUsed.SetBytes(h.ExtDataGasUsed.Bytes())
}

cpy := copyBaseHeader(header)
return WithHeaderExtras(cpy, extraCopy)
}

// TODO add test to make sure this mirrors copy with a no extra header.
func copyBaseHeader(h *ethtypes.Header) *ethtypes.Header {
cpy := *h
if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
cpy.Difficulty.Set(h.Difficulty)
}
if cpy.Number = new(big.Int); h.Number != nil {
cpy.Number.Set(h.Number)
}
if h.BaseFee != nil {
cpy.BaseFee = new(big.Int).Set(h.BaseFee)
}
if len(h.Extra) > 0 {
cpy.Extra = make([]byte, len(h.Extra))
copy(cpy.Extra, h.Extra)
}
if h.WithdrawalsHash != nil {
cpy.WithdrawalsHash = new(common.Hash)
*cpy.WithdrawalsHash = *h.WithdrawalsHash
}
if h.ExcessBlobGas != nil {
cpy.ExcessBlobGas = new(uint64)
*cpy.ExcessBlobGas = *h.ExcessBlobGas
}
if h.BlobGasUsed != nil {
cpy.BlobGasUsed = new(uint64)
*cpy.BlobGasUsed = *h.BlobGasUsed
}
if h.ParentBeaconRoot != nil {
cpy.ParentBeaconRoot = new(common.Hash)
*cpy.ParentBeaconRoot = *h.ParentBeaconRoot
}
return &cpy
}

//go:generate go run github.com/fjl/gencodec -type HeaderSerializable -field-override headerMarshaling -out gen_header_json.go
//go:generate go run github.com/ava-labs/libevm/rlp/rlpgen -type HeaderSerializable -out gen_header_rlp.go

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250
replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250121093001-0a85fe10c3ef
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8 h1:qN3MOBHB//Ynhgt5Vys3iVe42Sr0EWSeN18VL3ecXzE=
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8/go.mod h1:2B7+E5neLvkOr2zursGhebjU26d4AfB7RazPxBs8hHg=
github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250 h1:qOWsbdH7Nmv2xS9H2qSfDUVhIzEwMxyzGxSoXZjmYm4=
github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
github.com/ava-labs/libevm v0.0.0-20250121093001-0a85fe10c3ef h1:hl3X0EmZl7eCRi7wHgK0Qgb6wHXkKI71f63L7NY14p4=
github.com/ava-labs/libevm v0.0.0-20250121093001-0a85fe10c3ef/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down

0 comments on commit 2e8acfd

Please sign in to comment.