diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-26 20:10:31 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-26 20:10:31 +0800 |
commit | c139af582663d760f58e7b50dcb9f355c47ef47e (patch) | |
tree | 3c810319da3bc6e5b67bf34317ddbd1e0b0bfb56 | |
parent | 4523a00b91dbe98c6cb03acef362c5592973bcd3 (diff) | |
download | dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar.gz dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar.bz2 dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar.lz dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar.xz dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.tar.zst dexon-c139af582663d760f58e7b50dcb9f355c47ef47e.zip |
GetBalanceArgs
-rw-r--r-- | rpc/api.go | 6 | ||||
-rw-r--r-- | rpc/args.go | 11 | ||||
-rw-r--r-- | rpc/args_test.go | 65 |
3 files changed, 56 insertions, 26 deletions
diff --git a/rpc/api.go b/rpc/api.go index 6f3e78cc0..aba47eee2 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -94,11 +94,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err return err } - if err := args.requirements(); err != nil { - return err - } - - v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance() + v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address.Hex()).Balance() *reply = common.ToHex(v.Bytes()) case "eth_getStorage", "eth_storageAt": args := new(GetStorageArgs) diff --git a/rpc/args.go b/rpc/args.go index 93af3258f..81343dd01 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -251,7 +251,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { } type GetBalanceArgs struct { - Address string + Address common.Address BlockNumber int64 } @@ -269,7 +269,7 @@ func (args *GetBalanceArgs) 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 { @@ -280,13 +280,6 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) { return nil } -func (args *GetBalanceArgs) requirements() error { - if len(args.Address) == 0 { - return NewValidationError("Address", "cannot be blank") - } - return nil -} - type GetDataArgs struct { Address string BlockNumber int64 diff --git a/rpc/args_test.go b/rpc/args_test.go index e13549d00..4792f4f88 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -24,7 +24,7 @@ func TestSha3(t *testing.T) { func TestGetBalanceArgs(t *testing.T) { input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "0x1f"]` expected := new(GetBalanceArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" + expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.BlockNumber = 31 args := new(GetBalanceArgs) @@ -32,10 +32,6 @@ func TestGetBalanceArgs(t *testing.T) { t.Error(err) } - if err := args.requirements(); err != nil { - t.Error(err) - } - if args.Address != expected.Address { t.Errorf("Address should be %v but is %v", expected.Address, args.Address) } @@ -48,7 +44,7 @@ func TestGetBalanceArgs(t *testing.T) { func TestGetBalanceArgsLatest(t *testing.T) { input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"]` expected := new(GetBalanceArgs) - expected.Address = "0x407d73d8a49eeb85d32cf465507dd71d507100c1" + expected.Address = common.HexToAddress("0x407d73d8a49eeb85d32cf465507dd71d507100c1") expected.BlockNumber = -1 args := new(GetBalanceArgs) @@ -56,10 +52,6 @@ func TestGetBalanceArgsLatest(t *testing.T) { t.Error(err) } - if err := args.requirements(); err != nil { - t.Error(err) - } - if args.Address != expected.Address { t.Errorf("Address should be %v but is %v", expected.Address, args.Address) } @@ -69,15 +61,64 @@ func TestGetBalanceArgsLatest(t *testing.T) { } } -func TestGetBalanceEmptyArgs(t *testing.T) { +func TestGetBalanceArgsEmpty(t *testing.T) { input := `[]` args := new(GetBalanceArgs) 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 TestGetBalanceArgsInvalid(t *testing.T) { + input := `6` + + args := new(GetBalanceArgs) + 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 TestGetBalanceArgsBlockInvalid(t *testing.T) { + input := `["0x407d73d8a49eeb85d32cf465507dd71d507100c1", false]` + + args := new(GetBalanceArgs) + 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 TestGetBalanceArgsAddressInvalid(t *testing.T) { + input := `[-9, "latest"]` + + args := new(GetBalanceArgs) + 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 TestGetBlockByHashArgs(t *testing.T) { |