-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
99 lines (78 loc) · 3.09 KB
/
client.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package types
import (
"fmt"
"sort"
"strings"
proto "github.com/gogo/protobuf/proto"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
host "github.com/bianjieai/tibc-go/modules/tibc/core/24-host"
"github.com/bianjieai/tibc-go/modules/tibc/core/exported"
)
var (
_ codectypes.UnpackInterfacesMessage = IdentifiedClientState{}
_ codectypes.UnpackInterfacesMessage = ConsensusStateWithHeight{}
)
// NewIdentifiedClientState creates a new IdentifiedClientState instance
func NewIdentifiedClientState(chainName string, clientState exported.ClientState) IdentifiedClientState {
msg, ok := clientState.(proto.Message)
if !ok {
panic(fmt.Errorf("cannot proto marshal %T", clientState))
}
anyClientState, err := codectypes.NewAnyWithValue(msg)
if err != nil {
panic(err)
}
return IdentifiedClientState{
ChainName: chainName,
ClientState: anyClientState,
}
}
// UnpackInterfaces implements UnpackInterfacesMesssage.UnpackInterfaces
func (ics IdentifiedClientState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return unpacker.UnpackAny(ics.ClientState, new(exported.ClientState))
}
var _ sort.Interface = IdentifiedClientStates{}
// IdentifiedClientStates defines a slice of ClientConsensusStates that supports the sort interface
type IdentifiedClientStates []IdentifiedClientState
// Len implements sort.Interface
func (ics IdentifiedClientStates) Len() int { return len(ics) }
// Less implements sort.Interface
func (ics IdentifiedClientStates) Less(i, j int) bool { return ics[i].ChainName < ics[j].ChainName }
// Swap implements sort.Interface
func (ics IdentifiedClientStates) Swap(i, j int) { ics[i], ics[j] = ics[j], ics[i] }
// Sort is a helper function to sort the set of IdentifiedClientStates in place
func (ics IdentifiedClientStates) Sort() IdentifiedClientStates {
sort.Sort(ics)
return ics
}
// NewConsensusStateWithHeight creates a new ConsensusStateWithHeight instance
func NewConsensusStateWithHeight(height Height, consensusState exported.ConsensusState) ConsensusStateWithHeight {
msg, ok := consensusState.(proto.Message)
if !ok {
panic(fmt.Errorf("cannot proto marshal %T", consensusState))
}
anyConsensusState, err := codectypes.NewAnyWithValue(msg)
if err != nil {
panic(err)
}
return ConsensusStateWithHeight{
Height: height,
ConsensusState: anyConsensusState,
}
}
// UnpackInterfaces implements UnpackInterfacesMesssage.UnpackInterfaces
func (cswh ConsensusStateWithHeight) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return unpacker.UnpackAny(cswh.ConsensusState, new(exported.ConsensusState))
}
// ValidateClientType validates the client type. It cannot be blank or empty. It must be a valid
// client identifier when used with '0' or the maximum uint64 as the sequence.
func ValidateClientType(clientType string) error {
if strings.TrimSpace(clientType) == "" {
return sdkerrors.Wrap(ErrInvalidClientType, "client type cannot be blank")
}
if err := host.ClientIdentifierValidator(clientType); err != nil {
return sdkerrors.Wrap(err, "client type being invalid")
}
return nil
}