Skip to content

Commit

Permalink
simple dot graph gen
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Jun 18, 2024
1 parent f43e88e commit 9bc9f7d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ferranbt/fastssz
go 1.18

require (
github.com/emicklei/dot v1.6.2
github.com/golang/snappy v0.0.3
github.com/minio/sha256-simd v1.0.0
github.com/mitchellh/mapstructure v1.3.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/dot v1.6.2 h1:08GN+DD79cy/tzN6uLCT84+2Wk9u+wvqP+Hkx/dIR8A=
github.com/emicklei/dot v1.6.2/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
31 changes: 31 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"math"

"github.com/emicklei/dot"
)

// Proof represents a merkle proof against a general index.
Expand Down Expand Up @@ -86,6 +89,34 @@ type Node struct {
value []byte
}

func (n *Node) Draw(w io.Writer) {
g := dot.NewGraph(dot.Directed)
n.draw(1, g)
g.Write(w)
}

func (n *Node) draw(levelOrder int, g *dot.Graph) dot.Node {
var h string
if n.left != nil || n.right != nil {
h = hex.EncodeToString(n.Hash())
}
if n.value != nil {
h = hex.EncodeToString(n.value)
}
dn := g.Node(fmt.Sprintf("n%d", levelOrder)).
Label(fmt.Sprintf("%d\n%s..%s", levelOrder, h[:3], h[len(h)-3:]))

if n.left != nil {
ln := n.left.draw(2*levelOrder, g)
g.Edge(dn, ln).Label("0")
}
if n.right != nil {
rn := n.right.draw(2*levelOrder+1, g)
g.Edge(dn, rn).Label("1")
}
return dn
}

func (n *Node) Show(maxDepth int) {
fmt.Printf("--- Show node ---\n")
n.show(0, maxDepth)
Expand Down

0 comments on commit 9bc9f7d

Please sign in to comment.