From adab2e16bdf24c2eaf498722187fb36c04a5376b Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 12 Dec 2016 12:25:59 +0100 Subject: [PATCH] rpc: remove HexBytes, replace all uses with hexutil.Bytes --- internal/ethapi/api.go | 18 +++++++++--------- rpc/types.go | 30 ------------------------------ rpc/types_test.go | 22 ---------------------- whisper/shhapi/api.go | 11 ++++++----- 4 files changed, 15 insertions(+), 66 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 607df6b0cd..85e37d31ef 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -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), @@ -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"` @@ -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()), @@ -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), @@ -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) @@ -828,7 +828,7 @@ 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()) } @@ -836,7 +836,7 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByBlockNumberAndIndex(ctx co } // 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()) } @@ -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 @@ -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, diff --git a/rpc/types.go b/rpc/types.go index ebe3883735..89c5b5bc9b 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -17,8 +17,6 @@ package rpc import ( - "bytes" - "encoding/hex" "fmt" "math" "math/big" @@ -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 -} diff --git a/rpc/types_test.go b/rpc/types_test.go index 5482557b89..c2c5c6db6c 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -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) - } - } -} diff --git a/whisper/shhapi/api.go b/whisper/shhapi/api.go index f2597e133e..8a0bd92144 100644 --- a/whisper/shhapi/api.go +++ b/whisper/shhapi/api.go @@ -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" @@ -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 } @@ -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 } @@ -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 {