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