2017-08-17 03:04:35 -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 (
|
2017-08-18 00:24:35 -07:00
|
|
|
"context"
|
2017-08-17 03:04:35 -07:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-08-21 21:45:32 -07:00
|
|
|
"time"
|
2017-08-17 03:04:35 -07:00
|
|
|
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/phayes/freeport"
|
|
|
|
|
|
|
|
"github.com/getamis/istanbul-tools/genesis"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Blockchain interface {
|
2017-08-21 02:17:52 -07:00
|
|
|
AddValidators(numOfValidators int) ([]Ethereum, error)
|
2017-08-21 21:45:32 -07:00
|
|
|
EnsureConsensusWorking(geths []Ethereum, t time.Duration) error
|
2017-08-22 00:44:43 -07:00
|
|
|
Start(bool) error
|
2017-08-21 20:26:04 -07:00
|
|
|
Stop(bool) error
|
2017-08-17 03:04:35 -07:00
|
|
|
Validators() []Ethereum
|
|
|
|
Finalize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockchain(numOfValidators int, options ...Option) (bc *blockchain) {
|
2017-08-21 02:17:52 -07:00
|
|
|
bc = &blockchain{opts: options}
|
2017-08-17 03:04:35 -07:00
|
|
|
|
2017-08-18 00:24:35 -07:00
|
|
|
var err error
|
|
|
|
bc.dockerClient, err = client.NewEnvClient()
|
2017-08-17 03:04:35 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot connect to Docker daemon, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-21 02:17:52 -07:00
|
|
|
bc.addValidators(numOfValidators)
|
2017-08-17 03:04:35 -07:00
|
|
|
return bc
|
|
|
|
}
|
|
|
|
|
2017-08-22 22:59:37 -07:00
|
|
|
func NewDefaultBlockchain(numOfValidators int) (bc *blockchain) {
|
|
|
|
return NewBlockchain(numOfValidators,
|
|
|
|
ImageRepository("quay.io/amis/geth"),
|
|
|
|
ImageTag("istanbul_develop"),
|
|
|
|
DataDir("/data"),
|
|
|
|
WebSocket(),
|
|
|
|
WebSocketAddress("0.0.0.0"),
|
|
|
|
WebSocketAPI("admin,eth,net,web3,personal,miner,istanbul"),
|
|
|
|
WebSocketOrigin("*"),
|
|
|
|
NAT("any"),
|
|
|
|
NoDiscover(),
|
|
|
|
Etherbase("1a9afb711302c5f83b5902843d1c007a1a137632"),
|
|
|
|
Mine(),
|
|
|
|
Logging(false),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-17 03:04:35 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type blockchain struct {
|
2017-08-20 21:04:05 -07:00
|
|
|
dockerClient *client.Client
|
|
|
|
genesisFile string
|
|
|
|
validators []Ethereum
|
2017-08-21 02:17:52 -07:00
|
|
|
opts []Option
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) AddValidators(numOfValidators int) ([]Ethereum, error) {
|
|
|
|
// TODO: need a lock
|
|
|
|
lastLen := len(bc.validators)
|
|
|
|
bc.addValidators(numOfValidators)
|
|
|
|
|
|
|
|
newValidators := bc.validators[lastLen:]
|
|
|
|
if err := bc.start(newValidators); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// propose new validators as validator in consensus
|
|
|
|
for _, v := range bc.validators[:lastLen] {
|
|
|
|
istClient := v.NewIstanbulClient()
|
|
|
|
for _, newV := range newValidators {
|
|
|
|
if err := istClient.ProposeValidator(context.Background(), newV.Address(), true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:44:43 -07:00
|
|
|
if err := bc.connectAll(true); err != nil {
|
2017-08-21 02:17:52 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newValidators, nil
|
2017-08-17 03:04:35 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 21:45:32 -07:00
|
|
|
func (bc *blockchain) EnsureConsensusWorking(geths []Ethereum, t time.Duration) error {
|
|
|
|
errCh := make(chan error, len(geths))
|
|
|
|
quitCh := make(chan struct{}, len(geths))
|
|
|
|
for _, geth := range geths {
|
|
|
|
go geth.ConsensusMonitor(errCh, quitCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.NewTimer(t)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-timeout.C:
|
|
|
|
for i := 0; i < len(geths); i++ {
|
|
|
|
quitCh <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:44:43 -07:00
|
|
|
func (bc *blockchain) Start(strong bool) error {
|
2017-08-21 02:17:52 -07:00
|
|
|
if err := bc.start(bc.validators); err != nil {
|
|
|
|
return err
|
2017-08-17 03:04:35 -07:00
|
|
|
}
|
2017-08-22 00:44:43 -07:00
|
|
|
return bc.connectAll(strong)
|
2017-08-17 03:04:35 -07:00
|
|
|
}
|
|
|
|
|
2017-08-21 20:26:04 -07:00
|
|
|
func (bc *blockchain) Stop(force bool) error {
|
2017-08-17 03:04:35 -07:00
|
|
|
for _, v := range bc.validators {
|
2017-08-21 20:26:04 -07:00
|
|
|
if err := v.Stop(); err != nil && !force {
|
2017-08-17 03:04:35 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) Finalize() {
|
|
|
|
os.RemoveAll(filepath.Dir(bc.genesisFile))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) Validators() []Ethereum {
|
|
|
|
return bc.validators
|
|
|
|
}
|
2017-08-18 00:24:35 -07:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-21 02:17:52 -07:00
|
|
|
func (bc *blockchain) addValidators(numOfValidators int) error {
|
|
|
|
keys, addrs := generateKeys(numOfValidators)
|
|
|
|
bc.setupGenesis(addrs)
|
|
|
|
bc.setupValidators(keys, bc.opts...)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:44:43 -07:00
|
|
|
func (bc *blockchain) connectAll(strong bool) error {
|
2017-08-21 02:17:52 -07:00
|
|
|
for i, v := range bc.validators {
|
|
|
|
istClient := v.NewIstanbulClient()
|
|
|
|
for j, v := range bc.validators {
|
2017-08-22 00:44:43 -07:00
|
|
|
if (strong && j > i) || (!strong && j == i+1) {
|
|
|
|
err := istClient.AddPeer(context.Background(), v.NodeAddress())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-21 02:17:52 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 00:24:35 -07:00
|
|
|
}
|
2017-08-21 02:17:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) setupGenesis(addrs []common.Address) {
|
|
|
|
if bc.genesisFile == "" {
|
|
|
|
setupDir, err := generateRandomDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to create setup dir", err)
|
|
|
|
}
|
|
|
|
err = genesis.Save(setupDir, genesis.New(addrs))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to save genesis", err)
|
|
|
|
}
|
|
|
|
bc.genesisFile = filepath.Join(setupDir, genesis.FileName)
|
2017-08-18 00:24:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) setupValidators(keys []*ecdsa.PrivateKey, options ...Option) {
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
var opts []Option
|
|
|
|
opts = append(opts, options...)
|
|
|
|
|
|
|
|
// Host data directory
|
|
|
|
dataDir, err := generateRandomDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to create data dir", err)
|
|
|
|
}
|
|
|
|
opts = append(opts, HostDataDir(dataDir))
|
|
|
|
opts = append(opts, HostPort(freeport.GetPort()))
|
|
|
|
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
|
|
|
|
opts = append(opts, Key(keys[i]))
|
|
|
|
|
|
|
|
geth := NewEthereum(
|
|
|
|
bc.dockerClient,
|
|
|
|
opts...,
|
|
|
|
)
|
|
|
|
|
|
|
|
err = geth.Init(bc.genesisFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to init genesis", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bc.validators = append(bc.validators, geth)
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 01:00:59 -07:00
|
|
|
|
2017-08-21 02:17:52 -07:00
|
|
|
func (bc *blockchain) start(validators []Ethereum) error {
|
|
|
|
for _, v := range validators {
|
|
|
|
if err := v.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *blockchain) stop(validators []Ethereum) error {
|
|
|
|
for _, v := range validators {
|
|
|
|
if err := v.Stop(); err != nil {
|
|
|
|
return err
|
2017-08-19 01:00:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|