github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/filters/api_test.go (about)

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