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.

207 lines
5.5 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. //Tx save
  72. var newTx TxModel
  73. newTx.Hex = blockTx.Hex
  74. newTx.Txid = blockTx.Txid
  75. newTx.Hash = blockTx.Hash
  76. newTx.BlockHash = block.Hash
  77. newTx.BlockHeight = strconv.FormatInt(block.Height, 10)
  78. newTx.Time = blockTx.Time
  79. newTx.DateT = unixTimeToTime(block.Time)
  80. newTx.Date.Year, newTx.Date.Month, newTx.Date.Day, newTx.Date.Hour = decomposeDate(block.Time)
  81. var originAddresses []string
  82. var outputAddresses []string
  83. var outputAmount []float64
  84. for _, Vo := range blockTx.Vout {
  85. for _, outputAddr := range Vo.ScriptPubKey.Addresses {
  86. outputAddresses = append(outputAddresses, outputAddr)
  87. outputAmount = append(outputAmount, Vo.Value)
  88. var n2 NodeModel
  89. n2.Id = outputAddr
  90. n2.Label = outputAddr
  91. n2.Title = outputAddr
  92. n2.Group = strconv.FormatInt(block.Height, 10)
  93. n2.Value = 1
  94. n2.Shape = "dot"
  95. n2.Type = "address"
  96. saveNode(nodeCollection, n2)
  97. //Address
  98. var addr AddressModel
  99. addr.Hash = outputAddr
  100. addr.InBittrex = false
  101. saveAddress(addr)
  102. var newVout Vout
  103. newVout.Value = Vo.Value
  104. newVout.Address = outputAddr
  105. newTx.Vout = append(newTx.Vout, newVout)
  106. }
  107. }
  108. for _, Vi := range blockTx.Vin {
  109. th, err := chainhash.NewHashFromStr(Vi.Txid)
  110. check(err)
  111. txVi, err := client.GetRawTransactionVerbose(th)
  112. check(err)
  113. //if len(txVi.Vout[Vi.Vout].ScriptPubKey.Addresses) > 0 {
  114. //add tx to newBlock
  115. newBlock.Tx = append(newBlock.Tx, blockTx.Txid)
  116. //Tx save
  117. for _, originAddr := range txVi.Vout[Vi.Vout].ScriptPubKey.Addresses {
  118. originAddresses = append(originAddresses, originAddr)
  119. var newVin Vin
  120. newVin.Txid = blockTx.Txid
  121. newVin.Amount = txVi.Vout[Vi.Vout].Value
  122. newVin.Address = originAddr
  123. newTx.Vin = append(newTx.Vin, newVin)
  124. var n1 NodeModel
  125. n1.Id = originAddr
  126. n1.Label = originAddr
  127. n1.Title = originAddr
  128. n1.Group = strconv.FormatInt(block.Height, 10)
  129. n1.Value = 1
  130. n1.Shape = "dot"
  131. n1.Type = "address"
  132. saveNode(nodeCollection, n1)
  133. //Address
  134. var addr AddressModel
  135. addr.Hash = originAddr
  136. addr.InBittrex = false
  137. saveAddress(addr)
  138. for k, outputAddr := range outputAddresses {
  139. var eIn EdgeModel
  140. eIn.From = originAddr
  141. eIn.To = txHash
  142. eIn.Label = txVi.Vout[Vi.Vout].Value
  143. eIn.Txid = blockTx.Txid
  144. eIn.Arrows = "to"
  145. eIn.BlockHeight = block.Height
  146. saveEdge(edgeCollection, eIn)
  147. var eOut EdgeModel
  148. eOut.From = txHash
  149. eOut.To = outputAddr
  150. eOut.Label = outputAmount[k]
  151. eOut.Txid = blockTx.Txid
  152. eOut.Arrows = "to"
  153. eOut.BlockHeight = block.Height
  154. saveEdge(edgeCollection, eOut)
  155. //date analysis
  156. //dateAnalysis(e, tx.Time)
  157. //hour analysis
  158. hourAnalysis(eIn, blockTx.Time)
  159. //newTx.To = outputAddr
  160. }
  161. }
  162. /*} else {
  163. originAddresses = append(originAddresses, "origin")
  164. }*/
  165. }
  166. saveTx(newTx)
  167. fmt.Print("originAddresses: ")
  168. fmt.Println(len(originAddresses))
  169. fmt.Print("outputAddresses: ")
  170. fmt.Println(len(outputAddresses))
  171. }
  172. }
  173. saveBlock(newBlock)
  174. }
  175. //set the next block
  176. blockHash = block.NextHash
  177. }
  178. fmt.Print("realBlocks added (blocks with Fee and Amount values): ")
  179. fmt.Println(realBlocks)
  180. fmt.Println("reached the end of blockchain")
  181. }