github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/eth/filters/api_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package filters
    13  
    14  import (
    15  	"encoding/json"
    16  	"fmt"
    17  	"testing"
    18  
    19  	"github.com/Sberex/go-sberex/common"
    20  	"github.com/Sberex/go-sberex/rpc"
    21  )
    22  
    23  func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
    24  	var (
    25  		fromBlock rpc.BlockNumber = 0x123435
    26  		toBlock   rpc.BlockNumber = 0xabcdef
    27  		address0                  = common.StringToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
    28  		address1                  = common.StringToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
    29  		topic0                    = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
    30  		topic1                    = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
    31  		topic2                    = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
    32  	)
    33  
    34  	// default values
    35  	var test0 FilterCriteria
    36  	if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	if test0.FromBlock != nil {
    40  		t.Fatalf("expected nil, got %d", test0.FromBlock)
    41  	}
    42  	if test0.ToBlock != nil {
    43  		t.Fatalf("expected nil, got %d", test0.ToBlock)
    44  	}
    45  	if len(test0.Addresses) != 0 {
    46  		t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
    47  	}
    48  	if len(test0.Topics) != 0 {
    49  		t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
    50  	}
    51  
    52  	// from, to block number
    53  	var test1 FilterCriteria
    54  	vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
    55  	if err := json.Unmarshal([]byte(vector), &test1); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if test1.FromBlock.Int64() != fromBlock.Int64() {
    59  		t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
    60  	}
    61  	if test1.ToBlock.Int64() != toBlock.Int64() {
    62  		t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
    63  	}
    64  
    65  	// single address
    66  	var test2 FilterCriteria
    67  	vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())
    68  	if err := json.Unmarshal([]byte(vector), &test2); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if len(test2.Addresses) != 1 {
    72  		t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
    73  	}
    74  	if test2.Addresses[0] != address0 {
    75  		t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
    76  	}
    77  
    78  	// multiple address
    79  	var test3 FilterCriteria
    80  	vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())
    81  	if err := json.Unmarshal([]byte(vector), &test3); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if len(test3.Addresses) != 2 {
    85  		t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
    86  	}
    87  	if test3.Addresses[0] != address0 {
    88  		t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
    89  	}
    90  	if test3.Addresses[1] != address1 {
    91  		t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
    92  	}
    93  
    94  	// single topic
    95  	var test4 FilterCriteria
    96  	vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
    97  	if err := json.Unmarshal([]byte(vector), &test4); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if len(test4.Topics) != 1 {
   101  		t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
   102  	}
   103  	if len(test4.Topics[0]) != 1 {
   104  		t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
   105  	}
   106  	if test4.Topics[0][0] != topic0 {
   107  		t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
   108  	}
   109  
   110  	// test multiple "AND" topics
   111  	var test5 FilterCriteria
   112  	vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
   113  	if err := json.Unmarshal([]byte(vector), &test5); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	if len(test5.Topics) != 2 {
   117  		t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
   118  	}
   119  	if len(test5.Topics[0]) != 1 {
   120  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
   121  	}
   122  	if test5.Topics[0][0] != topic0 {
   123  		t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
   124  	}
   125  	if len(test5.Topics[1]) != 1 {
   126  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
   127  	}
   128  	if test5.Topics[1][0] != topic1 {
   129  		t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
   130  	}
   131  
   132  	// test optional topic
   133  	var test6 FilterCriteria
   134  	vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
   135  	if err := json.Unmarshal([]byte(vector), &test6); err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	if len(test6.Topics) != 3 {
   139  		t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
   140  	}
   141  	if len(test6.Topics[0]) != 1 {
   142  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
   143  	}
   144  	if test6.Topics[0][0] != topic0 {
   145  		t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
   146  	}
   147  	if len(test6.Topics[1]) != 0 {
   148  		t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))
   149  	}
   150  	if len(test6.Topics[2]) != 1 {
   151  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
   152  	}
   153  	if test6.Topics[2][0] != topic2 {
   154  		t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
   155  	}
   156  
   157  	// test OR topics
   158  	var test7 FilterCriteria
   159  	vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
   160  	if err := json.Unmarshal([]byte(vector), &test7); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	if len(test7.Topics) != 3 {
   164  		t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
   165  	}
   166  	if len(test7.Topics[0]) != 2 {
   167  		t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
   168  	}
   169  	if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
   170  		t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
   171  			topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
   172  		)
   173  	}
   174  	if len(test7.Topics[1]) != 0 {
   175  		t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))
   176  	}
   177  	if len(test7.Topics[2]) != 0 {
   178  		t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))
   179  	}
   180  }