Make Tendermint genesis agnostic; wait for file to start node

This commit is contained in:
Jae Kwon
2015-10-18 07:20:05 -07:00
parent 313e25adf1
commit 26905b1260
8 changed files with 134 additions and 102 deletions

View File

@ -25,4 +25,6 @@ WARNING: THIS STEP WILL GIVE CONTROL OF THE CURRENT USER TO THE DEV TEAM.
### Install/Update Tendermint ### Install/Update Tendermint
go get -u github.com/tendermint/tendermint/cmd/tendermint go get -u github.com/tendermint/tendermint/cmd/tendermint
tendermint node mkdir -p ~/.tendermint
cp $GOPATH/src/github.com/tendermint/tendermint/config/tendermint/genesis.json ~/.tendermint/
tendermint node --seeds="goldenalchemist.chaintest.net:46656"

View File

@ -24,7 +24,7 @@ func parseFlags(config cfg.Config, args []string) {
flags.BoolVar(&printHelp, "help", false, "Print this help message.") flags.BoolVar(&printHelp, "help", false, "Print this help message.")
flags.StringVar(&moniker, "moniker", config.GetString("moniker"), "Node Name") flags.StringVar(&moniker, "moniker", config.GetString("moniker"), "Node Name")
flags.StringVar(&nodeLaddr, "node_laddr", config.GetString("node_laddr"), "Node listen address. (0.0.0.0:0 means any interface, any port)") flags.StringVar(&nodeLaddr, "node_laddr", config.GetString("node_laddr"), "Node listen address. (0.0.0.0:0 means any interface, any port)")
flags.StringVar(&seeds, "seeds", config.GetString("seeds"), "Comma delimited seed nodes") flags.StringVar(&seeds, "seeds", config.GetString("seeds"), "Comma delimited host:port seed nodes")
flags.BoolVar(&fastSync, "fast_sync", config.GetBool("fast_sync"), "Fast blockchain syncing") flags.BoolVar(&fastSync, "fast_sync", config.GetBool("fast_sync"), "Fast blockchain syncing")
flags.BoolVar(&skipUPNP, "skip_upnp", config.GetBool("skip_upnp"), "Skip UPNP configuration") flags.BoolVar(&skipUPNP, "skip_upnp", config.GetBool("skip_upnp"), "Skip UPNP configuration")
flags.StringVar(&rpcLaddr, "rpc_laddr", config.GetString("rpc_laddr"), "RPC listen address. Port required") flags.StringVar(&rpcLaddr, "rpc_laddr", config.GetString("rpc_laddr"), "RPC listen address. Port required")

View File

@ -16,13 +16,14 @@ func main() {
fmt.Println(`Tendermint fmt.Println(`Tendermint
Commands: Commands:
node Run the tendermint node node Run the tendermint node
gen_account Generate new account keypair show_validator Show this node's validator info
gen_validator Generate new validator keypair gen_account Generate new account keypair
get_account Get account balance gen_validator Generate new validator keypair
send_tx Sign and publish a SendTx get_account Get account balance
probe_upnp Test UPnP functionality send_tx Sign and publish a SendTx
version Show version info probe_upnp Test UPnP functionality
version Show version info
`) `)
return return
} }
@ -35,6 +36,8 @@ Commands:
switch args[0] { switch args[0] {
case "node": case "node":
node.RunNode() node.RunNode()
case "show_validator":
show_validator()
case "gen_account": case "gen_account":
gen_account() gen_account()
case "gen_validator": case "gen_validator":

View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/wire"
)
func show_validator() {
privValidatorFile := config.GetString("priv_validator_file")
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
fmt.Println(string(wire.JSONBytes(privValidator.PubKey)))
}

View File

@ -25,7 +25,6 @@ func initTMRoot(rootDir string) {
EnsureDir(rootDir) EnsureDir(rootDir)
configFilePath := path.Join(rootDir, "config.toml") configFilePath := path.Join(rootDir, "config.toml")
genesisFilePath := path.Join(rootDir, "genesis.json")
// Write default config file if missing. // Write default config file if missing.
if !FileExists(configFilePath) { if !FileExists(configFilePath) {
@ -33,9 +32,6 @@ func initTMRoot(rootDir string) {
// moniker := cfg.Prompt("Type hostname: ", "anonymous") // moniker := cfg.Prompt("Type hostname: ", "anonymous")
MustWriteFile(configFilePath, []byte(defaultConfig("anonymous"))) MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")))
} }
if !FileExists(genesisFilePath) {
MustWriteFile(genesisFilePath, []byte(defaultGenesis))
}
} }
func GetConfig(rootDir string) cfg.Config { func GetConfig(rootDir string) cfg.Config {
@ -88,7 +84,7 @@ var defaultConfigTmpl = `# This is a TOML config file.
moniker = "__MONIKER__" moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656" node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656" seeds = ""
fast_sync = true fast_sync = true
db_backend = "leveldb" db_backend = "leveldb"
log_level = "notice" log_level = "notice"
@ -99,79 +95,3 @@ func defaultConfig(moniker string) (defaultConfig string) {
defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1) defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
return return
} }
var defaultGenesis = `{
"chain_id": "tendermint_testnet_11.c",
"accounts": [
{
"address": "9FCBA7F840A0BFEBBE755E853C9947270A912D04",
"amount": 1991999998000000
},
{
"address": "964B1493BBE3312278B7DEB94C39149F7899A345",
"amount": 100000000000000
},
{
"address": "B9FA4AB462B9C6BF6A62DB4AE77C9E7087209A04",
"amount": 1000000000000
},
{
"address": "F171824590D69386F709E7B6704B369C5A370D60",
"amount": 1000000000000
},
{
"address": "56EFE746A13D9A6054AC89C3E2A361C2DB8B9EAE",
"amount": 1000000000000
},
{
"address": "7C2E032D8407EDF66A04D88CF0E1D9B15D98AE2D",
"amount": 1000000000000
},
{
"address": "636EF5823E082AD66EBC203FD4DFB1031F0C61CA",
"amount": 1000000000000
},
{
"address": "9008419E6351360A59B124E707E4CA2A5BFB9BE6",
"amount": 1000000000000
},
{
"address": "C78F48919B8A4030AD3E5ED643F8D2302E41953D",
"amount": 1000000000000
},
{
"address": "5290AC90CE2422DDC3F91F6A246F7E3C542EA51A",
"amount": 1000000000000
},
{
"address": "A88A61069B6660F30F65E8786AFDD4F1D8F625E9",
"amount": 1000000
},
{
"address": "EE2EE9247973B4AFC3867CFE5F415410AC251B61",
"amount": 1000000
}
],
"validators": [
{
"pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
"amount": 100000000000
},
{
"pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
"amount": 100000000000
},
{
"pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
"amount": 100000000000
},
{
"pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
"amount": 100000000000
},
{
"pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
"amount": 100000000000
}
]
}`

View File

@ -0,0 +1,75 @@
{
"chain_id": "tendermint_testnet_11.c",
"accounts": [
{
"address": "9FCBA7F840A0BFEBBE755E853C9947270A912D04",
"amount": 1991999998000000
},
{
"address": "964B1493BBE3312278B7DEB94C39149F7899A345",
"amount": 100000000000000
},
{
"address": "B9FA4AB462B9C6BF6A62DB4AE77C9E7087209A04",
"amount": 1000000000000
},
{
"address": "F171824590D69386F709E7B6704B369C5A370D60",
"amount": 1000000000000
},
{
"address": "56EFE746A13D9A6054AC89C3E2A361C2DB8B9EAE",
"amount": 1000000000000
},
{
"address": "7C2E032D8407EDF66A04D88CF0E1D9B15D98AE2D",
"amount": 1000000000000
},
{
"address": "636EF5823E082AD66EBC203FD4DFB1031F0C61CA",
"amount": 1000000000000
},
{
"address": "9008419E6351360A59B124E707E4CA2A5BFB9BE6",
"amount": 1000000000000
},
{
"address": "C78F48919B8A4030AD3E5ED643F8D2302E41953D",
"amount": 1000000000000
},
{
"address": "5290AC90CE2422DDC3F91F6A246F7E3C542EA51A",
"amount": 1000000000000
},
{
"address": "A88A61069B6660F30F65E8786AFDD4F1D8F625E9",
"amount": 1000000
},
{
"address": "EE2EE9247973B4AFC3867CFE5F415410AC251B61",
"amount": 1000000
}
],
"validators": [
{
"pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
"amount": 100000000000
},
{
"pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
"amount": 100000000000
},
{
"pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
"amount": 100000000000
},
{
"pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
"amount": 100000000000
},
{
"pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
"amount": 100000000000
}
]
}

View File

@ -5,7 +5,6 @@ import (
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -76,18 +75,8 @@ func NewNode() *Node {
config.Set("chain_id", state.ChainID) config.Set("chain_id", state.ChainID)
// Get PrivValidator // Get PrivValidator
var privValidator *types.PrivValidator
privValidatorFile := config.GetString("priv_validator_file") privValidatorFile := config.GetString("priv_validator_file")
if _, err := os.Stat(privValidatorFile); err == nil { privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
privValidator = types.LoadPrivValidator(privValidatorFile)
log.Notice("Loaded PrivValidator",
"file", privValidatorFile, "privValidator", privValidator)
} else {
privValidator = types.GenPrivValidator()
privValidator.SetFile(privValidatorFile)
privValidator.Save()
log.Notice("Generated PrivValidator", "file", privValidatorFile)
}
// Generate node PrivKey // Generate node PrivKey
privKey := acm.GenPrivKeyEd25519() privKey := acm.GenPrivKeyEd25519()
@ -298,6 +287,19 @@ func makeNodeInfo(sw *p2p.Switch, privKey acm.PrivKeyEd25519) *types.NodeInfo {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
func RunNode() { func RunNode() {
// Wait until the genesis doc becomes available
genDocFile := config.GetString("genesis_file")
if !FileExists(genDocFile) {
log.Notice(Fmt("Waiting for genesis file %v...", genDocFile))
for {
time.Sleep(time.Second)
if FileExists(genDocFile) {
break
}
}
}
// Create & start node // Create & start node
n := NewNode() n := NewNode()
l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr")) l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"))

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"os"
"sync" "sync"
acm "github.com/tendermint/tendermint/account" acm "github.com/tendermint/tendermint/account"
@ -79,6 +80,21 @@ func LoadPrivValidator(filePath string) *PrivValidator {
return privVal return privVal
} }
func LoadOrGenPrivValidator(filePath string) *PrivValidator {
var privValidator *PrivValidator
if _, err := os.Stat(filePath); err == nil {
privValidator = LoadPrivValidator(filePath)
log.Notice("Loaded PrivValidator",
"file", filePath, "privValidator", privValidator)
} else {
privValidator = GenPrivValidator()
privValidator.SetFile(filePath)
privValidator.Save()
log.Notice("Generated PrivValidator", "file", filePath)
}
return privValidator
}
func (privVal *PrivValidator) SetFile(filePath string) { func (privVal *PrivValidator) SetFile(filePath string) {
privVal.mtx.Lock() privVal.mtx.Lock()
defer privVal.mtx.Unlock() defer privVal.mtx.Unlock()