Prefer args as strings not objects

This commit is contained in:
Taylor Gerring 2015-03-26 22:35:42 +01:00
parent 7eed7e1d96
commit bb12dbe233
3 changed files with 51 additions and 53 deletions

@ -94,7 +94,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address.Hex()).Balance() v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
*reply = common.ToHex(v.Bytes()) *reply = common.ToHex(v.Bytes())
case "eth_getStorage", "eth_storageAt": case "eth_getStorage", "eth_storageAt":
args := new(GetStorageArgs) args := new(GetStorageArgs)
@ -102,15 +102,15 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
*reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address.Hex()).Storage() *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
case "eth_getStorageAt": case "eth_getStorageAt":
args := new(GetStorageAtArgs) args := new(GetStorageAtArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address.Hex()) state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address)
value := state.StorageString(args.Key.Hex()) value := state.StorageString(args.Key)
*reply = common.Bytes2Hex(value.Bytes()) *reply = common.Bytes2Hex(value.Bytes())
case "eth_getTransactionCount": case "eth_getTransactionCount":
@ -119,14 +119,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
*reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address.Hex()) *reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
case "eth_getBlockTransactionCountByHash": case "eth_getBlockTransactionCountByHash":
args := new(GetBlockByHashArgs) args := new(GetBlockByHashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash.Hex())) block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash))
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
case "eth_getBlockTransactionCountByNumber": case "eth_getBlockTransactionCountByNumber":
args := new(GetBlockByNumberArgs) args := new(GetBlockByNumberArgs)
@ -142,7 +142,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
block := api.xeth().EthBlockByHash(args.BlockHash.Hex()) block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block) br := NewBlockRes(block)
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes()) *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
case "eth_getUncleCountByBlockNumber": case "eth_getUncleCountByBlockNumber":
@ -159,14 +159,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
*reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address.Hex()) *reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address)
case "eth_sendTransaction", "eth_transact": case "eth_sendTransaction", "eth_transact":
args := new(NewTxArgs) args := new(NewTxArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
v, err := api.xeth().Transact(args.From.Hex(), args.To.Hex(), args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) v, err := api.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
if err != nil { if err != nil {
return err return err
} }
@ -177,7 +177,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From.Hex(), args.To.Hex(), args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data)
if err != nil { if err != nil {
return err return err
} }
@ -191,7 +191,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
block := api.xeth().EthBlockByHash(args.BlockHash.Hex()) block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block) br := NewBlockRes(block)
br.fullTx = args.IncludeTxs br.fullTx = args.IncludeTxs
@ -222,7 +222,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
block := api.xeth().EthBlockByHash(args.Hash.Hex()) block := api.xeth().EthBlockByHash(args.Hash)
br := NewBlockRes(block) br := NewBlockRes(block)
br.fullTx = true br.fullTx = true
@ -250,14 +250,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash.Hex())) br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash))
if args.Index > int64(len(br.Uncles)) || args.Index < 0 { if args.Index > int64(len(br.Uncles)) || args.Index < 0 {
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uhash := br.Uncles[args.Index].Hex() uhash := br.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle *reply = uncle
case "eth_getUncleByBlockNumberAndIndex": case "eth_getUncleByBlockNumberAndIndex":
@ -274,8 +274,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uhash := v.Uncles[args.Index].Hex() uhash := v.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle *reply = uncle
case "eth_getCompilers": case "eth_getCompilers":
@ -332,7 +332,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
*reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, args.Digest, args.Header) *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
case "db_putString": case "db_putString":
args := new(DbArgs) args := new(DbArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {

@ -121,8 +121,8 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
} }
type NewTxArgs struct { type NewTxArgs struct {
From common.Address From string
To common.Address To string
Value *big.Int Value *big.Int
Gas *big.Int Gas *big.Int
GasPrice *big.Int GasPrice *big.Int
@ -154,8 +154,8 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
return NewValidationError("from", "is required") return NewValidationError("from", "is required")
} }
args.From = common.HexToAddress(ext.From) args.From = ext.From
args.To = common.HexToAddress(ext.To) args.To = ext.To
args.Value = common.String2Big(ext.Value) args.Value = common.String2Big(ext.Value)
args.Gas = common.String2Big(ext.Gas) args.Gas = common.String2Big(ext.Gas)
args.GasPrice = common.String2Big(ext.GasPrice) args.GasPrice = common.String2Big(ext.GasPrice)
@ -172,7 +172,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
} }
type GetStorageArgs struct { type GetStorageArgs struct {
Address common.Address Address string
BlockNumber int64 BlockNumber int64
} }
@ -190,7 +190,7 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
if !ok { if !ok {
return NewInvalidTypeError("address", "not a string") return NewInvalidTypeError("address", "not a string")
} }
args.Address = common.HexToAddress(addstr) args.Address = addstr
if len(obj) > 1 { if len(obj) > 1 {
if err := blockHeight(obj[1], &args.BlockNumber); err != nil { if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
@ -202,8 +202,8 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
} }
type GetStorageAtArgs struct { type GetStorageAtArgs struct {
Address common.Address Address string
Key common.Hash Key string
BlockNumber int64 BlockNumber int64
} }
@ -221,13 +221,13 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
if !ok { if !ok {
return NewInvalidTypeError("address", "not a string") return NewInvalidTypeError("address", "not a string")
} }
args.Address = common.HexToAddress(addstr) args.Address = addstr
keystr, ok := obj[1].(string) keystr, ok := obj[1].(string)
if !ok { if !ok {
return NewInvalidTypeError("key", "not a string") return NewInvalidTypeError("key", "not a string")
} }
args.Key = common.HexToHash(keystr) args.Key = keystr
if len(obj) > 2 { if len(obj) > 2 {
if err := blockHeight(obj[2], &args.BlockNumber); err != nil { if err := blockHeight(obj[2], &args.BlockNumber); err != nil {
@ -239,7 +239,7 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
} }
type GetTxCountArgs struct { type GetTxCountArgs struct {
Address common.Address Address string
BlockNumber int64 BlockNumber int64
} }
@ -257,7 +257,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
if !ok { if !ok {
return NewInvalidTypeError("address", "not a string") return NewInvalidTypeError("address", "not a string")
} }
args.Address = common.HexToAddress(addstr) args.Address = addstr
if len(obj) > 1 { if len(obj) > 1 {
if err := blockHeight(obj[1], &args.BlockNumber); err != nil { if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
@ -269,7 +269,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
} }
type GetBalanceArgs struct { type GetBalanceArgs struct {
Address common.Address Address string
BlockNumber int64 BlockNumber int64
} }
@ -287,7 +287,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
if !ok { if !ok {
return NewInvalidTypeError("address", "not a string") return NewInvalidTypeError("address", "not a string")
} }
args.Address = common.HexToAddress(addstr) args.Address = addstr
if len(obj) > 1 { if len(obj) > 1 {
if err := blockHeight(obj[1], &args.BlockNumber); err != nil { if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
@ -299,7 +299,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
} }
type GetDataArgs struct { type GetDataArgs struct {
Address common.Address Address string
BlockNumber int64 BlockNumber int64
} }
@ -317,7 +317,7 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
if !ok { if !ok {
return NewInvalidTypeError("address", "not a string") return NewInvalidTypeError("address", "not a string")
} }
args.Address = common.HexToAddress(addstr) args.Address = addstr
if len(obj) > 1 { if len(obj) > 1 {
if err := blockHeight(obj[1], &args.BlockNumber); err != nil { if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
@ -763,8 +763,8 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
type SubmitWorkArgs struct { type SubmitWorkArgs struct {
Nonce uint64 Nonce uint64
Header common.Hash Header string
Digest common.Hash Digest string
} }
func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) {
@ -788,13 +788,13 @@ func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) {
return NewInvalidTypeError("header", "not a string") return NewInvalidTypeError("header", "not a string")
} }
args.Header = common.HexToHash(objstr) args.Header = objstr
if objstr, ok = obj[2].(string); !ok { if objstr, ok = obj[2].(string); !ok {
return NewInvalidTypeError("digest", "not a string") return NewInvalidTypeError("digest", "not a string")
} }
args.Digest = common.HexToHash(objstr) args.Digest = objstr
return nil return nil
} }

@ -6,8 +6,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum/go-ethereum/common"
) )
func ExpectValidationError(err error) string { func ExpectValidationError(err error) string {
@ -106,7 +104,7 @@ func TestSha3ArgsDataInvalid(t *testing.T) {
func TestGetBalanceArgs(t *testing.T) { func TestGetBalanceArgs(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]`
expected := new(GetBalanceArgs) expected := new(GetBalanceArgs)
expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.BlockNumber = 31 expected.BlockNumber = 31
args := new(GetBalanceArgs) args := new(GetBalanceArgs)
@ -126,7 +124,7 @@ func TestGetBalanceArgs(t *testing.T) {
func TestGetBalanceArgsLatest(t *testing.T) { func TestGetBalanceArgsLatest(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetBalanceArgs) expected := new(GetBalanceArgs)
expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.BlockNumber = -1 expected.BlockNumber = -1
args := new(GetBalanceArgs) args := new(GetBalanceArgs)
@ -316,8 +314,8 @@ func TestNewTxArgs(t *testing.T) {
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}, "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"},
"0x10"]` "0x10"]`
expected := new(NewTxArgs) expected := new(NewTxArgs)
expected.From = common.HexToAddress("0xb60e8dd61c5d32be8058bb8eb970870f07233155") expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
expected.To = common.HexToAddress("0xd46e8dd67c5d32be8058bb8eb970870f072445675") expected.To = "0xd46e8dd67c5d32be8058bb8eb970870f072445675"
expected.Gas = big.NewInt(30400) expected.Gas = big.NewInt(30400)
expected.GasPrice = big.NewInt(10000000000000) expected.GasPrice = big.NewInt(10000000000000)
expected.Value = big.NewInt(10000000000000) expected.Value = big.NewInt(10000000000000)
@ -361,7 +359,7 @@ func TestNewTxArgs(t *testing.T) {
func TestNewTxArgsBlockInt(t *testing.T) { func TestNewTxArgsBlockInt(t *testing.T) {
input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}, 5]` input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}, 5]`
expected := new(NewTxArgs) expected := new(NewTxArgs)
expected.From = common.HexToAddress("0xb60e8dd61c5d32be8058bb8eb970870f07233155") expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
expected.BlockNumber = big.NewInt(5).Int64() expected.BlockNumber = big.NewInt(5).Int64()
args := new(NewTxArgs) args := new(NewTxArgs)
@ -381,7 +379,7 @@ func TestNewTxArgsBlockInt(t *testing.T) {
func TestNewTxArgsBlockInvalid(t *testing.T) { func TestNewTxArgsBlockInvalid(t *testing.T) {
input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}, false]` input := `[{"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"}, false]`
expected := new(NewTxArgs) expected := new(NewTxArgs)
expected.From = common.HexToAddress("0xb60e8dd61c5d32be8058bb8eb970870f07233155") expected.From = "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
expected.BlockNumber = big.NewInt(5).Int64() expected.BlockNumber = big.NewInt(5).Int64()
args := new(NewTxArgs) args := new(NewTxArgs)
@ -438,7 +436,7 @@ func TestNewTxArgsFromEmpty(t *testing.T) {
func TestGetStorageArgs(t *testing.T) { func TestGetStorageArgs(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]`
expected := new(GetStorageArgs) expected := new(GetStorageArgs)
expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.BlockNumber = -1 expected.BlockNumber = -1
args := new(GetStorageArgs) args := new(GetStorageArgs)
@ -498,8 +496,8 @@ func TestGetStorageAddressInt(t *testing.T) {
func TestGetStorageAtArgs(t *testing.T) { func TestGetStorageAtArgs(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0", "0x2"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x0", "0x2"]`
expected := new(GetStorageAtArgs) expected := new(GetStorageAtArgs)
expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.Key = common.HexToHash("0x0") expected.Key = "0x0"
expected.BlockNumber = 2 expected.BlockNumber = 2
args := new(GetStorageAtArgs) args := new(GetStorageAtArgs)
@ -573,7 +571,7 @@ func TestGetStorageAtArgsValueNotString(t *testing.T) {
func TestGetTxCountArgs(t *testing.T) { func TestGetTxCountArgs(t *testing.T) {
input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"]` input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"]`
expected := new(GetTxCountArgs) expected := new(GetTxCountArgs)
expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1"
expected.BlockNumber = -2 expected.BlockNumber = -2
args := new(GetTxCountArgs) args := new(GetTxCountArgs)
@ -633,7 +631,7 @@ func TestGetTxCountBlockheightInvalid(t *testing.T) {
func TestGetDataArgs(t *testing.T) { func TestGetDataArgs(t *testing.T) {
input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", "latest"]` input := `["0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8", "latest"]`
expected := new(GetDataArgs) expected := new(GetDataArgs)
expected.Address = common.HexToAddress("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8") expected.Address = "0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8"
expected.BlockNumber = -1 expected.BlockNumber = -1
args := new(GetDataArgs) args := new(GetDataArgs)
@ -1505,8 +1503,8 @@ func TestSubmitWorkArgs(t *testing.T) {
input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]` input := `["0x0000000000000001", "0x1234567890abcdef1234567890abcdef", "0xD1GE5700000000000000000000000000"]`
expected := new(SubmitWorkArgs) expected := new(SubmitWorkArgs)
expected.Nonce = 1 expected.Nonce = 1
expected.Header = common.HexToHash("0x1234567890abcdef1234567890abcdef") expected.Header = "0x1234567890abcdef1234567890abcdef"
expected.Digest = common.HexToHash("0xD1GE5700000000000000000000000000") expected.Digest = "0xD1GE5700000000000000000000000000"
args := new(SubmitWorkArgs) args := new(SubmitWorkArgs)
if err := json.Unmarshal([]byte(input), &args); err != nil { if err := json.Unmarshal([]byte(input), &args); err != nil {