diff --git a/.gitignore b/.gitignore index eb614c3..c085c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ /*.conf /*.json *.mp4 +logs diff --git a/README.md b/README.md index 87fd807..a772b2e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,18 @@ # goBlockchainDataAnalysis blockchain data analysis, written in Go + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis01.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis02.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis05.png "goBlockchainDataAnalysis") + + +![goBlockchainDataAnalysis](https://raw.githubusercontent.com/arnaucode/goBlockchainDataAnalysis/master/goBlockchainDataAnalysis04.png "goBlockchainDataAnalysis") + ### Install 1. Nodejs & NPM https://nodejs.org/ 2. MongoDB https://www.mongodb.com/ diff --git a/errors.go b/errors.go index 9e83a92..e997ec1 100644 --- a/errors.go +++ b/errors.go @@ -1,9 +1,9 @@ package main -import "fmt" +import "log" func check(err error) { if err != nil { - fmt.Println(err) + log.Println(err) } } diff --git a/goBlockchainDataAnalysis05.png b/goBlockchainDataAnalysis05.png new file mode 100644 index 0000000..833983c Binary files /dev/null and b/goBlockchainDataAnalysis05.png differ diff --git a/log.go b/log.go new file mode 100644 index 0000000..d12c469 --- /dev/null +++ b/log.go @@ -0,0 +1,19 @@ +package main + +import ( + "io" + "log" + "os" + "time" +) + +func savelog() { + timeS := time.Now().String() + _ = os.Mkdir("logs", os.ModePerm) + logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +} diff --git a/main.go b/main.go index 8b5d156..e36d2d3 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "net/http" "os" @@ -20,6 +19,7 @@ var dateCountCollection *mgo.Collection var hourCountCollection *mgo.Collection func main() { + savelog() //read goBlockchainDataAbalysis config readConfig("config.json") @@ -33,50 +33,46 @@ func main() { dateCountCollection = getCollection(session, "dateCounts") hourCountCollection = getCollection(session, "hourCounts") - // create new client instance - client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{ - HTTPPostMode: true, - DisableTLS: true, - Host: config.Host + ":" + config.Port, - User: config.User, - Pass: config.Pass, - }, nil) - if err != nil { - log.Fatalf("error creating new btc client: %v", err) - } - - // list accounts - accounts, err := client.ListAccounts() - if err != nil { - log.Fatalf("error listing accounts: %v", err) - } - // iterate over accounts (map[string]btcutil.Amount) and write to stdout - for label, amount := range accounts { - log.Printf("%s: %s", label, amount) - } if len(os.Args) > 1 { if os.Args[1] == "-explore" { + // create new client instance + client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{ + HTTPPostMode: true, + DisableTLS: true, + Host: config.Host + ":" + config.Port, + User: config.User, + Pass: config.Pass, + }, nil) + if err != nil { + log.Fatalf("error creating new btc client: %v", err) + } + + // list accounts + accounts, err := client.ListAccounts() + if err != nil { + log.Fatalf("error listing accounts: %v", err) + } + // iterate over accounts (map[string]btcutil.Amount) and write to stdout + for label, amount := range accounts { + log.Printf("%s: %s", label, amount) + } color.Blue("starting to explore blockchain") explore(client, config.GenesisBlock) - } - /*if os.Args[1] == "-tree" { - color.Blue("starting to make tree") - addressTree(client, "fY3HZxu7HFKRcYzVSTXRZpAJMP4qba2oR6") - }*/ - } - // Get the current block count. - blockCount, err := client.GetBlockCount() - if err != nil { - log.Fatal(err) + // Get the current block count. + blockCount, err := client.GetBlockCount() + if err != nil { + log.Fatal(err) + } + log.Printf("Block count: %d", blockCount) + } } - log.Printf("Block count: %d", blockCount) //http server start readServerConfig("./serverConfig.json") - color.Green("server running") - fmt.Print("port: ") - color.Green(serverConfig.ServerPort) + log.Println("server running") + log.Print("port: ") + log.Println(serverConfig.ServerPort) router := NewRouter() headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"}) diff --git a/serverRoutes.go b/serverRoutes.go index 28734ad..3743cdd 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -26,6 +26,12 @@ var routes = Routes{ "/alladdresses", AllAddresses, }, + Route{ + "GetLastTx", + "Get", + "/lasttx", + GetLastTx, + }, Route{ "AddressNetwork", "GET", @@ -50,14 +56,6 @@ var routes = Routes{ "/houranalysis", GetHourAnalysis, }, - /* - Route{ - "SelectItem", - "GET", - "/selectItem/{userid}/{itemid}", - SelectItem, - }, - */ } //ROUTES @@ -95,6 +93,19 @@ func AllAddresses(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, string(jsonNodes)) } +func GetLastTx(w http.ResponseWriter, r *http.Request) { + ipFilter(w, r) + + nodes := []NodeModel{} + err := nodeCollection.Find(bson.M{}).Limit(10).Sort("-$natural").All(&nodes) + check(err) + + //convert []resp struct to json + jNodes, err := json.Marshal(nodes) + check(err) + + fmt.Fprintln(w, string(jNodes)) +} func AddressNetwork(w http.ResponseWriter, r *http.Request) { ipFilter(w, r) diff --git a/web/views/main/main.html b/web/views/main/main.html index 28a23b9..11a3126 100644 --- a/web/views/main/main.html +++ b/web/views/main/main.html @@ -3,10 +3,14 @@
{{node.id}}
+