github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/eth/filters/api_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package filters
    26  
    27  import (
    28  	"encoding/json"
    29  	"fmt"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/rpc"
    34  )
    35  
    36  func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
    37  	var (
    38  		fromBlock rpc.BlockNumber = 0x123435
    39  		toBlock   rpc.BlockNumber = 0xabcdef
    40  		address0                  = common.HexToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
    41  		address1                  = common.HexToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
    42  		topic0                    = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
    43  		topic1                    = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
    44  		topic2                    = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
    45  	)
    46  
    47  //默认值
    48  	var test0 FilterCriteria
    49  	if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if test0.FromBlock != nil {
    53  		t.Fatalf("expected nil, got %d", test0.FromBlock)
    54  	}
    55  	if test0.ToBlock != nil {
    56  		t.Fatalf("expected nil, got %d", test0.ToBlock)
    57  	}
    58  	if len(test0.Addresses) != 0 {
    59  		t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
    60  	}
    61  	if len(test0.Topics) != 0 {
    62  		t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
    63  	}
    64  
    65  //从,到块号
    66  	var test1 FilterCriteria
    67  	vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
    68  	if err := json.Unmarshal([]byte(vector), &test1); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if test1.FromBlock.Int64() != fromBlock.Int64() {
    72  		t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
    73  	}
    74  	if test1.ToBlock.Int64() != toBlock.Int64() {
    75  		t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
    76  	}
    77  
    78  //单地址
    79  	var test2 FilterCriteria
    80  	vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())
    81  	if err := json.Unmarshal([]byte(vector), &test2); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if len(test2.Addresses) != 1 {
    85  		t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
    86  	}
    87  	if test2.Addresses[0] != address0 {
    88  		t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
    89  	}
    90  
    91  //多个地址
    92  	var test3 FilterCriteria
    93  	vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())
    94  	if err := json.Unmarshal([]byte(vector), &test3); err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	if len(test3.Addresses) != 2 {
    98  		t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
    99  	}
   100  	if test3.Addresses[0] != address0 {
   101  		t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
   102  	}
   103  	if test3.Addresses[1] != address1 {
   104  		t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
   105  	}
   106  
   107  //单一话题
   108  	var test4 FilterCriteria
   109  	vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
   110  	if err := json.Unmarshal([]byte(vector), &test4); err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	if len(test4.Topics) != 1 {
   114  		t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
   115  	}
   116  	if len(test4.Topics[0]) != 1 {
   117  		t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
   118  	}
   119  	if test4.Topics[0][0] != topic0 {
   120  		t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
   121  	}
   122  
   123  //测试多个“和”主题
   124  	var test5 FilterCriteria
   125  	vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
   126  	if err := json.Unmarshal([]byte(vector), &test5); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	if len(test5.Topics) != 2 {
   130  		t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
   131  	}
   132  	if len(test5.Topics[0]) != 1 {
   133  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
   134  	}
   135  	if test5.Topics[0][0] != topic0 {
   136  		t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
   137  	}
   138  	if len(test5.Topics[1]) != 1 {
   139  		t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
   140  	}
   141  	if test5.Topics[1][0] != topic1 {
   142  		t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
   143  	}
   144  
   145  //测试可选主题
   146  	var test6 FilterCriteria
   147  	vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
   148  	if err := json.Unmarshal([]byte(vector), &test6); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	if len(test6.Topics) != 3 {
   152  		t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
   153  	}
   154  	if len(test6.Topics[0]) != 1 {
   155  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
   156  	}
   157  	if test6.Topics[0][0] != topic0 {
   158  		t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
   159  	}
   160  	if len(test6.Topics[1]) != 0 {
   161  		t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))
   162  	}
   163  	if len(test6.Topics[2]) != 1 {
   164  		t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
   165  	}
   166  	if test6.Topics[2][0] != topic2 {
   167  		t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
   168  	}
   169  
   170  //测试或主题
   171  	var test7 FilterCriteria
   172  	vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
   173  	if err := json.Unmarshal([]byte(vector), &test7); err != nil {
   174  		t.Fatal(err)
   175  	}
   176  	if len(test7.Topics) != 3 {
   177  		t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
   178  	}
   179  	if len(test7.Topics[0]) != 2 {
   180  		t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
   181  	}
   182  	if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
   183  		t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
   184  			topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
   185  		)
   186  	}
   187  	if len(test7.Topics[1]) != 0 {
   188  		t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))
   189  	}
   190  	if len(test7.Topics[2]) != 0 {
   191  		t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))
   192  	}
   193  }