-
Notifications
You must be signed in to change notification settings - Fork 1
/
height.go
50 lines (40 loc) · 1.1 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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package base
import (
"context"
"encoding/binary"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
errorConstants "github.com/AssetMantle/schema/go/errors/constants"
"github.com/AssetMantle/schema/go/types"
)
var _ types.Height = (*Height)(nil)
func (height *Height) ValidateBasic() error {
if height.Value < -1 {
return errorConstants.IncorrectFormat
}
return nil
}
func (height *Height) Bytes() []byte {
Bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(Bytes, uint64(height.Get()))
return Bytes
}
func (height *Height) Compare(compareHeight types.Height) int {
if height.Get() > compareHeight.Get() {
return 1
} else if height.Get() < compareHeight.Get() {
return -1
}
return 0
}
func (height *Height) Get() int64 { return height.Value }
func NewHeight(value int64) types.Height {
if value < 0 {
value = -1
}
return &Height{Value: value}
}
func CurrentHeight(context context.Context) types.Height {
return NewHeight(sdkTypes.UnwrapSDKContext(context).BlockHeight())
}