eth/filters: drop null topics, don't special case them

This commit is contained in:
Péter Szilágyi 2017-09-22 21:38:25 +03:00
parent 673007d7ae
commit f56e5df756
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
2 changed files with 21 additions and 30 deletions

View file

@ -453,11 +453,9 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &raw); err != nil { if err := json.Unmarshal(data, &raw); err != nil {
return err return err
} }
if raw.From != nil { if raw.From != nil {
args.FromBlock = big.NewInt(raw.From.Int64()) args.FromBlock = big.NewInt(raw.From.Int64())
} }
if raw.ToBlock != nil { if raw.ToBlock != nil {
args.ToBlock = big.NewInt(raw.ToBlock.Int64()) 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. // 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. // JSON null values are converted to common.Hash{} and ignored by the filter manager.
if len(raw.Topics) > 0 { if len(raw.Topics) > 0 {
args.Topics = make([][]common.Hash, len(raw.Topics)) args.Topics = make([][]common.Hash, 0, len(raw.Topics))
for i, t := range raw.Topics { for _, t := range raw.Topics {
switch topic := t.(type) { switch topic := t.(type) {
case nil: case nil:
// ignore topic when matching logs // ignore topic when matching logs
args.Topics[i] = []common.Hash{{}}
case string: case string:
// match specific topic // match specific topic
@ -506,28 +503,32 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
if err != nil { if err != nil {
return err return err
} }
args.Topics[i] = []common.Hash{top} args.Topics = append(args.Topics, []common.Hash{top})
case []interface{}: case []interface{}:
// or case e.g. [null, "topic0", "topic1"] // or case e.g. [null, "topic0", "topic1"]
topics := make([]common.Hash, 0, 4)
for _, rawTopic := range topic { for _, rawTopic := range topic {
if rawTopic == nil { if rawTopic == nil {
args.Topics[i] = append(args.Topics[i], common.Hash{}) continue
} else if topic, ok := rawTopic.(string); ok { } else if topic, ok := rawTopic.(string); ok {
parsed, err := decodeTopic(topic) parsed, err := decodeTopic(topic)
if err != nil { if err != nil {
return err return err
} }
args.Topics[i] = append(args.Topics[i], parsed) topics = append(topics, parsed)
} else { } else {
return fmt.Errorf("invalid topic(s)") return fmt.Errorf("invalid topic(s)")
} }
} }
args.Topics = append(args.Topics, topics)
default: default:
return fmt.Errorf("invalid topic(s)") return fmt.Errorf("invalid topic(s)")
} }
} }
} }
return nil return nil
} }

View file

@ -34,7 +34,6 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca") topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3") topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce") topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
nullTopic = common.Hash{}
) )
// default values // default values
@ -141,8 +140,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
if err := json.Unmarshal([]byte(vector), &test6); err != nil { if err := json.Unmarshal([]byte(vector), &test6); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(test6.Topics) != 3 { if len(test6.Topics) != 2 {
t.Fatalf("expected 3 topics, got %d", len(test6.Topics)) t.Fatalf("expected 2 topics, got %d", len(test6.Topics))
} }
if len(test6.Topics[0]) != 1 { if len(test6.Topics[0]) != 1 {
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0])) 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 { if len(test6.Topics[1]) != 1 {
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[1])) t.Fatalf("expected 1 topic, got %d", len(test6.Topics[1]))
} }
if test6.Topics[1][0] != nullTopic { if len(test6.Topics[1]) != 1 {
t.Fatalf("got %x, expected empty hash", test6.Topics[1][0])
}
if len(test6.Topics[2]) != 1 {
t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2])) t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
} }
if test6.Topics[2][0] != topic2 { if test6.Topics[1][0] != topic2 {
t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2) t.Fatalf("got %x, expected %x", test6.Topics[1][0], topic2)
} }
// test OR topics // test OR topics
@ -169,8 +165,8 @@ func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
if err := json.Unmarshal([]byte(vector), &test7); err != nil { if err := json.Unmarshal([]byte(vector), &test7); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(test7.Topics) != 3 { if len(test7.Topics) != 2 {
t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics)) t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics))
} }
if len(test7.Topics[0]) != 2 { if len(test7.Topics[0]) != 2 {
t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0])) 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 { 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 { if test7.Topics[1][0] != topic2 {
t.Fatalf("expected empty hash, got %x", test7.Topics[1][0]) t.Fatalf("invalid topics expected [%x], got [%x]",
} topic2, 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],
) )
} }
} }