/
height.go
49 lines (41 loc) · 1.21 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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package base
import (
"context"
"encoding/binary"
"fmt"
"github.com/AssetMantle/schema/go/types"
"github.com/AssetMantle/schema/go/types/constants"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
)
var _ types.Height = (*Height)(nil)
func (height *Height) ValidateBasic() error {
if height.Value < constants.InfiniteHeight {
return fmt.Errorf("height value %d is out of bound", height.Value)
}
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 difference := height.Get() - compareHeight.Get(); difference == 0 {
return 0
} else if difference > 0 || height.Get() == constants.InfiniteHeight {
return 1
}
return -1
}
func (height *Height) Get() int64 { return height.Value }
func NewHeight(value int64) types.Height {
if value < 0 {
value = constants.InfiniteHeight
}
return &Height{Value: value}
}
func CurrentHeight(context context.Context) types.Height {
return NewHeight(sdkTypes.UnwrapSDKContext(context).BlockHeight())
}