From f56e5df75663421b7f44140fdb72e82f38949938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Fri, 22 Sep 2017 21:38:25 +0300 Subject: [PATCH] eth/filters: drop null topics, don't special case them --- eth/filters/api.go | 19 ++++++++++--------- eth/filters/api_test.go | 32 +++++++++++--------------------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 6e1d48adb6..58dd0ba0d2 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -453,11 +453,9 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &raw); err != nil { return err } - if raw.From != nil { args.FromBlock = big.NewInt(raw.From.Int64()) } - if raw.ToBlock != nil { args.ToBlock = big.NewInt(raw.ToBlock.Int64()) } @@ -493,12 +491,11 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error { // topics is an array consisting of strings and/or arrays of strings. // JSON null values are converted to common.Hash{} and ignored by the filter manager. if len(raw.Topics) > 0 { - args.Topics = make([][]common.Hash, len(raw.Topics)) - for i, t := range raw.Topics { + args.Topics = make([][]common.Hash, 0, len(raw.Topics)) + for _, t := range raw.Topics { switch topic := t.(type) { case nil: // ignore topic when matching logs - args.Topics[i] = []common.Hash{{}} case string: // match specific topic @@ -506,28 +503,32 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error { if err != nil { return err } - args.Topics[i] = []common.Hash{top} + args.Topics = append(args.Topics, []common.Hash{top}) + case []interface{}: // or case e.g. [null, "topic0", "topic1"] + topics := make([]common.Hash, 0, 4) + for _, rawTopic := range topic { if rawTopic == nil { - args.Topics[i] = append(args.Topics[i], common.Hash{}) + continue } else if topic, ok := rawTopic.(string); ok { parsed, err := decodeTopic(topic) if err != nil { return err } - args.Topics[i] = append(args.Topics[i], parsed) + topics = append(topics, parsed) } else { return fmt.Errorf("invalid topic(s)") } } + args.Topics = append(args.Topics, topics) + default: return fmt.Errorf("invalid topic(s)") } } } - return nil } diff --git a/eth/filters/api_test.go b/eth/filters/api_test.go index 068a5ea243..fa9d31c5ab 100644 --- a/eth/filters/api_test.go +++ b/eth/filters/api_test.go @@ -34,7 +34,6 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) { topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca") topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3") topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce") - nullTopic = common.Hash{} ) // default values @@ -141,8 +140,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) { if err := json.Unmarshal([]byte(vector), &test6); err != nil { t.Fatal(err) } - if len(test6.Topics) != 3 { - t.Fatalf("expected 3 topics, got %d", len(test6.Topics)) + if len(test6.Topics) != 2 { + t.Fatalf("expected 2 topics, got %d", len(test6.Topics)) } if len(test6.Topics[0]) != 1 { t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0])) @@ -153,14 +152,11 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) { if len(test6.Topics[1]) != 1 { t.Fatalf("expected 1 topic, got %d", len(test6.Topics[1])) } - if test6.Topics[1][0] != nullTopic { - t.Fatalf("got %x, expected empty hash", test6.Topics[1][0]) - } - if len(test6.Topics[2]) != 1 { + if len(test6.Topics[1]) != 1 { t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2])) } - if test6.Topics[2][0] != topic2 { - t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2) + if test6.Topics[1][0] != topic2 { + t.Fatalf("got %x, expected %x", test6.Topics[1][0], topic2) } // test OR topics @@ -169,8 +165,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) { if err := json.Unmarshal([]byte(vector), &test7); err != nil { t.Fatal(err) } - if len(test7.Topics) != 3 { - t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics)) + if len(test7.Topics) != 2 { + t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics)) } if len(test7.Topics[0]) != 2 { t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0])) @@ -181,17 +177,11 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) { ) } if len(test7.Topics[1]) != 1 { - t.Fatalf("expected 1 topic, got %d topics", len(test7.Topics[1])) + t.Fatalf("expected 1 topics, got %d topics", len(test7.Topics[2])) } - if test7.Topics[1][0] != nullTopic { - t.Fatalf("expected empty hash, got %x", test7.Topics[1][0]) - } - if len(test7.Topics[2]) != 2 { - t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[2])) - } - if test7.Topics[2][0] != topic2 || test7.Topics[2][1] != nullTopic { - t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]", - topic2, nullTopic, test7.Topics[2][0], test7.Topics[2][1], + if test7.Topics[1][0] != topic2 { + t.Fatalf("invalid topics expected [%x], got [%x]", + topic2, test7.Topics[1][0], ) } }