arnaucube
arnaucube/bcPublic Self-hosted repository
bc/peer/main.go
peer/main.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"os"
 6	"time"
 7
 8	blockchainlib "./blockchainlib"
 9	p2plib "./p2plib"
10	"github.com/fatih/color"
11)
12
13var tp p2plib.ThisPeer
14var blockchain blockchainlib.Blockchain
15
16func main() {
17
18	if len(os.Args) < 3 {
19		color.Red("need to call:")
20		color.Red("./peer client 3001 3002")
21		os.Exit(3)
22	}
23
24	color.Blue("Starting Peer")
25	//read configuration file
26	readConfig("config.json")
27
28	//initialize blockchainlib
29	//InitializeBlockchain(role, ip, port, restport, serverip, serverport)
30	config.RESTPort = os.Args[3]
31	tp := blockchain.InitializeBlockchain(os.Args[1], "127.0.0.1",
32		os.Args[2], os.Args[3], config.ServerIP, config.ServerPort)
33
34	if tp.RunningPeer.Role == "client" {
35		color.Red("http://" + config.IP + ":" + config.ServerRESTPort)
36		fmt.Println(blockchain.GenesisBlock)
37		blockchain.ReconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.GenesisBlock)
38	}
39	color.Blue("peer and blokcchain initialized")
40	go runRestServer() //TODO this will not be necessary, due the communications will go full over tcp connections
41
42	for tp.Running {
43		time.Sleep(1000 * time.Millisecond)
44	}
45}