Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
don't dup nodekey gen in configure
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
Gregory Hill committed Jun 20, 2019
1 parent f62f930 commit d26fd0d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
85 changes: 40 additions & 45 deletions cmd/burrow/commands/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"io/ioutil"
"strings"

"github.com/hyperledger/burrow/consensus/tendermint"

"github.com/hyperledger/burrow/dump"

"github.com/hyperledger/burrow/config/deployment"
"github.com/hyperledger/burrow/config/source"
"github.com/hyperledger/burrow/consensus/tendermint"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/dump"
"github.com/hyperledger/burrow/execution"
"github.com/hyperledger/burrow/execution/state"
"github.com/hyperledger/burrow/genesis/spec"
Expand Down Expand Up @@ -43,10 +41,10 @@ func Configure(output Output) func(cmd *cli.Cmd) {
keysDir := cmd.StringOpt("keys-dir", "", "Directory where keys are stored")

configTemplateIn := cmd.StringsOpt("config-template-in", nil,
fmt.Sprintf("Go text/template input filename to generate config file specified with --config-out"))
"Go text/template input filename to generate config file specified with --config-out")

configTemplateOut := cmd.StringsOpt("config-out", nil,
"Go text/template output file. Template filename specified with --config-template-in")
"Go text/template output filename. Template filename specified with --config-template-in")

separateGenesisDoc := cmd.StringOpt("w separate-genesis-doc", "", "Emit a separate genesis doc as JSON or TOML")

Expand Down Expand Up @@ -107,7 +105,7 @@ func Configure(output Output) func(cmd *cli.Cmd) {
output.Fatalf("--config-template-in and --config-out must be specified the same number of times")
}

pkg := deployment.Config{}
pkg := deployment.Config{Keys: make(map[crypto.Address]deployment.Key)}

// Genesis Spec
if *genesisSpecOpt != "" {
Expand All @@ -134,8 +132,6 @@ func Configure(output Output) func(cmd *cli.Cmd) {
output.Fatalf("could get all keys: %v", err)
}

pkg = deployment.Config{Keys: make(map[crypto.Address]deployment.Key)}

for k := range allNames {
addr, err := crypto.AddressFromHexString(allNames[k])
if err != nil {
Expand Down Expand Up @@ -198,36 +194,6 @@ func Configure(output Output) func(cmd *cli.Cmd) {
conf.GenesisDoc.AppHash = st.Hash()
}

if conf.GenesisDoc != nil {
pkg.GenesisDoc = conf.GenesisDoc

for _, v := range conf.GenesisDoc.Validators {
nodeKey := tendermint.NewNodeKey()
nodeAddress, _ := crypto.AddressFromHexString(string(nodeKey.ID()))

cdc := amino.NewCodec()
cryptoAmino.RegisterAmino(cdc)
json, err := cdc.MarshalJSON(nodeKey)
if err != nil {
output.Fatalf("go-amino failed to json marshall private key: %v", err)
}
pkg.Keys[nodeAddress] = deployment.Key{Name: v.Name, Address: nodeAddress, KeyJSON: json}

pkg.Validators = append(pkg.Validators, deployment.Validator{
Name: v.Name,
Address: v.Address,
NodeAddress: nodeAddress,
})
}

for ind := range *configTemplateIn {
err := processTemplate(&pkg, (*configTemplateIn)[ind], (*configTemplateOut)[ind])
if err != nil {
output.Fatalf("could not template from %s to %s: %v", (*configTemplateIn)[ind], (*configTemplateOut)[ind], err)
}
}
}

// Logging
if *loggingOpt != "" {
ops := strings.Split(*loggingOpt, ",")
Expand Down Expand Up @@ -266,24 +232,54 @@ func Configure(output Output) func(cmd *cli.Cmd) {
conf.Tendermint.CreateEmptyBlocks = *emptyBlocksOpt
}

peers := make([]string, 0)
if conf.GenesisDoc != nil {
cdc := amino.NewCodec()
cryptoAmino.RegisterAmino(cdc)
pkg.GenesisDoc = conf.GenesisDoc

for _, val := range conf.GenesisDoc.Validators {
nodeKey := tendermint.NewNodeKey()
nodeAddress, _ := crypto.AddressFromHexString(string(nodeKey.ID()))

json, err := cdc.MarshalJSON(nodeKey)
if err != nil {
output.Fatalf("go-amino failed to json marshal private key: %v", err)
}
pkg.Keys[nodeAddress] = deployment.Key{Name: val.Name, Address: nodeAddress, KeyJSON: json}

pkg.Validators = append(pkg.Validators, deployment.Validator{
Name: val.Name,
Address: val.Address,
NodeAddress: nodeAddress,
})
}

for ind := range *configTemplateIn {
err := processTemplate(&pkg, (*configTemplateIn)[ind], (*configTemplateOut)[ind])
if err != nil {
output.Fatalf("could not template from %s to %s: %v", (*configTemplateIn)[ind], (*configTemplateOut)[ind], err)
}
}
}

if *pool {
peers := make([]string, 0)
for i := range conf.GenesisDoc.Validators {
for i, val := range pkg.Validators {
tmConf, err := conf.Tendermint.Config(fmt.Sprintf(".burrow%03d", i), conf.Execution.TimeoutFactor)
if err != nil {
output.Fatalf("could not obtain config for %03d: %v", i, err)
}
nodeKey, err := tendermint.EnsureNodeKey(tmConf.NodeKeyFile())
nodeKey := pkg.Keys[val.NodeAddress]
err = tendermint.WriteNodeKey(tmConf.NodeKeyFile(), nodeKey.KeyJSON)
if err != nil {
output.Fatalf("failed to create node key for %03d: %v", i, err)
}
peers = append(peers, fmt.Sprintf("tcp://%[email protected]:%d", nodeKey.ID(), 26656+i))
peers = append(peers, fmt.Sprintf("tcp://%[email protected]:%d", nodeKey.Address.String(), 26656+i))
}
for i, acc := range conf.GenesisDoc.Accounts {
// set stuff
conf.Address = &acc.Address
conf.Tendermint.PersistentPeers = strings.Join(peers, ",")

conf.BurrowDir = fmt.Sprintf(".burrow%03d", i)
conf.Tendermint.ListenHost = rpc.LocalHost
conf.Tendermint.ListenPort = fmt.Sprint(26656 + i)
Expand Down Expand Up @@ -317,7 +313,6 @@ func processTemplate(pkg *deployment.Config, templateIn, templateOut string) err
if err != nil {
return err
}
fmt.Println(templateIn)
output, err := pkg.Dump(templateIn, string(data))
if err != nil {
return err
Expand Down
24 changes: 17 additions & 7 deletions consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tendermint

import (
"encoding/json"
"io/ioutil"
"os"
"path"

Expand Down Expand Up @@ -111,6 +113,21 @@ func NewNodeInfo(ni p2p.DefaultNodeInfo) *NodeInfo {
}
}

func NewNodeKey() *p2p.NodeKey {
privKey := ed25519.GenPrivKey()
return &p2p.NodeKey{
PrivKey: privKey,
}
}

func WriteNodeKey(nodeKeyFile string, key json.RawMessage) error {
err := os.MkdirAll(path.Dir(nodeKeyFile), 0777)
if err != nil {
return err
}
return ioutil.WriteFile(nodeKeyFile, key, 0600)
}

func EnsureNodeKey(nodeKeyFile string) (*p2p.NodeKey, error) {
err := os.MkdirAll(path.Dir(nodeKeyFile), 0777)
if err != nil {
Expand All @@ -119,10 +136,3 @@ func EnsureNodeKey(nodeKeyFile string) (*p2p.NodeKey, error) {

return p2p.LoadOrGenNodeKey(nodeKeyFile)
}

func NewNodeKey() *p2p.NodeKey {
privKey := ed25519.GenPrivKey()
return &p2p.NodeKey{
PrivKey: privKey,
}
}

0 comments on commit d26fd0d

Please sign in to comment.