mirror of
https://github.com/arnaucube/goBlockchainDataAnalysis.git
synced 2026-02-07 11:46:38 +01:00
saving the blocks in the MongoDB
This commit is contained in:
@@ -8,26 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func explore(client *btcrpcclient.Client, blockHash string) {
|
func explore(client *btcrpcclient.Client, blockHash string) {
|
||||||
|
var realBlocks int
|
||||||
for blockHash != "" {
|
for blockHash != "" {
|
||||||
//generate hash from string
|
//generate hash from string
|
||||||
bh, err := chainhash.NewHashFromStr(blockHash)
|
bh, err := chainhash.NewHashFromStr(blockHash)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Print("blockHash: ")
|
|
||||||
fmt.Println(bh)
|
|
||||||
block, err := client.GetBlockVerbose(bh)
|
block, err := client.GetBlockVerbose(bh)
|
||||||
check(err)
|
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])
|
th, err := chainhash.NewHashFromStr(block.Tx[0])
|
||||||
check(err)
|
check(err)
|
||||||
tx, err := client.GetRawTransactionVerbose(th)
|
tx, err := client.GetRawTransactionVerbose(th)
|
||||||
@@ -37,9 +25,6 @@ func explore(client *btcrpcclient.Client, blockHash string) {
|
|||||||
for _, Vo := range tx.Vout {
|
for _, Vo := range tx.Vout {
|
||||||
totalFee = totalFee + Vo.Value
|
totalFee = totalFee + Vo.Value
|
||||||
}
|
}
|
||||||
fmt.Print("totalFee: ")
|
|
||||||
fmt.Print(totalFee)
|
|
||||||
fmt.Println(" FAIR")
|
|
||||||
|
|
||||||
//for each Tx, get the Tx value
|
//for each Tx, get the Tx value
|
||||||
var totalAmount float64
|
var totalAmount float64
|
||||||
@@ -49,22 +34,32 @@ func explore(client *btcrpcclient.Client, blockHash string) {
|
|||||||
if k > 0 {
|
if k > 0 {
|
||||||
th, err := chainhash.NewHashFromStr(txHash)
|
th, err := chainhash.NewHashFromStr(txHash)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Print("tx hash: ")
|
|
||||||
fmt.Println(th)
|
|
||||||
tx, err := client.GetRawTransactionVerbose(th)
|
tx, err := client.GetRawTransactionVerbose(th)
|
||||||
check(err)
|
check(err)
|
||||||
for _, Vo := range tx.Vout {
|
for _, Vo := range tx.Vout {
|
||||||
totalAmount = totalAmount + Vo.Value
|
totalAmount = totalAmount + Vo.Value
|
||||||
}
|
}
|
||||||
fmt.Print("totalAmount: ")
|
|
||||||
fmt.Print(totalAmount)
|
|
||||||
fmt.Println(" FAIR")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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("-----")
|
fmt.Println("-----")
|
||||||
|
realBlocks++
|
||||||
|
}
|
||||||
|
|
||||||
//set the next block
|
//set the next block
|
||||||
blockHash = block.NextHash
|
blockHash = block.NextHash
|
||||||
}
|
}
|
||||||
|
fmt.Print("realBlocks (blocks with Fee and Amount values): ")
|
||||||
|
fmt.Println(realBlocks)
|
||||||
fmt.Println("reached the end of blockchain")
|
fmt.Println("reached the end of blockchain")
|
||||||
}
|
}
|
||||||
12
main.go
12
main.go
@@ -3,13 +3,23 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
|
|
||||||
"github.com/btcsuite/btcrpcclient"
|
"github.com/btcsuite/btcrpcclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
var blockCollection *mgo.Collection
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
//read goBlockchainDataAbalysis config
|
||||||
readConfig("config.json")
|
readConfig("config.json")
|
||||||
|
|
||||||
|
//connect with mongodb
|
||||||
|
readMongodbConfig("./mongodbConfig.json")
|
||||||
|
session, err := getSession()
|
||||||
|
check(err)
|
||||||
|
blockCollection = getCollection(session, "blocks")
|
||||||
|
|
||||||
// create new client instance
|
// create new client instance
|
||||||
client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
|
client, err := btcrpcclient.New(&btcrpcclient.ConnConfig{
|
||||||
HTTPPostMode: true,
|
HTTPPostMode: true,
|
||||||
|
|||||||
9
mongoModels.go
Normal file
9
mongoModels.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type BlockModel struct {
|
||||||
|
Hash string
|
||||||
|
Height int64
|
||||||
|
Confirmations uint64
|
||||||
|
Amount float64
|
||||||
|
Fee float64
|
||||||
|
}
|
||||||
65
mongoOperations.go
Normal file
65
mongoOperations.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user