diff options
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 14 | ||||
-rw-r--r-- | rpc/args.go | 103 | ||||
-rw-r--r-- | rpc/args_test.go | 56 | ||||
-rw-r--r-- | rpc/http.go | 5 |
4 files changed, 118 insertions, 60 deletions
diff --git a/rpc/api.go b/rpc/api.go index f75ad811d..9fbe2c565 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -56,15 +56,15 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err *reply = api.xeth().IsListening() case "net_peerCount": *reply = newHexNum(api.xeth().PeerCount()) - case "eth_version": + case "eth_protocolVersion": *reply = api.xeth().EthVersion() case "eth_coinbase": *reply = newHexData(api.xeth().Coinbase()) case "eth_mining": *reply = api.xeth().IsMining() case "eth_gasPrice": - v := xeth.DefaultGas() - *reply = newHexData(v.Bytes()) + v := xeth.DefaultGasPrice() + *reply = newHexNum(v.Bytes()) case "eth_accounts": *reply = api.xeth().Accounts() case "eth_blockNumber": @@ -189,7 +189,11 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } // TODO unwrap the parent method's ToHex call - *reply = newHexData(common.FromHex(v)) + if v == "0x0" { + *reply = newHexData([]byte{}) + } else { + *reply = newHexData(common.FromHex(v)) + } case "eth_flush": return NewNotImplementedError(req.Method) case "eth_getBlockByHash": @@ -487,6 +491,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return NewNotImplementedError(req.Method) } - rpclogger.DebugDetailf("Reply: %T %s", reply, reply) + glog.V(logger.Detail).Infof("Reply: %T %s\n", reply, reply) return nil } diff --git a/rpc/args.go b/rpc/args.go index d03f914a7..7694a3d3f 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -53,22 +53,23 @@ func blockHeight(raw interface{}, number *int64) error { return nil } -func numString(raw interface{}, number *int64) error { +func numString(raw interface{}) (*big.Int, error) { + var number *big.Int // Parse as integer num, ok := raw.(float64) if ok { - *number = int64(num) - return nil + number = big.NewInt(int64(num)) + return number, nil } // Parse as string/hexstring str, ok := raw.(string) - if !ok { - return NewInvalidTypeError("", "not a number or string") + if ok { + number = common.String2Big(str) + return number, nil } - *number = common.String2Big(str).Int64() - return nil + return nil, NewInvalidTypeError("", "not a number or string") } // func toNumber(v interface{}) (int64, error) { @@ -144,12 +145,8 @@ func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) { return NewInsufficientParamsError(len(obj), 2) } - if v, ok := obj[0].(float64); ok { - args.BlockNumber = int64(v) - } else if v, ok := obj[0].(string); ok { - args.BlockNumber = common.Big(v).Int64() - } else { - return NewInvalidTypeError("blockNumber", "not a number or string") + if err := blockHeight(obj[0], &args.BlockNumber); err != nil { + return err } args.IncludeTxs = obj[1].(bool) @@ -202,33 +199,36 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { args.To = ext.To args.Data = ext.Data - var num int64 + var num *big.Int if ext.Value == nil { - num = 0 + num = big.NewInt(0) } else { - if err := numString(ext.Value, &num); err != nil { + num, err = numString(ext.Value) + if err != nil { return err } } - args.Value = big.NewInt(num) + args.Value = num + num = nil if ext.Gas == nil { - num = 0 + num = big.NewInt(0) } else { - if err := numString(ext.Gas, &num); err != nil { + if num, err = numString(ext.Gas); err != nil { return err } } - args.Gas = big.NewInt(num) + args.Gas = num + num = nil if ext.GasPrice == nil { - num = 0 + num = big.NewInt(0) } else { - if err := numString(ext.GasPrice, &num); err != nil { + if num, err = numString(ext.GasPrice); err != nil { return err } } - args.GasPrice = big.NewInt(num) + args.GasPrice = num // Check for optional BlockNumber param if len(obj) > 1 { @@ -286,33 +286,33 @@ func (args *CallArgs) UnmarshalJSON(b []byte) (err error) { } args.To = ext.To - var num int64 + var num *big.Int if ext.Value == nil { - num = int64(0) + num = big.NewInt(0) } else { - if err := numString(ext.Value, &num); err != nil { + if num, err = numString(ext.Value); err != nil { return err } } - args.Value = big.NewInt(num) + args.Value = num if ext.Gas == nil { - num = int64(0) + num = big.NewInt(0) } else { - if err := numString(ext.Gas, &num); err != nil { + if num, err = numString(ext.Gas); err != nil { return err } } - args.Gas = big.NewInt(num) + args.Gas = num if ext.GasPrice == nil { - num = int64(0) + num = big.NewInt(0) } else { - if err := numString(ext.GasPrice, &num); err != nil { + if num, err = numString(ext.GasPrice); err != nil { return err } } - args.GasPrice = big.NewInt(num) + args.GasPrice = num args.Data = ext.Data @@ -535,11 +535,11 @@ func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) { return err } - arg1, ok := obj[1].(string) - if !ok { - return NewInvalidTypeError("index", "not a string") + var arg1 *big.Int + if arg1, err = numString(obj[1]); err != nil { + return err } - args.Index = common.Big(arg1).Int64() + args.Index = arg1.Int64() return nil } @@ -655,6 +655,7 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { // return NewDecodeParamError(fmt.Sprintf("ToBlock %v", err)) var num int64 + var numBig *big.Int // if blank then latest if obj[0].FromBlock == nil { @@ -682,22 +683,22 @@ func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) { args.Latest = num if obj[0].Limit == nil { - num = defaultLogLimit + numBig = big.NewInt(defaultLogLimit) } else { - if err := numString(obj[0].Limit, &num); err != nil { + if numBig, err = numString(obj[0].Limit); err != nil { return err } } - args.Max = int(num) + args.Max = int(numBig.Int64()) if obj[0].Offset == nil { - num = defaultLogOffset + numBig = big.NewInt(defaultLogOffset) } else { - if err := numString(obj[0].Offset, &num); err != nil { + if numBig, err = numString(obj[0].Offset); err != nil { return err } } - args.Skip = int(num) + args.Skip = int(numBig.Int64()) if obj[0].Address != nil { marg, ok := obj[0].Address.([]interface{}) @@ -894,16 +895,16 @@ func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) { args.From = obj[0].From args.Topics = obj[0].Topics - var num int64 - if err := numString(obj[0].Priority, &num); err != nil { + var num *big.Int + if num, err = numString(obj[0].Priority); err != nil { return err } - args.Priority = uint32(num) + args.Priority = uint32(num.Int64()) - if err := numString(obj[0].Ttl, &num); err != nil { + if num, err = numString(obj[0].Ttl); err != nil { return err } - args.Ttl = uint32(num) + args.Ttl = uint32(num.Int64()) return nil } @@ -973,11 +974,11 @@ func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) { return NewInsufficientParamsError(len(obj), 1) } - var num int64 - if err := numString(obj[0], &num); err != nil { + var num *big.Int + if num, err = numString(obj[0]); err != nil { return err } - args.Id = int(num) + args.Id = int(num.Int64()) return nil } diff --git a/rpc/args_test.go b/rpc/args_test.go index cfe6c0c45..2f011bfd9 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -355,6 +355,25 @@ func TestGetBlockByNumberArgsBlockHex(t *testing.T) { t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) } } +func TestGetBlockByNumberArgsWords(t *testing.T) { + input := `["earliest", true]` + expected := new(GetBlockByNumberArgs) + expected.BlockNumber = 0 + expected.IncludeTxs = true + + args := new(GetBlockByNumberArgs) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if args.BlockNumber != expected.BlockNumber { + t.Errorf("BlockNumber should be %v but is %v", expected.BlockNumber, args.BlockNumber) + } + + if args.IncludeTxs != expected.IncludeTxs { + t.Errorf("IncludeTxs should be %v but is %v", expected.IncludeTxs, args.IncludeTxs) + } +} func TestGetBlockByNumberEmpty(t *testing.T) { input := `[]` @@ -2165,6 +2184,21 @@ func TestBlockNumArgs(t *testing.T) { } } +func TestBlockNumArgsWord(t *testing.T) { + input := `["pending"]` + expected := new(BlockNumIndexArgs) + expected.BlockNumber = -2 + + args := new(BlockNumArg) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if expected.BlockNumber != args.BlockNumber { + t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) + } +} + func TestBlockNumArgsInvalid(t *testing.T) { input := `{}` @@ -2214,6 +2248,26 @@ func TestBlockNumIndexArgs(t *testing.T) { } } +func TestBlockNumIndexArgsWord(t *testing.T) { + input := `["latest", 67]` + expected := new(BlockNumIndexArgs) + expected.BlockNumber = -1 + expected.Index = 67 + + args := new(BlockNumIndexArgs) + if err := json.Unmarshal([]byte(input), &args); err != nil { + t.Error(err) + } + + if expected.BlockNumber != args.BlockNumber { + t.Errorf("BlockNumber shoud be %#v but is %#v", expected.BlockNumber, args.BlockNumber) + } + + if expected.Index != args.Index { + t.Errorf("Index shoud be %#v but is %#v", expected.Index, args.Index) + } +} + func TestBlockNumIndexArgsEmpty(t *testing.T) { input := `[]` @@ -2245,7 +2299,7 @@ func TestBlockNumIndexArgsBlocknumInvalid(t *testing.T) { } func TestBlockNumIndexArgsIndexInvalid(t *testing.T) { - input := `["0x29a", 1]` + input := `["0x29a", true]` args := new(BlockNumIndexArgs) str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), &args)) diff --git a/rpc/http.go b/rpc/http.go index f9c646908..4760601d8 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -13,7 +13,6 @@ import ( "github.com/rs/cors" ) -var rpclogger = logger.NewLogger("RPC") var rpclistener *stoppableTCPListener const ( @@ -31,7 +30,7 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) if err != nil { - rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err) + glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err) return err } rpclistener = l @@ -136,7 +135,7 @@ func send(writer io.Writer, v interface{}) (n int, err error) { var payload []byte payload, err = json.MarshalIndent(v, "", "\t") if err != nil { - rpclogger.Fatalln("Error marshalling JSON", err) + glog.V(logger.Error).Infoln("Error marshalling JSON", err) return 0, err } glog.V(logger.Detail).Infof("Sending payload: %s", payload) |