-
Notifications
You must be signed in to change notification settings - Fork 580
/
connection.go
128 lines (110 loc) · 3.96 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
package types
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)
var _ exported.ConnectionI = (*ConnectionEnd)(nil)
// 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 ProtoVersionsToExported(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)
// NewCounterparty creates a new Counterparty instance.
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
}
// 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,
}
}
// ValidateBasic performs a basic validation of the connection identifier and connection fields.
func (ic IdentifiedConnection) ValidateBasic() error {
if err := host.ConnectionIdentifierValidator(ic.Id); err != nil {
return sdkerrors.Wrap(err, "invalid connection ID")
}
connection := NewConnectionEnd(ic.State, ic.ClientId, ic.Counterparty, ic.Versions, ic.DelayPeriod)
return connection.ValidateBasic()
}