Skip to content

Commit

Permalink
rpc: remove HexBytes, replace all uses with hexutil.Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Dec 20, 2016
1 parent a3e3235 commit adab2e1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 66 deletions.
18 changes: 9 additions & 9 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx
"miner": head.Coinbase,
"difficulty": rpc.NewHexNumber(head.Difficulty),
"totalDifficulty": rpc.NewHexNumber(s.b.GetTd(b.Hash())),
"extraData": rpc.HexBytes(head.Extra),
"extraData": hexutil.Bytes(head.Extra),
"size": rpc.NewHexNumber(b.Size().Int64()),
"gasLimit": rpc.NewHexNumber(head.GasLimit),
"gasUsed": rpc.NewHexNumber(head.GasUsed),
Expand Down Expand Up @@ -682,7 +682,7 @@ type RPCTransaction struct {
Gas *rpc.HexNumber `json:"gas"`
GasPrice *rpc.HexNumber `json:"gasPrice"`
Hash common.Hash `json:"hash"`
Input rpc.HexBytes `json:"input"`
Input hexutil.Bytes `json:"input"`
Nonce *rpc.HexNumber `json:"nonce"`
To *common.Address `json:"to"`
TransactionIndex *rpc.HexNumber `json:"transactionIndex"`
Expand All @@ -705,7 +705,7 @@ func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction {
Gas: rpc.NewHexNumber(tx.Gas()),
GasPrice: rpc.NewHexNumber(tx.GasPrice()),
Hash: tx.Hash(),
Input: rpc.HexBytes(tx.Data()),
Input: hexutil.Bytes(tx.Data()),
Nonce: rpc.NewHexNumber(tx.Nonce()),
To: tx.To(),
Value: rpc.NewHexNumber(tx.Value()),
Expand All @@ -732,7 +732,7 @@ func newRPCTransactionFromBlockIndex(b *types.Block, txIndex int) (*RPCTransacti
Gas: rpc.NewHexNumber(tx.Gas()),
GasPrice: rpc.NewHexNumber(tx.GasPrice()),
Hash: tx.Hash(),
Input: rpc.HexBytes(tx.Data()),
Input: hexutil.Bytes(tx.Data()),
Nonce: rpc.NewHexNumber(tx.Nonce()),
To: tx.To(),
TransactionIndex: rpc.NewHexNumber(txIndex),
Expand All @@ -747,7 +747,7 @@ func newRPCTransactionFromBlockIndex(b *types.Block, txIndex int) (*RPCTransacti
}

// newRPCRawTransactionFromBlockIndex returns the bytes of a transaction given a block and a transaction index.
func newRPCRawTransactionFromBlockIndex(b *types.Block, txIndex int) (rpc.HexBytes, error) {
func newRPCRawTransactionFromBlockIndex(b *types.Block, txIndex int) (hexutil.Bytes, error) {
if txIndex >= 0 && txIndex < len(b.Transactions()) {
tx := b.Transactions()[txIndex]
return rlp.EncodeToBytes(tx)
Expand Down Expand Up @@ -828,15 +828,15 @@ func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context
}

// GetRawTransactionByBlockNumberAndIndex returns the bytes of the transaction for the given block number and index.
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index rpc.HexNumber) (rpc.HexBytes, error) {
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index rpc.HexNumber) (hexutil.Bytes, error) {
if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil {
return newRPCRawTransactionFromBlockIndex(block, index.Int())
}
return nil, nil
}

// GetRawTransactionByBlockHashAndIndex returns the bytes of the transaction for the given block hash and index.
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index rpc.HexNumber) (rpc.HexBytes, error) {
func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index rpc.HexNumber) (hexutil.Bytes, error) {
if block, _ := s.b.GetBlock(ctx, blockHash); block != nil {
return newRPCRawTransactionFromBlockIndex(block, index.Int())
}
Expand Down Expand Up @@ -909,7 +909,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, txH
}

// GetRawTransactionByHash returns the bytes of the transaction for the given hash.
func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, txHash common.Hash) (rpc.HexBytes, error) {
func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, txHash common.Hash) (hexutil.Bytes, error) {
var tx *types.Transaction
var err error

Expand Down Expand Up @@ -950,7 +950,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (ma
from, _ := types.Sender(signer, tx)

fields := map[string]interface{}{
"root": rpc.HexBytes(receipt.PostState),
"root": hexutil.Bytes(receipt.PostState),
"blockHash": txBlock,
"blockNumber": rpc.NewHexNumber(blockIndex),
"transactionHash": txHash,
Expand Down
30 changes: 0 additions & 30 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package rpc

import (
"bytes"
"encoding/hex"
"fmt"
"math"
"math/big"
Expand Down Expand Up @@ -274,31 +272,3 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}

// HexBytes JSON-encodes as hex with 0x prefix.
type HexBytes []byte

func (b HexBytes) MarshalJSON() ([]byte, error) {
result := make([]byte, len(b)*2+4)
copy(result, `"0x`)
hex.Encode(result[3:], b)
result[len(result)-1] = '"'
return result, nil
}

func (b *HexBytes) UnmarshalJSON(input []byte) error {
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}
if !bytes.HasPrefix(input, []byte("0x")) {
return fmt.Errorf("missing 0x prefix for hex byte array")
}
input = input[2:]
if len(input) == 0 {
*b = nil
return nil
}
*b = make([]byte, len(input)/2)
_, err := hex.Decode(*b, input)
return err
}
22 changes: 0 additions & 22 deletions rpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,3 @@ func TestHexNumberMarshalJSON(t *testing.T) {
t.Fatalf("Invalid json.Marshal, expected '%s', got '%s'", exp, got)
}
}

var hexBytesTests = []struct{ in, out []byte }{
{in: []byte(`"0x"`), out: []byte{}},
{in: []byte(`"0x00"`), out: []byte{0}},
{in: []byte(`"0x01ff"`), out: []byte{0x01, 0xFF}},
}

func TestHexBytes(t *testing.T) {
for i, test := range hexBytesTests {
var dec HexBytes
if err := json.Unmarshal(test.in, &dec); err != nil {
t.Fatalf("test %d: can't decode: %v", i, err)
}
enc, _ := json.Marshal(HexBytes(test.out))
if !bytes.Equal(dec, test.out) {
t.Errorf("test %d: wrong decoded value 0x%x", i, dec)
}
if !bytes.Equal(enc, test.in) {
t.Errorf("test %d: wrong encoded value %#q", i, enc)
}
}
}
11 changes: 6 additions & 5 deletions whisper/shhapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
mathrand "math/rand"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (api *PublicWhisperAPI) Version() (*rpc.HexNumber, error) {

// MarkPeerTrusted marks specific peer trusted, which will allow it
// to send historic (expired) messages.
func (api *PublicWhisperAPI) MarkPeerTrusted(peerID rpc.HexBytes) error {
func (api *PublicWhisperAPI) MarkPeerTrusted(peerID hexutil.Bytes) error {
if api.whisper == nil {
return whisperOffLineErr
}
Expand All @@ -92,7 +93,7 @@ func (api *PublicWhisperAPI) MarkPeerTrusted(peerID rpc.HexBytes) error {
// data contains parameters (time frame, payment details, etc.), required
// by the remote email-like server. Whisper is not aware about the data format,
// it will just forward the raw data to the server.
func (api *PublicWhisperAPI) RequestHistoricMessages(peerID rpc.HexBytes, data rpc.HexBytes) error {
func (api *PublicWhisperAPI) RequestHistoricMessages(peerID hexutil.Bytes, data hexutil.Bytes) error {
if api.whisper == nil {
return whisperOffLineErr
}
Expand Down Expand Up @@ -388,12 +389,12 @@ type PostArgs struct {
To string `json:"to"`
KeyName string `json:"keyname"`
Topic whisperv5.TopicType `json:"topic"`
Padding rpc.HexBytes `json:"padding"`
Payload rpc.HexBytes `json:"payload"`
Padding hexutil.Bytes `json:"padding"`
Payload hexutil.Bytes `json:"payload"`
WorkTime uint32 `json:"worktime"`
PoW float64 `json:"pow"`
FilterID uint32 `json:"filterID"`
PeerID rpc.HexBytes `json:"peerID"`
PeerID hexutil.Bytes `json:"peerID"`
}

type WhisperFilterArgs struct {
Expand Down

0 comments on commit adab2e1

Please sign in to comment.