2017-08-08 00:31:13 -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/>.
|
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-08-17 03:04:35 -07:00
|
|
|
"crypto/ecdsa"
|
2017-08-11 01:18:35 -07:00
|
|
|
"errors"
|
2017-08-17 03:04:35 -07:00
|
|
|
"fmt"
|
2017-08-08 00:31:13 -07:00
|
|
|
"io"
|
2017-08-15 20:22:29 -07:00
|
|
|
"io/ioutil"
|
2017-08-08 00:31:13 -07:00
|
|
|
"log"
|
2017-08-11 01:18:35 -07:00
|
|
|
"math/big"
|
2017-08-18 00:24:35 -07:00
|
|
|
"net"
|
2017-08-08 00:31:13 -07:00
|
|
|
"os"
|
2017-08-10 23:03:12 -07:00
|
|
|
"path/filepath"
|
2017-08-08 00:31:13 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-08-14 23:50:56 -07:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2017-09-01 01:52:08 -07:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2017-09-06 20:30:30 -07:00
|
|
|
docker "github.com/docker/docker/client"
|
2017-08-11 01:18:35 -07:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-08-17 03:04:35 -07:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2017-08-21 02:15:14 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-08-21 00:04:26 -07:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2017-08-21 02:15:14 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-08-18 00:24:35 -07:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2017-08-17 03:04:35 -07:00
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
"github.com/getamis/istanbul-tools/client"
|
2017-08-31 23:53:35 -07:00
|
|
|
istcommon "github.com/getamis/istanbul-tools/common"
|
2017-08-17 03:04:35 -07:00
|
|
|
"github.com/getamis/istanbul-tools/genesis"
|
2017-08-08 00:31:13 -07:00
|
|
|
)
|
|
|
|
|
2017-08-11 01:18:35 -07:00
|
|
|
const (
|
2017-09-05 20:14:05 -07:00
|
|
|
healthCheckRetryCount = 20
|
2017-08-11 01:18:35 -07:00
|
|
|
healthCheckRetryDelay = 2 * time.Second
|
|
|
|
)
|
|
|
|
|
2017-08-21 00:04:26 -07:00
|
|
|
var (
|
2017-08-22 20:20:42 -07:00
|
|
|
ErrNoBlock = errors.New("no block generated")
|
2017-08-21 00:04:26 -07:00
|
|
|
ErrConsensusTimeout = errors.New("consensus timeout")
|
|
|
|
)
|
|
|
|
|
2017-08-11 01:18:35 -07:00
|
|
|
type Ethereum interface {
|
|
|
|
Init(string) error
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
2017-08-15 00:19:43 -07:00
|
|
|
|
2017-08-18 00:24:35 -07:00
|
|
|
NodeAddress() string
|
2017-08-21 02:15:14 -07:00
|
|
|
Address() common.Address
|
2017-08-18 00:24:35 -07:00
|
|
|
|
|
|
|
ContainerID() string
|
2017-08-15 00:19:43 -07:00
|
|
|
Host() string
|
2017-09-06 20:30:30 -07:00
|
|
|
NewClient() *client.Client
|
2017-08-21 00:04:26 -07:00
|
|
|
ConsensusMonitor(err chan<- error, quit chan struct{})
|
2017-08-23 00:03:54 -07:00
|
|
|
|
2017-08-24 02:46:26 -07:00
|
|
|
WaitForProposed(expectedAddress common.Address, t time.Duration) error
|
2017-08-23 00:03:54 -07:00
|
|
|
WaitForPeersConnected(int) error
|
2017-08-22 02:05:21 -07:00
|
|
|
WaitForBlocks(int, ...time.Duration) error
|
2017-08-23 00:03:54 -07:00
|
|
|
WaitForBlockHeight(int) error
|
2017-08-24 01:09:59 -07:00
|
|
|
// Want for block for no more than the given number during the given time duration
|
|
|
|
WaitForNoBlocks(int, time.Duration) error
|
2017-08-23 00:03:54 -07:00
|
|
|
|
|
|
|
AddPeer(string) error
|
2017-08-22 02:05:21 -07:00
|
|
|
|
|
|
|
StartMining() error
|
|
|
|
StopMining() error
|
2017-09-05 00:53:15 -07:00
|
|
|
|
|
|
|
DockerEnv() []string
|
|
|
|
DockerBinds() []string
|
2017-08-11 01:18:35 -07:00
|
|
|
}
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
func NewEthereum(c *docker.Client, options ...Option) *ethereum {
|
2017-08-15 03:24:06 -07:00
|
|
|
eth := ðereum{
|
2017-09-06 20:30:30 -07:00
|
|
|
dockerClient: c,
|
2017-08-09 01:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range options {
|
2017-08-15 03:24:06 -07:00
|
|
|
opt(eth)
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
2017-08-09 01:53:14 -07:00
|
|
|
|
2017-08-15 03:24:06 -07:00
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("reference", eth.Image())
|
|
|
|
|
|
|
|
images, err := c.ImageList(context.Background(), types.ImageListOptions{
|
|
|
|
Filters: filters,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(images) == 0 || err != nil {
|
2017-09-06 20:30:30 -07:00
|
|
|
out, err := eth.dockerClient.ImagePull(context.Background(), eth.Image(), types.ImagePullOptions{})
|
2017-08-15 03:24:06 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot pull %s, err: %v", eth.Image(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if eth.logging {
|
|
|
|
io.Copy(os.Stdout, out)
|
|
|
|
} else {
|
2017-08-15 20:22:29 -07:00
|
|
|
io.Copy(ioutil.Discard, out)
|
2017-08-15 03:24:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eth
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type ethereum struct {
|
2017-08-11 01:18:35 -07:00
|
|
|
ok bool
|
2017-08-09 01:53:14 -07:00
|
|
|
flags []string
|
2017-08-10 23:03:12 -07:00
|
|
|
dataDir string
|
2017-09-01 01:52:08 -07:00
|
|
|
ip string
|
2017-08-11 01:18:35 -07:00
|
|
|
port string
|
|
|
|
rpcPort string
|
2017-08-17 00:01:07 -07:00
|
|
|
wsPort string
|
2017-08-09 01:53:14 -07:00
|
|
|
hostName string
|
2017-08-08 00:31:13 -07:00
|
|
|
containerID string
|
2017-08-18 00:24:35 -07:00
|
|
|
node *discover.Node
|
2017-08-15 00:09:41 -07:00
|
|
|
|
2017-09-05 00:53:15 -07:00
|
|
|
//Quorum only
|
|
|
|
isQuorum bool
|
|
|
|
dockerEnv []string
|
|
|
|
dockerBinds []string
|
|
|
|
|
2017-09-01 01:52:08 -07:00
|
|
|
imageRepository string
|
|
|
|
imageTag string
|
|
|
|
dockerNetworkName string
|
2017-08-15 00:09:41 -07:00
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
logging bool
|
|
|
|
dockerClient *docker.Client
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:03:12 -07:00
|
|
|
func (eth *ethereum) Init(genesisFile string) error {
|
2017-08-31 23:53:35 -07:00
|
|
|
if err := istcommon.SaveNodeKey(eth.key, eth.dataDir); err != nil {
|
2017-08-17 03:04:35 -07:00
|
|
|
log.Fatal("Failed to save nodekey", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
binds := []string{
|
|
|
|
genesisFile + ":" + filepath.Join("/", genesis.FileName),
|
|
|
|
}
|
|
|
|
if eth.dataDir != "" {
|
|
|
|
binds = append(binds, eth.dataDir+":"+utils.DataDirFlag.Value.Value)
|
|
|
|
}
|
2017-08-18 00:24:35 -07:00
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
resp, err := eth.dockerClient.ContainerCreate(context.Background(),
|
2017-08-10 23:03:12 -07:00
|
|
|
&container.Config{
|
2017-08-15 00:09:41 -07:00
|
|
|
Image: eth.Image(),
|
2017-08-10 23:03:12 -07:00
|
|
|
Cmd: []string{
|
|
|
|
"init",
|
|
|
|
"--" + utils.DataDirFlag.Name,
|
2017-08-17 03:04:35 -07:00
|
|
|
utils.DataDirFlag.Value.Value,
|
|
|
|
filepath.Join("/", genesis.FileName),
|
2017-08-10 23:03:12 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&container.HostConfig{
|
2017-08-17 03:04:35 -07:00
|
|
|
Binds: binds,
|
2017-08-10 23:03:12 -07:00
|
|
|
}, nil, "")
|
2017-08-08 00:31:13 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to create container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-15 03:24:06 -07:00
|
|
|
id := resp.ID
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
if err := eth.dockerClient.ContainerStart(context.Background(), id, types.ContainerStartOptions{}); err != nil {
|
2017-08-15 03:24:06 -07:00
|
|
|
log.Printf("Failed to start container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
resC, errC := eth.dockerClient.ContainerWait(context.Background(), id, container.WaitConditionNotRunning)
|
2017-08-15 03:24:06 -07:00
|
|
|
select {
|
|
|
|
case <-resC:
|
|
|
|
case <-errC:
|
|
|
|
log.Printf("Failed to wait container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:24:35 -07:00
|
|
|
if eth.logging {
|
|
|
|
eth.showLog(context.Background())
|
|
|
|
}
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
return eth.dockerClient.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true})
|
2017-08-10 23:03:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Start() error {
|
2017-08-22 02:05:21 -07:00
|
|
|
defer func() {
|
|
|
|
if eth.logging {
|
|
|
|
go eth.showLog(context.Background())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-17 00:01:07 -07:00
|
|
|
exposedPorts := make(map[nat.Port]struct{})
|
|
|
|
portBindings := nat.PortMap{}
|
|
|
|
|
|
|
|
if eth.rpcPort != "" {
|
2017-08-17 03:04:35 -07:00
|
|
|
port := fmt.Sprintf("%d", utils.RPCPortFlag.Value)
|
|
|
|
exposedPorts[nat.Port(port)] = struct{}{}
|
|
|
|
portBindings[nat.Port(port)] = []nat.PortBinding{
|
2017-08-17 00:01:07 -07:00
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: eth.rpcPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if eth.wsPort != "" {
|
2017-08-17 03:04:35 -07:00
|
|
|
port := fmt.Sprintf("%d", utils.WSPortFlag.Value)
|
|
|
|
exposedPorts[nat.Port(port)] = struct{}{}
|
|
|
|
portBindings[nat.Port(port)] = []nat.PortBinding{
|
2017-08-17 00:01:07 -07:00
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: eth.wsPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 03:04:35 -07:00
|
|
|
binds := []string{}
|
2017-09-05 00:53:15 -07:00
|
|
|
binds = append(binds, eth.dockerBinds...)
|
2017-08-17 03:04:35 -07:00
|
|
|
if eth.dataDir != "" {
|
|
|
|
binds = append(binds, eth.dataDir+":"+utils.DataDirFlag.Value.Value)
|
|
|
|
}
|
|
|
|
|
2017-09-01 01:52:08 -07:00
|
|
|
var networkingConfig *network.NetworkingConfig
|
|
|
|
if eth.ip != "" && eth.dockerNetworkName != "" {
|
|
|
|
endpointsConfig := make(map[string]*network.EndpointSettings)
|
|
|
|
endpointsConfig[eth.dockerNetworkName] = &network.EndpointSettings{
|
|
|
|
IPAMConfig: &network.EndpointIPAMConfig{
|
|
|
|
IPv4Address: eth.ip,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
networkingConfig = &network.NetworkingConfig{
|
|
|
|
EndpointsConfig: endpointsConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
resp, err := eth.dockerClient.ContainerCreate(context.Background(),
|
2017-08-10 23:03:12 -07:00
|
|
|
&container.Config{
|
2017-08-17 00:01:07 -07:00
|
|
|
Hostname: "geth-" + eth.hostName,
|
|
|
|
Image: eth.Image(),
|
|
|
|
Cmd: eth.flags,
|
|
|
|
ExposedPorts: exposedPorts,
|
2017-09-05 00:53:15 -07:00
|
|
|
Env: eth.DockerEnv(),
|
2017-08-10 23:03:12 -07:00
|
|
|
},
|
|
|
|
&container.HostConfig{
|
2017-08-17 03:04:35 -07:00
|
|
|
Binds: binds,
|
2017-08-17 00:01:07 -07:00
|
|
|
PortBindings: portBindings,
|
2017-09-01 01:52:08 -07:00
|
|
|
}, networkingConfig, "")
|
2017-08-10 23:03:12 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to create container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 00:31:13 -07:00
|
|
|
eth.containerID = resp.ID
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
err = eth.dockerClient.ContainerStart(context.Background(), eth.containerID, types.ContainerStartOptions{})
|
2017-08-11 01:18:35 -07:00
|
|
|
if err != nil {
|
2017-09-01 01:52:08 -07:00
|
|
|
log.Printf("Failed to start container, err: %v, ip:%v", err, eth.ip)
|
2017-08-11 01:18:35 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < healthCheckRetryCount; i++ {
|
2017-08-15 00:19:43 -07:00
|
|
|
cli := eth.NewClient()
|
|
|
|
if cli == nil {
|
2017-08-31 20:26:54 -07:00
|
|
|
<-time.After(healthCheckRetryDelay)
|
2017-08-11 01:18:35 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = cli.BlockByNumber(context.Background(), big.NewInt(0))
|
|
|
|
if err != nil {
|
2017-08-22 02:05:21 -07:00
|
|
|
<-time.After(healthCheckRetryDelay)
|
2017-08-11 01:18:35 -07:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
eth.ok = true
|
2017-08-22 02:05:21 -07:00
|
|
|
break
|
2017-08-11 01:18:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !eth.ok {
|
|
|
|
return errors.New("Failed to start geth")
|
|
|
|
}
|
|
|
|
|
2017-09-01 01:52:08 -07:00
|
|
|
containerIP := eth.ip
|
|
|
|
if containerIP == "" {
|
2017-09-06 20:30:30 -07:00
|
|
|
containerJSON, err := eth.dockerClient.ContainerInspect(context.Background(), eth.containerID)
|
2017-09-01 01:52:08 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Print("Failed to inspect container,", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containerIP = containerJSON.NetworkSettings.IPAddress
|
2017-08-18 00:24:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if eth.key != nil {
|
|
|
|
eth.node = discover.NewNode(
|
|
|
|
discover.PubkeyID(ð.key.PublicKey),
|
2017-09-01 01:52:08 -07:00
|
|
|
net.ParseIP(containerIP),
|
2017-08-18 00:24:35 -07:00
|
|
|
0,
|
|
|
|
uint16(utils.ListenPortFlag.Value))
|
|
|
|
}
|
|
|
|
|
2017-08-11 01:18:35 -07:00
|
|
|
return nil
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Stop() error {
|
2017-09-06 20:30:30 -07:00
|
|
|
err := eth.dockerClient.ContainerStop(context.Background(), eth.containerID, nil)
|
2017-08-08 00:31:13 -07:00
|
|
|
if err != nil {
|
2017-09-05 00:53:15 -07:00
|
|
|
fmt.Printf("error on stop container:%v", err)
|
2017-08-08 00:31:13 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:05:21 -07:00
|
|
|
defer os.RemoveAll(eth.dataDir)
|
2017-08-17 03:04:35 -07:00
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
return eth.dockerClient.ContainerRemove(context.Background(), eth.containerID,
|
2017-08-08 00:31:13 -07:00
|
|
|
types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Wait(t time.Duration) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t)
|
|
|
|
defer cancel()
|
2017-09-06 20:30:30 -07:00
|
|
|
_, errCh := eth.dockerClient.ContainerWait(ctx, eth.containerID, "")
|
2017-08-08 00:31:13 -07:00
|
|
|
return <-errCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Running() bool {
|
2017-09-06 20:30:30 -07:00
|
|
|
containers, err := eth.dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
|
2017-08-08 00:31:13 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to list containers, err: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
if c.ID == eth.containerID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
func (eth *ethereum) NewClient() *client.Client {
|
2017-08-17 00:01:07 -07:00
|
|
|
var scheme, port string
|
|
|
|
|
|
|
|
if eth.rpcPort != "" {
|
|
|
|
scheme = "http://"
|
|
|
|
port = eth.rpcPort
|
|
|
|
}
|
|
|
|
if eth.wsPort != "" {
|
|
|
|
scheme = "ws://"
|
|
|
|
port = eth.wsPort
|
|
|
|
}
|
2017-09-06 20:30:30 -07:00
|
|
|
client, err := client.Dial(scheme + eth.Host() + ":" + port)
|
2017-08-19 01:00:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2017-08-18 00:24:35 -07:00
|
|
|
func (eth *ethereum) NodeAddress() string {
|
|
|
|
if eth.node != nil {
|
|
|
|
return eth.node.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-08-21 02:15:14 -07:00
|
|
|
func (eth *ethereum) Address() common.Address {
|
|
|
|
return crypto.PubkeyToAddress(eth.key.PublicKey)
|
|
|
|
}
|
|
|
|
|
2017-08-21 00:04:26 -07:00
|
|
|
func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) {
|
|
|
|
cli := eth.NewClient()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
subCh := make(chan *ethtypes.Header)
|
|
|
|
|
|
|
|
sub, err := cli.SubscribeNewHead(ctx, subCh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Sprintf("subscribe error:%v", err))
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2017-08-22 22:43:19 -07:00
|
|
|
timer := time.NewTimer(10 * time.Second)
|
|
|
|
blockNumber := uint64(0)
|
2017-08-21 00:04:26 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-sub.Err():
|
|
|
|
log.Printf("Connection lost: %v", err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
2017-08-24 02:46:26 -07:00
|
|
|
case <-timer.C: // FIXME: this event may be missed
|
2017-08-22 22:43:19 -07:00
|
|
|
if blockNumber == 0 {
|
|
|
|
errCh <- ErrNoBlock
|
|
|
|
} else {
|
|
|
|
errCh <- ErrConsensusTimeout
|
|
|
|
}
|
2017-08-22 08:18:43 -07:00
|
|
|
return
|
2017-08-21 00:04:26 -07:00
|
|
|
case head := <-subCh:
|
2017-08-22 22:43:19 -07:00
|
|
|
blockNumber = head.Number.Uint64()
|
2017-08-21 00:04:26 -07:00
|
|
|
// Ensure that mining is stable.
|
|
|
|
if head.Number.Uint64() < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block is generated by 2 seconds. We tolerate 1 second delay in consensus.
|
2017-08-22 22:43:19 -07:00
|
|
|
timer.Reset(3 * time.Second)
|
2017-08-21 00:04:26 -07:00
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:46:26 -07:00
|
|
|
// TODO: refactor with ConsensusMonitor
|
|
|
|
func (eth *ethereum) WaitForProposed(expectedAddress common.Address, timeout time.Duration) error {
|
|
|
|
cli := eth.NewClient()
|
|
|
|
|
|
|
|
subCh := make(chan *ethtypes.Header)
|
|
|
|
|
|
|
|
sub, err := cli.SubscribeNewHead(context.Background(), subCh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-sub.Err():
|
|
|
|
return err
|
|
|
|
case <-timer.C: // FIXME: this event may be missed
|
|
|
|
return errors.New("no result")
|
|
|
|
case head := <-subCh:
|
2017-08-25 00:14:18 -07:00
|
|
|
if GetProposer(head) == expectedAddress {
|
2017-08-24 02:46:26 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 00:03:54 -07:00
|
|
|
func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-23 00:03:54 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
2017-08-22 02:05:21 -07:00
|
|
|
defer client.Close()
|
2017-08-23 00:03:54 -07:00
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
|
|
|
for _ = range ticker.C {
|
|
|
|
infos, err := client.AdminPeers(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(infos) < expectedPeercount {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
ticker.Stop()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:05:21 -07:00
|
|
|
func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error {
|
2017-08-23 00:03:54 -07:00
|
|
|
var first *big.Int
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-23 00:03:54 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
2017-08-22 02:05:21 -07:00
|
|
|
defer client.Close()
|
2017-08-23 00:03:54 -07:00
|
|
|
|
2017-08-22 02:05:21 -07:00
|
|
|
var t time.Duration
|
|
|
|
if len(waitingTime) > 0 {
|
|
|
|
t = waitingTime[0]
|
|
|
|
} else {
|
|
|
|
t = 1 * time.Hour
|
|
|
|
}
|
2017-08-23 00:03:54 -07:00
|
|
|
|
2017-08-22 02:05:21 -07:00
|
|
|
timeout := time.After(t)
|
2017-08-23 00:03:54 -07:00
|
|
|
ticker := time.NewTicker(time.Millisecond * 500)
|
2017-08-22 02:05:21 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
2017-08-23 00:03:54 -07:00
|
|
|
ticker.Stop()
|
2017-08-22 02:05:21 -07:00
|
|
|
return ErrNoBlock
|
|
|
|
case <-ticker.C:
|
|
|
|
n, err := client.BlockNumber(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if first == nil {
|
|
|
|
first = new(big.Int).Set(n)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check if new blocks are getting generated
|
|
|
|
if new(big.Int).Sub(n, first).Int64() >= int64(num) {
|
|
|
|
ticker.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-23 00:03:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) WaitForBlockHeight(num int) error {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-23 00:03:54 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
2017-08-22 02:05:21 -07:00
|
|
|
defer client.Close()
|
2017-08-23 00:03:54 -07:00
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Millisecond * 500)
|
|
|
|
for _ = range ticker.C {
|
|
|
|
n, err := client.BlockNumber(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n.Int64() >= int64(num) {
|
|
|
|
ticker.Stop()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 01:09:59 -07:00
|
|
|
func (eth *ethereum) WaitForNoBlocks(num int, duration time.Duration) error {
|
|
|
|
var first *big.Int
|
|
|
|
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-24 01:09:59 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.After(duration)
|
|
|
|
tick := time.Tick(time.Millisecond * 500)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return nil
|
|
|
|
case <-tick:
|
|
|
|
n, err := client.BlockNumber(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if first == nil {
|
|
|
|
first = new(big.Int).Set(n)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check if new blocks are getting generated
|
|
|
|
if new(big.Int).Sub(n, first).Int64() > int64(num) {
|
|
|
|
return errors.New("generated more blocks than expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 00:03:54 -07:00
|
|
|
func (eth *ethereum) AddPeer(address string) error {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-23 00:03:54 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
return client.AddPeer(context.Background(), address)
|
|
|
|
}
|
|
|
|
|
2017-08-22 02:05:21 -07:00
|
|
|
func (eth *ethereum) StartMining() error {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-22 02:05:21 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
return client.StartMining(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) StopMining() error {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := eth.NewClient()
|
2017-08-22 02:05:21 -07:00
|
|
|
if client == nil {
|
|
|
|
return errors.New("failed to retrieve client")
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
return client.StopMining(context.Background())
|
|
|
|
}
|
|
|
|
|
2017-09-05 00:53:15 -07:00
|
|
|
func (eth *ethereum) DockerEnv() []string {
|
|
|
|
return eth.dockerEnv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) DockerBinds() []string {
|
|
|
|
return eth.dockerBinds
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:09:41 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-08 00:31:13 -07:00
|
|
|
func (eth *ethereum) showLog(context context.Context) {
|
2017-09-06 20:30:30 -07:00
|
|
|
if readCloser, err := eth.dockerClient.ContainerLogs(context, eth.containerID,
|
2017-08-08 00:31:13 -07:00
|
|
|
types.ContainerLogsOptions{ShowStderr: true, Follow: true}); err == nil {
|
|
|
|
defer readCloser.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, readCloser)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|