common, container, genesis: utilities refactoring

This commit is contained in:
Alan Chen 2017-09-01 14:53:35 +08:00
parent b226460511
commit 83e681c947
6 changed files with 15 additions and 72 deletions

View File

@ -14,7 +14,7 @@
// 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 utils
package common
import (
"crypto/ecdsa"
@ -52,7 +52,7 @@ func GenerateRandomDir() (string, error) {
func GenerateKeys(num int) (keys []*ecdsa.PrivateKey, nodekeys []string, addrs []common.Address) {
for i := 0; i < num; i++ {
nodekey := RandomHex()
nodekey := RandomHex()[2:]
nodekeys = append(nodekeys, nodekey)
key, err := crypto.HexToECDSA(nodekey)
@ -68,21 +68,6 @@ func GenerateKeys(num int) (keys []*ecdsa.PrivateKey, nodekeys []string, addrs [
return keys, nodekeys, addrs
}
// 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 {

View File

@ -30,7 +30,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/phayes/freeport"
"github.com/getamis/istanbul-tools/cmd/utils"
istcommon "github.com/getamis/istanbul-tools/common"
"github.com/getamis/istanbul-tools/genesis"
)
@ -138,7 +138,7 @@ func NewDefaultBlockchainWithFaulty(network *DockerNetwork, numOfNormal int, num
log.Fatalf("Failed to get free ip addresses, err: %v", err)
}
keys, _, addrs := utils.GenerateKeys(totalNodes)
keys, _, addrs := istcommon.GenerateKeys(totalNodes)
bc.setupGenesis(addrs)
// Create normal validators
bc.opts = normalOpts
@ -320,7 +320,7 @@ func (bc *blockchain) CreateNodes(num int, options ...Option) (nodes []Ethereum,
opts = append(opts, options...)
// Host data directory
dataDir, err := utils.GenerateRandomDir()
dataDir, err := istcommon.GenerateRandomDir()
if err != nil {
log.Println("Failed to create data dir", err)
return nil, err
@ -354,7 +354,7 @@ func (bc *blockchain) addValidators(numOfValidators int) error {
if err != nil {
return err
}
keys, _, addrs := utils.GenerateKeys(numOfValidators)
keys, _, addrs := istcommon.GenerateKeys(numOfValidators)
bc.setupGenesis(addrs)
bc.setupValidators(ips, keys, bc.opts...)
@ -395,7 +395,7 @@ func (bc *blockchain) setupValidators(ips []net.IP, keys []*ecdsa.PrivateKey, op
opts = append(opts, options...)
// Host data directory
dataDir, err := utils.GenerateRandomDir()
dataDir, err := istcommon.GenerateRandomDir()
if err != nil {
log.Fatal("Failed to create data dir", err)
}

View File

@ -33,6 +33,8 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
istcommon "github.com/getamis/istanbul-tools/common"
)
//TODO: refactor this with ethereum options?
@ -192,7 +194,7 @@ func (ct *constellation) Image() string {
func (ct *constellation) GenerateKey() (localWorkDir string, err error) {
// Generate empty password file
ct.localWorkDir, err = generateRandomDir()
ct.localWorkDir, err = istcommon.GenerateRandomDir()
if err != nil {
log.Printf("Failed to generate working dir, err: :%v\n", err)
return "", err

View File

@ -43,7 +43,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/p2p/discover"
istutils "github.com/getamis/istanbul-tools/cmd/utils"
istcommon "github.com/getamis/istanbul-tools/common"
"github.com/getamis/istanbul-tools/genesis"
"github.com/getamis/istanbul-tools/istclient"
)
@ -147,7 +147,7 @@ type ethereum struct {
}
func (eth *ethereum) Init(genesisFile string) error {
if err := istutils.SaveNodeKey(eth.key, eth.dataDir); err != nil {
if err := istcommon.SaveNodeKey(eth.key, eth.dataDir); err != nil {
log.Fatal("Failed to save nodekey", err)
return err
}

View File

@ -30,6 +30,8 @@ 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/common"
)
const (
@ -74,7 +76,7 @@ func NewFileAt(dir string, isQuorum bool, options ...Option) string {
}
func NewFile(isQuorum bool, options ...Option) string {
dir, err := generateRandomDir()
dir, err := common.GenerateRandomDir()
if err != nil {
log.Fatalf("Failed to create random directory, err: %v", err)
}

View File

@ -1,46 +0,0 @@
// 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 genesis
import (
"fmt"
"log"
"os"
"path/filepath"
uuid "github.com/satori/go.uuid"
)
const (
defaultLocalDir = "/tmp/gdata"
clientIdentifier = "genesis"
)
func generateRandomDir() (string, error) {
err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700)
if err != nil {
log.Fatal(err)
}
instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String()))
if err := os.MkdirAll(instanceDir, 0700); err != nil {
log.Println(fmt.Sprintf("Failed to create instance dir: %v", err))
return "", err
}
return instanceDir, nil
}