ethclient_test.go 3.9 KB

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