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
}
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())
case "eth_getStorage", "eth_storageAt":
args := new(GetStorageArgs)
@ -102,15 +102,15 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) 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":
args := new(GetStorageAtArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
return err
}
state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address.Hex())
value := state.StorageString(args.Key.Hex())
state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address)
value := state.StorageString(args.Key)
*reply = common.Bytes2Hex(value.Bytes())
case "eth_getTransactionCount":
@ -119,14 +119,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err
}
*reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address.Hex())
*reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
case "eth_getBlockTransactionCountByHash":
args := new(GetBlockByHashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
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())
case "eth_getBlockTransactionCountByNumber":
args := new(GetBlockByNumberArgs)
@ -142,7 +142,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err
}
block := api.xeth().EthBlockByHash(args.BlockHash.Hex())
block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block)
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
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 {
return err
}
*reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address.Hex())
*reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address)
case "eth_sendTransaction", "eth_transact":
args := new(NewTxArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {
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 {
return err
}
@ -177,7 +177,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) 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 {
return err
}
@ -191,7 +191,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err
}
block := api.xeth().EthBlockByHash(args.BlockHash.Hex())
block := api.xeth().EthBlockByHash(args.BlockHash)
br := NewBlockRes(block)
br.fullTx = args.IncludeTxs
@ -222,7 +222,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err
}
block := api.xeth().EthBlockByHash(args.Hash.Hex())
block := api.xeth().EthBlockByHash(args.Hash)
br := NewBlockRes(block)
br.fullTx = true
@ -250,14 +250,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) 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 {
return NewValidationError("Index", "does not exist")
}
uhash := br.Uncles[args.Index].Hex()
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash))
uhash := br.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle
case "eth_getUncleByBlockNumberAndIndex":
@ -274,8 +274,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return NewValidationError("Index", "does not exist")
}
uhash := v.Uncles[args.Index].Hex()
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash))
uhash := v.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle
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 {
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":
args := new(DbArgs)
if err := json.Unmarshal(req.Params, &args); err != nil {

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

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