Skip to content

Commit 24f2887

Browse files
committed
core/types: use package hexutil for JSON handling
1 parent 65e6319 commit 24f2887

File tree

6 files changed

+40
-392
lines changed

6 files changed

+40
-392
lines changed

core/types/block.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/ethereum/go-ethereum/common"
32+
"github.com/ethereum/go-ethereum/common/hexutil"
3233
"github.com/ethereum/go-ethereum/crypto/sha3"
3334
"github.com/ethereum/go-ethereum/rlp"
3435
)
@@ -63,20 +64,12 @@ func (n BlockNonce) Uint64() uint64 {
6364

6465
// MarshalJSON implements json.Marshaler
6566
func (n BlockNonce) MarshalJSON() ([]byte, error) {
66-
return []byte(fmt.Sprintf(`"0x%x"`, n)), nil
67+
return hexutil.Bytes(n[:]).MarshalJSON()
6768
}
6869

6970
// UnmarshalJSON implements json.Unmarshaler
7071
func (n *BlockNonce) UnmarshalJSON(input []byte) error {
71-
var b hexBytes
72-
if err := b.UnmarshalJSON(input); err != nil {
73-
return err
74-
}
75-
if len(b) != 8 {
76-
return errBadNonceSize
77-
}
78-
copy((*n)[:], b)
79-
return nil
72+
return hexutil.UnmarshalJSON("BlockNonce", input, n[:])
8073
}
8174

8275
// Header represents a block header in the Ethereum blockchain.
@@ -106,12 +99,12 @@ type jsonHeader struct {
10699
TxHash *common.Hash `json:"transactionsRoot"`
107100
ReceiptHash *common.Hash `json:"receiptsRoot"`
108101
Bloom *Bloom `json:"logsBloom"`
109-
Difficulty *hexBig `json:"difficulty"`
110-
Number *hexBig `json:"number"`
111-
GasLimit *hexBig `json:"gasLimit"`
112-
GasUsed *hexBig `json:"gasUsed"`
113-
Time *hexBig `json:"timestamp"`
114-
Extra *hexBytes `json:"extraData"`
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"`
115108
MixDigest *common.Hash `json:"mixHash"`
116109
Nonce *BlockNonce `json:"nonce"`
117110
}
@@ -151,12 +144,12 @@ func (h *Header) MarshalJSON() ([]byte, error) {
151144
TxHash: &h.TxHash,
152145
ReceiptHash: &h.ReceiptHash,
153146
Bloom: &h.Bloom,
154-
Difficulty: (*hexBig)(h.Difficulty),
155-
Number: (*hexBig)(h.Number),
156-
GasLimit: (*hexBig)(h.GasLimit),
157-
GasUsed: (*hexBig)(h.GasUsed),
158-
Time: (*hexBig)(h.Time),
159-
Extra: (*hexBytes)(&h.Extra),
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),
160153
MixDigest: &h.MixDigest,
161154
Nonce: &h.Nonce,
162155
})

core/types/bloom9.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/common/hexutil"
2425
"github.com/ethereum/go-ethereum/core/vm"
2526
"github.com/ethereum/go-ethereum/crypto"
2627
)
@@ -77,20 +78,12 @@ func (b Bloom) TestBytes(test []byte) bool {
7778

7879
// MarshalJSON encodes b as a hex string with 0x prefix.
7980
func (b Bloom) MarshalJSON() ([]byte, error) {
80-
return []byte(fmt.Sprintf(`"%#x"`, b[:])), nil
81+
return hexutil.Bytes(b[:]).MarshalJSON()
8182
}
8283

8384
// UnmarshalJSON b as a hex string with 0x prefix.
8485
func (b *Bloom) UnmarshalJSON(input []byte) error {
85-
var dec hexBytes
86-
if err := dec.UnmarshalJSON(input); err != nil {
87-
return err
88-
}
89-
if len(dec) != bloomLength {
90-
return fmt.Errorf("invalid bloom size, want %d bytes", bloomLength)
91-
}
92-
copy((*b)[:], dec)
93-
return nil
86+
return hexutil.UnmarshalJSON("Bloom", input, b[:])
9487
}
9588

9689
func CreateBloom(receipts Receipts) Bloom {

core/types/json.go

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)