diff --git a/cli.go b/cli.go index 366a7a6..df4af02 100644 --- a/cli.go +++ b/cli.go @@ -19,7 +19,7 @@ func (cli *CLI) printUsage() { fmt.Println(" printchain - Print all the blocks of the blockchain") 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(" 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() { @@ -47,7 +47,6 @@ func (cli *CLI) Run() { sendFrom := sendCmd.String("from", "", "Source wallet address") sendTo := sendCmd.String("to", "", "Destination wallet address") sendAmount := sendCmd.Int("amount", 0, "Amount to send") - startNodeID := startNodeCmd.Int("node-id", 0, "Node ID") switch os.Args[1] { case "getbalance": @@ -137,10 +136,11 @@ func (cli *CLI) Run() { } if startNodeCmd.Parsed() { - if *startNodeID == 0 { + nodeID := os.Getenv("NODE_ID") + if nodeID == "" { startNodeCmd.Usage() os.Exit(1) } - cli.startNode(*startNodeID) + cli.startNode(nodeID) } } diff --git a/cli_startnode.go b/cli_startnode.go index 5b64f3c..289ccd4 100644 --- a/cli_startnode.go +++ b/cli_startnode.go @@ -2,7 +2,7 @@ package main import "fmt" -func (cli *CLI) startNode(nodeID int) { - fmt.Printf("Starting node %d\n", nodeID) +func (cli *CLI) startNode(nodeID string) { + fmt.Printf("Starting node %s\n", nodeID) StartServer(nodeID) } diff --git a/server.go b/server.go index a071ba0..ba0906d 100644 --- a/server.go +++ b/server.go @@ -11,7 +11,7 @@ import ( ) const protocol = "tcp" -const dnsNodeID = 3000 +const dnsNodeID = "3000" const nodeVersion = 1 const commandLength = 12 @@ -149,8 +149,8 @@ func handleConnection(conn net.Conn) { } // StartServer starts a node -func StartServer(nodeID int) { - nodeAddress = fmt.Sprintf("localhost:%d", nodeID) +func StartServer(nodeID string) { + nodeAddress = fmt.Sprintf("localhost:%s", nodeID) ln, err := net.Listen(protocol, nodeAddress) if err != nil { log.Panic(err) @@ -158,7 +158,7 @@ func StartServer(nodeID int) { defer ln.Close() if nodeID != dnsNodeID { - sendVersion(fmt.Sprintf("localhost:%d", dnsNodeID)) + sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID)) } for {