Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added visualization for tables #134

Merged
merged 3 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/pics/pics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func prefixGroups1() {
}

keys := generatePrefixGroups()
visual.StartGraph(f)
visual.StartGraph(f, false)
for i, key := range keys {
visual.QuadVertical(f, []byte(key), len(key), fmt.Sprintf("q_%x", key))
visual.Circle(f, fmt.Sprintf("e_%d", i), fmt.Sprintf("%d", i), false)
Expand Down Expand Up @@ -69,7 +69,7 @@ func prefixGroups2() {

keys := generatePrefixGroups()
sort.Strings(keys)
visual.StartGraph(f)
visual.StartGraph(f, false)
for i, key := range keys {
visual.QuadVertical(f, []byte(key), len(key), fmt.Sprintf("q_%x", key))
visual.Circle(f, fmt.Sprintf("e_%d", i), fmt.Sprintf("%d", i), false)
Expand Down Expand Up @@ -120,7 +120,7 @@ func prefixGroups3() {
}
}
}
visual.StartGraph(f)
visual.StartGraph(f, false)
var prefixStack []string
for i, key := range keys {
// Close all the groups that do not contain the current key
Expand Down Expand Up @@ -202,7 +202,7 @@ func prefixGroups4() {
tr.Update(hexKey, []byte(vs), 0)
hightlights = append(hightlights, []byte(key))
}
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(tr, f, &trie.VisualOpts{
Highlights: hightlights,
IndexColors: visual.QuadIndexColors,
Expand Down Expand Up @@ -243,7 +243,7 @@ func prefixGroups5() {
folds = append(folds, hexKey)
}
tr.Fold(folds[:8])
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(tr, f, &trie.VisualOpts{
Highlights: hightlights,
IndexColors: visual.QuadIndexColors,
Expand Down Expand Up @@ -285,7 +285,7 @@ func prefixGroups6() {
}
tr.Fold(folds[:8])
tr.Fold(folds[8:16])
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(tr, f, &trie.VisualOpts{
Highlights: hightlights,
IndexColors: visual.QuadIndexColors,
Expand Down Expand Up @@ -329,7 +329,7 @@ func prefixGroups7() {
tr.Fold(folds[8:16])
tr.Fold(folds[16:24])
tr.Fold(folds[24:])
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(tr, f, &trie.VisualOpts{
Highlights: hightlights,
IndexColors: visual.QuadIndexColors,
Expand Down Expand Up @@ -374,7 +374,7 @@ func prefixGroups8() {
}
}
tr.Fold(folds)
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(tr, f, &trie.VisualOpts{
Highlights: hightlights,
IndexColors: visual.QuadIndexColors,
Expand Down
101 changes: 82 additions & 19 deletions cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import (
"context"
"encoding/binary"
"fmt"
"math/big"
"os"
"os/exec"
"sort"

"github.com/ledgerwatch/turbo-geth/trie"
"github.com/ledgerwatch/turbo-geth/visual"

"github.com/ledgerwatch/bolt"
"github.com/ledgerwatch/turbo-geth/accounts/abi/bind"
"github.com/ledgerwatch/turbo-geth/accounts/abi/bind/backends"
"github.com/ledgerwatch/turbo-geth/cmd/pics/contracts"
Expand All @@ -26,6 +19,12 @@ import (
"github.com/ledgerwatch/turbo-geth/crypto"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/params"
"github.com/ledgerwatch/turbo-geth/trie"
"github.com/ledgerwatch/turbo-geth/visual"
"math/big"
"os"
"os/exec"
"sort"
)

func constructCodeMap(tds *state.TrieDbState) (map[common.Hash][]byte, error) {
Expand Down Expand Up @@ -55,7 +54,7 @@ func statePicture(t *trie.Trie, codeMap map[common.Hash][]byte, number int, keyC
indexColors = visual.QuadIndexColors
fontColors = visual.QuadFontColors
}
visual.StartGraph(f)
visual.StartGraph(f, false)
trie.Visual(t, f, &trie.VisualOpts{
Highlights: highlights,
IndexColors: indexColors,
Expand Down Expand Up @@ -84,7 +83,7 @@ func keyTape(t *trie.Trie, number int) error {
if err != nil {
return err
}
visual.StartGraph(f)
visual.StartGraph(f, false)
fk := trie.FullKeys(t)
sort.Strings(fk)
for i, key := range fk {
Expand All @@ -109,19 +108,80 @@ func keyTape(t *trie.Trie, number int) error {
return nil
}

func stateDatabaseMap(db *bolt.DB, number int) error {
filename := fmt.Sprintf("state_%d.dot", number)
f, err := os.Create(filename)
if err != nil {
return err
}
i := 0
visual.StartGraph(f, true)

if err := db.View(func(readTx *bolt.Tx) error {
return readTx.ForEach(func(name []byte, b *bolt.Bucket) error {

visual.StartCluster(f, i, string(name))

if err := b.ForEach(func(k, v []byte) error {
fmt.Fprintf(f, `k_%d -> v_%d`, i, i)
keyKeyBytes := &trie.Keybytes{
Data: k,
Odd: false,
Terminating: false,
}
valKeyBytes := &trie.Keybytes{
Data: v,
Odd: false,
Terminating: false,
}
key := keyKeyBytes.ToHex()
val := valKeyBytes.ToHex()
visual.Horizontal(f, key, 0, fmt.Sprintf("k_%d", i), visual.HexIndexColors, visual.HexFontColors, 110)
if len(val) > 0 {
if len(val) > 32 {
shortenedVal := val[:32]
visual.Horizontal(f, shortenedVal, 0, fmt.Sprintf("v_%d", i), visual.HexIndexColors, visual.HexFontColors, 100)
} else {
visual.Horizontal(f, val, 0, fmt.Sprintf("v_%d", i), visual.HexIndexColors, visual.HexFontColors, 110)
}
} else {
visual.Circle(f, fmt.Sprintf("v_%d", i), "...", false)
}
i++
return nil
}); err != nil {
return err
}
visual.EndCluster(f)
return nil
})
}); err != nil {
panic(err)
}
visual.EndGraph(f)
if err := f.Close(); err != nil {
return err
}
cmd := exec.Command("dot", "-Tpng:gd", "-O", filename)
if output, err := cmd.CombinedOutput(); err != nil {
fmt.Printf("error: %v, output: %s\n", err, output)
}
return nil
}

func initialState1() error {
fmt.Printf("Initial state 1\n")
// Configure and generate a sample block chain
var (
db = ethdb.NewMemDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
key1, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
address = crypto.PubkeyToAddress(key.PublicKey)
address1 = crypto.PubkeyToAddress(key1.PublicKey)
address2 = crypto.PubkeyToAddress(key2.PublicKey)
theAddr = common.Address{1}
gspec = &core.Genesis{
db, dbBolt = ethdb.NewMemDatabase2()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
key1, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
address = crypto.PubkeyToAddress(key.PublicKey)
address1 = crypto.PubkeyToAddress(key1.PublicKey)
address2 = crypto.PubkeyToAddress(key2.PublicKey)
theAddr = common.Address{1}
gspec = &core.Genesis{
Config: &params.ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: new(big.Int),
Expand Down Expand Up @@ -415,5 +475,8 @@ func initialState1() error {
return err
}

if err = stateDatabaseMap(dbBolt, 16); err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ require (
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20180302121509-abf0ba0be5d5
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gotest.tools v2.2.0+incompatible // indirect
)
)
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
35 changes: 29 additions & 6 deletions visual/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import (
"io"
)

func StartGraph(w io.Writer) {
fmt.Fprintf(w,
`digraph trie {
node [shape=none margin=0 width=0 height=0]
edge [dir = none headport=n tailport=s]
`)
func StartGraph(w io.Writer, tall bool) {
if tall {
fmt.Fprintf(w,
`digraph trie {
rankdir=LR;
node [shape=none margin=0 width=0 height=0]
edge [dir = none headport=w tailport=e]
`)
} else {
fmt.Fprintf(w,
`digraph trie {
node [shape=none margin=0 width=0 height=0]
edge [dir = none headport=n tailport=s]
`)
}
}

func EndGraph(w io.Writer) {
Expand All @@ -36,3 +45,17 @@ func Box(w io.Writer, name string, label string) {
`%s [label="%s" shape=box margin=0.1 width=0 height=0 fillcolor="#FF6403" style=filled];
`, name, label)
}

func StartCluster(w io.Writer, number int, label string) {
fmt.Fprintf(w,
`subgraph cluster_%d {
label = "%s";
color = black;
`, number, label)
}

func EndCluster(w io.Writer) {
fmt.Fprintf(w,
`}
`)
}
35 changes: 35 additions & 0 deletions visual/hexary.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,38 @@ func Vertical(w io.Writer, hex []byte, highlighted int, name string, indexColors
>];
`)
}

func Horizontal(w io.Writer, hex []byte, highlighted int, name string, indexColors []string, fontColors []string, compression int) {
fmt.Fprintf(w,
`
%s [label=<
<table border="0" color="#000000" cellborder="1" cellspacing="0">
<tr>`, name)
if hex[len(hex)-1] == 16 {
hex = hex[:len(hex)-1]
} else {
compression = 0 // No compression for non-terminal keys
}
for i, h := range hex {
if i < len(hex)-compression-2 || i > len(hex)-3 {
if i < highlighted {
fmt.Fprintf(w,
` <td bgcolor="%s"><font color="%s">%s</font></td>
`, indexColors[h], fontColors[h], hexIndices[h])
} else {
fmt.Fprintf(w,
` <td bgcolor="%s"></td>
`, indexColors[h])
}
} else if compression > 0 && i == len(hex)-3 {
fmt.Fprintf(w,
` <td border="0">-</td>
`)
}
}
fmt.Fprintf(w,
`
</tr></table>
>];
`)
}