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.

150 lines
3.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "gopkg.in/mgo.v2/bson"
  6. "github.com/btcsuite/btcd/chaincfg/chainhash"
  7. "github.com/btcsuite/btcrpcclient"
  8. "github.com/fatih/color"
  9. )
  10. func explore(client *btcrpcclient.Client, blockHash string) {
  11. var realBlocks int
  12. var nOrigin NodeModel
  13. nOrigin.Id = "origin"
  14. nOrigin.Label = "origin"
  15. nOrigin.Title = "origin"
  16. nOrigin.Group = "origin"
  17. nOrigin.Value = 1
  18. nOrigin.Shape = "dot"
  19. saveNode(nodeCollection, nOrigin)
  20. for blockHash != "" {
  21. //generate hash from string
  22. bh, err := chainhash.NewHashFromStr(blockHash)
  23. check(err)
  24. block, err := client.GetBlockVerbose(bh)
  25. check(err)
  26. //if len(block.Tx) < 10 {
  27. for k, txHash := range block.Tx {
  28. if k > 0 {
  29. realBlocks++
  30. fmt.Print("Block Height: ")
  31. fmt.Print(block.Height)
  32. fmt.Print(", num of Tx: ")
  33. fmt.Print(k)
  34. fmt.Print("/")
  35. fmt.Println(len(block.Tx) - 1)
  36. th, err := chainhash.NewHashFromStr(txHash)
  37. check(err)
  38. tx, err := client.GetRawTransactionVerbose(th)
  39. check(err)
  40. var originAddresses []string
  41. var outputAddresses []string
  42. for _, Vo := range tx.Vout {
  43. //if Vo.Value > 0 {
  44. for _, outputAddr := range Vo.ScriptPubKey.Addresses {
  45. outputAddresses = append(outputAddresses, outputAddr)
  46. var n2 NodeModel
  47. n2.Id = outputAddr
  48. n2.Label = outputAddr
  49. n2.Title = outputAddr
  50. n2.Group = strconv.FormatInt(block.Height, 10)
  51. n2.Value = 1
  52. n2.Shape = "dot"
  53. saveNode(nodeCollection, n2)
  54. }
  55. //}
  56. }
  57. for _, Vi := range tx.Vin {
  58. th, err := chainhash.NewHashFromStr(Vi.Txid)
  59. check(err)
  60. txVi, err := client.GetRawTransactionVerbose(th)
  61. check(err)
  62. if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
  63. for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
  64. originAddresses = append(originAddresses, originAddr)
  65. var n1 NodeModel
  66. n1.Id = originAddr
  67. n1.Label = originAddr
  68. n1.Title = originAddr
  69. n1.Group = string(block.Height)
  70. n1.Value = 1
  71. n1.Shape = "dot"
  72. saveNode(nodeCollection, n1)
  73. for _, outputAddr := range outputAddresses {
  74. var e EdgeModel
  75. e.From = originAddr
  76. e.To = outputAddr
  77. e.Label = txVi.Vout[Vi.Vout].Value
  78. e.Txid = tx.Txid
  79. e.Arrows = "to"
  80. e.BlockHeight = block.Height
  81. saveEdge(edgeCollection, e)
  82. //fmt.Println(e)
  83. }
  84. }
  85. } else {
  86. originAddresses = append(originAddresses, "origin")
  87. }
  88. }
  89. fmt.Print("originAddresses: ")
  90. fmt.Println(len(originAddresses))
  91. fmt.Print("outputAddresses: ")
  92. fmt.Println(len(outputAddresses))
  93. }
  94. }
  95. //}
  96. //set the next block
  97. blockHash = block.NextHash
  98. }
  99. fmt.Print("realBlocks (blocks with Fee and Amount values): ")
  100. fmt.Println(realBlocks)
  101. fmt.Println("reached the end of blockchain")
  102. }
  103. func addressTree(address string) NetworkModel {
  104. var network NetworkModel
  105. var currentEdge EdgeModel
  106. currentEdge.From = "a"
  107. currentEdge.To = "b"
  108. for currentEdge.From != currentEdge.To {
  109. color.Green("for")
  110. fmt.Println(address)
  111. //get edges before the address
  112. edges := []EdgeModel{}
  113. err := edgeCollection.Find(bson.M{"to": address}).All(&edges)
  114. check(err)
  115. for _, edge := range edges {
  116. network.Edges = append(network.Edges, edge)
  117. fmt.Println(edge)
  118. }
  119. //get all nodes from edges
  120. for _, edge := range edges {
  121. node := NodeModel{}
  122. err := nodeCollection.Find(bson.M{"id": edge.From}).One(&node)
  123. check(err)
  124. if nodeInNodes(network.Nodes, node) == false {
  125. network.Nodes = append(network.Nodes, node)
  126. }
  127. err = nodeCollection.Find(bson.M{"id": edge.To}).One(&node)
  128. check(err)
  129. if nodeInNodes(network.Nodes, node) == false {
  130. network.Nodes = append(network.Nodes, node)
  131. }
  132. }
  133. address = edges[0].From
  134. currentEdge = edges[0]
  135. }
  136. return network
  137. }