Set node ID via an env. var

This commit is contained in:
Ivan Kuznetsov 2017-10-01 10:29:04 +07:00
parent 4a9f7be98c
commit 504b6c85bf
3 changed files with 10 additions and 10 deletions

8
cli.go
View File

@ -19,7 +19,7 @@ func (cli *CLI) printUsage() {
fmt.Println(" printchain - Print all the blocks of the blockchain") fmt.Println(" printchain - Print all the blocks of the blockchain")
fmt.Println(" reindexutxo - Rebuilds the UTXO set") fmt.Println(" reindexutxo - Rebuilds the UTXO set")
fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO") fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO")
fmt.Println(" startnode -node-id NODE_ID - Start a node with specified ID") fmt.Println(" startnode - Start a node with ID specified in NODE_ID env. var")
} }
func (cli *CLI) validateArgs() { func (cli *CLI) validateArgs() {
@ -47,7 +47,6 @@ func (cli *CLI) Run() {
sendFrom := sendCmd.String("from", "", "Source wallet address") sendFrom := sendCmd.String("from", "", "Source wallet address")
sendTo := sendCmd.String("to", "", "Destination wallet address") sendTo := sendCmd.String("to", "", "Destination wallet address")
sendAmount := sendCmd.Int("amount", 0, "Amount to send") sendAmount := sendCmd.Int("amount", 0, "Amount to send")
startNodeID := startNodeCmd.Int("node-id", 0, "Node ID")
switch os.Args[1] { switch os.Args[1] {
case "getbalance": case "getbalance":
@ -137,10 +136,11 @@ func (cli *CLI) Run() {
} }
if startNodeCmd.Parsed() { if startNodeCmd.Parsed() {
if *startNodeID == 0 { nodeID := os.Getenv("NODE_ID")
if nodeID == "" {
startNodeCmd.Usage() startNodeCmd.Usage()
os.Exit(1) os.Exit(1)
} }
cli.startNode(*startNodeID) cli.startNode(nodeID)
} }
} }

View File

@ -2,7 +2,7 @@ package main
import "fmt" import "fmt"
func (cli *CLI) startNode(nodeID int) { func (cli *CLI) startNode(nodeID string) {
fmt.Printf("Starting node %d\n", nodeID) fmt.Printf("Starting node %s\n", nodeID)
StartServer(nodeID) StartServer(nodeID)
} }

View File

@ -11,7 +11,7 @@ import (
) )
const protocol = "tcp" const protocol = "tcp"
const dnsNodeID = 3000 const dnsNodeID = "3000"
const nodeVersion = 1 const nodeVersion = 1
const commandLength = 12 const commandLength = 12
@ -149,8 +149,8 @@ func handleConnection(conn net.Conn) {
} }
// StartServer starts a node // StartServer starts a node
func StartServer(nodeID int) { func StartServer(nodeID string) {
nodeAddress = fmt.Sprintf("localhost:%d", nodeID) nodeAddress = fmt.Sprintf("localhost:%s", nodeID)
ln, err := net.Listen(protocol, nodeAddress) ln, err := net.Listen(protocol, nodeAddress)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -158,7 +158,7 @@ func StartServer(nodeID int) {
defer ln.Close() defer ln.Close()
if nodeID != dnsNodeID { if nodeID != dnsNodeID {
sendVersion(fmt.Sprintf("localhost:%d", dnsNodeID)) sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID))
} }
for { for {