diff --git a/cli.go b/cli.go index 72693ea..366a7a6 100644 --- a/cli.go +++ b/cli.go @@ -19,6 +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") } func (cli *CLI) validateArgs() { @@ -39,12 +40,14 @@ func (cli *CLI) Run() { printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError) reindexUTXOCmd := flag.NewFlagSet("reindexutxo", flag.ExitOnError) sendCmd := flag.NewFlagSet("send", flag.ExitOnError) + startNodeCmd := flag.NewFlagSet("startnode", flag.ExitOnError) getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for") createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to") 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": @@ -72,13 +75,18 @@ func (cli *CLI) Run() { if err != nil { log.Panic(err) } + case "reindexutxo": + err := reindexUTXOCmd.Parse(os.Args[2:]) + if err != nil { + log.Panic(err) + } case "send": err := sendCmd.Parse(os.Args[2:]) if err != nil { log.Panic(err) } - case "reindexutxo": - err := reindexUTXOCmd.Parse(os.Args[2:]) + case "startnode": + err := startNodeCmd.Parse(os.Args[2:]) if err != nil { log.Panic(err) } @@ -127,4 +135,12 @@ func (cli *CLI) Run() { cli.send(*sendFrom, *sendTo, *sendAmount) } + + if startNodeCmd.Parsed() { + if *startNodeID == 0 { + startNodeCmd.Usage() + os.Exit(1) + } + cli.startNode(*startNodeID) + } } diff --git a/cli_startnode.go b/cli_startnode.go new file mode 100644 index 0000000..5b64f3c --- /dev/null +++ b/cli_startnode.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func (cli *CLI) startNode(nodeID int) { + fmt.Printf("Starting node %d\n", nodeID) + StartServer(nodeID) +} diff --git a/server.go b/server.go new file mode 100644 index 0000000..ff88b79 --- /dev/null +++ b/server.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "io" + "log" + "net" +) + +func handleConnection(conn net.Conn) { + io.Copy(conn, conn) + conn.Close() +} + +// StartServer starts a node +func StartServer(nodeID int) { + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", nodeID)) + if err != nil { + log.Panic(err) + } + defer ln.Close() + + for { + conn, err := ln.Accept() + if err != nil { + log.Panic(err) + } + go handleConnection(conn) + } +}