From 44d832f0ed5586048e8fed77193ae43828461a35 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 21 Jul 2017 17:49:15 +0200 Subject: [PATCH] saving the blocks in the MongoDB --- exploreBlocks.go => exploreBlockchain.go | 39 +++++++------- main.go | 12 ++++- mongoModels.go | 9 ++++ mongoOperations.go | 65 ++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 23 deletions(-) rename exploreBlocks.go => exploreBlockchain.go (66%) create mode 100644 mongoModels.go create mode 100644 mongoOperations.go diff --git a/exploreBlocks.go b/exploreBlockchain.go similarity index 66% rename from exploreBlocks.go rename to exploreBlockchain.go index 43c2944..9d793df 100644 --- a/exploreBlocks.go +++ b/exploreBlockchain.go @@ -8,26 +8,14 @@ import ( ) func explore(client *btcrpcclient.Client, blockHash string) { + var realBlocks int for blockHash != "" { //generate hash from string bh, err := chainhash.NewHashFromStr(blockHash) check(err) - fmt.Print("blockHash: ") - fmt.Println(bh) block, err := client.GetBlockVerbose(bh) check(err) - fmt.Print("height: ") - fmt.Println(block.Height) - fmt.Print("rawTx: ") - fmt.Println(block.RawTx) - fmt.Print("Tx: ") - fmt.Println(block.Tx) - fmt.Print("Time: ") - fmt.Println(block.Time) - fmt.Print("Confirmations: ") - fmt.Println(block.Confirmations) - fmt.Print("Fee: ") th, err := chainhash.NewHashFromStr(block.Tx[0]) check(err) tx, err := client.GetRawTransactionVerbose(th) @@ -37,9 +25,6 @@ func explore(client *btcrpcclient.Client, blockHash string) { for _, Vo := range tx.Vout { totalFee = totalFee + Vo.Value } - fmt.Print("totalFee: ") - fmt.Print(totalFee) - fmt.Println(" FAIR") //for each Tx, get the Tx value var totalAmount float64 @@ -49,22 +34,32 @@ func explore(client *btcrpcclient.Client, blockHash string) { if k > 0 { th, err := chainhash.NewHashFromStr(txHash) check(err) - fmt.Print("tx hash: ") - fmt.Println(th) tx, err := client.GetRawTransactionVerbose(th) check(err) for _, Vo := range tx.Vout { totalAmount = totalAmount + Vo.Value } - fmt.Print("totalAmount: ") - fmt.Print(totalAmount) - fmt.Println(" FAIR") } } - fmt.Println("-----") + if totalAmount > 0 { + var newBlock BlockModel + newBlock.Hash = block.Hash + newBlock.Height = block.Height + newBlock.Confirmations = block.Confirmations + newBlock.Amount = totalAmount + newBlock.Fee = totalFee + saveBlock(blockCollection, newBlock) + fmt.Println(newBlock.Height) + fmt.Println(newBlock.Amount) + fmt.Println(newBlock.Fee) + fmt.Println("-----") + realBlocks++ + } //set the next block blockHash = block.NextHash } + fmt.Print("realBlocks (blocks with Fee and Amount values): ") + fmt.Println(realBlocks) fmt.Println("reached the end of blockchain") } diff --git a/main.go b/main.go index 8eae80e..60e3a12 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,23 @@ package main import ( "log" + mgo "gopkg.in/mgo.v2" + "github.com/btcsuite/btcrpcclient" ) -func main() { +var blockCollection *mgo.Collection +func main() { + //read goBlockchainDataAbalysis config readConfig("config.json") + //connect with mongodb + readMongodbConfig("./mongodbConfig.json") + session, err := getSession() + check(err) + blockCollection = getCollection(session, "blocks") + // create new client instance client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{ HTTPPostMode: true, diff --git a/mongoModels.go b/mongoModels.go new file mode 100644 index 0000000..becee4f --- /dev/null +++ b/mongoModels.go @@ -0,0 +1,9 @@ +package main + +type BlockModel struct { + Hash string + Height int64 + Confirmations uint64 + Amount float64 + Fee float64 +} diff --git a/mongoOperations.go b/mongoOperations.go new file mode 100644 index 0000000..f03f67b --- /dev/null +++ b/mongoOperations.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + + mgo "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" +) + +//MongoConfig stores the configuration of mongodb to connect +type MongoConfig struct { + Ip string `json:"ip"` + Database string `json:"database"` +} + +var mongoConfig MongoConfig + +func readMongodbConfig(path string) { + file, e := ioutil.ReadFile(path) + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &mongoConfig) +} + +func getSession() (*mgo.Session, error) { + session, err := mgo.Dial("mongodb://" + mongoConfig.Ip) + if err != nil { + panic(err) + } + //defer session.Close() + + // Optional. Switch the session to a monotonic behavior. + session.SetMode(mgo.Monotonic, true) + + // Optional. Switch the session to a monotonic behavior. + session.SetMode(mgo.Monotonic, true) + + return session, err +} +func getCollection(session *mgo.Session, collection string) *mgo.Collection { + + c := session.DB(mongoConfig.Database).C(collection) + return c +} +func saveBlock(c *mgo.Collection, block BlockModel) { + //first, check if the item already exists + result := BlockModel{} + err := c.Find(bson.M{"hash": block.Hash}).One(&result) + if err != nil { + //item not found, so let's add a new entry + err = c.Insert(block) + check(err) + } else { + err = c.Update(bson.M{"hash": block.Hash}, &block) + if err != nil { + log.Fatal(err) + } + } + +}