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 block.Height > 0 {
 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					//save Tx
 46					var nTx NodeModel
 47					nTx.Id = txHash
 48					nTx.Label = txHash
 49					nTx.Title = txHash
 50					nTx.Group = strconv.FormatInt(block.Height, 10)
 51					nTx.Value = 1
 52					nTx.Shape = "square"
 53					nTx.Type = "tx"
 54					saveNode(nodeCollection, nTx)
 55
 56					var originAddresses []string
 57					var outputAddresses []string
 58					var outputAmount []float64
 59					for _, Vo := range tx.Vout {
 60						//if Vo.Value > 0 {
 61						for _, outputAddr := range Vo.ScriptPubKey.Addresses {
 62							outputAddresses = append(outputAddresses, outputAddr)
 63							outputAmount = append(outputAmount, Vo.Value)
 64							var n2 NodeModel
 65							n2.Id = outputAddr
 66							n2.Label = outputAddr
 67							n2.Title = outputAddr
 68							n2.Group = strconv.FormatInt(block.Height, 10)
 69							n2.Value = 1
 70							n2.Shape = "dot"
 71							n2.Type = "address"
 72							saveNode(nodeCollection, n2)
 73						}
 74						//}
 75					}
 76					for _, Vi := range tx.Vin {
 77						th, err := chainhash.NewHashFromStr(Vi.Txid)
 78						check(err)
 79						txVi, err := client.GetRawTransactionVerbose(th)
 80						check(err)
 81						if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
 82							for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
 83								originAddresses = append(originAddresses, originAddr)
 84								var n1 NodeModel
 85								n1.Id = originAddr
 86								n1.Label = originAddr
 87								n1.Title = originAddr
 88								n1.Group = strconv.FormatInt(block.Height, 10)
 89								n1.Value = 1
 90								n1.Shape = "dot"
 91								n1.Type = "address"
 92								saveNode(nodeCollection, n1)
 93
 94								for k, outputAddr := range outputAddresses {
 95									var eIn EdgeModel
 96									eIn.From = originAddr
 97									eIn.To = txHash
 98									eIn.Label = txVi.Vout[Vi.Vout].Value
 99									eIn.Txid = tx.Txid
100									eIn.Arrows = "to"
101									eIn.BlockHeight = block.Height
102									saveEdge(edgeCollection, eIn)
103
104									var eOut EdgeModel
105									eOut.From = txHash
106									eOut.To = outputAddr
107									eOut.Label = outputAmount[k]
108									eOut.Txid = tx.Txid
109									eOut.Arrows = "to"
110									eOut.BlockHeight = block.Height
111									saveEdge(edgeCollection, eOut)
112
113									//date analysis
114									//dateAnalysis(e, tx.Time)
115									//hour analysis
116									hourAnalysis(eIn, tx.Time)
117								}
118							}
119						} else {
120							originAddresses = append(originAddresses, "origin")
121						}
122
123					}
124					fmt.Print("originAddresses: ")
125					fmt.Println(len(originAddresses))
126					fmt.Print("outputAddresses: ")
127					fmt.Println(len(outputAddresses))
128				}
129			}
130		}
131		//set the next block
132		blockHash = block.NextHash
133	}
134
135	fmt.Print("realBlocks (blocks with Fee and Amount values): ")
136	fmt.Println(realBlocks)
137	fmt.Println("reached the end of blockchain")
138}