-
Notifications
You must be signed in to change notification settings - Fork 179
/
height.go
83 lines (68 loc) · 1.37 KB
/
height.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package request
import (
"fmt"
"math"
"strconv"
)
const sealed = "sealed"
const final = "final"
// Special height values
const SealedHeight uint64 = math.MaxUint64 - 1
const FinalHeight uint64 = math.MaxUint64 - 2
const EmptyHeight uint64 = math.MaxUint64 - 3
type Height uint64
func (h *Height) Parse(raw string) error {
if raw == "" { // allow empty
*h = Height(EmptyHeight)
return nil
}
if raw == sealed {
*h = Height(SealedHeight)
return nil
}
if raw == final {
*h = Height(FinalHeight)
return nil
}
height, err := strconv.ParseUint(raw, 0, 64)
if err != nil {
return fmt.Errorf("invalid height format")
}
if height >= EmptyHeight {
return fmt.Errorf("invalid height value")
}
*h = Height(height)
return nil
}
func (h Height) Flow() uint64 {
return uint64(h)
}
type Heights []Height
func (h *Heights) Parse(raw []string) error {
var height Height
heights := make([]Height, 0)
uniqueHeights := make(map[string]bool)
for _, r := range raw {
err := height.Parse(r)
if err != nil {
return err
}
// don't include empty heights
if height == Height(EmptyHeight) {
continue
}
if !uniqueHeights[r] {
uniqueHeights[r] = true
heights = append(heights, height)
}
}
*h = heights
return nil
}
func (h Heights) Flow() []uint64 {
heights := make([]uint64, len(h))
for i, he := range h {
heights[i] = he.Flow()
}
return heights
}