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