arnaucube
arnaucube/goBlockchainDataAnalysisPublic Self-hosted repository
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		for k, txHash := range block.Tx {
47			//first Tx is the Fee
48			//after first Tx is the Sent Amount
49			if k > 0 {
50				th, err := chainhash.NewHashFromStr(txHash)
51				check(err)
52				tx, err := client.GetRawTransactionVerbose(th)
53				check(err)
54				var originAddress string
55				for _, Vi := range tx.Vin {
56					th, err := chainhash.NewHashFromStr(Vi.Txid)
57					check(err)
58					txVi, err := client.GetRawTransactionVerbose(th)
59					check(err)
60					if len(txVi.Vout[0].ScriptPubKey.Addresses) > 0 {
61						originAddress = txVi.Vout[0].ScriptPubKey.Addresses[0]
62					}
63				}
64				for _, Vo := range tx.Vout {
65					totalAmount = totalAmount + Vo.Value
66
67					var blockTx TxModel
68					blockTx.Txid = tx.Txid
69					blockTx.Amount = Vo.Value
70					blockTx.From = originAddress
71					blockTx.To = Vo.ScriptPubKey.Addresses[0]
72					newBlock.Tx = append(newBlock.Tx, blockTx)
73				}
74			}
75		}
76
77		if totalAmount > 0 {
78			newBlock.Amount = totalAmount
79			saveBlock(blockCollection, newBlock)
80			fmt.Print("Height: ")
81			fmt.Println(newBlock.Height)
82			fmt.Print("Amount: ")
83			fmt.Println(newBlock.Amount)
84			fmt.Print("Fee: ")
85			fmt.Println(newBlock.Fee)
86			fmt.Println("-----")
87			realBlocks++
88		}
89
90		//set the next block
91		blockHash = block.NextHash
92
93		//analyze block creator
94
95	}
96	fmt.Print("realBlocks (blocks with Fee and Amount values): ")
97	fmt.Println(realBlocks)
98	fmt.Println("reached the end of blockchain")
99}