/
channel.go
172 lines (152 loc) · 4.96 KB
/
channel.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package types
import (
"strings"
sdk "github.com/creatachain/creata-sdk/types"
sdkerrors "github.com/creatachain/creata-sdk/types/errors"
host "github.com/creatachain/creata-sdk/x/icp/core/24-host"
"github.com/creatachain/creata-sdk/x/icp/core/exported"
)
var (
_ exported.ChannelI = (*Channel)(nil)
_ exported.CounterpartyChannelI = (*Counterparty)(nil)
)
// NewChannel creates a new Channel instance
func NewChannel(
state State, ordering Order, counterparty Counterparty,
hops []string, version string,
) Channel {
return Channel{
State: state,
Ordering: ordering,
Counterparty: counterparty,
ConnectionHops: hops,
Version: version,
}
}
// GetState implements Channel interface.
func (ch Channel) GetState() int32 {
return int32(ch.State)
}
// GetOrdering implements Channel interface.
func (ch Channel) GetOrdering() int32 {
return int32(ch.Ordering)
}
// GetCounterparty implements Channel interface.
func (ch Channel) GetCounterparty() exported.CounterpartyChannelI {
return ch.Counterparty
}
// GetConnectionHops implements Channel interface.
func (ch Channel) GetConnectionHops() []string {
return ch.ConnectionHops
}
// GetVersion implements Channel interface.
func (ch Channel) GetVersion() string {
return ch.Version
}
// ValidateBasic performs a basic validation of the channel fields
func (ch Channel) ValidateBasic() error {
if ch.State == UNINITIALIZED {
return ErrInvalidChannelState
}
if !(ch.Ordering == ORDERED || ch.Ordering == UNORDERED) {
return sdkerrors.Wrap(ErrInvalidChannelOrdering, ch.Ordering.String())
}
if len(ch.ConnectionHops) != 1 {
return sdkerrors.Wrap(
ErrTooManyConnectionHops,
"current ICP version only supports one connection hop",
)
}
if err := host.ConnectionIdentifierValidator(ch.ConnectionHops[0]); err != nil {
return sdkerrors.Wrap(err, "invalid connection hop ID")
}
return ch.Counterparty.ValidateBasic()
}
// NewCounterparty returns a new Counterparty instance
func NewCounterparty(portID, channelID string) Counterparty {
return Counterparty{
PortId: portID,
ChannelId: channelID,
}
}
// GetPortID implements CounterpartyChannelI interface
func (c Counterparty) GetPortID() string {
return c.PortId
}
// GetChannelID implements CounterpartyChannelI interface
func (c Counterparty) GetChannelID() string {
return c.ChannelId
}
// ValidateBasic performs a basic validation check of the identifiers
func (c Counterparty) ValidateBasic() error {
if err := host.PortIdentifierValidator(c.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty port ID")
}
if c.ChannelId != "" {
if err := host.ChannelIdentifierValidator(c.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty channel ID")
}
}
return nil
}
// NewIdentifiedChannel creates a new IdentifiedChannel instance
func NewIdentifiedChannel(portID, channelID string, ch Channel) IdentifiedChannel {
return IdentifiedChannel{
State: ch.State,
Ordering: ch.Ordering,
Counterparty: ch.Counterparty,
ConnectionHops: ch.ConnectionHops,
Version: ch.Version,
PortId: portID,
ChannelId: channelID,
}
}
// ValidateBasic performs a basic validation of the identifiers and channel fields.
func (ic IdentifiedChannel) ValidateBasic() error {
if err := host.ChannelIdentifierValidator(ic.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid channel ID")
}
if err := host.PortIdentifierValidator(ic.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid port ID")
}
channel := NewChannel(ic.State, ic.Ordering, ic.Counterparty, ic.ConnectionHops, ic.Version)
return channel.ValidateBasic()
}
// NewResultAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Result
// type in the Response field.
func NewResultAcknowledgement(result []byte) Acknowledgement {
return Acknowledgement{
Response: &Acknowledgement_Result{
Result: result,
},
}
}
// NewErrorAcknowledgement returns a new instance of Acknowledgement using an Acknowledgement_Error
// type in the Response field.
func NewErrorAcknowledgement(err string) Acknowledgement {
return Acknowledgement{
Response: &Acknowledgement_Error{
Error: err,
},
}
}
// GetBytes is a helper for serialising acknowledgements
func (ack Acknowledgement) GetBytes() []byte {
return sdk.MustSortJSON(SubModuleCdc.MustMarshalJSON(&ack))
}
// ValidateBasic performs a basic validation of the acknowledgement
func (ack Acknowledgement) ValidateBasic() error {
switch resp := ack.Response.(type) {
case *Acknowledgement_Result:
if len(resp.Result) == 0 {
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement result cannot be empty")
}
case *Acknowledgement_Error:
if strings.TrimSpace(resp.Error) == "" {
return sdkerrors.Wrap(ErrInvalidAcknowledgement, "acknowledgement error cannot be empty")
}
default:
return sdkerrors.Wrapf(ErrInvalidAcknowledgement, "unsupported acknowledgement response field type %T", resp)
}
return nil
}