@@ -19,8 +19,6 @@ package types
1919
2020import (
2121 "encoding/binary"
22- "encoding/json"
23- "errors"
2422 "fmt"
2523 "io"
2624 "math/big"
3937 EmptyUncleHash = CalcUncleHash (nil )
4038)
4139
42- var (
43- errMissingHeaderMixDigest = errors .New ("missing mixHash in JSON block header" )
44- errMissingHeaderFields = errors .New ("missing required JSON block header fields" )
45- errBadNonceSize = errors .New ("invalid block nonce size, want 8 bytes" )
46- )
47-
4840// A BlockNonce is a 64-bit hash which proves (combined with the
4941// mix-hash) that a sufficient amount of computation has been carried
5042// out on a block.
@@ -72,41 +64,35 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
7264 return hexutil .UnmarshalFixedText ("BlockNonce" , input , n [:])
7365}
7466
67+ //go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
68+
7569// Header represents a block header in the Ethereum blockchain.
7670type Header struct {
77- ParentHash common.Hash // Hash to the previous block
78- UncleHash common.Hash // Uncles of this block
79- Coinbase common.Address // The coin base address
80- Root common.Hash // Block Trie state
81- TxHash common.Hash // Tx sha
82- ReceiptHash common.Hash // Receipt sha
83- Bloom Bloom // Bloom
84- Difficulty * big.Int // Difficulty for the current block
85- Number * big.Int // The block number
86- GasLimit * big.Int // Gas limit
87- GasUsed * big.Int // Gas used
88- Time * big.Int // Creation time
89- Extra []byte // Extra data
90- MixDigest common.Hash // for quick difficulty verification
91- Nonce BlockNonce
92- }
93-
94- type jsonHeader struct {
95- ParentHash * common.Hash `json:"parentHash"`
96- UncleHash * common.Hash `json:"sha3Uncles"`
97- Coinbase * common.Address `json:"miner"`
98- Root * common.Hash `json:"stateRoot"`
99- TxHash * common.Hash `json:"transactionsRoot"`
100- ReceiptHash * common.Hash `json:"receiptsRoot"`
101- Bloom * Bloom `json:"logsBloom"`
102- Difficulty * hexutil.Big `json:"difficulty"`
103- Number * hexutil.Big `json:"number"`
104- GasLimit * hexutil.Big `json:"gasLimit"`
105- GasUsed * hexutil.Big `json:"gasUsed"`
106- Time * hexutil.Big `json:"timestamp"`
107- Extra * hexutil.Bytes `json:"extraData"`
108- MixDigest * common.Hash `json:"mixHash"`
109- Nonce * BlockNonce `json:"nonce"`
71+ ParentHash common.Hash `json:"parentHash"`
72+ UncleHash common.Hash `json:"sha3Uncles"`
73+ Coinbase common.Address `json:"miner"`
74+ Root common.Hash `json:"stateRoot"`
75+ TxHash common.Hash `json:"transactionsRoot"`
76+ ReceiptHash common.Hash `json:"receiptsRoot"`
77+ Bloom Bloom `json:"logsBloom"`
78+ Difficulty * big.Int `json:"difficulty"`
79+ Number * big.Int `json:"number"`
80+ GasLimit * big.Int `json:"gasLimit"`
81+ GasUsed * big.Int `json:"gasUsed"`
82+ Time * big.Int `json:"timestamp"`
83+ Extra []byte `json:"extraData"`
84+ MixDigest common.Hash `json:"mixHash"`
85+ Nonce BlockNonce `json:"nonce"`
86+ }
87+
88+ // field type overrides for gencodec
89+ type headerMarshaling struct {
90+ Difficulty * hexutil.Big
91+ Number * hexutil.Big
92+ GasLimit * hexutil.Big
93+ GasUsed * hexutil.Big
94+ Time * hexutil.Big
95+ Extra hexutil.Bytes
11096}
11197
11298// Hash returns the block hash of the header, which is simply the keccak256 hash of its
@@ -134,65 +120,6 @@ func (h *Header) HashNoNonce() common.Hash {
134120 })
135121}
136122
137- // MarshalJSON encodes headers into the web3 RPC response block format.
138- func (h * Header ) MarshalJSON () ([]byte , error ) {
139- return json .Marshal (& jsonHeader {
140- ParentHash : & h .ParentHash ,
141- UncleHash : & h .UncleHash ,
142- Coinbase : & h .Coinbase ,
143- Root : & h .Root ,
144- TxHash : & h .TxHash ,
145- ReceiptHash : & h .ReceiptHash ,
146- Bloom : & h .Bloom ,
147- Difficulty : (* hexutil .Big )(h .Difficulty ),
148- Number : (* hexutil .Big )(h .Number ),
149- GasLimit : (* hexutil .Big )(h .GasLimit ),
150- GasUsed : (* hexutil .Big )(h .GasUsed ),
151- Time : (* hexutil .Big )(h .Time ),
152- Extra : (* hexutil .Bytes )(& h .Extra ),
153- MixDigest : & h .MixDigest ,
154- Nonce : & h .Nonce ,
155- })
156- }
157-
158- // UnmarshalJSON decodes headers from the web3 RPC response block format.
159- func (h * Header ) UnmarshalJSON (input []byte ) error {
160- var dec jsonHeader
161- if err := json .Unmarshal (input , & dec ); err != nil {
162- return err
163- }
164- // Ensure that all fields are set. MixDigest is checked separately because
165- // it is a recent addition to the spec (as of August 2016) and older RPC server
166- // implementations might not provide it.
167- if dec .MixDigest == nil {
168- return errMissingHeaderMixDigest
169- }
170- if dec .ParentHash == nil || dec .UncleHash == nil || dec .Coinbase == nil ||
171- dec .Root == nil || dec .TxHash == nil || dec .ReceiptHash == nil ||
172- dec .Bloom == nil || dec .Difficulty == nil || dec .Number == nil ||
173- dec .GasLimit == nil || dec .GasUsed == nil || dec .Time == nil ||
174- dec .Extra == nil || dec .Nonce == nil {
175- return errMissingHeaderFields
176- }
177- // Assign all values.
178- h .ParentHash = * dec .ParentHash
179- h .UncleHash = * dec .UncleHash
180- h .Coinbase = * dec .Coinbase
181- h .Root = * dec .Root
182- h .TxHash = * dec .TxHash
183- h .ReceiptHash = * dec .ReceiptHash
184- h .Bloom = * dec .Bloom
185- h .Difficulty = (* big .Int )(dec .Difficulty )
186- h .Number = (* big .Int )(dec .Number )
187- h .GasLimit = (* big .Int )(dec .GasLimit )
188- h .GasUsed = (* big .Int )(dec .GasUsed )
189- h .Time = (* big .Int )(dec .Time )
190- h .Extra = * dec .Extra
191- h .MixDigest = * dec .MixDigest
192- h .Nonce = * dec .Nonce
193- return nil
194- }
195-
196123func rlpHash (x interface {}) (h common.Hash ) {
197124 hw := sha3 .NewKeccak256 ()
198125 rlp .Encode (hw , x )
0 commit comments