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 (
"time"
"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/rlp"
)
......@@ -63,20 +64,12 @@ func (n BlockNonce) Uint64() uint64 {
// MarshalJSON implements json.Marshaler
func (n BlockNonce) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"0x%x"`, n)), nil
return hexutil.Bytes(n[:]).MarshalJSON()
}
// UnmarshalJSON implements json.Unmarshaler
func (n *BlockNonce) UnmarshalJSON(input []byte) error {
var b hexBytes
if err := b.UnmarshalJSON(input); err != nil {
return err
}
if len(b) != 8 {
return errBadNonceSize
}
copy((*n)[:], b)
return nil
return hexutil.UnmarshalJSON("BlockNonce", input, n[:])
}
// Header represents a block header in the Ethereum blockchain.
......@@ -106,12 +99,12 @@ type jsonHeader struct {
TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom *Bloom `json:"logsBloom"`
Difficulty *hexBig `json:"difficulty"`
Number *hexBig `json:"number"`
GasLimit *hexBig `json:"gasLimit"`
GasUsed *hexBig `json:"gasUsed"`
Time *hexBig `json:"timestamp"`
Extra *hexBytes `json:"extraData"`
Difficulty *hexutil.Big `json:"difficulty"`
Number *hexutil.Big `json:"number"`
GasLimit *hexutil.Big `json:"gasLimit"`
GasUsed *hexutil.Big `json:"gasUsed"`
Time *hexutil.Big `json:"timestamp"`
Extra *hexutil.Bytes `json:"extraData"`
MixDigest *common.Hash `json:"mixHash"`
Nonce *BlockNonce `json:"nonce"`
}
......@@ -151,12 +144,12 @@ func (h *Header) MarshalJSON() ([]byte, error) {
TxHash: &h.TxHash,
ReceiptHash: &h.ReceiptHash,
Bloom: &h.Bloom,
Difficulty: (*hexBig)(h.Difficulty),
Number: (*hexBig)(h.Number),
GasLimit: (*hexBig)(h.GasLimit),
GasUsed: (*hexBig)(h.GasUsed),
Time: (*hexBig)(h.Time),
Extra: (*hexBytes)(&h.Extra),
Difficulty: (*hexutil.Big)(h.Difficulty),
Number: (*hexutil.Big)(h.Number),
GasLimit: (*hexutil.Big)(h.GasLimit),
GasUsed: (*hexutil.Big)(h.GasUsed),
Time: (*hexutil.Big)(h.Time),
Extra: (*hexutil.Bytes)(&h.Extra),
MixDigest: &h.MixDigest,
Nonce: &h.Nonce,
})
......
......@@ -21,6 +21,7 @@ import (
"math/big"
"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/crypto"
)
......@@ -77,20 +78,12 @@ func (b Bloom) TestBytes(test []byte) bool {
// MarshalJSON encodes b as a hex string with 0x prefix.
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.
func (b *Bloom) UnmarshalJSON(input []byte) error {
var dec hexBytes
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
return hexutil.UnmarshalJSON("Bloom", input, b[:])
}
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 (
"math/big"
"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/rlp"
)
......@@ -49,12 +50,12 @@ type Receipt struct {
type jsonReceipt struct {
PostState *common.Hash `json:"root"`
CumulativeGasUsed *hexBig `json:"cumulativeGasUsed"`
CumulativeGasUsed *hexutil.Big `json:"cumulativeGasUsed"`
Bloom *Bloom `json:"logsBloom"`
Logs *vm.Logs `json:"logs"`
TxHash *common.Hash `json:"transactionHash"`
ContractAddress *common.Address `json:"contractAddress"`
GasUsed *hexBig `json:"gasUsed"`
GasUsed *hexutil.Big `json:"gasUsed"`
}
// NewReceipt creates a barebone transaction receipt, copying the init fields.
......@@ -90,12 +91,12 @@ func (r *Receipt) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonReceipt{
PostState: &root,
CumulativeGasUsed: (*hexBig)(r.CumulativeGasUsed),
CumulativeGasUsed: (*hexutil.Big)(r.CumulativeGasUsed),
Bloom: &r.Bloom,
Logs: &r.Logs,
TxHash: &r.TxHash,
ContractAddress: &r.ContractAddress,
GasUsed: (*hexBig)(r.GasUsed),
GasUsed: (*hexutil.Big)(r.GasUsed),
})
}
......
......@@ -27,6 +27,7 @@ import (
"sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
......@@ -69,15 +70,15 @@ type txdata struct {
type jsonTransaction struct {
Hash *common.Hash `json:"hash"`
AccountNonce *hexUint64 `json:"nonce"`
Price *hexBig `json:"gasPrice"`
GasLimit *hexBig `json:"gas"`
AccountNonce *hexutil.Uint64 `json:"nonce"`
Price *hexutil.Big `json:"gasPrice"`
GasLimit *hexutil.Big `json:"gas"`
Recipient *common.Address `json:"to"`
Amount *hexBig `json:"value"`
Payload *hexBytes `json:"input"`
V *hexBig `json:"v"`
R *hexBig `json:"r"`
S *hexBig `json:"s"`
Amount *hexutil.Big `json:"value"`
Payload *hexutil.Bytes `json:"input"`
V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
}
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) {
return json.Marshal(&jsonTransaction{
Hash: &hash,
AccountNonce: (*hexUint64)(&tx.data.AccountNonce),
Price: (*hexBig)(tx.data.Price),
GasLimit: (*hexBig)(tx.data.GasLimit),
AccountNonce: (*hexutil.Uint64)(&tx.data.AccountNonce),
Price: (*hexutil.Big)(tx.data.Price),
GasLimit: (*hexutil.Big)(tx.data.GasLimit),
Recipient: tx.data.Recipient,
Amount: (*hexBig)(tx.data.Amount),
Payload: (*hexBytes)(&tx.data.Payload),
V: (*hexBig)(tx.data.V),
R: (*hexBig)(tx.data.R),
S: (*hexBig)(tx.data.S),
Amount: (*hexutil.Big)(tx.data.Amount),
Payload: (*hexutil.Bytes)(&tx.data.Payload),
V: (*hexutil.Big)(tx.data.V),
R: (*hexutil.Big)(tx.data.R),
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