Przeglądaj źródła

Merge pull request #783 from tgerring/issue777

Issue 777
Jeffrey Wilcke 10 lat temu
rodzic
commit
359a9773a1
2 zmienionych plików z 61 dodań i 11 usunięć
  1. 6 10
      rpc/args.go
  2. 55 1
      rpc/args_test.go

+ 6 - 10
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)
@@ -539,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
 }

+ 55 - 1
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))