-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
client.go
111 lines (88 loc) · 3.64 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
100
101
102
103
104
105
106
107
108
109
110
111
package types
import (
"fmt"
"math"
"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/cosmos/cosmos-sdk/x/ibc/core/24-host"
"github.com/cosmos/cosmos-sdk/x/ibc/core/exported"
)
var (
_ codectypes.UnpackInterfacesMessage = IdentifiedClientState{}
_ codectypes.UnpackInterfacesMessage = ConsensusStateWithHeight{}
)
// NewIdentifiedClientState creates a new IdentifiedClientState instance
func NewIdentifiedClientState(clientID 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{
ClientId: clientID,
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].ClientId < ics[j].ClientId }
// 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")
}
smallestPossibleClientID := FormatClientIdentifier(clientType, 0)
largestPossibleClientID := FormatClientIdentifier(clientType, uint64(math.MaxUint64))
// IsValidClientID will check client type format and if the sequence is a uint64
if !IsValidClientID(smallestPossibleClientID) {
return sdkerrors.Wrap(ErrInvalidClientType, "")
}
if err := host.ClientIdentifierValidator(smallestPossibleClientID); err != nil {
return sdkerrors.Wrap(err, "client type results in smallest client identifier being invalid")
}
if err := host.ClientIdentifierValidator(largestPossibleClientID); err != nil {
return sdkerrors.Wrap(err, "client type results in largest client identifier being invalid")
}
return nil
}