github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/neatptc/filters/api_test.go (about)

     1  package filters
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/neatio-net/neatio/network/rpc"
     9  	"github.com/neatio-net/neatio/utilities/common"
    10  )
    11  
    12  func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
    13  	var (
    14  		fromBlock rpc.BlockNumber = 0x123435
    15  		toBlock   rpc.BlockNumber = 0xabcdef
    16  		address0                  = common.HexToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
    17  		address1                  = common.HexToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
    18  		topic0                    = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
    19  		topic1                    = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
    20  		topic2                    = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
    21  	)
    22  
    23  	var test0 FilterCriteria
    24  	if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	if test0.FromBlock != nil {
    28  		t.Fatalf("expected nil, got %d", test0.FromBlock)
    29  	}
    30  	if test0.ToBlock != nil {
    31  		t.Fatalf("expected nil, got %d", test0.ToBlock)
    32  	}
    33  	if len(test0.Addresses) != 0 {
    34  		t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
    35  	}
    36  	if len(test0.Topics) != 0 {
    37  		t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
    38  	}
    39  
    40  	var test1 FilterCriteria
    41  	vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
    42  	if err := json.Unmarshal([]byte(vector), &test1); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if test1.FromBlock.Int64() != fromBlock.Int64() {
    46  		t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
    47  	}
    48  	if test1.ToBlock.Int64() != toBlock.Int64() {
    49  		t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
    50  	}
    51  
    52  	var test2 FilterCriteria
    53  
    54  	vector = fmt.Sprintf(`{"address": "%s"}`, address0.String())
    55  	if err := json.Unmarshal([]byte(vector), &test2); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if len(test2.Addresses) != 1 {
    59  		t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
    60  	}
    61  	if test2.Addresses[0] != address0 {
    62  		t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
    63  	}
    64  
    65  	var test3 FilterCriteria
    66  
    67  	vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.String(), address1.String())
    68  	if err := json.Unmarshal([]byte(vector), &test3); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if len(test3.Addresses) != 2 {
    72  		t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
    73  	}
    74  	if test3.Addresses[0] != address0 {
    75  		t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
    76  	}
    77  	if test3.Addresses[1] != address1 {
    78  		t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
    79  	}
    80  
    81  	var test4 FilterCriteria
    82  	vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
    83  	if err := json.Unmarshal([]byte(vector), &test4); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if len(test4.Topics) != 1 {
    87  		t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
    88  	}
    89  	if len(test4.Topics[0]) != 1 {
    90  		t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
    91  	}
    92  	if test4.Topics[0][0] != topic0 {
    93  		t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
    94  	}
    95  
    96  	var test5 FilterCriteria
    97  	vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
    98  	if err := json.Unmarshal([]byte(vector), &test5); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	if len(test5.Topics) != 2 {
   102  		t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
   103  	}
   104  	if len(test5.Topics[0]) != 1 {
   105  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
   106  	}
   107  	if test5.Topics[0][0] != topic0 {
   108  		t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
   109  	}
   110  	if len(test5.Topics[1]) != 1 {
   111  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
   112  	}
   113  	if test5.Topics[1][0] != topic1 {
   114  		t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
   115  	}
   116  
   117  	var test6 FilterCriteria
   118  	vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
   119  	if err := json.Unmarshal([]byte(vector), &test6); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	if len(test6.Topics) != 3 {
   123  		t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
   124  	}
   125  	if len(test6.Topics[0]) != 1 {
   126  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
   127  	}
   128  	if test6.Topics[0][0] != topic0 {
   129  		t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
   130  	}
   131  	if len(test6.Topics[1]) != 0 {
   132  		t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))
   133  	}
   134  	if len(test6.Topics[2]) != 1 {
   135  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
   136  	}
   137  	if test6.Topics[2][0] != topic2 {
   138  		t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
   139  	}
   140  
   141  	var test7 FilterCriteria
   142  	vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
   143  	if err := json.Unmarshal([]byte(vector), &test7); err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	if len(test7.Topics) != 3 {
   147  		t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
   148  	}
   149  	if len(test7.Topics[0]) != 2 {
   150  		t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
   151  	}
   152  	if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
   153  		t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
   154  			topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
   155  		)
   156  	}
   157  	if len(test7.Topics[1]) != 0 {
   158  		t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))
   159  	}
   160  	if len(test7.Topics[2]) != 0 {
   161  		t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))
   162  	}
   163  }