-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathleaf.go
More file actions
74 lines (64 loc) · 1.8 KB
/
Copy pathleaf.go
File metadata and controls
74 lines (64 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package node
import (
"fmt"
"sync"
"github.com/qdm12/gotree"
)
var _ Node = (*Leaf)(nil)
// Leaf is a leaf in the trie.
type Leaf struct {
// Partial key bytes in nibbles (0 to f in hexadecimal)
Key []byte
Value []byte
// Dirty is true when the branch differs
// from the node stored in the database.
Dirty bool
HashDigest []byte
Encoding []byte
encodingMu sync.RWMutex
// Generation is incremented on every trie Snapshot() call.
// Each node also contain a certain Generation number,
// which is updated to match the trie Generation once they are
// inserted, moved or iterated over.
Generation uint64
sync.RWMutex
}
// NewLeaf creates a new leaf using the arguments given.
func NewLeaf(key, value []byte, dirty bool, generation uint64) *Leaf {
return &Leaf{
Key: key,
Value: value,
Dirty: dirty,
Generation: generation,
}
}
// Type returns LeafType.
func (l *Leaf) Type() Type {
return LeafType
}
func (l *Leaf) String() string {
return l.StringNode().String()
}
// StringNode returns a gotree compatible node for String methods.
func (l *Leaf) StringNode() (stringNode *gotree.Node) {
stringNode = gotree.New("Leaf")
stringNode.Appendf("Generation: %d", l.Generation)
stringNode.Appendf("Dirty: %t", l.Dirty)
stringNode.Appendf("Key: " + bytesToString(l.Key))
stringNode.Appendf("Value: " + bytesToString(l.Value))
stringNode.Appendf("Calculated encoding: " + bytesToString(l.Encoding))
stringNode.Appendf("Calculated digest: " + bytesToString(l.HashDigest))
return stringNode
}
func bytesToString(b []byte) (s string) {
switch {
case b == nil:
return "nil"
case len(b) <= 20:
return fmt.Sprintf("0x%x", b)
default:
return fmt.Sprintf("0x%x...%x", b[:8], b[len(b)-8:])
}
}