From 4523a00b91dbe98c6cb03acef362c5592973bcd3 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 26 Mar 2015 12:47:00 +0100 Subject: GetTxCountArgs --- rpc/api.go | 7 +------ rpc/args.go | 11 ++-------- rpc/args_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/rpc/api.go b/rpc/api.go index 10d2e529e..6f3e78cc0 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -123,12 +123,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - err := args.requirements() - if err != nil { - return err - } - - *reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) + *reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address.Hex()) case "eth_getBlockTransactionCountByHash": args := new(GetBlockByHashArgs) if err := json.Unmarshal(req.Params, &args); err != nil { diff --git a/rpc/args.go b/rpc/args.go index 84c9ee0f7..93af3258f 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -221,7 +221,7 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) { } type GetTxCountArgs struct { - Address string + Address common.Address BlockNumber int64 } @@ -239,7 +239,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { if !ok { return NewDecodeParamError("Address is not a string") } - args.Address = addstr + args.Address = common.HexToAddress(addstr) if len(obj) > 1 { if err := blockHeight(obj[1], &args.BlockNumber); err != nil { @@ -250,13 +250,6 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { return nil } -func (args *GetTxCountArgs) requirements() error { - if len(args.Address) == 0 { - return NewValidationError("Address", "cannot be blank") - } - return nil -} - type GetBalanceArgs struct { Address string BlockNumber int64 diff --git a/rpc/args_test.go b/rpc/args_test.go index dea34b956..e13549d00 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -559,20 +559,16 @@ func TestGetStorageAtArgsValueNotString(t *testing.T) { } func TestGetTxCountArgs(t *testing.T) { - input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` + input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "pending"]` expected := new(GetTxCountArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" - expected.BlockNumber = -1 + expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") + expected.BlockNumber = -2 args := new(GetTxCountArgs) if err := json.Unmarshal([]byte(input), &args); err != nil { t.Error(err) } - if err := args.requirements(); err != nil { - t.Error(err) - } - if expected.Address != args.Address { t.Errorf("Address shoud be %#v but is %#v", expected.Address, args.Address) } @@ -587,8 +583,58 @@ func TestGetTxCountEmptyArgs(t *testing.T) { args := new(GetTxCountArgs) err := json.Unmarshal([]byte(input), &args) - if err == nil { + switch err.(type) { + case nil: + t.Error("Expected error but didn't get one") + case *InsufficientParamsError: + break + default: + t.Errorf("Expected *rpc.InsufficientParamsError but got %T with message `%s`", err, err.Error()) + } +} + +func TestGetTxCountEmptyArgsInvalid(t *testing.T) { + input := `false` + + args := new(GetTxCountArgs) + err := json.Unmarshal([]byte(input), &args) + switch err.(type) { + case nil: t.Error("Expected error but didn't get one") + case *DecodeParamError: + break + default: + t.Errorf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error()) + } +} + +func TestGetTxCountAddressNotString(t *testing.T) { + input := `[false, "pending"]` + + args := new(GetTxCountArgs) + err := json.Unmarshal([]byte(input), &args) + switch err.(type) { + case nil: + t.Error("Expected error but didn't get one") + case *DecodeParamError: + break + default: + t.Errorf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error()) + } +} + +func TestGetTxCountBlockheightInvalid(t *testing.T) { + input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", {}]` + + args := new(GetTxCountArgs) + err := json.Unmarshal([]byte(input), &args) + switch err.(type) { + case nil: + t.Error("Expected error but didn't get one") + case *DecodeParamError: + break + default: + t.Errorf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error()) } } -- cgit