Skip to content

Commit 8cf08e4

Browse files
committed
core/types: use gencodec for JSON marshaling code
1 parent eee96a5 commit 8cf08e4

9 files changed

Lines changed: 510 additions & 312 deletions

File tree

‎core/types/block.go‎

Lines changed: 27 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package types
1919

2020
import (
2121
"encoding/binary"
22-
"encoding/json"
23-
"errors"
2422
"fmt"
2523
"io"
2624
"math/big"
@@ -39,12 +37,6 @@ var (
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.
7670
type 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-
196123
func rlpHash(x interface{}) (h common.Hash) {
197124
hw := sha3.NewKeccak256()
198125
rlp.Encode(hw, x)

‎core/types/gen_header_json.go‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// generated by github.com/fjl/gencodec, do not edit.
2+
3+
package types
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
"math/big"
9+
10+
"github.com/ethereum/go-ethereum/common"
11+
"github.com/ethereum/go-ethereum/common/hexutil"
12+
)
13+
14+
func (h *Header) MarshalJSON() ([]byte, error) {
15+
type HeaderJSON struct {
16+
ParentHash *common.Hash `json:"parentHash"`
17+
UncleHash *common.Hash `json:"sha3Uncles"`
18+
Coinbase *common.Address `json:"miner"`
19+
Root *common.Hash `json:"stateRoot"`
20+
TxHash *common.Hash `json:"transactionsRoot"`
21+
ReceiptHash *common.Hash `json:"receiptsRoot"`
22+
Bloom *Bloom `json:"logsBloom"`
23+
Difficulty *hexutil.Big `json:"difficulty"`
24+
Number *hexutil.Big `json:"number"`
25+
GasLimit *hexutil.Big `json:"gasLimit"`
26+
GasUsed *hexutil.Big `json:"gasUsed"`
27+
Time *hexutil.Big `json:"timestamp"`
28+
Extra hexutil.Bytes `json:"extraData"`
29+
MixDigest *common.Hash `json:"mixHash"`
30+
Nonce *BlockNonce `json:"nonce"`
31+
}
32+
var enc HeaderJSON
33+
enc.ParentHash = &h.ParentHash
34+
enc.UncleHash = &h.UncleHash
35+
enc.Coinbase = &h.Coinbase
36+
enc.Root = &h.Root
37+
enc.TxHash = &h.TxHash
38+
enc.ReceiptHash = &h.ReceiptHash
39+
enc.Bloom = &h.Bloom
40+
enc.Difficulty = (*hexutil.Big)(h.Difficulty)
41+
enc.Number = (*hexutil.Big)(h.Number)
42+
enc.GasLimit = (*hexutil.Big)(h.GasLimit)
43+
enc.GasUsed = (*hexutil.Big)(h.GasUsed)
44+
enc.Time = (*hexutil.Big)(h.Time)
45+
enc.Extra = h.Extra
46+
enc.MixDigest = &h.MixDigest
47+
enc.Nonce = &h.Nonce
48+
return json.Marshal(&enc)
49+
}
50+
51+
func (h *Header) UnmarshalJSON(input []byte) error {
52+
type HeaderJSON struct {
53+
ParentHash *common.Hash `json:"parentHash"`
54+
UncleHash *common.Hash `json:"sha3Uncles"`
55+
Coinbase *common.Address `json:"miner"`
56+
Root *common.Hash `json:"stateRoot"`
57+
TxHash *common.Hash `json:"transactionsRoot"`
58+
ReceiptHash *common.Hash `json:"receiptsRoot"`
59+
Bloom *Bloom `json:"logsBloom"`
60+
Difficulty *hexutil.Big `json:"difficulty"`
61+
Number *hexutil.Big `json:"number"`
62+
GasLimit *hexutil.Big `json:"gasLimit"`
63+
GasUsed *hexutil.Big `json:"gasUsed"`
64+
Time *hexutil.Big `json:"timestamp"`
65+
Extra hexutil.Bytes `json:"extraData"`
66+
MixDigest *common.Hash `json:"mixHash"`
67+
Nonce *BlockNonce `json:"nonce"`
68+
}
69+
var dec HeaderJSON
70+
if err := json.Unmarshal(input, &dec); err != nil {
71+
return err
72+
}
73+
var x Header
74+
if dec.ParentHash == nil {
75+
return errors.New("missing required field 'parentHash' for Header")
76+
}
77+
x.ParentHash = *dec.ParentHash
78+
if dec.UncleHash == nil {
79+
return errors.New("missing required field 'sha3Uncles' for Header")
80+
}
81+
x.UncleHash = *dec.UncleHash
82+
if dec.Coinbase == nil {
83+
return errors.New("missing required field 'miner' for Header")
84+
}
85+
x.Coinbase = *dec.Coinbase
86+
if dec.Root == nil {
87+
return errors.New("missing required field 'stateRoot' for Header")
88+
}
89+
x.Root = *dec.Root
90+
if dec.TxHash == nil {
91+
return errors.New("missing required field 'transactionsRoot' for Header")
92+
}
93+
x.TxHash = *dec.TxHash
94+
if dec.ReceiptHash == nil {
95+
return errors.New("missing required field 'receiptsRoot' for Header")
96+
}
97+
x.ReceiptHash = *dec.ReceiptHash
98+
if dec.Bloom == nil {
99+
return errors.New("missing required field 'logsBloom' for Header")
100+
}
101+
x.Bloom = *dec.Bloom
102+
if dec.Difficulty == nil {
103+
return errors.New("missing required field 'difficulty' for Header")
104+
}
105+
x.Difficulty = (*big.Int)(dec.Difficulty)
106+
if dec.Number == nil {
107+
return errors.New("missing required field 'number' for Header")
108+
}
109+
x.Number = (*big.Int)(dec.Number)
110+
if dec.GasLimit == nil {
111+
return errors.New("missing required field 'gasLimit' for Header")
112+
}
113+
x.GasLimit = (*big.Int)(dec.GasLimit)
114+
if dec.GasUsed == nil {
115+
return errors.New("missing required field 'gasUsed' for Header")
116+
}
117+
x.GasUsed = (*big.Int)(dec.GasUsed)
118+
if dec.Time == nil {
119+
return errors.New("missing required field 'timestamp' for Header")
120+
}
121+
x.Time = (*big.Int)(dec.Time)
122+
if dec.Extra == nil {
123+
return errors.New("missing required field 'extraData' for Header")
124+
}
125+
x.Extra = dec.Extra
126+
if dec.MixDigest == nil {
127+
return errors.New("missing required field 'mixHash' for Header")
128+
}
129+
x.MixDigest = *dec.MixDigest
130+
if dec.Nonce == nil {
131+
return errors.New("missing required field 'nonce' for Header")
132+
}
133+
x.Nonce = *dec.Nonce
134+
*h = x
135+
return nil
136+
}

‎core/types/gen_log_json.go‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// generated by github.com/fjl/gencodec, do not edit.
2+
3+
package types
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
9+
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/common/hexutil"
11+
)
12+
13+
func (l *Log) MarshalJSON() ([]byte, error) {
14+
type LogJSON struct {
15+
Address *common.Address `json:"address"`
16+
Topics []common.Hash `json:"topics"`
17+
Data hexutil.Bytes `json:"data"`
18+
BlockNumber *hexutil.Uint64 `json:"blockNumber" optional:"yes"`
19+
TxHash *common.Hash `json:"transactionHash"`
20+
TxIndex *hexutil.Uint `json:"transactionIndex"`
21+
BlockHash *common.Hash `json:"blockHash" optional:"yes"`
22+
Index *hexutil.Uint `json:"logIndex"`
23+
Removed *bool `json:"removed" optional:"yes"`
24+
}
25+
var enc LogJSON
26+
enc.Address = &l.Address
27+
enc.Topics = l.Topics
28+
enc.Data = l.Data
29+
enc.BlockNumber = (*hexutil.Uint64)(&l.BlockNumber)
30+
enc.TxHash = &l.TxHash
31+
enc.TxIndex = (*hexutil.Uint)(&l.TxIndex)
32+
enc.BlockHash = &l.BlockHash
33+
enc.Index = (*hexutil.Uint)(&l.Index)
34+
enc.Removed = &l.Removed
35+
return json.Marshal(&enc)
36+
}
37+
38+
func (l *Log) UnmarshalJSON(input []byte) error {
39+
type LogJSON struct {
40+
Address *common.Address `json:"address"`
41+
Topics []common.Hash `json:"topics"`
42+
Data hexutil.Bytes `json:"data"`
43+
BlockNumber *hexutil.Uint64 `json:"blockNumber" optional:"yes"`
44+
TxHash *common.Hash `json:"transactionHash"`
45+
TxIndex *hexutil.Uint `json:"transactionIndex"`
46+
BlockHash *common.Hash `json:"blockHash" optional:"yes"`
47+
Index *hexutil.Uint `json:"logIndex"`
48+
Removed *bool `json:"removed" optional:"yes"`
49+
}
50+
var dec LogJSON
51+
if err := json.Unmarshal(input, &dec); err != nil {
52+
return err
53+
}
54+
var x Log
55+
if dec.Address == nil {
56+
return errors.New("missing required field 'address' for Log")
57+
}
58+
x.Address = *dec.Address
59+
if dec.Topics == nil {
60+
return errors.New("missing required field 'topics' for Log")
61+
}
62+
x.Topics = dec.Topics
63+
if dec.Data == nil {
64+
return errors.New("missing required field 'data' for Log")
65+
}
66+
x.Data = dec.Data
67+
if dec.BlockNumber != nil {
68+
x.BlockNumber = uint64(*dec.BlockNumber)
69+
}
70+
if dec.TxHash == nil {
71+
return errors.New("missing required field 'transactionHash' for Log")
72+
}
73+
x.TxHash = *dec.TxHash
74+
if dec.TxIndex == nil {
75+
return errors.New("missing required field 'transactionIndex' for Log")
76+
}
77+
x.TxIndex = uint(*dec.TxIndex)
78+
if dec.BlockHash != nil {
79+
x.BlockHash = *dec.BlockHash
80+
}
81+
if dec.Index == nil {
82+
return errors.New("missing required field 'logIndex' for Log")
83+
}
84+
x.Index = uint(*dec.Index)
85+
if dec.Removed != nil {
86+
x.Removed = *dec.Removed
87+
}
88+
*l = x
89+
return nil
90+
}

0 commit comments

Comments
 (0)