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.

124 lines
3.8 KiB

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "time"
  8. mgo "gopkg.in/mgo.v2"
  9. "gopkg.in/mgo.v2/bson"
  10. "github.com/btcsuite/btcd/rpcclient"
  11. "github.com/gorilla/handlers"
  12. )
  13. var statsCollection *mgo.Collection
  14. var blockCollection *mgo.Collection
  15. var txCollection *mgo.Collection
  16. var addressCollection *mgo.Collection
  17. var nodeCollection *mgo.Collection
  18. var edgeCollection *mgo.Collection
  19. var dateCountCollection *mgo.Collection
  20. var hourCountCollection *mgo.Collection
  21. func main() {
  22. savelog()
  23. //read goBlockchainDataAbalysis config
  24. readConfig("config.json")
  25. //connect with mongodb
  26. //readMongodbConfig("./mongodbConfig.json")
  27. session, err := getSession()
  28. check(err)
  29. statsCollection = getCollection(session, "stats")
  30. blockCollection = getCollection(session, "blocks")
  31. txCollection = getCollection(session, "txs")
  32. addressCollection = getCollection(session, "address")
  33. nodeCollection = getCollection(session, "nodes")
  34. edgeCollection = getCollection(session, "edges")
  35. dateCountCollection = getCollection(session, "dateCounts")
  36. hourCountCollection = getCollection(session, "hourCounts")
  37. if len(os.Args) > 1 {
  38. if os.Args[1] == "-explore" {
  39. // create new client instance
  40. client, err := rpcclient.New(&rpcclient.ConnConfig{
  41. HTTPPostMode: true,
  42. DisableTLS: true,
  43. Host: config.Host + ":" + config.Port,
  44. User: config.User,
  45. Pass: config.Pass,
  46. }, nil)
  47. if err != nil {
  48. log.Fatalf("error creating new btc client: %v", err)
  49. }
  50. // list accounts
  51. accounts, err := client.ListAccounts()
  52. if err != nil {
  53. log.Fatalf("error listing accounts: %v", err)
  54. }
  55. // iterate over accounts (map[string]btcutil.Amount) and write to stdout
  56. for label, amount := range accounts {
  57. log.Printf("%s: %s", label, amount)
  58. }
  59. log.Println("starting to explore blockchain")
  60. start := time.Now()
  61. explore(client, config.GenesisBlock)
  62. log.Println("blockchain exploration finished, time:")
  63. log.Println(time.Since(start))
  64. // Get the current block count.
  65. blockCount, err := client.GetBlockCount()
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. log.Printf("Block count: %d", blockCount)
  70. }
  71. if os.Args[1] == "-continue" {
  72. // create new client instance
  73. client, err := rpcclient.New(&rpcclient.ConnConfig{
  74. HTTPPostMode: true,
  75. DisableTLS: true,
  76. Host: config.Host + ":" + config.Port,
  77. User: config.User,
  78. Pass: config.Pass,
  79. }, nil)
  80. check(err)
  81. //get last block stored in mongodb
  82. lastBlock := BlockModel{}
  83. err = blockCollection.Find(bson.M{}).Sort("-$natural").One(&lastBlock)
  84. check(err)
  85. log.Println("Getting last block stored in MongoDB. Hash: " + string(lastBlock.Hash) + ", BlockHeight: " + strconv.FormatInt(lastBlock.Height, 10))
  86. log.Println("continuing blockchain exploration since last block in mongodb")
  87. start := time.Now()
  88. explore(client, string(lastBlock.Hash))
  89. log.Println("blockchain exploration finished, time:")
  90. log.Println(time.Since(start))
  91. }
  92. }
  93. //run thw webserver
  94. go webserver()
  95. //http server start
  96. //readServerConfig("./serverConfig.json")
  97. log.Println("server running")
  98. log.Print("port: ")
  99. log.Println(config.Server.ServerPort)
  100. router := NewRouter()
  101. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"})
  102. originsOk := handlers.AllowedOrigins([]string{"*"})
  103. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  104. log.Fatal(http.ListenAndServe(":"+config.Server.ServerPort, handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  105. //log.Fatal(http.ListenAndServe(":"+serverConfig.ServerPort, router))
  106. }
  107. func webserver() {
  108. log.Println("webserver in port " + config.Server.WebServerPort)
  109. http.Handle("/", http.FileServer(http.Dir("./web")))
  110. http.ListenAndServe(":"+config.Server.WebServerPort, nil)
  111. }