-
Notifications
You must be signed in to change notification settings - Fork 65
/
executionpayloadheader_yaml.go
95 lines (86 loc) · 3.59 KB
/
executionpayloadheader_yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright © 2023 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deneb
import (
"bytes"
"encoding/json"
"fmt"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
)
// executionPayloadHeaderYAML is the spec representation of the struct.
type executionPayloadHeaderYAML struct {
ParentHash phase0.Hash32 `yaml:"parent_hash"`
FeeRecipient bellatrix.ExecutionAddress `yaml:"fee_recipient"`
StateRoot phase0.Root `yaml:"state_root"`
ReceiptsRoot phase0.Root `yaml:"receipts_root"`
LogsBloom string `yaml:"logs_bloom"`
PrevRandao string `yaml:"prev_randao"`
BlockNumber uint64 `yaml:"block_number"`
GasLimit uint64 `yaml:"gas_limit"`
GasUsed uint64 `yaml:"gas_used"`
Timestamp uint64 `yaml:"timestamp"`
ExtraData string `yaml:"extra_data"`
BaseFeePerGas string `yaml:"base_fee_per_gas"`
BlockHash phase0.Hash32 `yaml:"block_hash"`
TransactionsRoot phase0.Root `yaml:"transactions_root"`
WithdrawalsRoot phase0.Root `yaml:"withdrawals_root"`
BlobGasUsed uint64 `yaml:"blob_gas_used"`
ExcessBlobGas uint64 `yaml:"excess_blob_gas"`
}
// MarshalYAML implements yaml.Marshaler.
func (e *ExecutionPayloadHeader) MarshalYAML() ([]byte, error) {
extraData := "0x"
if len(e.ExtraData) > 0 {
extraData = fmt.Sprintf("%#x", e.ExtraData)
}
yamlBytes, err := yaml.MarshalWithOptions(&executionPayloadHeaderYAML{
ParentHash: e.ParentHash,
FeeRecipient: e.FeeRecipient,
StateRoot: e.StateRoot,
ReceiptsRoot: e.ReceiptsRoot,
LogsBloom: fmt.Sprintf("%#x", e.LogsBloom),
PrevRandao: fmt.Sprintf("%#x", e.PrevRandao),
BlockNumber: e.BlockNumber,
GasLimit: e.GasLimit,
GasUsed: e.GasUsed,
Timestamp: e.Timestamp,
ExtraData: extraData,
BaseFeePerGas: e.BaseFeePerGas.Dec(),
BlockHash: e.BlockHash,
TransactionsRoot: e.TransactionsRoot,
WithdrawalsRoot: e.WithdrawalsRoot,
BlobGasUsed: e.BlobGasUsed,
ExcessBlobGas: e.ExcessBlobGas,
}, yaml.Flow(true))
if err != nil {
return nil, err
}
return bytes.ReplaceAll(yamlBytes, []byte(`"`), []byte(`'`)), nil
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (e *ExecutionPayloadHeader) UnmarshalYAML(input []byte) error {
// This is very inefficient, but YAML is only used for spec tests so we do this
// rather than maintain a custom YAML unmarshaller.
var unmarshaled executionPayloadHeaderJSON
if err := yaml.Unmarshal(input, &unmarshaled); err != nil {
return errors.Wrap(err, "failed to unmarshal YAML")
}
marshaled, err := json.Marshal(unmarshaled)
if err != nil {
return errors.Wrap(err, "failed to marshal JSON")
}
return e.UnmarshalJSON(marshaled)
}