github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/eth/filters/api_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:37</date>
    10  //</624450088755793920>
    11  
    12  
    13  package filters
    14  
    15  import (
    16  	"encoding/json"
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/rpc"
    22  )
    23  
    24  func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
    25  	var (
    26  		fromBlock rpc.BlockNumber = 0x123435
    27  		toBlock   rpc.BlockNumber = 0xabcdef
    28  		address0                  = common.HexToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
    29  		address1                  = common.HexToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
    30  		topic0                    = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
    31  		topic1                    = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
    32  		topic2                    = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
    33  	)
    34  
    35  //默认值
    36  	var test0 FilterCriteria
    37  	if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if test0.FromBlock != nil {
    41  		t.Fatalf("expected nil, got %d", test0.FromBlock)
    42  	}
    43  	if test0.ToBlock != nil {
    44  		t.Fatalf("expected nil, got %d", test0.ToBlock)
    45  	}
    46  	if len(test0.Addresses) != 0 {
    47  		t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
    48  	}
    49  	if len(test0.Topics) != 0 {
    50  		t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
    51  	}
    52  
    53  //从,到块号
    54  	var test1 FilterCriteria
    55  	vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
    56  	if err := json.Unmarshal([]byte(vector), &test1); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if test1.FromBlock.Int64() != fromBlock.Int64() {
    60  		t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
    61  	}
    62  	if test1.ToBlock.Int64() != toBlock.Int64() {
    63  		t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
    64  	}
    65  
    66  //单地址
    67  	var test2 FilterCriteria
    68  	vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())
    69  	if err := json.Unmarshal([]byte(vector), &test2); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if len(test2.Addresses) != 1 {
    73  		t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
    74  	}
    75  	if test2.Addresses[0] != address0 {
    76  		t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
    77  	}
    78  
    79  //多个地址
    80  	var test3 FilterCriteria
    81  	vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())
    82  	if err := json.Unmarshal([]byte(vector), &test3); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if len(test3.Addresses) != 2 {
    86  		t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
    87  	}
    88  	if test3.Addresses[0] != address0 {
    89  		t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
    90  	}
    91  	if test3.Addresses[1] != address1 {
    92  		t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
    93  	}
    94  
    95  //单一话题
    96  	var test4 FilterCriteria
    97  	vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
    98  	if err := json.Unmarshal([]byte(vector), &test4); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	if len(test4.Topics) != 1 {
   102  		t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
   103  	}
   104  	if len(test4.Topics[0]) != 1 {
   105  		t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
   106  	}
   107  	if test4.Topics[0][0] != topic0 {
   108  		t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
   109  	}
   110  
   111  //测试多个“和”主题
   112  	var test5 FilterCriteria
   113  	vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
   114  	if err := json.Unmarshal([]byte(vector), &test5); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if len(test5.Topics) != 2 {
   118  		t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
   119  	}
   120  	if len(test5.Topics[0]) != 1 {
   121  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
   122  	}
   123  	if test5.Topics[0][0] != topic0 {
   124  		t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
   125  	}
   126  	if len(test5.Topics[1]) != 1 {
   127  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
   128  	}
   129  	if test5.Topics[1][0] != topic1 {
   130  		t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
   131  	}
   132  
   133  //测试可选主题
   134  	var test6 FilterCriteria
   135  	vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
   136  	if err := json.Unmarshal([]byte(vector), &test6); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	if len(test6.Topics) != 3 {
   140  		t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
   141  	}
   142  	if len(test6.Topics[0]) != 1 {
   143  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
   144  	}
   145  	if test6.Topics[0][0] != topic0 {
   146  		t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
   147  	}
   148  	if len(test6.Topics[1]) != 0 {
   149  		t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))
   150  	}
   151  	if len(test6.Topics[2]) != 1 {
   152  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
   153  	}
   154  	if test6.Topics[2][0] != topic2 {
   155  		t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
   156  	}
   157  
   158  //测试或主题
   159  	var test7 FilterCriteria
   160  	vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
   161  	if err := json.Unmarshal([]byte(vector), &test7); err != nil {
   162  		t.Fatal(err)
   163  	}
   164  	if len(test7.Topics) != 3 {
   165  		t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
   166  	}
   167  	if len(test7.Topics[0]) != 2 {
   168  		t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
   169  	}
   170  	if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
   171  		t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
   172  			topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
   173  		)
   174  	}
   175  	if len(test7.Topics[1]) != 0 {
   176  		t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))
   177  	}
   178  	if len(test7.Topics[2]) != 0 {
   179  		t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))
   180  	}
   181  }
   182