From 35ad9febceacbb0a40efe84b08205bb13222c1b5 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 22 Apr 2015 13:14:10 -0500 Subject: Update eth_getBlockByNumber to accept words --- rpc/args.go | 8 ++------ rpc/args_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'rpc') diff --git a/rpc/args.go b/rpc/args.go index d31773ff7..9f99456e6 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -145,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) diff --git a/rpc/args_test.go b/rpc/args_test.go index cfe6c0c45..e78981a41 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 := `[]` -- cgit v1.2.3 From 2ea2261156eae9e7d783ff5f8663ad95113c8386 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 22 Apr 2015 13:24:10 -0500 Subject: Accept num or hex as index --- rpc/args.go | 8 ++++---- rpc/args_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'rpc') diff --git a/rpc/args.go b/rpc/args.go index 9f99456e6..7694a3d3f 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -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 } diff --git a/rpc/args_test.go b/rpc/args_test.go index e78981a41..9e0cf179f 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -2264,7 +2264,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)) -- cgit v1.2.3 From 20bae2b8f6fb46440f0b7e6d83916c422fd84567 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 22 Apr 2015 13:24:29 -0500 Subject: Tests for magic words --- rpc/args_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'rpc') diff --git a/rpc/args_test.go b/rpc/args_test.go index 9e0cf179f..2f011bfd9 100644 --- a/rpc/args_test.go +++ b/rpc/args_test.go @@ -2184,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 := `{}` @@ -2233,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 := `[]` -- cgit v1.2.3