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