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.

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