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		if block.Height > config.StartFromBlock {
 29			var newBlock BlockModel
 30			newBlock.Hash = block.Hash
 31			newBlock.Confirmations = block.Confirmations
 32			newBlock.Size = block.Size
 33			newBlock.Height = block.Height
 34			//newBlock.Amount = block.Amount
 35			//newBlock.Fee = block.Fee
 36			newBlock.PreviousHash = block.PreviousHash
 37			newBlock.NextHash = block.NextHash
 38			newBlock.Time = block.Time
 39			newBlock.DateT = unixTimeToTime(block.Time)
 40			newBlock.Date.Year, newBlock.Date.Month, newBlock.Date.Day, newBlock.Date.Hour = decomposeDate(block.Time)
 41
 42			for k, txHash := range block.Tx {
 43				if k > 0 {
 44					realBlocks++
 45					fmt.Print("Block Height: ")
 46					fmt.Print(block.Height)
 47					fmt.Print(", num of Tx: ")
 48					fmt.Print(k)
 49					fmt.Print("/")
 50					fmt.Println(len(block.Tx) - 1)
 51
 52					th, err := chainhash.NewHashFromStr(txHash)
 53					check(err)
 54					blockTx, err := client.GetRawTransactionVerbose(th)
 55					check(err)
 56
 57					//save Tx Node
 58					var nodeTx NodeModel
 59					nodeTx.Id = txHash
 60					nodeTx.Label = txHash
 61					nodeTx.Title = txHash
 62					nodeTx.Group = strconv.FormatInt(block.Height, 10)
 63					nodeTx.Value = 1
 64					nodeTx.Shape = "square"
 65					nodeTx.Type = "tx"
 66					saveNode(nodeCollection, nodeTx)
 67
 68					var originAddresses []string
 69					var outputAddresses []string
 70					var outputAmount []float64
 71					for _, Vo := range blockTx.Vout {
 72						for _, outputAddr := range Vo.ScriptPubKey.Addresses {
 73							outputAddresses = append(outputAddresses, outputAddr)
 74							outputAmount = append(outputAmount, Vo.Value)
 75							var n2 NodeModel
 76							n2.Id = outputAddr
 77							n2.Label = outputAddr
 78							n2.Title = outputAddr
 79							n2.Group = strconv.FormatInt(block.Height, 10)
 80							n2.Value = 1
 81							n2.Shape = "dot"
 82							n2.Type = "address"
 83							saveNode(nodeCollection, n2)
 84
 85							//Address
 86							var addr AddressModel
 87							addr.Hash = outputAddr
 88							addr.InBittrex = false
 89							saveAddress(addr)
 90						}
 91					}
 92					for _, Vi := range blockTx.Vin {
 93						th, err := chainhash.NewHashFromStr(Vi.Txid)
 94						check(err)
 95						txVi, err := client.GetRawTransactionVerbose(th)
 96						check(err)
 97						if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
 98							//add tx to newBlock
 99							newBlock.Tx = append(newBlock.Tx, blockTx.Txid)
100
101							//Tx save
102							var newTx TxModel
103							newTx.Hex = blockTx.Hex
104							newTx.Txid = blockTx.Txid
105							newTx.Hash = blockTx.Hash
106							newTx.BlockHash = block.Hash
107							newTx.BlockHeight = strconv.FormatInt(block.Height, 10)
108							newTx.Time = blockTx.Time
109							newTx.DateT = unixTimeToTime(block.Time)
110							newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time)
111							for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
112								originAddresses = append(originAddresses, originAddr)
113
114								newTx.From = originAddr
115
116								var n1 NodeModel
117								n1.Id = originAddr
118								n1.Label = originAddr
119								n1.Title = originAddr
120								n1.Group = strconv.FormatInt(block.Height, 10)
121								n1.Value = 1
122								n1.Shape = "dot"
123								n1.Type = "address"
124								saveNode(nodeCollection, n1)
125
126								//Address
127								var addr AddressModel
128								addr.Hash = originAddr
129								addr.InBittrex = false
130								saveAddress(addr)
131
132								for k, outputAddr := range outputAddresses {
133									var eIn EdgeModel
134									eIn.From = originAddr
135									eIn.To = txHash
136									eIn.Label = txVi.Vout[Vi.Vout].Value
137									eIn.Txid = blockTx.Txid
138									eIn.Arrows = "to"
139									eIn.BlockHeight = block.Height
140									saveEdge(edgeCollection, eIn)
141
142									var eOut EdgeModel
143									eOut.From = txHash
144									eOut.To = outputAddr
145									eOut.Label = outputAmount[k]
146									eOut.Txid = blockTx.Txid
147									eOut.Arrows = "to"
148									eOut.BlockHeight = block.Height
149									saveEdge(edgeCollection, eOut)
150
151									//date analysis
152									//dateAnalysis(e, tx.Time)
153									//hour analysis
154									hourAnalysis(eIn, blockTx.Time)
155
156									newTx.To = outputAddr
157
158								}
159							}
160							saveTx(newTx)
161						} else {
162							originAddresses = append(originAddresses, "origin")
163						}
164
165					}
166					fmt.Print("originAddresses: ")
167					fmt.Println(len(originAddresses))
168					fmt.Print("outputAddresses: ")
169					fmt.Println(len(outputAddresses))
170				}
171			}
172			saveBlock(newBlock)
173		}
174		//set the next block
175		blockHash = block.NextHash
176	}
177
178	fmt.Print("realBlocks added (blocks with Fee and Amount values): ")
179	fmt.Println(realBlocks)
180	fmt.Println("reached the end of blockchain")
181}