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 //Tx save
83 var newTx TxModel
84 newTx.Hex = blockTx.Hex
85 newTx.Txid = blockTx.Txid
86 newTx.Hash = blockTx.Hash
87 newTx.BlockHash = block.Hash
88 newTx.BlockHeight = strconv.FormatInt(block.Height, 10)
89 newTx.Time = blockTx.Time
90 newTx.DateT = unixTimeToTime(block.Time)
91 newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time)
92
93 var originAddresses []string
94 var outputAddresses []string
95 var outputAmount []float64
96 for _, Vo := range blockTx.Vout {
97 for _, outputAddr := range Vo.ScriptPubKey.Addresses {
98 outputAddresses = append(outputAddresses, outputAddr)
99 outputAmount = append(outputAmount, Vo.Value)
100 var n2 NodeModel
101 n2.Id = outputAddr
102 n2.Label = outputAddr
103 n2.Title = outputAddr
104 n2.Group = strconv.FormatInt(block.Height, 10)
105 n2.Value = 1
106 n2.Shape = "dot"
107 n2.Type = "address"
108 saveNode(nodeCollection, n2)
109
110 //Address
111 var addr AddressModel
112 addr.Hash = outputAddr
113 addr.InBittrex = false
114 saveAddress(addr)
115
116 var newVout Vout
117 newVout.Value = Vo.Value
118 newVout.Address = outputAddr
119 newTx.Vout = append(newTx.Vout, newVout)
120 }
121 }
122 for _, Vi := range blockTx.Vin {
123 th, err := chainhash.NewHashFromStr(Vi.Txid)
124 check(err)
125 txVi, err := client.GetRawTransactionVerbose(th)
126 check(err)
127
128 if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
129 //add tx to newBlock
130 newBlock.Tx = append(newBlock.Tx, blockTx.Txid)
131
132 //Tx save
133 for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
134 originAddresses = append(originAddresses, originAddr)
135
136 var newVin Vin
137 newVin.Txid = blockTx.Txid
138 newVin.Amount = txVi.Vout[Vi.Vout].Value
139 newVin.Address = originAddr
140 newTx.Vin = append(newTx.Vin, newVin)
141
142 var n1 NodeModel
143 n1.Id = originAddr
144 n1.Label = originAddr
145 n1.Title = originAddr
146 n1.Group = strconv.FormatInt(block.Height, 10)
147 n1.Value = 1
148 n1.Shape = "dot"
149 n1.Type = "address"
150 saveNode(nodeCollection, n1)
151
152 //Address
153 var addr AddressModel
154 addr.Hash = originAddr
155 addr.InBittrex = false
156 saveAddress(addr)
157
158 for k, outputAddr := range outputAddresses {
159 var eIn EdgeModel
160 eIn.From = originAddr
161 eIn.To = txHash
162 eIn.Label = txVi.Vout[Vi.Vout].Value
163 eIn.Txid = blockTx.Txid
164 eIn.Arrows = "to"
165 eIn.BlockHeight = block.Height
166 saveEdge(edgeCollection, eIn)
167
168 var eOut EdgeModel
169 eOut.From = txHash
170 eOut.To = outputAddr
171 eOut.Label = outputAmount[k]
172 eOut.Txid = blockTx.Txid
173 eOut.Arrows = "to"
174 eOut.BlockHeight = block.Height
175 saveEdge(edgeCollection, eOut)
176
177 //date analysis
178 //dateAnalysis(e, tx.Time)
179 //hour analysis
180 hourAnalysis(eIn, blockTx.Time)
181
182 //newTx.To = outputAddr
183
184 }
185 }
186 saveTx(newTx)
187 } else {
188 originAddresses = append(originAddresses, "origin")
189 }
190
191 }
192 fmt.Print("originAddresses: ")
193 fmt.Println(len(originAddresses))
194 fmt.Print("outputAddresses: ")
195 fmt.Println(len(outputAddresses))
196 }
197 }
198 saveBlock(newBlock)
199 }
200 //set the next block
201 blockHash = block.NextHash
202 }
203
204 fmt.Print("realBlocks added (blocks with Fee and Amount values): ")
205 fmt.Println(realBlocks)
206 fmt.Println("reached the end of blockchain")
207}