arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
goBlockchainDataAnalysis/exploreBlockchain.go
exploreBlockchain.goRaw
  1package main
  2
  3import (
  4	"fmt"
  5	"strconv"
  6
  7	"github.com/btcsuite/btcd/chaincfg/chainhash"
  8	"github.com/btcsuite/btcrpcclient"
  9)
 10
 11func explore(client *btcrpcclient.Client, blockHash string) {
 12	var realBlocks int
 13	var nOrigin NodeModel
 14	nOrigin.Id = "origin"
 15	nOrigin.Label = "origin"
 16	nOrigin.Title = "origin"
 17	nOrigin.Group = "origin"
 18	nOrigin.Value = 1
 19	nOrigin.Shape = "dot"
 20	saveNode(nodeCollection, nOrigin)
 21
 22	for blockHash != "" {
 23		//generate hash from string
 24		bh, err := chainhash.NewHashFromStr(blockHash)
 25		check(err)
 26		block, err := client.GetBlockVerbose(bh)
 27		check(err)
 28
 29		//if len(block.Tx) < 10 {
 30		for k, txHash := range block.Tx {
 31			if k > 0 {
 32				realBlocks++
 33				fmt.Print("Block Height: ")
 34				fmt.Print(block.Height)
 35				fmt.Print(", num of Tx: ")
 36				fmt.Print(k)
 37				fmt.Print("/")
 38				fmt.Println(len(block.Tx) - 1)
 39
 40				th, err := chainhash.NewHashFromStr(txHash)
 41				check(err)
 42				tx, err := client.GetRawTransactionVerbose(th)
 43				check(err)
 44
 45				var originAddresses []string
 46				var outputAddresses []string
 47				for _, Vo := range tx.Vout {
 48					//if Vo.Value > 0 {
 49					for _, outputAddr := range Vo.ScriptPubKey.Addresses {
 50						outputAddresses = append(outputAddresses, outputAddr)
 51						var n2 NodeModel
 52						n2.Id = outputAddr
 53						n2.Label = outputAddr
 54						n2.Title = outputAddr
 55						n2.Group = strconv.FormatInt(block.Height, 10)
 56						n2.Value = 1
 57						n2.Shape = "dot"
 58						saveNode(nodeCollection, n2)
 59					}
 60					//}
 61				}
 62				for _, Vi := range tx.Vin {
 63					th, err := chainhash.NewHashFromStr(Vi.Txid)
 64					check(err)
 65					txVi, err := client.GetRawTransactionVerbose(th)
 66					check(err)
 67					if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
 68						for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
 69							originAddresses = append(originAddresses, originAddr)
 70							var n1 NodeModel
 71							n1.Id = originAddr
 72							n1.Label = originAddr
 73							n1.Title = originAddr
 74							n1.Group = string(block.Height)
 75							n1.Value = 1
 76							n1.Shape = "dot"
 77							saveNode(nodeCollection, n1)
 78
 79							for _, outputAddr := range outputAddresses {
 80								var e EdgeModel
 81								e.From = originAddr
 82								e.To = outputAddr
 83								e.Label = txVi.Vout[Vi.Vout].Value
 84								e.Txid = tx.Txid
 85								e.Arrows = "to"
 86								e.BlockHeight = block.Height
 87								saveEdge(edgeCollection, e)
 88								//fmt.Println(e)
 89							}
 90						}
 91					} else {
 92						originAddresses = append(originAddresses, "origin")
 93					}
 94
 95				}
 96				fmt.Print("originAddresses: ")
 97				fmt.Println(len(originAddresses))
 98				fmt.Print("outputAddresses: ")
 99				fmt.Println(len(outputAddresses))
100			}
101		}
102		//}
103		//set the next block
104		blockHash = block.NextHash
105	}
106
107	fmt.Print("realBlocks (blocks with Fee and Amount values): ")
108	fmt.Println(realBlocks)
109	fmt.Println("reached the end of blockchain")
110}