/
height.go
61 lines (48 loc) · 1.08 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
package base
import (
"fmt"
"strconv"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/isvalid"
)
var (
NilHeight = Height(-2)
PreGenesisHeight = Height(-1)
GenesisHeight = Height(0)
)
// Height stands for height of Block
type Height int64
func NewHeightFromString(s string) (Height, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return NilHeight, err
}
return Height(i), nil
}
func NewHeightFromBytes(b []byte) (Height, error) {
i, err := util.BytesToInt64(b)
if err != nil {
return NilHeight, err
}
return Height(i), nil
}
// IsValid checks Height.
func (ht Height) IsValid([]byte) error {
if ht < PreGenesisHeight {
return isvalid.InvalidError.Errorf("height must be greater than %d; height=%d", PreGenesisHeight, ht)
}
return nil
}
// Int64 returns int64 of height.
func (ht Height) Int64() int64 {
return int64(ht)
}
func (ht Height) Bytes() []byte {
return util.Int64ToBytes(int64(ht))
}
func (ht Height) String() string {
return fmt.Sprintf("%d", ht)
}
func (ht Height) IsEmpty() bool {
return ht == NilHeight
}