You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.0 KiB

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