Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.96 KB | None | 0 0
  1. package app
  2.  
  3. import java.io.File
  4. import java.util.Scanner
  5.  
  6. import guru.nidi.graphviz.engine._
  7. import guru.nidi.graphviz.model.Factory._
  8. import fr.acinq.bitcoin.{Block, Transaction, TxIn, TxOut}
  9. import guru.nidi.graphviz.attribute.Attributes
  10. import guru.nidi.graphviz.model.{Graph, Label, Node}
  11.  
  12. import scala.collection.JavaConverters._
  13. import scala.io.Source._
  14. import scala.util.{Failure, Success, Try}
  15.  
  16. object Chainviz extends App {
  17.  
  18.   val keyboard = new Scanner(System.in)
  19.   var found = false
  20.   var block: Block = _
  21.  
  22.   while(!found) {
  23.     print("Enter full path of the block data:")
  24.     val blockFile = keyboard.nextLine()
  25.    
  26.     Try(Block.read(fromFile(blockFile).mkString)) match {
  27.       case Success(parsedBlock) =>
  28.         found = true
  29.         block = parsedBlock
  30.       case Failure(err) =>
  31.         System.err.println(s"\nError parsing block: ${err.getMessage}")
  32.         System.err.flush()
  33.     }
  34.    
  35.   }
  36.  
  37.   println(s"Parsed block ${block.header.hash.toString()}")
  38.   println(s"Found ${block.tx.size} transactions")
  39.  
  40.  
  41.   var g = graph(s"block ${block.header.hash.toString}")
  42.   .labeled(Label.of("time?"))
  43.  
  44.   var i = 0
  45.   while(i < block.tx.size){
  46.     val tx = block.tx(i)
  47.    
  48.     g = g.`with`( txToNode(tx) )
  49.     i = i + 1
  50.   }
  51.  
  52.  
  53.   printGraph(g)
  54.  
  55.  
  56.   def txToNode(tx: Transaction): Node = {
  57.     node(tx.txid.toString.substring(0, 8))
  58.       .`with`(Attributes.attr("value", valueTransacted(tx) ))
  59.       .`with`(Attributes.attr("time", block.header.time))
  60.       .`with`(Attributes.attr("inputNumber", tx.txIn.size))
  61.       .`with`(Attributes.attr("outputNumber", tx.txOut.size))
  62.   }
  63.  
  64.   def valueTransacted(tx: Transaction): Long = {
  65.     val totOut = tx.txOut.foldLeft(0L)( (acc, txOutput) => acc + txOutput.amount.amount )
  66.     totOut
  67.   }
  68.  
  69.   def printGraph(g:Graph): Unit = {
  70.     Graphviz.fromGraph(g)
  71.       .render(Format.XDOT)
  72.       .toFile(new File(s"example/ex.dot"))
  73.   }
  74.  
  75.  
  76.   println("Terminated")
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement