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