|
3 | 3 |
|
4 | 4 | package trie
|
5 | 5 |
|
6 |
| -import ( |
7 |
| - "bytes" |
8 |
| - "fmt" |
9 |
| - |
10 |
| - "github.com/ChainSafe/gossamer/internal/trie/node" |
11 |
| - "github.com/ChainSafe/gossamer/internal/trie/pools" |
12 |
| - "github.com/ChainSafe/gossamer/lib/common" |
13 |
| - |
14 |
| - "github.com/disiqueira/gotree" |
15 |
| -) |
16 |
| - |
17 | 6 | // String returns the trie stringified through pre-order traversal
|
18 | 7 | func (t *Trie) String() string {
|
19 | 8 | if t.root == nil {
|
20 | 9 | return "empty"
|
21 | 10 | }
|
22 | 11 |
|
23 |
| - tree := gotree.New(fmt.Sprintf("Trie root=0x%x", t.root.GetHash())) |
24 |
| - t.string(tree, t.root, 0) |
25 |
| - return fmt.Sprintf("\n%s", tree.Print()) |
26 |
| -} |
27 |
| - |
28 |
| -func (t *Trie) string(tree gotree.Tree, curr Node, idx int) { |
29 |
| - switch c := curr.(type) { |
30 |
| - case *node.Branch: |
31 |
| - buffer := pools.EncodingBuffers.Get().(*bytes.Buffer) |
32 |
| - buffer.Reset() |
33 |
| - |
34 |
| - _ = c.Encode(buffer) |
35 |
| - encoding := buffer.Bytes() |
36 |
| - |
37 |
| - var bstr string |
38 |
| - if len(encoding) > 1024 { |
39 |
| - bstr = fmt.Sprintf("idx=%d %s hash=%x gen=%d", |
40 |
| - idx, c, common.MustBlake2bHash(encoding), c.GetGeneration()) |
41 |
| - } else { |
42 |
| - bstr = fmt.Sprintf("idx=%d %s encode=%x gen=%d", idx, c.String(), encoding, c.GetGeneration()) |
43 |
| - } |
44 |
| - |
45 |
| - pools.EncodingBuffers.Put(buffer) |
46 |
| - |
47 |
| - sub := tree.Add(bstr) |
48 |
| - for i, child := range c.Children { |
49 |
| - if child != nil { |
50 |
| - t.string(sub, child, i) |
51 |
| - } |
52 |
| - } |
53 |
| - case *node.Leaf: |
54 |
| - buffer := pools.EncodingBuffers.Get().(*bytes.Buffer) |
55 |
| - buffer.Reset() |
56 |
| - |
57 |
| - _ = c.Encode(buffer) |
58 |
| - |
59 |
| - encoding := buffer.Bytes() |
60 |
| - |
61 |
| - var bstr string |
62 |
| - if len(encoding) > 1024 { |
63 |
| - bstr = fmt.Sprintf("idx=%d %s hash=%x gen=%d", |
64 |
| - idx, c.String(), common.MustBlake2bHash(encoding), c.GetGeneration()) |
65 |
| - } else { |
66 |
| - bstr = fmt.Sprintf("idx=%d %s encode=%x gen=%d", idx, c.String(), encoding, c.GetGeneration()) |
67 |
| - } |
68 |
| - |
69 |
| - pools.EncodingBuffers.Put(buffer) |
70 |
| - |
71 |
| - tree.Add(bstr) |
72 |
| - default: |
73 |
| - return |
74 |
| - } |
| 12 | + return t.root.String() |
75 | 13 | }
|
0 commit comments