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.

138 lines
3.6 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/btcsuite/btcd/chaincfg/chainhash"
  6. "github.com/btcsuite/btcrpcclient"
  7. )
  8. func explore(client *btcrpcclient.Client, blockHash string) {
  9. var realBlocks int
  10. var nOrigin NodeModel
  11. nOrigin.Id = "origin"
  12. nOrigin.Label = "origin"
  13. nOrigin.Title = "origin"
  14. nOrigin.Group = "origin"
  15. nOrigin.Value = 1
  16. nOrigin.Shape = "dot"
  17. saveNode(nodeCollection, nOrigin)
  18. for blockHash != "" {
  19. //generate hash from string
  20. bh, err := chainhash.NewHashFromStr(blockHash)
  21. check(err)
  22. block, err := client.GetBlockVerbose(bh)
  23. check(err)
  24. if block.Height > config.StartFromBlock {
  25. for k, txHash := range block.Tx {
  26. if k > 0 {
  27. realBlocks++
  28. fmt.Print("Block Height: ")
  29. fmt.Print(block.Height)
  30. fmt.Print(", num of Tx: ")
  31. fmt.Print(k)
  32. fmt.Print("/")
  33. fmt.Println(len(block.Tx) - 1)
  34. th, err := chainhash.NewHashFromStr(txHash)
  35. check(err)
  36. tx, err := client.GetRawTransactionVerbose(th)
  37. check(err)
  38. //save Tx
  39. var nTx NodeModel
  40. nTx.Id = txHash
  41. nTx.Label = txHash
  42. nTx.Title = txHash
  43. nTx.Group = strconv.FormatInt(block.Height, 10)
  44. nTx.Value = 1
  45. nTx.Shape = "square"
  46. nTx.Type = "tx"
  47. saveNode(nodeCollection, nTx)
  48. var originAddresses []string
  49. var outputAddresses []string
  50. var outputAmount []float64
  51. for _, Vo := range tx.Vout {
  52. //if Vo.Value > 0 {
  53. for _, outputAddr := range Vo.ScriptPubKey.Addresses {
  54. outputAddresses = append(outputAddresses, outputAddr)
  55. outputAmount = append(outputAmount, Vo.Value)
  56. var n2 NodeModel
  57. n2.Id = outputAddr
  58. n2.Label = outputAddr
  59. n2.Title = outputAddr
  60. n2.Group = strconv.FormatInt(block.Height, 10)
  61. n2.Value = 1
  62. n2.Shape = "dot"
  63. n2.Type = "address"
  64. saveNode(nodeCollection, n2)
  65. }
  66. //}
  67. }
  68. for _, Vi := range tx.Vin {
  69. th, err := chainhash.NewHashFromStr(Vi.Txid)
  70. check(err)
  71. txVi, err := client.GetRawTransactionVerbose(th)
  72. check(err)
  73. if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
  74. for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
  75. originAddresses = append(originAddresses, originAddr)
  76. var n1 NodeModel
  77. n1.Id = originAddr
  78. n1.Label = originAddr
  79. n1.Title = originAddr
  80. n1.Group = strconv.FormatInt(block.Height, 10)
  81. n1.Value = 1
  82. n1.Shape = "dot"
  83. n1.Type = "address"
  84. saveNode(nodeCollection, n1)
  85. for k, outputAddr := range outputAddresses {
  86. var eIn EdgeModel
  87. eIn.From = originAddr
  88. eIn.To = txHash
  89. eIn.Label = txVi.Vout[Vi.Vout].Value
  90. eIn.Txid = tx.Txid
  91. eIn.Arrows = "to"
  92. eIn.BlockHeight = block.Height
  93. saveEdge(edgeCollection, eIn)
  94. var eOut EdgeModel
  95. eOut.From = txHash
  96. eOut.To = outputAddr
  97. eOut.Label = outputAmount[k]
  98. eOut.Txid = tx.Txid
  99. eOut.Arrows = "to"
  100. eOut.BlockHeight = block.Height
  101. saveEdge(edgeCollection, eOut)
  102. //date analysis
  103. //dateAnalysis(e, tx.Time)
  104. //hour analysis
  105. hourAnalysis(eIn, tx.Time)
  106. }
  107. }
  108. } else {
  109. originAddresses = append(originAddresses, "origin")
  110. }
  111. }
  112. fmt.Print("originAddresses: ")
  113. fmt.Println(len(originAddresses))
  114. fmt.Print("outputAddresses: ")
  115. fmt.Println(len(outputAddresses))
  116. }
  117. }
  118. }
  119. //set the next block
  120. blockHash = block.NextHash
  121. }
  122. fmt.Print("realBlocks (blocks with Fee and Amount values): ")
  123. fmt.Println(realBlocks)
  124. fmt.Println("reached the end of blockchain")
  125. }