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