github.com/klaytn/klaytn@v1.12.1/node/cn/filters/api_test.go (about)

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