Browse Source

Make "To" field optional in whisper filter

Taylor Gerring 10 năm trước cách đây
mục cha
commit
3908590578
2 tập tin đã thay đổi với 33 bổ sung5 xóa
  1. 8 5
      rpc/args.go
  2. 25 0
      rpc/args_test.go

+ 8 - 5
rpc/args.go

@@ -1021,12 +1021,15 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
 		return NewInsufficientParamsError(len(obj), 1)
 	}
 
-	var argstr string
-	argstr, ok := obj[0].To.(string)
-	if !ok {
-		return NewInvalidTypeError("to", "is not a string")
+	if obj[0].To == nil {
+		args.To = ""
+	} else {
+		argstr, ok := obj[0].To.(string)
+		if !ok {
+			return NewInvalidTypeError("to", "is not a string")
+		}
+		args.To = argstr
 	}
-	args.To = argstr
 
 	t := make([]string, len(obj[0].Topics))
 	for i, j := range obj[0].Topics {

+ 25 - 0
rpc/args_test.go

@@ -1805,6 +1805,16 @@ func TestWhisperFilterArgsEmpty(t *testing.T) {
 	}
 }
 
+func TestWhisperFilterArgsToInt(t *testing.T) {
+	input := `[{"to": 2}]`
+
+	args := new(WhisperFilterArgs)
+	str := ExpectInvalidTypeError(json.Unmarshal([]byte(input), args))
+	if len(str) > 0 {
+		t.Error(str)
+	}
+}
+
 func TestWhisperFilterArgsToBool(t *testing.T) {
 	input := `[{"topics": ["0x68656c6c6f20776f726c64"], "to": false}]`
 
@@ -1815,6 +1825,21 @@ func TestWhisperFilterArgsToBool(t *testing.T) {
 	}
 }
 
+func TestWhisperFilterArgsToMissing(t *testing.T) {
+	input := `[{"topics": ["0x68656c6c6f20776f726c64"]}]`
+	expected := new(WhisperFilterArgs)
+	expected.To = ""
+
+	args := new(WhisperFilterArgs)
+	if err := json.Unmarshal([]byte(input), &args); err != nil {
+		t.Error(err)
+	}
+
+	if args.To != expected.To {
+		t.Errorf("To shoud be %v but is %v", expected.To, args.To)
+	}
+}
+
 func TestWhisperFilterArgsTopicInt(t *testing.T) {
 	input := `[{"topics": [6], "to": "0x34ag445g3455b34"}]`