2017-08-18 02:27:49 -07:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
package client
|
2017-08-18 02:27:49 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
2017-08-25 00:14:18 -07:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-08-18 02:27:49 -07:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-08-19 00:59:22 -07:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-08-18 02:27:49 -07:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2017-09-06 20:30:30 -07:00
|
|
|
"github.com/getamis/go-ethereum/ethclient"
|
2017-08-18 02:27:49 -07:00
|
|
|
)
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
type client struct {
|
2017-09-06 20:30:30 -07:00
|
|
|
c *rpc.Client
|
|
|
|
ethClient *ethclient.Client
|
2017-08-18 02:27:49 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func Dial(rawurl string) (Client, error) {
|
2017-08-18 02:27:49 -07:00
|
|
|
c, err := rpc.Dial(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-24 19:19:21 -07:00
|
|
|
return &client{
|
2017-09-06 20:30:30 -07:00
|
|
|
c: c,
|
|
|
|
ethClient: ethclient.NewClient(c),
|
|
|
|
}, nil
|
2017-08-18 02:27:49 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (c *client) Close() {
|
2017-08-18 02:27:49 -07:00
|
|
|
c.c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) AddPeer(ctx context.Context, nodeURL string) error {
|
2017-08-18 02:27:49 -07:00
|
|
|
var r bool
|
|
|
|
// TODO: Result needs to be verified
|
|
|
|
// The response data type are bytes, but we cannot parse...
|
|
|
|
err := ic.c.CallContext(ctx, &r, "admin_addPeer", nodeURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) AdminPeers(ctx context.Context) ([]*p2p.PeerInfo, error) {
|
2017-08-19 00:59:22 -07:00
|
|
|
var r []*p2p.PeerInfo
|
2017-08-18 02:27:49 -07:00
|
|
|
// The response data type are bytes, but we cannot parse...
|
|
|
|
err := ic.c.CallContext(ctx, &r, "admin_peers")
|
|
|
|
if err != nil {
|
2017-08-19 00:59:22 -07:00
|
|
|
return nil, err
|
2017-08-18 02:27:49 -07:00
|
|
|
}
|
2017-08-19 00:59:22 -07:00
|
|
|
return r, err
|
2017-08-18 02:27:49 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) NodeInfo(ctx context.Context) (*p2p.PeerInfo, error) {
|
2017-08-22 00:44:43 -07:00
|
|
|
var r *p2p.PeerInfo
|
|
|
|
err := ic.c.CallContext(ctx, &r, "admin_nodeInfo")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2017-08-21 03:13:26 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) BlockNumber(ctx context.Context) (*big.Int, error) {
|
2017-08-21 03:13:26 -07:00
|
|
|
var r string
|
|
|
|
err := ic.c.CallContext(ctx, &r, "eth_blockNumber")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h, err := hexutil.DecodeBig(r)
|
|
|
|
return h, err
|
|
|
|
}
|
|
|
|
|
2017-08-18 02:27:49 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) StartMining(ctx context.Context) error {
|
2017-08-18 02:27:49 -07:00
|
|
|
var r []byte
|
|
|
|
// TODO: Result needs to be verified
|
|
|
|
// The response data type are bytes, but we cannot parse...
|
|
|
|
err := ic.c.CallContext(ctx, &r, "miner_start", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) StopMining(ctx context.Context) error {
|
2017-08-22 00:36:27 -07:00
|
|
|
err := ic.c.CallContext(ctx, nil, "miner_stop", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-18 02:27:49 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) SendTransaction(ctx context.Context, from, to common.Address, value *big.Int) (txHash string, err error) {
|
2017-09-06 23:42:27 -07:00
|
|
|
var hex hexutil.Bytes
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"from": from,
|
|
|
|
"to": to,
|
|
|
|
"value": (*hexutil.Big)(value),
|
|
|
|
}
|
|
|
|
if err = ic.c.CallContext(ctx, &hex, "eth_sendTransaction", arg); err == nil {
|
|
|
|
txHash = hex.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) CreateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int) (txHash string, err error) {
|
2017-09-06 23:42:27 -07:00
|
|
|
var hex hexutil.Bytes
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"from": from,
|
|
|
|
"gas": (*hexutil.Big)(gas),
|
|
|
|
"data": bytecode,
|
|
|
|
}
|
|
|
|
if err = ic.c.CallContext(ctx, &hex, "eth_sendTransaction", arg); err == nil {
|
|
|
|
txHash = hex.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) CreatePrivateContract(ctx context.Context, from common.Address, bytecode string, gas *big.Int, privateFor []string) (txHash string, err error) {
|
2017-09-06 23:42:27 -07:00
|
|
|
var hex hexutil.Bytes
|
|
|
|
arg := map[string]interface{}{
|
|
|
|
"from": from,
|
|
|
|
"gas": (*hexutil.Big)(gas),
|
|
|
|
"data": bytecode,
|
|
|
|
"privateFor": privateFor,
|
|
|
|
}
|
|
|
|
if err = ic.c.CallContext(ctx, &hex, "eth_sendTransaction", arg); err == nil {
|
|
|
|
txHash = hex.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) ProposeValidator(ctx context.Context, address common.Address, auth bool) error {
|
2017-08-18 02:27:49 -07:00
|
|
|
var r []byte
|
|
|
|
// TODO: Result needs to be verified with other method
|
|
|
|
// The response data type are bytes, but we cannot parse...
|
|
|
|
err := ic.c.CallContext(ctx, &r, "istanbul_propose", address, auth)
|
|
|
|
if err != nil {
|
|
|
|
return ethereum.NotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-25 00:14:18 -07:00
|
|
|
type addresses []common.Address
|
|
|
|
|
|
|
|
func (addrs addresses) Len() int {
|
|
|
|
return len(addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addrs addresses) Less(i, j int) bool {
|
|
|
|
return strings.Compare(addrs[i].String(), addrs[j].String()) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (addrs addresses) Swap(i, j int) {
|
|
|
|
addrs[i], addrs[j] = addrs[j], addrs[i]
|
|
|
|
}
|
|
|
|
|
2017-09-24 19:19:21 -07:00
|
|
|
func (ic *client) GetValidators(ctx context.Context, blockNumbers *big.Int) ([]common.Address, error) {
|
2017-08-18 02:27:49 -07:00
|
|
|
var r []common.Address
|
|
|
|
err := ic.c.CallContext(ctx, &r, "istanbul_getValidators", toNumArg(blockNumbers))
|
|
|
|
if err == nil && r == nil {
|
|
|
|
return nil, ethereum.NotFound
|
|
|
|
}
|
2017-08-25 00:14:18 -07:00
|
|
|
|
|
|
|
sort.Sort(addresses(r))
|
|
|
|
|
2017-08-18 02:27:49 -07:00
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func toNumArg(number *big.Int) string {
|
|
|
|
if number == nil {
|
|
|
|
return "latest"
|
|
|
|
}
|
|
|
|
return hexutil.EncodeBig(number)
|
|
|
|
}
|