mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
ethclient: move tests to separate package
This is done to make the RevertErrorData example code look better.
This commit is contained in:
parent
f17139da36
commit
8bfa7d8287
3 changed files with 180 additions and 152 deletions
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package ethclient
|
||||
package ethclient_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
|
@ -35,6 +35,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
|
|
@ -42,145 +43,19 @@ import (
|
|||
|
||||
// Verify that Client implements the ethereum interfaces.
|
||||
var (
|
||||
_ = ethereum.ChainReader(&Client{})
|
||||
_ = ethereum.TransactionReader(&Client{})
|
||||
_ = ethereum.ChainStateReader(&Client{})
|
||||
_ = ethereum.ChainSyncReader(&Client{})
|
||||
_ = ethereum.ContractCaller(&Client{})
|
||||
_ = ethereum.GasEstimator(&Client{})
|
||||
_ = ethereum.GasPricer(&Client{})
|
||||
_ = ethereum.LogFilterer(&Client{})
|
||||
_ = ethereum.PendingStateReader(&Client{})
|
||||
// _ = ethereum.PendingStateEventer(&Client{})
|
||||
_ = ethereum.PendingContractCaller(&Client{})
|
||||
_ = ethereum.ChainReader(ðclient.Client{})
|
||||
_ = ethereum.TransactionReader(ðclient.Client{})
|
||||
_ = ethereum.ChainStateReader(ðclient.Client{})
|
||||
_ = ethereum.ChainSyncReader(ðclient.Client{})
|
||||
_ = ethereum.ContractCaller(ðclient.Client{})
|
||||
_ = ethereum.GasEstimator(ðclient.Client{})
|
||||
_ = ethereum.GasPricer(ðclient.Client{})
|
||||
_ = ethereum.LogFilterer(ðclient.Client{})
|
||||
_ = ethereum.PendingStateReader(ðclient.Client{})
|
||||
// _ = ethereum.PendingStateEventer(ðclient.Client{})
|
||||
_ = ethereum.PendingContractCaller(ðclient.Client{})
|
||||
)
|
||||
|
||||
func TestToFilterArg(t *testing.T) {
|
||||
blockHashErr := errors.New("cannot specify both BlockHash and FromBlock/ToBlock")
|
||||
addresses := []common.Address{
|
||||
common.HexToAddress("0xD36722ADeC3EdCB29c8e7b5a47f352D701393462"),
|
||||
}
|
||||
blockHash := common.HexToHash(
|
||||
"0xeb94bb7d78b73657a9d7a99792413f50c0a45c51fc62bdcb08a53f18e9a2b4eb",
|
||||
)
|
||||
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
input ethereum.FilterQuery
|
||||
output interface{}
|
||||
err error
|
||||
}{
|
||||
{
|
||||
"without BlockHash",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
FromBlock: big.NewInt(1),
|
||||
ToBlock: big.NewInt(2),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "0x1",
|
||||
"toBlock": "0x2",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with nil fromBlock and nil toBlock",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "0x0",
|
||||
"toBlock": "latest",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with negative fromBlock and negative toBlock",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
FromBlock: big.NewInt(-1),
|
||||
ToBlock: big.NewInt(-1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "pending",
|
||||
"toBlock": "pending",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with blockhash",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"blockHash": blockHash,
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with blockhash and from block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
FromBlock: big.NewInt(1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
{
|
||||
"with blockhash and to block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
ToBlock: big.NewInt(1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
{
|
||||
"with blockhash and both from / to block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
FromBlock: big.NewInt(1),
|
||||
ToBlock: big.NewInt(2),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
} {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
output, err := toFilterArg(testCase.input)
|
||||
if (testCase.err == nil) != (err == nil) {
|
||||
t.Fatalf("expected error %v but got %v", testCase.err, err)
|
||||
}
|
||||
if testCase.err != nil {
|
||||
if testCase.err.Error() != err.Error() {
|
||||
t.Fatalf("expected error %v but got %v", testCase.err, err)
|
||||
}
|
||||
} else if !reflect.DeepEqual(testCase.output, output) {
|
||||
t.Fatalf("expected filter arg %v but got %v", testCase.output, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
||||
|
|
@ -337,7 +212,7 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) {
|
|||
}
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
|
|
@ -386,7 +261,7 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
|
|
@ -402,7 +277,7 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testTransactionInBlock(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
// Get current block by number.
|
||||
block, err := ec.BlockByNumber(context.Background(), nil)
|
||||
|
|
@ -434,7 +309,7 @@ func testTransactionInBlock(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testChainID(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
id, err := ec.ChainID(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
|
|
@ -445,7 +320,7 @@ func testChainID(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testGetBlock(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
// Get current block number
|
||||
blockNumber, err := ec.BlockNumber(context.Background())
|
||||
|
|
@ -490,7 +365,7 @@ func testGetBlock(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testStatusFunctions(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
// Sync progress
|
||||
progress, err := ec.SyncProgress(context.Background())
|
||||
|
|
@ -553,7 +428,7 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testCallContractAtHash(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
// EstimateGas
|
||||
msg := ethereum.CallMsg{
|
||||
|
|
@ -580,7 +455,7 @@ func testCallContractAtHash(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testCallContract(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
// EstimateGas
|
||||
msg := ethereum.CallMsg{
|
||||
|
|
@ -607,7 +482,7 @@ func testCallContract(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
|
||||
block, err := ec.HeaderByNumber(context.Background(), big.NewInt(1))
|
||||
if err != nil {
|
||||
|
|
@ -710,7 +585,7 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
|
||||
func testTransactionSender(t *testing.T, client *rpc.Client) {
|
||||
ec := NewClient(client)
|
||||
ec := ethclient.NewClient(client)
|
||||
ctx := context.Background()
|
||||
|
||||
// Retrieve testTx1 via RPC.
|
||||
|
|
@ -750,7 +625,7 @@ func testTransactionSender(t *testing.T, client *rpc.Client) {
|
|||
}
|
||||
}
|
||||
|
||||
func sendTransaction(ec *Client) error {
|
||||
func sendTransaction(ec *ethclient.Client) error {
|
||||
chainID, err := ec.ChainID(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -778,7 +653,7 @@ func sendTransaction(ec *Client) error {
|
|||
func ExampleRevertErrorData() {
|
||||
// First create an ethclient.Client instance.
|
||||
ctx := context.Background()
|
||||
ec, _ := DialContext(ctx, exampleNode.HTTPEndpoint())
|
||||
ec, _ := ethclient.DialContext(ctx, exampleNode.HTTPEndpoint())
|
||||
|
||||
// Call the contract.
|
||||
// Note we expect the call to return an error.
|
||||
|
|
@ -793,7 +668,7 @@ func ExampleRevertErrorData() {
|
|||
}
|
||||
|
||||
// Extract the low-level revert data from the error.
|
||||
revertData, ok := RevertErrorData(err)
|
||||
revertData, ok := ethclient.RevertErrorData(err)
|
||||
if !ok {
|
||||
panic("unpacking revert failed")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package ethclient
|
||||
package ethclient_test
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
|
|
|
|||
153
ethclient/types_test.go
Normal file
153
ethclient/types_test.go
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package ethclient
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
func TestToFilterArg(t *testing.T) {
|
||||
blockHashErr := errors.New("cannot specify both BlockHash and FromBlock/ToBlock")
|
||||
addresses := []common.Address{
|
||||
common.HexToAddress("0xD36722ADeC3EdCB29c8e7b5a47f352D701393462"),
|
||||
}
|
||||
blockHash := common.HexToHash(
|
||||
"0xeb94bb7d78b73657a9d7a99792413f50c0a45c51fc62bdcb08a53f18e9a2b4eb",
|
||||
)
|
||||
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
input ethereum.FilterQuery
|
||||
output interface{}
|
||||
err error
|
||||
}{
|
||||
{
|
||||
"without BlockHash",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
FromBlock: big.NewInt(1),
|
||||
ToBlock: big.NewInt(2),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "0x1",
|
||||
"toBlock": "0x2",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with nil fromBlock and nil toBlock",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "0x0",
|
||||
"toBlock": "latest",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with negative fromBlock and negative toBlock",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
FromBlock: big.NewInt(-1),
|
||||
ToBlock: big.NewInt(-1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"fromBlock": "pending",
|
||||
"toBlock": "pending",
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with blockhash",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"address": addresses,
|
||||
"blockHash": blockHash,
|
||||
"topics": [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"with blockhash and from block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
FromBlock: big.NewInt(1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
{
|
||||
"with blockhash and to block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
ToBlock: big.NewInt(1),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
{
|
||||
"with blockhash and both from / to block",
|
||||
ethereum.FilterQuery{
|
||||
Addresses: addresses,
|
||||
BlockHash: &blockHash,
|
||||
FromBlock: big.NewInt(1),
|
||||
ToBlock: big.NewInt(2),
|
||||
Topics: [][]common.Hash{},
|
||||
},
|
||||
nil,
|
||||
blockHashErr,
|
||||
},
|
||||
} {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
output, err := toFilterArg(testCase.input)
|
||||
if (testCase.err == nil) != (err == nil) {
|
||||
t.Fatalf("expected error %v but got %v", testCase.err, err)
|
||||
}
|
||||
if testCase.err != nil {
|
||||
if testCase.err.Error() != err.Error() {
|
||||
t.Fatalf("expected error %v but got %v", testCase.err, err)
|
||||
}
|
||||
} else if !reflect.DeepEqual(testCase.output, output) {
|
||||
t.Fatalf("expected filter arg %v but got %v", testCase.output, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue