-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
201 lines (176 loc) · 7.15 KB
/
connection.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package types
import (
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
commitmenttypes "github.com/ci123chain/ci123chain/pkg/ibc/core/commitment/types"
"github.com/ci123chain/ci123chain/pkg/ibc/core/exported"
"github.com/ci123chain/ci123chain/pkg/ibc/core/host"
)
var _ exported.ConnectionI = (*ConnectionEnd)(nil)
//type ConnectionEnd struct {
// // client associated with this connection.
// ClientId string `protobuf:"bytes,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty" yaml:"client_id"`
// // IBC version which can be utilised to determine encodings or protocols for
// // channels or packets utilising this connection.
// Versions []*Version `protobuf:"bytes,2,rep,name=versions,proto3" json:"versions,omitempty"`
// // current state of the connection end.
// State State `protobuf:"varint,3,opt,name=state,proto3,enum=ibc.collactor.connection.v1.State" json:"state,omitempty"`
// // counterparty chain associated with this connection.
// Counterparty Counterparty `protobuf:"bytes,4,opt,name=counterparty,proto3" json:"counterparty"`
// // delay period that must pass before a consensus state can be used for packet-verification
// // NOTE: delay period logic is only implemented by some clients.
// DelayPeriod uint64 `protobuf:"varint,5,opt,name=delay_period,json=delayPeriod,proto3" json:"delay_period,omitempty" yaml:"delay_period"`
//
//}
// NewConnectionEnd creates a new ConnectionEnd instance.
func NewConnectionEnd(state State, clientID string, counterparty Counterparty, versions []*Version, delayPeriod uint64) ConnectionEnd {
return ConnectionEnd{
ClientId: clientID,
Versions: versions,
State: state,
Counterparty: counterparty,
DelayPeriod: delayPeriod,
}
}
// GetState implements the Connection interface
func (c ConnectionEnd) GetState() int32 {
return int32(c.State)
}
// GetClientID implements the Connection interface
func (c ConnectionEnd) GetClientID() string {
return c.ClientId
}
// GetCounterparty implements the Connection interface
func (c ConnectionEnd) GetCounterparty() exported.CounterpartyConnectionI {
return c.Counterparty
}
// GetVersions implements the Connection interface
func (c ConnectionEnd) GetVersions() []exported.Version {
return VersionsToExported(c.Versions)
}
// GetDelayPeriod implements the Connection interface
func (c ConnectionEnd) GetDelayPeriod() uint64 {
return c.DelayPeriod
}
// ValidateBasic implements the Connection interface.
// NOTE: the protocol supports that the connection and client IDs match the
// counterparty's.
func (c ConnectionEnd) ValidateBasic() error {
if err := host.ClientIdentifierValidator(c.ClientId); err != nil {
return sdkerrors.Wrap(err, "invalid client ID")
}
if len(c.Versions) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidVersion, "empty connection versions")
}
for _, version := range c.Versions {
if err := ValidateVersion(version); err != nil {
return err
}
}
return c.Counterparty.ValidateBasic()
}
var _ exported.CounterpartyConnectionI = (*Counterparty)(nil)
//// Counterparty defines the counterparty chain associated with a connection end.
//type Counterparty struct {
// // identifies the client on the counterparty chain associated with a given
// // connection.
// ClientId string `protobuf:"bytes,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty" yaml:"client_id"`
// // identifies the connection end on the counterparty chain associated with a
// // given connection.
// ConnectionId string `protobuf:"bytes,2,opt,name=connection_id,json=connectionId,proto3" json:"connection_id,omitempty" yaml:"connection_id"`
// // commitment merkle prefix of the counterparty chain.
// Prefix commitmenttypes.MerklePrefix `protobuf:"bytes,3,opt,name=prefix,proto3" json:"prefix"`
//}
func NewCounterparty(clientID, connectionID string, prefix commitmenttypes.MerklePrefix) Counterparty {
return Counterparty{
ClientId: clientID,
ConnectionId: connectionID,
Prefix: prefix,
}
}
// GetClientID implements the CounterpartyConnectionI interface
func (c Counterparty) GetClientID() string {
return c.ClientId
}
// GetConnectionID implements the CounterpartyConnectionI interface
func (c Counterparty) GetConnectionID() string {
return c.ConnectionId
}
// GetPrefix implements the CounterpartyConnectionI interface
func (c Counterparty) GetPrefix() exported.Prefix {
return &c.Prefix
}
// ValidateBasic performs a basic validation check of the identifiers and prefix
func (c Counterparty) ValidateBasic() error {
if c.ConnectionId != "" {
if err := host.ConnectionIdentifierValidator(c.ConnectionId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty connection ID")
}
}
if err := host.ClientIdentifierValidator(c.ClientId); err != nil {
return sdkerrors.Wrap(err, "invalid counterparty client ID")
}
if c.Prefix.Empty() {
return sdkerrors.Wrap(ErrInvalidCounterparty, "counterparty prefix cannot be empty")
}
return nil
}
//type State int32
//
//const (
// // Default State
// UNINITIALIZED State = 0
// // A connection end has just started the opening handshake.
// INIT State = 1
// // A connection end has acknowledged the handshake step on the counterparty
// // chain.
// TRYOPEN State = 2
// // A connection end has completed the handshake.
// OPEN State = 3
//)
//
//var State_name = map[int32]string{
// 0: "STATE_UNINITIALIZED_UNSPECIFIED",
// 1: "STATE_INIT",
// 2: "STATE_TRYOPEN",
// 3: "STATE_OPEN",
//}
//
//var State_value = map[string]int32{
// "STATE_UNINITIALIZED_UNSPECIFIED": 0,
// "STATE_INIT": 1,
// "STATE_TRYOPEN": 2,
// "STATE_OPEN": 3,
//}
//
//
//func (x State) String() string {
// return State_name[int32(x)]
//}
//// IdentifiedConnection defines a connection with additional connection
//// identifier field.
//type IdentifiedConnection struct {
// // connection identifier.
// Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty" yaml:"id"`
// // client associated with this connection.
// ClientId string `protobuf:"bytes,2,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty" yaml:"client_id"`
// // IBC version which can be utilised to determine encodings or protocols for
// // channels or packets utilising this connection
// Versions []*Version `protobuf:"bytes,3,rep,name=versions,proto3" json:"versions,omitempty"`
// // current state of the connection end.
// State State `protobuf:"varint,4,opt,name=state,proto3,enum=ibc.core.connection.v1.State" json:"state,omitempty"`
// // counterparty chain associated with this connection.
// Counterparty Counterparty `protobuf:"bytes,5,opt,name=counterparty,proto3" json:"counterparty"`
// // delay period associated with this connection.
// DelayPeriod uint64 `protobuf:"varint,6,opt,name=delay_period,json=delayPeriod,proto3" json:"delay_period,omitempty" yaml:"delay_period"`
//}
// NewIdentifiedConnection creates a new IdentifiedConnection instance
func NewIdentifiedConnection(connectionID string, conn ConnectionEnd) IdentifiedConnection {
return IdentifiedConnection{
Id: connectionID,
ClientId: conn.ClientId,
Versions: conn.Versions,
State: conn.State,
Counterparty: conn.Counterparty,
DelayPeriod: conn.DelayPeriod,
}
}