diff --git a/cmd/utils/utils.go b/common/utils.go similarity index 86% rename from cmd/utils/utils.go rename to common/utils.go index 74fa41ba..2f5c200d 100644 --- a/cmd/utils/utils.go +++ b/common/utils.go @@ -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 . -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 { diff --git a/container/blockchain.go b/container/blockchain.go index 4e00e7eb..4e99e2c7 100644 --- a/container/blockchain.go +++ b/container/blockchain.go @@ -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) } diff --git a/container/constellation.go b/container/constellation.go index ba8c0e39..11de8b90 100644 --- a/container/constellation.go +++ b/container/constellation.go @@ -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 diff --git a/container/ethereum.go b/container/ethereum.go index 1a0dbab2..fdc7fab5 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -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 } diff --git a/genesis/genesis.go b/genesis/genesis.go index a5f6a433..325500e5 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -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) } diff --git a/genesis/utils.go b/genesis/utils.go deleted file mode 100644 index de329b5f..00000000 --- a/genesis/utils.go +++ /dev/null @@ -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 . - -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 -}