From 47b647f010b7dc6e29f8e30b533bd02de37b9601 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 1 Sep 2017 00:39:42 +0200 Subject: [PATCH] added continuous block exploration each minute --- continuousTasks.go | 39 +++++++++++++++++++++++++++++++++++++++ main.go | 23 ++--------------------- 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 continuousTasks.go diff --git a/continuousTasks.go b/continuousTasks.go new file mode 100644 index 0000000..d65e472 --- /dev/null +++ b/continuousTasks.go @@ -0,0 +1,39 @@ +package main + +import ( + "log" + "strconv" + "time" + + "github.com/btcsuite/btcd/rpcclient" + "gopkg.in/mgo.v2/bson" +) + +func explorationContinue() { + // create new client instance + client, err := rpcclient.New(&rpcclient.ConnConfig{ + HTTPPostMode: true, + DisableTLS: true, + Host: config.Host + ":" + config.Port, + User: config.User, + Pass: config.Pass, + }, nil) + check(err) + //get last block stored in mongodb + lastBlock := BlockModel{} + err = blockCollection.Find(bson.M{}).Sort("-$natural").One(&lastBlock) + check(err) + log.Println("Getting last block stored in MongoDB. Hash: " + string(lastBlock.Hash) + ", BlockHeight: " + strconv.FormatInt(lastBlock.Height, 10)) + log.Println("continuing blockchain exploration since last block in mongodb") + start := time.Now() + explore(client, string(lastBlock.Hash)) + log.Println("blockchain exploration finished, time:") + log.Println(time.Since(start)) +} + +func continuousExploration() { + for { + explorationContinue() + time.Sleep(time.Second * 60) + } +} diff --git a/main.go b/main.go index cc49af5..c613211 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,9 @@ import ( "log" "net/http" "os" - "strconv" "time" mgo "gopkg.in/mgo.v2" - "gopkg.in/mgo.v2/bson" "github.com/btcsuite/btcd/rpcclient" "github.com/gorilla/handlers" @@ -78,27 +76,10 @@ func main() { log.Printf("Block count: %d", blockCount) } if os.Args[1] == "-continue" { - // create new client instance - client, err := rpcclient.New(&rpcclient.ConnConfig{ - HTTPPostMode: true, - DisableTLS: true, - Host: config.Host + ":" + config.Port, - User: config.User, - Pass: config.Pass, - }, nil) - check(err) - //get last block stored in mongodb - lastBlock := BlockModel{} - err = blockCollection.Find(bson.M{}).Sort("-$natural").One(&lastBlock) - check(err) - log.Println("Getting last block stored in MongoDB. Hash: " + string(lastBlock.Hash) + ", BlockHeight: " + strconv.FormatInt(lastBlock.Height, 10)) - log.Println("continuing blockchain exploration since last block in mongodb") - start := time.Now() - explore(client, string(lastBlock.Hash)) - log.Println("blockchain exploration finished, time:") - log.Println(time.Since(start)) + explorationContinue() } } + go continuousExploration() //run thw webserver go webserver()