github.com/hyperion-hyn/go-ethereum@v2.4.0+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/stretchr/testify/assert" 26 27 "github.com/ethereum/go-ethereum" 28 "github.com/ethereum/go-ethereum/common" 29 ) 30 31 // Verify that Client implements the ethereum interfaces. 32 var ( 33 _ = ethereum.ChainReader(&Client{}) 34 _ = ethereum.TransactionReader(&Client{}) 35 _ = ethereum.ChainStateReader(&Client{}) 36 _ = ethereum.ChainSyncReader(&Client{}) 37 _ = ethereum.ContractCaller(&Client{}) 38 _ = ethereum.GasEstimator(&Client{}) 39 _ = ethereum.GasPricer(&Client{}) 40 _ = ethereum.LogFilterer(&Client{}) 41 _ = ethereum.PendingStateReader(&Client{}) 42 // _ = ethereum.PendingStateEventer(&Client{}) 43 _ = ethereum.PendingContractCaller(&Client{}) 44 ) 45 46 func TestToFilterArg(t *testing.T) { 47 blockHashErr := fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock") 48 addresses := []common.Address{ 49 common.HexToAddress("0xD36722ADeC3EdCB29c8e7b5a47f352D701393462"), 50 } 51 blockHash := common.HexToHash( 52 "0xeb94bb7d78b73657a9d7a99792413f50c0a45c51fc62bdcb08a53f18e9a2b4eb", 53 ) 54 55 for _, testCase := range []struct { 56 name string 57 input ethereum.FilterQuery 58 output interface{} 59 err error 60 }{ 61 { 62 "without BlockHash", 63 ethereum.FilterQuery{ 64 Addresses: addresses, 65 FromBlock: big.NewInt(1), 66 ToBlock: big.NewInt(2), 67 Topics: [][]common.Hash{}, 68 }, 69 map[string]interface{}{ 70 "address": addresses, 71 "fromBlock": "0x1", 72 "toBlock": "0x2", 73 "topics": [][]common.Hash{}, 74 }, 75 nil, 76 }, 77 { 78 "with nil fromBlock and nil toBlock", 79 ethereum.FilterQuery{ 80 Addresses: addresses, 81 Topics: [][]common.Hash{}, 82 }, 83 map[string]interface{}{ 84 "address": addresses, 85 "fromBlock": "0x0", 86 "toBlock": "latest", 87 "topics": [][]common.Hash{}, 88 }, 89 nil, 90 }, 91 { 92 "with blockhash", 93 ethereum.FilterQuery{ 94 Addresses: addresses, 95 BlockHash: &blockHash, 96 Topics: [][]common.Hash{}, 97 }, 98 map[string]interface{}{ 99 "address": addresses, 100 "blockHash": blockHash, 101 "topics": [][]common.Hash{}, 102 }, 103 nil, 104 }, 105 { 106 "with blockhash and from block", 107 ethereum.FilterQuery{ 108 Addresses: addresses, 109 BlockHash: &blockHash, 110 FromBlock: big.NewInt(1), 111 Topics: [][]common.Hash{}, 112 }, 113 nil, 114 blockHashErr, 115 }, 116 { 117 "with blockhash and to block", 118 ethereum.FilterQuery{ 119 Addresses: addresses, 120 BlockHash: &blockHash, 121 ToBlock: big.NewInt(1), 122 Topics: [][]common.Hash{}, 123 }, 124 nil, 125 blockHashErr, 126 }, 127 { 128 "with blockhash and both from / to block", 129 ethereum.FilterQuery{ 130 Addresses: addresses, 131 BlockHash: &blockHash, 132 FromBlock: big.NewInt(1), 133 ToBlock: big.NewInt(2), 134 Topics: [][]common.Hash{}, 135 }, 136 nil, 137 blockHashErr, 138 }, 139 } { 140 t.Run(testCase.name, func(t *testing.T) { 141 output, err := toFilterArg(testCase.input) 142 if (testCase.err == nil) != (err == nil) { 143 t.Fatalf("expected error %v but got %v", testCase.err, err) 144 } 145 if testCase.err != nil { 146 if testCase.err.Error() != err.Error() { 147 t.Fatalf("expected error %v but got %v", testCase.err, err) 148 } 149 } else if !reflect.DeepEqual(testCase.output, output) { 150 t.Fatalf("expected filter arg %v but got %v", testCase.output, output) 151 } 152 }) 153 } 154 } 155 156 func TestClient_PreparePrivateTransaction_whenTypical(t *testing.T) { 157 testObject := NewClient(nil) 158 159 _, err := testObject.PreparePrivateTransaction([]byte("arbitrary payload"), "arbitrary private from") 160 161 assert.Error(t, err) 162 } 163 164 func TestClient_PreparePrivateTransaction_whenClientIsConfigured(t *testing.T) { 165 expectedData := []byte("arbitrary data") 166 testObject := NewClient(nil) 167 testObject.pc = &privateTransactionManagerStubClient{expectedData} 168 169 actualData, err := testObject.PreparePrivateTransaction([]byte("arbitrary payload"), "arbitrary private from") 170 171 assert.NoError(t, err) 172 assert.Equal(t, expectedData, actualData) 173 } 174 175 type privateTransactionManagerStubClient struct { 176 expectedData []byte 177 } 178 179 func (s *privateTransactionManagerStubClient) storeRaw(data []byte, privateFrom string) ([]byte, error) { 180 return s.expectedData, nil 181 }