You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. blockchainlib "./blockchainlib"
  7. p2plib "./p2plib"
  8. "github.com/fatih/color"
  9. )
  10. var tp p2plib.ThisPeer
  11. var blockchain blockchainlib.Blockchain
  12. func main() {
  13. if len(os.Args) < 3 {
  14. color.Red("need to call:")
  15. color.Red("./peer client 3001 3002")
  16. os.Exit(3)
  17. }
  18. color.Blue("Starting Peer")
  19. //read configuration file
  20. readConfig("config.json")
  21. //read the stored blockchain
  22. err := blockchain.ReadFromDisk()
  23. check(err)
  24. blockchain.Print()
  25. //initialize p2plib
  26. configuredMsgCases := createMsgHandlerCases()
  27. tp = p2plib.InitializePeer(os.Args[1], "127.0.0.1",
  28. os.Args[2], os.Args[3], config.ServerIP, config.ServerPort, configuredMsgCases)
  29. if tp.RunningPeer.Role == "client" {
  30. color.Red("http://" + config.IP + ":" + config.ServerRESTPort)
  31. fmt.Println(blockchain.GenesisBlock)
  32. blockchain.ReconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.GenesisBlock)
  33. }
  34. color.Blue("initialized")
  35. go runRestServer()
  36. fmt.Println(tp.Running)
  37. for tp.Running {
  38. time.Sleep(1000 * time.Millisecond)
  39. }
  40. }