container, genesis: support docker network and refactoring
This commit is contained in:
parent
e9e78036e3
commit
1cb353604d
|
@ -17,15 +17,20 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/phayes/freeport"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"github.com/getamis/istanbul-tools/genesis"
|
||||
)
|
||||
|
@ -38,63 +43,18 @@ type Blockchain interface {
|
|||
}
|
||||
|
||||
func NewBlockchain(numOfValidators int, options ...Option) (bc *blockchain) {
|
||||
var keys []*ecdsa.PrivateKey
|
||||
var addrs []common.Address
|
||||
|
||||
bc = &blockchain{}
|
||||
|
||||
for i := 0; i < numOfValidators; i++ {
|
||||
key, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't generate key: " + err.Error())
|
||||
}
|
||||
keys = append(keys, key)
|
||||
|
||||
addr := crypto.PubkeyToAddress(key.PublicKey)
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
dockerClient, err := client.NewEnvClient()
|
||||
var err error
|
||||
bc.dockerClient, err = client.NewEnvClient()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot connect to Docker daemon, err: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < numOfValidators; i++ {
|
||||
opts := make([]Option, len(options))
|
||||
copy(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(
|
||||
dockerClient,
|
||||
opts...,
|
||||
)
|
||||
|
||||
err = geth.Init(bc.genesisFile)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to init genesis", err)
|
||||
}
|
||||
|
||||
bc.validators = append(bc.validators, geth)
|
||||
}
|
||||
bc.setupNetwork()
|
||||
keys, addrs := generateKeys(numOfValidators)
|
||||
bc.setupGenesis(addrs)
|
||||
bc.setupValidators(keys, options...)
|
||||
|
||||
return bc
|
||||
}
|
||||
|
@ -102,15 +62,24 @@ func NewBlockchain(numOfValidators int, options ...Option) (bc *blockchain) {
|
|||
// ----------------------------------------------------------------------------
|
||||
|
||||
type blockchain struct {
|
||||
genesisFile string
|
||||
validators []Ethereum
|
||||
dockerClient *client.Client
|
||||
dockerNetworkID string
|
||||
netClass string
|
||||
genesisFile string
|
||||
validators []Ethereum
|
||||
}
|
||||
|
||||
func (bc *blockchain) Start() error {
|
||||
for _, v := range bc.validators {
|
||||
for i, v := range bc.validators {
|
||||
if err := v.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := bc.dockerClient.NetworkConnect(context.Background(), bc.dockerNetworkID, v.ContainerID(), &network.EndpointSettings{
|
||||
IPAddress: fmt.Sprintf(bc.netClass+".%d", i+1),
|
||||
}); err != nil {
|
||||
log.Printf("Failed to connect to network '%s', %v", bc.dockerNetworkID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -128,8 +97,73 @@ func (bc *blockchain) Stop() error {
|
|||
|
||||
func (bc *blockchain) Finalize() {
|
||||
os.RemoveAll(filepath.Dir(bc.genesisFile))
|
||||
|
||||
bc.dockerClient.NetworkRemove(context.Background(), bc.dockerNetworkID)
|
||||
}
|
||||
|
||||
func (bc *blockchain) Validators() []Ethereum {
|
||||
return bc.validators
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
func (bc *blockchain) setupNetwork() {
|
||||
name := "net" + uuid.NewV4().String()
|
||||
bc.netClass = fmt.Sprintf("172.16.%d", rand.Uint32()%255)
|
||||
resp, err := bc.dockerClient.NetworkCreate(context.Background(), name, types.NetworkCreate{
|
||||
IPAM: &network.IPAM{
|
||||
Config: []network.IPAMConfig{
|
||||
network.IPAMConfig{
|
||||
Subnet: bc.netClass + ".0/24",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Failed to setup blockchain network,", err)
|
||||
}
|
||||
|
||||
bc.dockerNetworkID = resp.ID
|
||||
}
|
||||
|
||||
func (bc *blockchain) setupGenesis(addrs []common.Address) {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,10 @@ func (eth *ethereum) Image() string {
|
|||
return eth.imageRepository + ":" + eth.imageTag
|
||||
}
|
||||
|
||||
func (eth *ethereum) ContainerID() string {
|
||||
return eth.containerID
|
||||
}
|
||||
|
||||
func (eth *ethereum) Host() string {
|
||||
var host string
|
||||
daemonHost := eth.client.DaemonHost()
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
@ -36,6 +37,7 @@ import (
|
|||
"github.com/docker/go-connections/nat"
|
||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||
|
||||
"github.com/getamis/istanbul-tools/genesis"
|
||||
)
|
||||
|
@ -50,6 +52,9 @@ type Ethereum interface {
|
|||
Start() error
|
||||
Stop() error
|
||||
|
||||
NodeAddress() string
|
||||
|
||||
ContainerID() string
|
||||
Host() string
|
||||
NewClient() *ethclient.Client
|
||||
}
|
||||
|
@ -95,6 +100,8 @@ type ethereum struct {
|
|||
wsPort string
|
||||
hostName string
|
||||
containerID string
|
||||
ipAddress string
|
||||
node *discover.Node
|
||||
|
||||
imageRepository string
|
||||
imageTag string
|
||||
|
@ -116,6 +123,7 @@ func (eth *ethereum) Init(genesisFile string) error {
|
|||
if eth.dataDir != "" {
|
||||
binds = append(binds, eth.dataDir+":"+utils.DataDirFlag.Value.Value)
|
||||
}
|
||||
|
||||
resp, err := eth.client.ContainerCreate(context.Background(),
|
||||
&container.Config{
|
||||
Image: eth.Image(),
|
||||
|
@ -134,12 +142,6 @@ func (eth *ethereum) Init(genesisFile string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if eth.logging {
|
||||
go eth.showLog(context.Background())
|
||||
}
|
||||
}()
|
||||
|
||||
id := resp.ID
|
||||
|
||||
if err := eth.client.ContainerStart(context.Background(), id, types.ContainerStartOptions{}); err != nil {
|
||||
|
@ -147,10 +149,7 @@ func (eth *ethereum) Init(genesisFile string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
resC, errC := eth.client.ContainerWait(ctx, id, container.WaitConditionNotRunning)
|
||||
resC, errC := eth.client.ContainerWait(context.Background(), id, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case <-resC:
|
||||
case <-errC:
|
||||
|
@ -158,6 +157,10 @@ func (eth *ethereum) Init(genesisFile string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if eth.logging {
|
||||
eth.showLog(context.Background())
|
||||
}
|
||||
|
||||
return eth.client.ContainerRemove(context.Background(), id,
|
||||
types.ContainerRemoveOptions{
|
||||
Force: true,
|
||||
|
@ -254,6 +257,21 @@ func (eth *ethereum) Start() error {
|
|||
return errors.New("Failed to start geth")
|
||||
}
|
||||
|
||||
containerJSON, err := eth.client.ContainerInspect(context.Background(), eth.containerID)
|
||||
if err != nil {
|
||||
log.Print("Failed to inspect container,", err)
|
||||
return err
|
||||
}
|
||||
|
||||
eth.ipAddress = containerJSON.NetworkSettings.IPAddress
|
||||
if eth.key != nil {
|
||||
eth.node = discover.NewNode(
|
||||
discover.PubkeyID(ð.key.PublicKey),
|
||||
net.ParseIP(containerJSON.NetworkSettings.IPAddress),
|
||||
0,
|
||||
uint16(utils.ListenPortFlag.Value))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -313,6 +331,14 @@ func (eth *ethereum) NewClient() *ethclient.Client {
|
|||
return client
|
||||
}
|
||||
|
||||
func (eth *ethereum) NodeAddress() string {
|
||||
if eth.node != nil {
|
||||
return eth.node.String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
func (eth *ethereum) showLog(context context.Context) {
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
|
||||
"github.com/getamis/go-ethereum/cmd/utils"
|
||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||
)
|
||||
|
||||
type Option func(*ethereum)
|
||||
|
|
|
@ -20,10 +20,13 @@ import (
|
|||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/getamis/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
|
@ -33,6 +36,10 @@ const (
|
|||
nodekeyFileName = "nodekey"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UTC().UnixNano())
|
||||
}
|
||||
|
||||
func generateRandomDir() (string, error) {
|
||||
err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700)
|
||||
if err != nil {
|
||||
|
@ -48,6 +55,21 @@ func generateRandomDir() (string, error) {
|
|||
return instanceDir, nil
|
||||
}
|
||||
|
||||
func generateKeys(num int) (keys []*ecdsa.PrivateKey, addrs []common.Address) {
|
||||
for i := 0; i < num; i++ {
|
||||
key, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't generate key: " + err.Error())
|
||||
}
|
||||
keys = append(keys, key)
|
||||
|
||||
addr := crypto.PubkeyToAddress(key.PublicKey)
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
return keys, addrs
|
||||
}
|
||||
|
||||
func saveNodeKey(key *ecdsa.PrivateKey, dataDir string) error {
|
||||
keyDir := filepath.Join(dataDir, clientIdentifier)
|
||||
if err := os.MkdirAll(keyDir, 0700); err != nil {
|
||||
|
|
|
@ -19,10 +19,10 @@ package genesis
|
|||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/big"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"log"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
|
||||
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue