forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_path.go
55 lines (49 loc) · 1.09 KB
/
proof_path.go
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
package iavl
import (
"fmt"
"strings"
)
//----------------------------------------
// PathToLeaf represents an inner path to a leaf node.
// Note that the nodes are ordered such that the last one is closest
// to the root of the tree.
type PathToLeaf []ProofInnerNode
func (pl PathToLeaf) String() string {
return pl.stringIndented("")
}
func (pl PathToLeaf) stringIndented(indent string) string {
if len(pl) == 0 {
return "empty-PathToLeaf"
}
strs := make([]string, 0, len(pl))
for i, pin := range pl {
if i == 20 {
strs = append(strs, fmt.Sprintf("... (%v total)", len(pl)))
break
}
strs = append(strs, fmt.Sprintf("%v:%v", i, pin.stringIndented(indent+" ")))
}
return fmt.Sprintf(`PathToLeaf{
%s %v
%s}`,
indent, strings.Join(strs, "\n"+indent+" "),
indent)
}
// returns -1 if invalid.
func (pl PathToLeaf) Index() (idx int64) {
for i, node := range pl {
switch {
case node.Left == nil:
continue
case node.Right == nil:
if i < len(pl)-1 {
idx += node.Size - pl[i+1].Size
} else {
idx += node.Size - 1
}
default:
return -1
}
}
return idx
}