arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
saving the blocks in the MongoDB
arnaucode idoctnef@openmailbox.org
Fri, 21 Jul 2017 17:49:15 +0200
commit

44d832f0ed5586048e8fed77193ae43828461a35

parent

d296958ff3bdccf7e4f758d60ee519eea74c54b6

4 files changed, 102 insertions(+), 23 deletions(-)

jump to
M exploreBlocks.go → exploreBlockchain.go

@@ -8,26 +8,14 @@ "github.com/btcsuite/btcrpcclient"

) 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 @@ var totalFee float64

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 @@ //after first Tx is the Sent Amount

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") }
M main.go → main.go

@@ -3,12 +3,22 @@

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{
A mongoModels.go

@@ -0,0 +1,9 @@

+package main + +type BlockModel struct { + Hash string + Height int64 + Confirmations uint64 + Amount float64 + Fee float64 +}
A 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) + } + } + +}