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