-
Notifications
You must be signed in to change notification settings - Fork 606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: allow ics27 to select and return default JSON encoded metadata #1550
Changes from 18 commits
20d1003
c25ca5f
2d8d25f
3941f55
ae9fef6
3247100
44c7293
0f3f132
64dab2c
76d0eef
af201e0
f8928f6
bf866c8
909f2e5
ffdfc56
8975dd2
9a50acc
31e7455
e078e9d
f08c217
d9cbb0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,26 +28,35 @@ func (k Keeper) OnChanOpenInit( | |
chanCap *capabilitytypes.Capability, | ||
counterparty channeltypes.Counterparty, | ||
version string, | ||
) error { | ||
) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the implication of changing an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this instance yes, the In other situations we may need to more careful changing function sigs but this doesn't really cause any issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to add to that: in general, we would also update the ibc spec for changes to these fn sigs. But like @damiannolan said the calling func already has this signature. |
||
if order != channeltypes.ORDERED { | ||
return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order) | ||
return "", sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order) | ||
} | ||
|
||
if !strings.HasPrefix(portID, icatypes.PortPrefix) { | ||
return sdkerrors.Wrapf(icatypes.ErrInvalidControllerPort, "expected %s{owner-account-address}, got %s", icatypes.PortPrefix, portID) | ||
return "", sdkerrors.Wrapf(icatypes.ErrInvalidControllerPort, "expected %s{owner-account-address}, got %s", icatypes.PortPrefix, portID) | ||
} | ||
|
||
if counterparty.PortId != icatypes.PortID { | ||
return sdkerrors.Wrapf(icatypes.ErrInvalidHostPort, "expected %s, got %s", icatypes.PortID, counterparty.PortId) | ||
return "", sdkerrors.Wrapf(icatypes.ErrInvalidHostPort, "expected %s, got %s", icatypes.PortID, counterparty.PortId) | ||
} | ||
|
||
var metadata icatypes.Metadata | ||
if err := icatypes.ModuleCdc.UnmarshalJSON([]byte(version), &metadata); err != nil { | ||
return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata") | ||
if strings.TrimSpace(version) == "" { | ||
connection, err := k.channelKeeper.GetConnection(ctx, connectionHops[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is connectionHops hardcoded to the first value of the array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first value is the source chain connection end AFAIK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ConnectionHops is an ordered list of connection ids, so using Channel spec says:
|
||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a super tiny nit but you have the err statement in the second else clause in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But we need to use the if err := invokeFunc(); err != nil {
// handle err
} If we use the following then we can't use the connection below, outside the scope of the conditional: if connection, err := k.channelKeeper.GetConnection(ctx, connectionHops[0]); err != nil {
// connection and err are only available at this level of scope
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, that makes sense 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should technically be able to if you put the usage in an else clause, but I personally prefer the way you have it now rather than adding additional |
||
return "", err | ||
} | ||
|
||
metadata = icatypes.NewDefaultMetadata(connectionHops[0], connection.GetCounterparty().GetConnectionID()) | ||
chatton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
if err := icatypes.ModuleCdc.UnmarshalJSON([]byte(version), &metadata); err != nil { | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] rather than having nested if/else clauses it might be cleaner to have a helper function to return the metadata (using early returns) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took a look at this and feel like there's not a massive difference making a helper function. You'd either need to return a new metadata type or take a pointer and a bunch of other args too... I'm pretty indifferent about it |
||
return "", sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata") | ||
} | ||
} | ||
|
||
if err := icatypes.ValidateControllerMetadata(ctx, k.channelKeeper, connectionHops, metadata); err != nil { | ||
return err | ||
return "", err | ||
} | ||
|
||
activeChannelID, found := k.GetActiveChannelID(ctx, connectionHops[0], portID) | ||
|
@@ -58,15 +67,15 @@ func (k Keeper) OnChanOpenInit( | |
} | ||
|
||
if channel.State == channeltypes.OPEN { | ||
return sdkerrors.Wrapf(icatypes.ErrActiveChannelAlreadySet, "existing active channel %s for portID %s is already OPEN", activeChannelID, portID) | ||
return "", sdkerrors.Wrapf(icatypes.ErrActiveChannelAlreadySet, "existing active channel %s for portID %s is already OPEN", activeChannelID, portID) | ||
} | ||
|
||
if !icatypes.IsPreviousMetadataEqual(channel.Version, metadata) { | ||
return sdkerrors.Wrap(icatypes.ErrInvalidVersion, "previous active channel metadata does not match provided version") | ||
return "", sdkerrors.Wrap(icatypes.ErrInvalidVersion, "previous active channel metadata does not match provided version") | ||
} | ||
} | ||
|
||
return nil | ||
return string(icatypes.ModuleCdc.MustMarshalJSON(&metadata)), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to make sure I understand, if this panics this is handled at the sdk level right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, its caught by the grpc recovery interceptor (essentially a middleware handler in grpc land) |
||
} | ||
|
||
// OnChanOpenAck sets the active channel for the interchain account/owner pair | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,29 @@ func NewMetadata(version, controllerConnectionID, hostConnectionID, accAddress, | |
} | ||
} | ||
|
||
// NewDefaultMetadata creates and returns a new ICS27 Metadata instance containing the default ICS27 Metadata values | ||
// with the provided controller and host connection identifiers | ||
func NewDefaultMetadata(connectionConnectionID, hostConnectionID string) Metadata { | ||
metadata := Metadata{ | ||
ControllerConnectionId: connectionConnectionID, | ||
HostConnectionId: hostConnectionID, | ||
Encoding: EncodingProtobuf, | ||
TxType: TxTypeSDKMultiMsg, | ||
Version: Version, | ||
} | ||
|
||
return metadata | ||
} | ||
|
||
// NewDefaultMetadataString creates and returns a new JSON encoded version string containing the default ICS27 Metadata values | ||
// with the provided controller and host connection identifiers | ||
func NewDefaultMetadataString(controllerConnectionID, hostConnectionID string) string { | ||
metadata := Metadata{ | ||
Version: Version, | ||
ControllerConnectionId: controllerConnectionID, | ||
HostConnectionId: hostConnectionID, | ||
Encoding: EncodingProtobuf, | ||
TxType: TxTypeSDKMultiMsg, | ||
Version: Version, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can replace now this whole object initialization with a call to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call! |
||
|
||
return string(ModuleCdc.MustMarshalJSON(&metadata)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice readability improvement 🥇