arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
goBlockchainDataAnalysis/exploreBlockchain.go
exploreBlockchain.goRaw
  1package main
  2
  3import (
  4	"fmt"
  5	"strconv"
  6
  7	"gopkg.in/mgo.v2/bson"
  8
  9	"github.com/btcsuite/btcd/chaincfg/chainhash"
 10	"github.com/btcsuite/btcrpcclient"
 11	"github.com/fatih/color"
 12)
 13
 14func explore(client *btcrpcclient.Client, blockHash string) {
 15	var realBlocks int
 16	var nOrigin NodeModel
 17	nOrigin.Id = "origin"
 18	nOrigin.Label = "origin"
 19	nOrigin.Title = "origin"
 20	nOrigin.Group = "origin"
 21	nOrigin.Value = 1
 22	nOrigin.Shape = "dot"
 23	saveNode(nodeCollection, nOrigin)
 24
 25	for blockHash != "" {
 26		//generate hash from string
 27		bh, err := chainhash.NewHashFromStr(blockHash)
 28		check(err)
 29		block, err := client.GetBlockVerbose(bh)
 30		check(err)
 31
 32		//if len(block.Tx) < 10 {
 33		for k, txHash := range block.Tx {
 34			if k > 0 {
 35				realBlocks++
 36				fmt.Print("Block Height: ")
 37				fmt.Print(block.Height)
 38				fmt.Print(", num of Tx: ")
 39				fmt.Print(k)
 40				fmt.Print("/")
 41				fmt.Println(len(block.Tx) - 1)
 42
 43				th, err := chainhash.NewHashFromStr(txHash)
 44				check(err)
 45				tx, err := client.GetRawTransactionVerbose(th)
 46				check(err)
 47
 48				var originAddresses []string
 49				var outputAddresses []string
 50				for _, Vo := range tx.Vout {
 51					//if Vo.Value > 0 {
 52					for _, outputAddr := range Vo.ScriptPubKey.Addresses {
 53						outputAddresses = append(outputAddresses, outputAddr)
 54						var n2 NodeModel
 55						n2.Id = outputAddr
 56						n2.Label = outputAddr
 57						n2.Title = outputAddr
 58						n2.Group = strconv.FormatInt(block.Height, 10)
 59						n2.Value = 1
 60						n2.Shape = "dot"
 61						saveNode(nodeCollection, n2)
 62					}
 63					//}
 64				}
 65				for _, Vi := range tx.Vin {
 66					th, err := chainhash.NewHashFromStr(Vi.Txid)
 67					check(err)
 68					txVi, err := client.GetRawTransactionVerbose(th)
 69					check(err)
 70					if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
 71						for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
 72							originAddresses = append(originAddresses, originAddr)
 73							var n1 NodeModel
 74							n1.Id = originAddr
 75							n1.Label = originAddr
 76							n1.Title = originAddr
 77							n1.Group = string(block.Height)
 78							n1.Value = 1
 79							n1.Shape = "dot"
 80							saveNode(nodeCollection, n1)
 81
 82							for _, outputAddr := range outputAddresses {
 83								var e EdgeModel
 84								e.From = originAddr
 85								e.To = outputAddr
 86								e.Label = txVi.Vout[Vi.Vout].Value
 87								e.Txid = tx.Txid
 88								e.Arrows = "to"
 89								e.BlockHeight = block.Height
 90								saveEdge(edgeCollection, e)
 91								//fmt.Println(e)
 92							}
 93						}
 94					} else {
 95						originAddresses = append(originAddresses, "origin")
 96					}
 97
 98				}
 99				fmt.Print("originAddresses: ")
100				fmt.Println(len(originAddresses))
101				fmt.Print("outputAddresses: ")
102				fmt.Println(len(outputAddresses))
103			}
104		}
105		//}
106		//set the next block
107		blockHash = block.NextHash
108	}
109
110	fmt.Print("realBlocks (blocks with Fee and Amount values): ")
111	fmt.Println(realBlocks)
112	fmt.Println("reached the end of blockchain")
113}
114func addressTree(address string) NetworkModel {
115	var network NetworkModel
116
117	var currentEdge EdgeModel
118	currentEdge.From = "a"
119	currentEdge.To = "b"
120	for currentEdge.From != currentEdge.To {
121		color.Green("for")
122		fmt.Println(address)
123		//get edges before the address
124		edges := []EdgeModel{}
125		err := edgeCollection.Find(bson.M{"to": address}).All(&edges)
126		check(err)
127		for _, edge := range edges {
128			network.Edges = append(network.Edges, edge)
129			fmt.Println(edge)
130		}
131		//get all nodes from edges
132		for _, edge := range edges {
133			node := NodeModel{}
134			err := nodeCollection.Find(bson.M{"id": edge.From}).One(&node)
135			check(err)
136			if nodeInNodes(network.Nodes, node) == false {
137				network.Nodes = append(network.Nodes, node)
138			}
139
140			err = nodeCollection.Find(bson.M{"id": edge.To}).One(&node)
141			check(err)
142			if nodeInNodes(network.Nodes, node) == false {
143				network.Nodes = append(network.Nodes, node)
144			}
145		}
146		address = edges[0].From
147		currentEdge = edges[0]
148	}
149	return network
150}