From 0fddd0de2f8108bbf3657600747b4054fb45998f Mon Sep 17 00:00:00 2001 From: Miya Chen Date: Mon, 25 Sep 2017 10:19:21 +0800 Subject: [PATCH] client, common, container, k8s: make client.Client as interface type --- client/api.go | 71 +++++++++++++++++++ client/client.go | 30 ++++---- client/ethclient.go | 54 +++++++------- common/transactions.go | 2 +- container/ethereum.go | 4 +- k8s/ethereum.go | 2 +- k8s/transactor.go | 4 +- .../functional/private_transaction_test.go | 4 +- 8 files changed, 121 insertions(+), 50 deletions(-) create mode 100644 client/api.go diff --git a/client/api.go b/client/api.go new file mode 100644 index 00000000..7c83299e --- /dev/null +++ b/client/api.go @@ -0,0 +1,71 @@ +// Copyright 2017 AMIS Technologies +// 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 . + +package client + +import ( + "context" + "math/big" + + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/p2p" +) + +type Client interface { + Close() + AddPeer(ctx context.Context, nodeURL string) error + AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) + NodeInfo(ctx context.Context) (*p2p.PeerInfo, error) + BlockNumber(ctx context.Context) (*big.Int, error) + StartMining(ctx context.Context) error + StopMining(ctx context.Context) error + SendTransaction(ctx context.Context, from, to common.Address, value *big.Int) (string, error) + CreateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int) (string, error) + CreatePrivateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int, privateFor []string) (string, error) + ProposeValidator(ctx context.Context, address common.Address, auth bool) error + GetValidators(ctx context.Context, blockNumbers *big.Int) ([]common.Address, error) + + // eth client + BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) + BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) + HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) + HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) + TransactionByHash(ctx context.Context, hash common.Hash) (*types.Transaction, bool, error) + TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) + TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) + TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) + SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) + SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) + NetworkID(ctx context.Context) (*big.Int, error) + BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) + StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) + CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) + NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) + FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) + SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) + PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) + PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) + PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) + PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) + PendingTransactionCount(ctx context.Context) (uint, error) + CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) + PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) + SuggestGasPrice(ctx context.Context) (*big.Int, error) + EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) + SendRawTransaction(ctx context.Context, tx *types.Transaction) error +} diff --git a/client/client.go b/client/client.go index f0153a3d..7859c236 100644 --- a/client/client.go +++ b/client/client.go @@ -30,29 +30,29 @@ import ( "github.com/getamis/go-ethereum/ethclient" ) -type Client struct { +type client struct { c *rpc.Client ethClient *ethclient.Client } -func Dial(rawurl string) (*Client, error) { +func Dial(rawurl string) (Client, error) { c, err := rpc.Dial(rawurl) if err != nil { return nil, err } - return &Client{ + return &client{ c: c, ethClient: ethclient.NewClient(c), }, nil } -func (c *Client) Close() { +func (c *client) Close() { c.c.Close() } // ---------------------------------------------------------------------------- -func (ic *Client) AddPeer(ctx context.Context, nodeURL string) error { +func (ic *client) AddPeer(ctx context.Context, nodeURL string) error { var r bool // TODO: Result needs to be verified // The response data type are bytes, but we cannot parse... @@ -63,7 +63,7 @@ func (ic *Client) AddPeer(ctx context.Context, nodeURL string) error { return err } -func (ic *Client) AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) { +func (ic *client) AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) { var r []*p2p.PeerInfo // The response data type are bytes, but we cannot parse... err := ic.c.CallContext(ctx, &r, "admin_peers") @@ -73,7 +73,7 @@ func (ic *Client) AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) { return r, err } -func (ic *Client) NodeInfo(ctx context.Context) (*p2p.PeerInfo, error) { +func (ic *client) NodeInfo(ctx context.Context) (*p2p.PeerInfo, error) { var r *p2p.PeerInfo err := ic.c.CallContext(ctx, &r, "admin_nodeInfo") if err != nil { @@ -83,7 +83,7 @@ func (ic *Client) NodeInfo(ctx context.Context) (*p2p.PeerInfo, error) { } // ---------------------------------------------------------------------------- -func (ic *Client) BlockNumber(ctx context.Context) (*big.Int, error) { +func (ic *client) BlockNumber(ctx context.Context) (*big.Int, error) { var r string err := ic.c.CallContext(ctx, &r, "eth_blockNumber") if err != nil { @@ -95,7 +95,7 @@ func (ic *Client) BlockNumber(ctx context.Context) (*big.Int, error) { // ---------------------------------------------------------------------------- -func (ic *Client) StartMining(ctx context.Context) error { +func (ic *client) StartMining(ctx context.Context) error { var r []byte // TODO: Result needs to be verified // The response data type are bytes, but we cannot parse... @@ -106,7 +106,7 @@ func (ic *Client) StartMining(ctx context.Context) error { return err } -func (ic *Client) StopMining(ctx context.Context) error { +func (ic *client) StopMining(ctx context.Context) error { err := ic.c.CallContext(ctx, nil, "miner_stop", nil) if err != nil { return err @@ -116,7 +116,7 @@ func (ic *Client) StopMining(ctx context.Context) error { // ---------------------------------------------------------------------------- -func (ic *Client) SendTransaction(ctx context.Context, from, to common.Address, value *big.Int) (txHash string, err error) { +func (ic *client) SendTransaction(ctx context.Context, from, to common.Address, value *big.Int) (txHash string, err error) { var hex hexutil.Bytes arg := map[string]interface{}{ "from": from, @@ -129,7 +129,7 @@ func (ic *Client) SendTransaction(ctx context.Context, from, to common.Address, return } -func (ic *Client) CreateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int) (txHash string, err error) { +func (ic *client) CreateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int) (txHash string, err error) { var hex hexutil.Bytes arg := map[string]interface{}{ "from": from, @@ -142,7 +142,7 @@ func (ic *Client) CreateContract(ctx context.Context, from common.Address, bytec return } -func (ic *Client) CreatePrivateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int, privateFor []string) (txHash string, err error) { +func (ic *client) CreatePrivateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int, privateFor []string) (txHash string, err error) { var hex hexutil.Bytes arg := map[string]interface{}{ "from": from, @@ -158,7 +158,7 @@ func (ic *Client) CreatePrivateContract(ctx context.Context, from common.Address // ---------------------------------------------------------------------------- -func (ic *Client) ProposeValidator(ctx context.Context, address common.Address, auth bool) error { +func (ic *client) ProposeValidator(ctx context.Context, address common.Address, auth bool) error { var r []byte // TODO: Result needs to be verified with other method // The response data type are bytes, but we cannot parse... @@ -183,7 +183,7 @@ func (addrs addresses) Swap(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] } -func (ic *Client) GetValidators(ctx context.Context, blockNumbers *big.Int) ([]common.Address, error) { +func (ic *client) GetValidators(ctx context.Context, blockNumbers *big.Int) ([]common.Address, error) { var r []common.Address err := ic.c.CallContext(ctx, &r, "istanbul_getValidators", toNumArg(blockNumbers)) if err == nil && r == nil { diff --git a/client/ethclient.go b/client/ethclient.go index e26089a9..fec86434 100644 --- a/client/ethclient.go +++ b/client/ethclient.go @@ -31,7 +31,7 @@ import ( // // Note that loading full blocks requires two requests. Use HeaderByHash // if you don't need all transactions or uncle headers. -func (c *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { +func (c *client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { return c.ethClient.BlockByHash(ctx, hash) } @@ -40,122 +40,122 @@ func (c *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Bloc // // Note that loading full blocks requires two requests. Use HeaderByNumber // if you don't need all transactions or uncle headers. -func (c *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { +func (c *client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { return c.ethClient.BlockByNumber(ctx, number) } // HeaderByHash returns the block header with the given hash. -func (c *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { +func (c *client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { return c.ethClient.HeaderByHash(ctx, hash) } // HeaderByNumber returns a block header from the current canonical chain. If number is // nil, the latest known header is returned. -func (c *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { +func (c *client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { return c.ethClient.HeaderByNumber(ctx, number) } // TransactionByHash returns the transaction with the given hash. -func (c *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) { +func (c *client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) { return c.ethClient.TransactionByHash(ctx, hash) } // TransactionCount returns the total number of transactions in the given block. -func (c *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) { +func (c *client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) { return c.ethClient.TransactionCount(ctx, blockHash) } // TransactionInBlock returns a single transaction at index in the given block. -func (c *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) { +func (c *client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) { return c.ethClient.TransactionInBlock(ctx, blockHash, index) } // TransactionReceipt returns the receipt of a transaction by transaction hash. // Note that the receipt is not available for pending transactions. -func (c *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { +func (c *client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { return c.ethClient.TransactionReceipt(ctx, txHash) } // SyncProgress retrieves the current progress of the sync algorithm. If there's // no sync currently running, it returns nil. -func (c *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) { +func (c *client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) { return c.ethClient.SyncProgress(ctx) } // SubscribeNewHead subscribes to notifications about the current blockchain head // on the given channel. -func (c *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { +func (c *client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { return c.ethClient.SubscribeNewHead(ctx, ch) } // State Access // NetworkID returns the network ID (also known as the chain ID) for this chain. -func (c *Client) NetworkID(ctx context.Context) (*big.Int, error) { +func (c *client) NetworkID(ctx context.Context) (*big.Int, error) { return c.ethClient.NetworkID(ctx) } // BalanceAt returns the wei balance of the given account. // The block number can be nil, in which case the balance is taken from the latest known block. -func (c *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { +func (c *client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { return c.ethClient.BalanceAt(ctx, account, blockNumber) } // StorageAt returns the value of key in the contract storage of the given account. // The block number can be nil, in which case the value is taken from the latest known block. -func (c *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) { +func (c *client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) { return c.ethClient.StorageAt(ctx, account, key, blockNumber) } // CodeAt returns the contract code of the given account. // The block number can be nil, in which case the code is taken from the latest known block. -func (c *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) { +func (c *client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) { return c.ethClient.CodeAt(ctx, account, blockNumber) } // NonceAt returns the account nonce of the given account. // The block number can be nil, in which case the nonce is taken from the latest known block. -func (c *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) { +func (c *client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) { return c.ethClient.NonceAt(ctx, account, blockNumber) } // Filters // FilterLogs executes a filter query. -func (c *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) { +func (c *client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) { return c.ethClient.FilterLogs(ctx, q) } // SubscribeFilterLogs subscribes to the results of a streaming filter query. -func (c *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { +func (c *client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { return c.ethClient.SubscribeFilterLogs(ctx, q, ch) } // Pending State // PendingBalanceAt returns the wei balance of the given account in the pending state. -func (c *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) { +func (c *client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) { return c.ethClient.PendingBalanceAt(ctx, account) } // PendingStorageAt returns the value of key in the contract storage of the given account in the pending state. -func (c *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) { +func (c *client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) { return c.ethClient.PendingStorageAt(ctx, account, key) } // PendingCodeAt returns the contract code of the given account in the pending state. -func (c *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { +func (c *client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { return c.ethClient.PendingCodeAt(ctx, account) } // PendingNonceAt returns the account nonce of the given account in the pending state. // This is the nonce that should be used for the next transaction. -func (c *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { +func (c *client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { return c.ethClient.PendingNonceAt(ctx, account) } // PendingTransactionCount returns the total number of transactions in the pending state. -func (c *Client) PendingTransactionCount(ctx context.Context) (uint, error) { +func (c *client) PendingTransactionCount(ctx context.Context) (uint, error) { return c.ethClient.PendingTransactionCount(ctx) } @@ -167,19 +167,19 @@ func (c *Client) PendingTransactionCount(ctx context.Context) (uint, error) { // blockNumber selects the block height at which the call runs. It can be nil, in which // case the code is taken from the latest known block. Note that state from very old // blocks might not be available. -func (c *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { +func (c *client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { return c.ethClient.CallContract(ctx, msg, blockNumber) } // PendingCallContract executes a message call transaction using the EVM. // The state seen by the contract call is the pending state. -func (c *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) { +func (c *client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) { return c.ethClient.PendingCallContract(ctx, msg) } // SuggestGasPrice retrieves the currently suggested gas price to allow a timely // execution of a transaction. -func (c *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) { +func (c *client) SuggestGasPrice(ctx context.Context) (*big.Int, error) { return c.ethClient.SuggestGasPrice(ctx) } @@ -187,7 +187,7 @@ func (c *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) { // the current pending state of the backend blockchain. There is no guarantee that this is // the true gas limit requirement as other transactions may be added or removed by miners, // but it should provide a basis for setting a reasonable default. -func (c *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) { +func (c *client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) { return c.ethClient.EstimateGas(ctx, msg) } @@ -195,6 +195,6 @@ func (c *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.In // // If the transaction was a contract creation use the TransactionReceipt method to get the // contract address after the transaction has been mined. -func (c *Client) SendRawTransaction(ctx context.Context, tx *types.Transaction) error { +func (c *client) SendRawTransaction(ctx context.Context, tx *types.Transaction) error { return c.ethClient.SendTransaction(ctx, tx) } diff --git a/common/transactions.go b/common/transactions.go index 1139af66..ab7be513 100644 --- a/common/transactions.go +++ b/common/transactions.go @@ -32,7 +32,7 @@ var ( DefaultGasLimit int64 = 22000 // the gas of ether tx should be 21000 ) -func SendEther(client *client.Client, from *ecdsa.PrivateKey, to common.Address, amount *big.Int, nonce uint64) error { +func SendEther(client client.Client, from *ecdsa.PrivateKey, to common.Address, amount *big.Int, nonce uint64) error { tx := types.NewTransaction(nonce, to, amount, big.NewInt(DefaultGasLimit), big.NewInt(DefaultGasPrice), []byte{}) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(2017)), from) if err != nil { diff --git a/container/ethereum.go b/container/ethereum.go index a4695990..d4f583a3 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -67,7 +67,7 @@ type Ethereum interface { ContainerID() string Host() string - NewClient() *client.Client + NewClient() client.Client ConsensusMonitor(err chan<- error, quit chan struct{}) WaitForProposed(expectedAddress common.Address, t time.Duration) error @@ -356,7 +356,7 @@ func (eth *ethereum) Running() bool { return false } -func (eth *ethereum) NewClient() *client.Client { +func (eth *ethereum) NewClient() client.Client { var scheme, port string if eth.rpcPort != "" { diff --git a/k8s/ethereum.go b/k8s/ethereum.go index ad6a1627..5c096b38 100644 --- a/k8s/ethereum.go +++ b/k8s/ethereum.go @@ -106,7 +106,7 @@ func (eth *ethereum) DockerBinds() []string { return nil } -func (eth *ethereum) NewClient() *client.Client { +func (eth *ethereum) NewClient() client.Client { for i := 0; i < healthCheckRetryCount; i++ { client, err := client.Dial("ws://" + eth.Host() + ":8546") if err != nil { diff --git a/k8s/transactor.go b/k8s/transactor.go index 7c013b74..d9f0c830 100644 --- a/k8s/transactor.go +++ b/k8s/transactor.go @@ -28,10 +28,10 @@ import ( ) type Transactor interface { - SendTransactions(*client.Client, *big.Int, time.Duration) error + SendTransactions(client.Client, *big.Int, time.Duration) error } -func (eth *ethereum) SendTransactions(client *client.Client, amount *big.Int, duration time.Duration) error { +func (eth *ethereum) SendTransactions(client client.Client, amount *big.Int, duration time.Duration) error { var fns []func() error for i, key := range eth.accounts { i := i diff --git a/tests/quorum/functional/private_transaction_test.go b/tests/quorum/functional/private_transaction_test.go index 6961fdc0..3a8be273 100644 --- a/tests/quorum/functional/private_transaction_test.go +++ b/tests/quorum/functional/private_transaction_test.go @@ -190,7 +190,7 @@ func genByteCodeWithValue(v int) string { return fmt.Sprintf("%s%064x", testBaseByteCode, v) } -func getTxReceipt(ethClient *client.Client, txHash common.Hash, timeout time.Duration) (*types.Receipt, error) { +func getTxReceipt(ethClient client.Client, txHash common.Hash, timeout time.Duration) (*types.Receipt, error) { timer := time.After(timeout) ticker := time.NewTicker(3 * time.Second) defer ticker.Stop() @@ -207,7 +207,7 @@ func getTxReceipt(ethClient *client.Client, txHash common.Hash, timeout time.Dur } } -func checkContractValue(ethClient *client.Client, txHash common.Hash, expValue int) error { +func checkContractValue(ethClient client.Client, txHash common.Hash, expValue int) error { receipt, err := getTxReceipt(ethClient, txHash, 10*time.Second) if err != nil { return err