Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app
- import java.io.File
- import java.util.Scanner
- import guru.nidi.graphviz.engine._
- import guru.nidi.graphviz.model.Factory._
- import fr.acinq.bitcoin.{Block, Transaction, TxIn, TxOut}
- import guru.nidi.graphviz.attribute.Attributes
- import guru.nidi.graphviz.model.{Graph, Label, Node}
- import scala.collection.JavaConverters._
- import scala.io.Source._
- import scala.util.{Failure, Success, Try}
- object Chainviz extends App {
- val keyboard = new Scanner(System.in)
- var found = false
- var block: Block = _
- while(!found) {
- print("Enter full path of the block data:")
- val blockFile = keyboard.nextLine()
- Try(Block.read(fromFile(blockFile).mkString)) match {
- case Success(parsedBlock) =>
- found = true
- block = parsedBlock
- case Failure(err) =>
- System.err.println(s"\nError parsing block: ${err.getMessage}")
- System.err.flush()
- }
- }
- println(s"Parsed block ${block.header.hash.toString()}")
- println(s"Found ${block.tx.size} transactions")
- var g = graph(s"block ${block.header.hash.toString}")
- .labeled(Label.of("time?"))
- var i = 0
- while(i < block.tx.size){
- val tx = block.tx(i)
- g = g.`with`( txToNode(tx) )
- i = i + 1
- }
- printGraph(g)
- def txToNode(tx: Transaction): Node = {
- node(tx.txid.toString.substring(0, 8))
- .`with`(Attributes.attr("value", valueTransacted(tx) ))
- .`with`(Attributes.attr("time", block.header.time))
- .`with`(Attributes.attr("inputNumber", tx.txIn.size))
- .`with`(Attributes.attr("outputNumber", tx.txOut.size))
- }
- def valueTransacted(tx: Transaction): Long = {
- val totOut = tx.txOut.foldLeft(0L)( (acc, txOutput) => acc + txOutput.amount.amount )
- totOut
- }
- def printGraph(g:Graph): Unit = {
- Graphviz.fromGraph(g)
- .render(Format.XDOT)
- .toFile(new File(s"example/ex.dot"))
- }
- println("Terminated")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement