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