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