Commit 24f28877 authored by Felix Lange's avatar Felix Lange

core/types: use package hexutil for JSON handling

parent 65e6319b
...@@ -29,6 +29,7 @@ import ( ...@@ -29,6 +29,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
...@@ -63,20 +64,12 @@ func (n BlockNonce) Uint64() uint64 { ...@@ -63,20 +64,12 @@ func (n BlockNonce) Uint64() uint64 {
// MarshalJSON implements json.Marshaler // MarshalJSON implements json.Marshaler
func (n BlockNonce) MarshalJSON() ([]byte, error) { func (n BlockNonce) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"0x%x"`, n)), nil return hexutil.Bytes(n[:]).MarshalJSON()
} }
// UnmarshalJSON implements json.Unmarshaler // UnmarshalJSON implements json.Unmarshaler
func (n *BlockNonce) UnmarshalJSON(input []byte) error { func (n *BlockNonce) UnmarshalJSON(input []byte) error {
var b hexBytes return hexutil.UnmarshalJSON("BlockNonce", input, n[:])
if err := b.UnmarshalJSON(input); err != nil {
return err
}
if len(b) != 8 {
return errBadNonceSize
}
copy((*n)[:], b)
return nil
} }
// Header represents a block header in the Ethereum blockchain. // Header represents a block header in the Ethereum blockchain.
...@@ -106,12 +99,12 @@ type jsonHeader struct { ...@@ -106,12 +99,12 @@ type jsonHeader struct {
TxHash *common.Hash `json:"transactionsRoot"` TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"` ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom *Bloom `json:"logsBloom"` Bloom *Bloom `json:"logsBloom"`
Difficulty *hexBig `json:"difficulty"` Difficulty *hexutil.Big `json:"difficulty"`
Number *hexBig `json:"number"` Number *hexutil.Big `json:"number"`
GasLimit *hexBig `json:"gasLimit"` GasLimit *hexutil.Big `json:"gasLimit"`
GasUsed *hexBig `json:"gasUsed"` GasUsed *hexutil.Big `json:"gasUsed"`
Time *hexBig `json:"timestamp"` Time *hexutil.Big `json:"timestamp"`
Extra *hexBytes `json:"extraData"` Extra *hexutil.Bytes `json:"extraData"`
MixDigest *common.Hash `json:"mixHash"` MixDigest *common.Hash `json:"mixHash"`
Nonce *BlockNonce `json:"nonce"` Nonce *BlockNonce `json:"nonce"`
} }
...@@ -151,12 +144,12 @@ func (h *Header) MarshalJSON() ([]byte, error) { ...@@ -151,12 +144,12 @@ func (h *Header) MarshalJSON() ([]byte, error) {
TxHash: &h.TxHash, TxHash: &h.TxHash,
ReceiptHash: &h.ReceiptHash, ReceiptHash: &h.ReceiptHash,
Bloom: &h.Bloom, Bloom: &h.Bloom,
Difficulty: (*hexBig)(h.Difficulty), Difficulty: (*hexutil.Big)(h.Difficulty),
Number: (*hexBig)(h.Number), Number: (*hexutil.Big)(h.Number),
GasLimit: (*hexBig)(h.GasLimit), GasLimit: (*hexutil.Big)(h.GasLimit),
GasUsed: (*hexBig)(h.GasUsed), GasUsed: (*hexutil.Big)(h.GasUsed),
Time: (*hexBig)(h.Time), Time: (*hexutil.Big)(h.Time),
Extra: (*hexBytes)(&h.Extra), Extra: (*hexutil.Bytes)(&h.Extra),
MixDigest: &h.MixDigest, MixDigest: &h.MixDigest,
Nonce: &h.Nonce, Nonce: &h.Nonce,
}) })
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
...@@ -77,20 +78,12 @@ func (b Bloom) TestBytes(test []byte) bool { ...@@ -77,20 +78,12 @@ func (b Bloom) TestBytes(test []byte) bool {
// MarshalJSON encodes b as a hex string with 0x prefix. // MarshalJSON encodes b as a hex string with 0x prefix.
func (b Bloom) MarshalJSON() ([]byte, error) { func (b Bloom) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%#x"`, b[:])), nil return hexutil.Bytes(b[:]).MarshalJSON()
} }
// UnmarshalJSON b as a hex string with 0x prefix. // UnmarshalJSON b as a hex string with 0x prefix.
func (b *Bloom) UnmarshalJSON(input []byte) error { func (b *Bloom) UnmarshalJSON(input []byte) error {
var dec hexBytes return hexutil.UnmarshalJSON("Bloom", input, b[:])
if err := dec.UnmarshalJSON(input); err != nil {
return err
}
if len(dec) != bloomLength {
return fmt.Errorf("invalid bloom size, want %d bytes", bloomLength)
}
copy((*b)[:], dec)
return nil
} }
func CreateBloom(receipts Receipts) Bloom { func CreateBloom(receipts Receipts) Bloom {
......
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"encoding/hex"
"fmt"
"math/big"
)
// JSON unmarshaling utilities.
type hexBytes []byte
func (b *hexBytes) MarshalJSON() ([]byte, error) {
if b != nil {
return []byte(fmt.Sprintf(`"0x%x"`, []byte(*b))), nil
}
return nil, nil
}
func (b *hexBytes) UnmarshalJSON(input []byte) error {
if len(input) < 2 || input[0] != '"' || input[len(input)-1] != '"' {
return fmt.Errorf("cannot unmarshal non-string into hexBytes")
}
input = input[1 : len(input)-1]
if len(input) < 2 || input[0] != '0' || input[1] != 'x' {
return fmt.Errorf("missing 0x prefix in hexBytes input %q", input)
}
dec := make(hexBytes, (len(input)-2)/2)
if _, err := hex.Decode(dec, input[2:]); err != nil {
return err
}
*b = dec
return nil
}
type hexBig big.Int
func (b *hexBig) MarshalJSON() ([]byte, error) {
if b != nil {
return []byte(fmt.Sprintf(`"0x%x"`, (*big.Int)(b))), nil
}
return nil, nil
}
func (b *hexBig) UnmarshalJSON(input []byte) error {
raw, err := checkHexNumber(input)
if err != nil {
return err
}
dec, ok := new(big.Int).SetString(string(raw), 16)
if !ok {
return fmt.Errorf("invalid hex number")
}
*b = (hexBig)(*dec)
return nil
}
type hexUint64 uint64
func (b *hexUint64) MarshalJSON() ([]byte, error) {
if b != nil {
return []byte(fmt.Sprintf(`"0x%x"`, *(*uint64)(b))), nil
}
return nil, nil
}
func (b *hexUint64) UnmarshalJSON(input []byte) error {
raw, err := checkHexNumber(input)
if err != nil {
return err
}
_, err = fmt.Sscanf(string(raw), "%x", b)
return err
}
func checkHexNumber(input []byte) (raw []byte, err error) {
if len(input) < 2 || input[0] != '"' || input[len(input)-1] != '"' {
return nil, fmt.Errorf("cannot unmarshal non-string into hex number")
}
input = input[1 : len(input)-1]
if len(input) < 2 || input[0] != '0' || input[1] != 'x' {
return nil, fmt.Errorf("missing 0x prefix in hex number input %q", input)
}
if len(input) == 2 {
return nil, fmt.Errorf("empty hex number")
}
raw = input[2:]
if len(raw)%2 != 0 {
raw = append([]byte{'0'}, raw...)
}
return raw, nil
}
This diff is collapsed.
...@@ -24,6 +24,7 @@ import ( ...@@ -24,6 +24,7 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
...@@ -49,12 +50,12 @@ type Receipt struct { ...@@ -49,12 +50,12 @@ type Receipt struct {
type jsonReceipt struct { type jsonReceipt struct {
PostState *common.Hash `json:"root"` PostState *common.Hash `json:"root"`
CumulativeGasUsed *hexBig `json:"cumulativeGasUsed"` CumulativeGasUsed *hexutil.Big `json:"cumulativeGasUsed"`
Bloom *Bloom `json:"logsBloom"` Bloom *Bloom `json:"logsBloom"`
Logs *vm.Logs `json:"logs"` Logs *vm.Logs `json:"logs"`
TxHash *common.Hash `json:"transactionHash"` TxHash *common.Hash `json:"transactionHash"`
ContractAddress *common.Address `json:"contractAddress"` ContractAddress *common.Address `json:"contractAddress"`
GasUsed *hexBig `json:"gasUsed"` GasUsed *hexutil.Big `json:"gasUsed"`
} }
// NewReceipt creates a barebone transaction receipt, copying the init fields. // NewReceipt creates a barebone transaction receipt, copying the init fields.
...@@ -90,12 +91,12 @@ func (r *Receipt) MarshalJSON() ([]byte, error) { ...@@ -90,12 +91,12 @@ func (r *Receipt) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonReceipt{ return json.Marshal(&jsonReceipt{
PostState: &root, PostState: &root,
CumulativeGasUsed: (*hexBig)(r.CumulativeGasUsed), CumulativeGasUsed: (*hexutil.Big)(r.CumulativeGasUsed),
Bloom: &r.Bloom, Bloom: &r.Bloom,
Logs: &r.Logs, Logs: &r.Logs,
TxHash: &r.TxHash, TxHash: &r.TxHash,
ContractAddress: &r.ContractAddress, ContractAddress: &r.ContractAddress,
GasUsed: (*hexBig)(r.GasUsed), GasUsed: (*hexutil.Big)(r.GasUsed),
}) })
} }
......
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"sync/atomic" "sync/atomic"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
...@@ -69,15 +70,15 @@ type txdata struct { ...@@ -69,15 +70,15 @@ type txdata struct {
type jsonTransaction struct { type jsonTransaction struct {
Hash *common.Hash `json:"hash"` Hash *common.Hash `json:"hash"`
AccountNonce *hexUint64 `json:"nonce"` AccountNonce *hexutil.Uint64 `json:"nonce"`
Price *hexBig `json:"gasPrice"` Price *hexutil.Big `json:"gasPrice"`
GasLimit *hexBig `json:"gas"` GasLimit *hexutil.Big `json:"gas"`
Recipient *common.Address `json:"to"` Recipient *common.Address `json:"to"`
Amount *hexBig `json:"value"` Amount *hexutil.Big `json:"value"`
Payload *hexBytes `json:"input"` Payload *hexutil.Bytes `json:"input"`
V *hexBig `json:"v"` V *hexutil.Big `json:"v"`
R *hexBig `json:"r"` R *hexutil.Big `json:"r"`
S *hexBig `json:"s"` S *hexutil.Big `json:"s"`
} }
func NewTransaction(nonce uint64, to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction { func NewTransaction(nonce uint64, to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) *Transaction {
...@@ -170,15 +171,15 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { ...@@ -170,15 +171,15 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonTransaction{ return json.Marshal(&jsonTransaction{
Hash: &hash, Hash: &hash,
AccountNonce: (*hexUint64)(&tx.data.AccountNonce), AccountNonce: (*hexutil.Uint64)(&tx.data.AccountNonce),
Price: (*hexBig)(tx.data.Price), Price: (*hexutil.Big)(tx.data.Price),
GasLimit: (*hexBig)(tx.data.GasLimit), GasLimit: (*hexutil.Big)(tx.data.GasLimit),
Recipient: tx.data.Recipient, Recipient: tx.data.Recipient,
Amount: (*hexBig)(tx.data.Amount), Amount: (*hexutil.Big)(tx.data.Amount),
Payload: (*hexBytes)(&tx.data.Payload), Payload: (*hexutil.Bytes)(&tx.data.Payload),
V: (*hexBig)(tx.data.V), V: (*hexutil.Big)(tx.data.V),
R: (*hexBig)(tx.data.R), R: (*hexutil.Big)(tx.data.R),
S: (*hexBig)(tx.data.S), S: (*hexutil.Big)(tx.data.S),
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment