goBlockchainDataAnalysis/exploreBlockchain.go
exploreBlockchain.goRaw
1package main
2
3import (
4 "fmt"
5
6 "github.com/btcsuite/btcd/chaincfg/chainhash"
7 "github.com/btcsuite/btcrpcclient"
8)
9
10func explore(client *btcrpcclient.Client, blockHash string) {
11 var realBlocks int
12 for blockHash != "" {
13 //generate hash from string
14 bh, err := chainhash.NewHashFromStr(blockHash)
15 check(err)
16 block, err := client.GetBlockVerbose(bh)
17 check(err)
18
19 var newBlock BlockModel
20 newBlock.Hash = block.Hash
21 newBlock.Height = block.Height
22 newBlock.Confirmations = block.Confirmations
23
24 //get Fee value
25 th, err := chainhash.NewHashFromStr(block.Tx[0])
26 check(err)
27 tx, err := client.GetRawTransactionVerbose(th)
28 check(err)
29 var totalFee float64
30 for _, Vo := range tx.Vout {
31 totalFee = totalFee + Vo.Value
32 }
33 newBlock.Fee = totalFee
34
35 //for each Tx, get the Tx value
36 var totalAmount float64
37 /*inside each block, there are []Tx
38 inside each Tx, if is the Tx[0], is the Fees
39 in the Tx[n] where n>0, each Tx is independent,
40 and inside each Tx there are []Vout.
41 Usually Vout[0] is the real Tx value
42 and the Vout[1] is the rest of the amount in the original wallet.
43 Each Tx moves all the wallet amount, and the realTx amount is sent to the destination
44 and the rest of the wallet amount, is send to another owned wallet
45 */
46 if len(block.Tx) < 10 {
47 for k, txHash := range block.Tx {
48 //first Tx is the Fee
49 //after first Tx is the Sent Amount
50 if k > 0 {
51 th, err := chainhash.NewHashFromStr(txHash)
52 check(err)
53 tx, err := client.GetRawTransactionVerbose(th)
54 check(err)
55 var originAddress string
56 for _, Vi := range tx.Vin {
57 th, err := chainhash.NewHashFromStr(Vi.Txid)
58 check(err)
59 txVi, err := client.GetRawTransactionVerbose(th)
60 check(err)
61 if len(txVi.Vout[0].ScriptPubKey.Addresses) > 0 {
62 originAddress = txVi.Vout[0].ScriptPubKey.Addresses[0]
63 }
64 }
65 for _, Vo := range tx.Vout {
66 totalAmount = totalAmount + Vo.Value
67
68 var blockTx TxModel
69 blockTx.Txid = tx.Txid
70 blockTx.Amount = Vo.Value
71 blockTx.From = originAddress
72 blockTx.To = Vo.ScriptPubKey.Addresses[0]
73 newBlock.Tx = append(newBlock.Tx, blockTx)
74 }
75 }
76 }
77
78 if totalAmount > 0 {
79 newBlock.Amount = totalAmount
80 saveBlock(blockCollection, newBlock)
81 fmt.Print("Height: ")
82 fmt.Println(newBlock.Height)
83 fmt.Print("Amount: ")
84 fmt.Println(newBlock.Amount)
85 fmt.Print("Fee: ")
86 fmt.Println(newBlock.Fee)
87 fmt.Println("-----")
88 realBlocks++
89 }
90 }
91
92 //set the next block
93 blockHash = block.NextHash
94
95 //analyze block creator
96 for _, t := range newBlock.Tx {
97 var n1 NodeModel
98 var n2 NodeModel
99 n1.Id = t.From
100 n1.Label = t.From
101 n1.Title = t.From
102 n1.Value = 1
103 n1.Shape = "dot"
104 n2.Id = t.To
105 n2.Label = t.To
106 n2.Title = t.To
107 n2.Value = 1
108 n2.Shape = "dot"
109
110 var e EdgeModel
111 e.From = t.From
112 e.To = t.To
113 e.Label = t.Amount
114 e.Txid = t.Txid
115
116 saveNode(nodeCollection, n1)
117 saveNode(nodeCollection, n2)
118 saveEdge(edgeCollection, e)
119 }
120
121 }
122 fmt.Print("realBlocks (blocks with Fee and Amount values): ")
123 fmt.Println(realBlocks)
124 fmt.Println("reached the end of blockchain")
125}