github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/ethclient/ethclient_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  //</624450090219606016>
    11  
    12  
    13  package ethclient
    14  
    15  import (
    16  	"fmt"
    17  	"math/big"
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/ethereum/go-ethereum"
    22  	"github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  //验证客户端是否实现了以太坊接口。
    26  var (
    27  	_ = ethereum.ChainReader(&Client{})
    28  	_ = ethereum.TransactionReader(&Client{})
    29  	_ = ethereum.ChainStateReader(&Client{})
    30  	_ = ethereum.ChainSyncReader(&Client{})
    31  	_ = ethereum.ContractCaller(&Client{})
    32  	_ = ethereum.GasEstimator(&Client{})
    33  	_ = ethereum.GasPricer(&Client{})
    34  	_ = ethereum.LogFilterer(&Client{})
    35  	_ = ethereum.PendingStateReader(&Client{})
    36  //_u=ethereum.pendingstateeventer(&client)
    37  	_ = ethereum.PendingContractCaller(&Client{})
    38  )
    39  
    40  func TestToFilterArg(t *testing.T) {
    41  	blockHashErr := fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
    42  	addresses := []common.Address{
    43  		common.HexToAddress("0xD36722ADeC3EdCB29c8e7b5a47f352D701393462"),
    44  	}
    45  	blockHash := common.HexToHash(
    46  		"0xeb94bb7d78b73657a9d7a99792413f50c0a45c51fc62bdcb08a53f18e9a2b4eb",
    47  	)
    48  
    49  	for _, testCase := range []struct {
    50  		name   string
    51  		input  ethereum.FilterQuery
    52  		output interface{}
    53  		err    error
    54  	}{
    55  		{
    56  			"without BlockHash",
    57  			ethereum.FilterQuery{
    58  				Addresses: addresses,
    59  				FromBlock: big.NewInt(1),
    60  				ToBlock:   big.NewInt(2),
    61  				Topics:    [][]common.Hash{},
    62  			},
    63  			map[string]interface{}{
    64  				"address":   addresses,
    65  				"fromBlock": "0x1",
    66  				"toBlock":   "0x2",
    67  				"topics":    [][]common.Hash{},
    68  			},
    69  			nil,
    70  		},
    71  		{
    72  			"with nil fromBlock and nil toBlock",
    73  			ethereum.FilterQuery{
    74  				Addresses: addresses,
    75  				Topics:    [][]common.Hash{},
    76  			},
    77  			map[string]interface{}{
    78  				"address":   addresses,
    79  				"fromBlock": "0x0",
    80  				"toBlock":   "latest",
    81  				"topics":    [][]common.Hash{},
    82  			},
    83  			nil,
    84  		},
    85  		{
    86  			"with blockhash",
    87  			ethereum.FilterQuery{
    88  				Addresses: addresses,
    89  				BlockHash: &blockHash,
    90  				Topics:    [][]common.Hash{},
    91  			},
    92  			map[string]interface{}{
    93  				"address":   addresses,
    94  				"blockHash": blockHash,
    95  				"topics":    [][]common.Hash{},
    96  			},
    97  			nil,
    98  		},
    99  		{
   100  			"with blockhash and from block",
   101  			ethereum.FilterQuery{
   102  				Addresses: addresses,
   103  				BlockHash: &blockHash,
   104  				FromBlock: big.NewInt(1),
   105  				Topics:    [][]common.Hash{},
   106  			},
   107  			nil,
   108  			blockHashErr,
   109  		},
   110  		{
   111  			"with blockhash and to block",
   112  			ethereum.FilterQuery{
   113  				Addresses: addresses,
   114  				BlockHash: &blockHash,
   115  				ToBlock:   big.NewInt(1),
   116  				Topics:    [][]common.Hash{},
   117  			},
   118  			nil,
   119  			blockHashErr,
   120  		},
   121  		{
   122  			"with blockhash and both from / to block",
   123  			ethereum.FilterQuery{
   124  				Addresses: addresses,
   125  				BlockHash: &blockHash,
   126  				FromBlock: big.NewInt(1),
   127  				ToBlock:   big.NewInt(2),
   128  				Topics:    [][]common.Hash{},
   129  			},
   130  			nil,
   131  			blockHashErr,
   132  		},
   133  	} {
   134  		t.Run(testCase.name, func(t *testing.T) {
   135  			output, err := toFilterArg(testCase.input)
   136  			if (testCase.err == nil) != (err == nil) {
   137  				t.Fatalf("expected error %v but got %v", testCase.err, err)
   138  			}
   139  			if testCase.err != nil {
   140  				if testCase.err.Error() != err.Error() {
   141  					t.Fatalf("expected error %v but got %v", testCase.err, err)
   142  				}
   143  			} else if !reflect.DeepEqual(testCase.output, output) {
   144  				t.Fatalf("expected filter arg %v but got %v", testCase.output, output)
   145  			}
   146  		})
   147  	}
   148  }
   149