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.

195 lines
5.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/btcsuite/btcd/chaincfg/chainhash"
  6. "github.com/btcsuite/btcd/rpcclient"
  7. )
  8. func explore(client *rpcclient.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. var newBlock BlockModel
  26. newBlock.Hash = block.Hash
  27. newBlock.Confirmations = block.Confirmations
  28. newBlock.Size = block.Size
  29. newBlock.Height = block.Height
  30. //newBlock.Amount = block.Amount
  31. //newBlock.Fee = block.Fee
  32. newBlock.PreviousHash = block.PreviousHash
  33. newBlock.NextHash = block.NextHash
  34. newBlock.Time = block.Time
  35. newBlock.DateT = unixTimeToTime(block.Time)
  36. newBlock.Date.Year, newBlock.Date.Month, newBlock.Date.Day, newBlock.Date.Hour = decomposeDate(block.Time)
  37. //stats blocks
  38. stats := getStats()
  39. stats.BlockCount++
  40. if len(block.Tx) > 1 {
  41. stats.RealBlockCount++
  42. }
  43. updateStats(stats)
  44. for k, txHash := range block.Tx {
  45. if k > 0 {
  46. realBlocks++
  47. //stats txs
  48. stats := getStats()
  49. stats.TxCount++
  50. updateStats(stats)
  51. fmt.Print("Block Height: ")
  52. fmt.Print(block.Height)
  53. fmt.Print(", num of Tx: ")
  54. fmt.Print(k)
  55. fmt.Print("/")
  56. fmt.Println(len(block.Tx) - 1)
  57. th, err := chainhash.NewHashFromStr(txHash)
  58. check(err)
  59. blockTx, err := client.GetRawTransactionVerbose(th)
  60. check(err)
  61. //save Tx Node
  62. var nodeTx NodeModel
  63. nodeTx.Id = txHash
  64. nodeTx.Label = txHash
  65. nodeTx.Title = txHash
  66. nodeTx.Group = strconv.FormatInt(block.Height, 10)
  67. nodeTx.Value = 1
  68. nodeTx.Shape = "square"
  69. nodeTx.Type = "tx"
  70. saveNode(nodeCollection, nodeTx)
  71. var originAddresses []string
  72. var outputAddresses []string
  73. var outputAmount []float64
  74. for _, Vo := range blockTx.Vout {
  75. for _, outputAddr := range Vo.ScriptPubKey.Addresses {
  76. outputAddresses = append(outputAddresses, outputAddr)
  77. outputAmount = append(outputAmount, Vo.Value)
  78. var n2 NodeModel
  79. n2.Id = outputAddr
  80. n2.Label = outputAddr
  81. n2.Title = outputAddr
  82. n2.Group = strconv.FormatInt(block.Height, 10)
  83. n2.Value = 1
  84. n2.Shape = "dot"
  85. n2.Type = "address"
  86. saveNode(nodeCollection, n2)
  87. //Address
  88. var addr AddressModel
  89. addr.Hash = outputAddr
  90. addr.InBittrex = false
  91. saveAddress(addr)
  92. }
  93. }
  94. for _, Vi := range blockTx.Vin {
  95. th, err := chainhash.NewHashFromStr(Vi.Txid)
  96. check(err)
  97. txVi, err := client.GetRawTransactionVerbose(th)
  98. check(err)
  99. if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
  100. //add tx to newBlock
  101. newBlock.Tx = append(newBlock.Tx, blockTx.Txid)
  102. //Tx save
  103. var newTx TxModel
  104. newTx.Hex = blockTx.Hex
  105. newTx.Txid = blockTx.Txid
  106. newTx.Hash = blockTx.Hash
  107. newTx.BlockHash = block.Hash
  108. newTx.BlockHeight = strconv.FormatInt(block.Height, 10)
  109. newTx.Time = blockTx.Time
  110. newTx.DateT = unixTimeToTime(block.Time)
  111. newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time)
  112. for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
  113. originAddresses = append(originAddresses, originAddr)
  114. newTx.From = originAddr
  115. var n1 NodeModel
  116. n1.Id = originAddr
  117. n1.Label = originAddr
  118. n1.Title = originAddr
  119. n1.Group = strconv.FormatInt(block.Height, 10)
  120. n1.Value = 1
  121. n1.Shape = "dot"
  122. n1.Type = "address"
  123. saveNode(nodeCollection, n1)
  124. //Address
  125. var addr AddressModel
  126. addr.Hash = originAddr
  127. addr.InBittrex = false
  128. saveAddress(addr)
  129. for k, outputAddr := range outputAddresses {
  130. var eIn EdgeModel
  131. eIn.From = originAddr
  132. eIn.To = txHash
  133. eIn.Label = txVi.Vout[Vi.Vout].Value
  134. eIn.Txid = blockTx.Txid
  135. eIn.Arrows = "to"
  136. eIn.BlockHeight = block.Height
  137. saveEdge(edgeCollection, eIn)
  138. var eOut EdgeModel
  139. eOut.From = txHash
  140. eOut.To = outputAddr
  141. eOut.Label = outputAmount[k]
  142. eOut.Txid = blockTx.Txid
  143. eOut.Arrows = "to"
  144. eOut.BlockHeight = block.Height
  145. saveEdge(edgeCollection, eOut)
  146. //date analysis
  147. //dateAnalysis(e, tx.Time)
  148. //hour analysis
  149. hourAnalysis(eIn, blockTx.Time)
  150. newTx.To = outputAddr
  151. }
  152. }
  153. saveTx(newTx)
  154. } else {
  155. originAddresses = append(originAddresses, "origin")
  156. }
  157. }
  158. fmt.Print("originAddresses: ")
  159. fmt.Println(len(originAddresses))
  160. fmt.Print("outputAddresses: ")
  161. fmt.Println(len(outputAddresses))
  162. }
  163. }
  164. saveBlock(newBlock)
  165. }
  166. //set the next block
  167. blockHash = block.NextHash
  168. }
  169. fmt.Print("realBlocks added (blocks with Fee and Amount values): ")
  170. fmt.Println(realBlocks)
  171. fmt.Println("reached the end of blockchain")
  172. }