forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 13
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
imp: remove kv generator and construct expected channel for both single and multi hop cases #45
Open
crodriguezvega
wants to merge
4
commits into
polymerdao:polymer/multihop-main
Choose a base branch
from
crodriguezvega:carlos/alternative-expected-channel-state-construction
base: polymer/multihop-main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bda9276
some improvements (naming, documentation, errors)
crodriguezvega 46b8d05
Merge branch 'polymerdao:polymer/multihop-main' into polymer/multihop…
crodriguezvega 17196d0
imp: remove kv generator and construct expected channel for both sing…
crodriguezvega 17638e4
remove unused function
crodriguezvega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,72 +152,55 @@ func (k Keeper) ChanOpenTry( | |
) | ||
} | ||
|
||
// handle multihop case | ||
var counterpartyHops []string | ||
if len(connectionHops) > 1 { | ||
kvGenerator := func(mProof *types.MsgMultihopProofs, lastHopConnectionEnd *connectiontypes.ConnectionEnd) (string, []byte, error) { | ||
// check version support | ||
if err := checkVersion(lastHopConnectionEnd, order); err != nil { | ||
return "", nil, err | ||
} | ||
|
||
// channel end storekey of the counterparty channel on the other end of the multihop channel | ||
key := host.ChannelPath(counterparty.PortId, counterparty.ChannelId) | ||
|
||
// connection hops | ||
counterpartyHops, err := mProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
// expectedCounterparty is the counterparty of the counterparty's channel end (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, "") | ||
expectedChannel := types.NewChannel(types.INIT, order, expectedCounterparty, counterpartyHops, counterpartyVersion) | ||
|
||
// expected value bytes | ||
bz, err := k.cdc.Marshal(&expectedChannel) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
return key, bz, nil | ||
var multihopProof types.MsgMultihopProofs | ||
if err := k.cdc.Unmarshal(proofInit, &multihopProof); err != nil { | ||
return "", nil, err | ||
} | ||
|
||
if err := k.connectionKeeper.VerifyMultihopMembership( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
connectionHops, kvGenerator); err != nil { | ||
// get the last hop connection on the other side of the multihop channel | ||
// the last hop connection is the connection end on the chain before the counterparty multihop chain | ||
lastHopConnectionEnd, err := multihopProof.GetLastHopConnectionEnd(k.cdc, connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
} else { | ||
// determine counterparty hops | ||
counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
|
||
// check version support | ||
if err := checkVersion(&connectionEnd, order); err != nil { | ||
if err := checkVersion(lastHopConnectionEnd, order); err != nil { | ||
return "", nil, err | ||
} | ||
|
||
// expectedCounterparty is the counterparty of the counterparty's channel end (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, "") | ||
expectedChannel := types.NewChannel( | ||
types.INIT, order, expectedCounterparty, | ||
counterpartyHops, counterpartyVersion, | ||
) | ||
|
||
if err := k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
counterparty.PortId, counterparty.ChannelId, expectedChannel, | ||
); err != nil { | ||
counterpartyHops, err = multihopProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
} else { | ||
// check version support | ||
if err := checkVersion(&connectionEnd, order); err != nil { | ||
return "", nil, err | ||
} | ||
|
||
counterpartyHops = []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
} | ||
|
||
var ( | ||
capKey *capabilitytypes.Capability | ||
err error | ||
expectedCounterparty := types.NewCounterparty(portID, "") | ||
expectedChannel := types.NewChannel( | ||
types.INIT, | ||
order, | ||
expectedCounterparty, | ||
counterpartyHops, | ||
counterpartyVersion, | ||
) | ||
|
||
capKey, err = k.scopedKeeper.NewCapability(ctx, host.ChannelCapabilityPath(portID, channelID)) | ||
if err := k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
counterparty.PortId, counterparty.ChannelId, expectedChannel, | ||
); err != nil { | ||
return "", nil, err | ||
} | ||
|
||
capKey, err := k.scopedKeeper.NewCapability(ctx, host.ChannelCapabilityPath(portID, channelID)) | ||
if err != nil { | ||
return "", nil, errorsmod.Wrapf(err, "could not create channel capability for port ID %s and channel ID %s", portID, channelID) | ||
} | ||
|
@@ -288,46 +271,39 @@ func (k Keeper) ChanOpenAck( | |
"connection state is not OPEN (got %s)", connectiontypes.State(connectionEnd.GetState()).String(), | ||
) | ||
} | ||
// verify multihop proof | ||
if len(channel.ConnectionHops) > 1 { | ||
|
||
kvGenerator := func(mProof *types.MsgMultihopProofs, _ *connectiontypes.ConnectionEnd) (string, []byte, error) { | ||
key := host.ChannelPath(channel.Counterparty.PortId, counterpartyChannelID) | ||
|
||
counterpartyHops, err := mProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
expectedCounterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.TRYOPEN, channel.Ordering, expectedCounterparty, | ||
counterpartyHops, counterpartyVersion, | ||
) | ||
value, err := expectedChannel.Marshal() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return key, value, nil | ||
var counterpartyHops []string | ||
if len(channel.ConnectionHops) > 1 { | ||
var ( | ||
err error | ||
multihopProof types.MsgMultihopProofs | ||
) | ||
if err = k.cdc.Unmarshal(proofTry, &multihopProof); err != nil { | ||
return err | ||
} | ||
|
||
return k.connectionKeeper.VerifyMultihopMembership( | ||
ctx, connectionEnd, proofHeight, proofTry, | ||
channel.ConnectionHops, kvGenerator) | ||
|
||
counterpartyHops, err = multihopProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
expectedCounterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.TRYOPEN, channel.Ordering, expectedCounterparty, | ||
counterpartyHops, counterpartyVersion, | ||
) | ||
|
||
return k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofTry, | ||
channel.Counterparty.PortId, counterpartyChannelID, | ||
expectedChannel, | ||
) | ||
counterpartyHops = []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
} | ||
|
||
// counterparty of the counterparty channel end (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.TRYOPEN, | ||
channel.Ordering, | ||
expectedCounterparty, | ||
counterpartyHops, | ||
counterpartyVersion, | ||
) | ||
|
||
return k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofTry, | ||
channel.Counterparty.PortId, counterpartyChannelID, | ||
expectedChannel) | ||
} | ||
|
||
// WriteOpenAckChannel writes an updated channel state for the successful OpenAck handshake step. | ||
|
@@ -394,43 +370,38 @@ func (k Keeper) ChanOpenConfirm( | |
) | ||
} | ||
|
||
// verify multihop proof or standard proof | ||
var counterpartyHops []string | ||
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 code below setting the counterparty hops is pretty much repeated in all handlers, so we could rug it under some common function. |
||
if len(channel.ConnectionHops) > 1 { | ||
kvGenerator := func(mProof *types.MsgMultihopProofs, _ *connectiontypes.ConnectionEnd) (string, []byte, error) { | ||
key := host.ChannelPath(channel.Counterparty.PortId, channel.Counterparty.ChannelId) | ||
|
||
counterpartyHops, err := mProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
counterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.OPEN, channel.Ordering, counterparty, | ||
counterpartyHops, channel.Version, | ||
) | ||
value, err := expectedChannel.Marshal() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return key, value, nil | ||
var ( | ||
err error | ||
multihopProof types.MsgMultihopProofs | ||
) | ||
if err = k.cdc.Unmarshal(proofAck, &multihopProof); err != nil { | ||
return err | ||
} | ||
|
||
return k.connectionKeeper.VerifyMultihopMembership( | ||
ctx, connectionEnd, proofHeight, proofAck, | ||
channel.ConnectionHops, kvGenerator) | ||
|
||
counterpartyHops, err = multihopProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
counterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.OPEN, channel.Ordering, counterparty, | ||
counterpartyHops, channel.Version) | ||
|
||
return k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofAck, | ||
channel.Counterparty.PortId, channel.Counterparty.ChannelId, | ||
expectedChannel) | ||
counterpartyHops = []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
} | ||
|
||
// counterparty of the counterparty channel end (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.OPEN, | ||
channel.Ordering, | ||
expectedCounterparty, | ||
counterpartyHops, | ||
channel.Version, | ||
) | ||
|
||
return k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofAck, | ||
channel.Counterparty.PortId, channel.Counterparty.ChannelId, | ||
expectedChannel) | ||
} | ||
|
||
// WriteOpenConfirmChannel writes an updated channel state for the successful OpenConfirm handshake step. | ||
|
@@ -548,47 +519,39 @@ func (k Keeper) ChanCloseConfirm( | |
) | ||
} | ||
|
||
// verify multihop proof | ||
var counterpartyHops []string | ||
if len(channel.ConnectionHops) > 1 { | ||
|
||
kvGenerator := func(mProof *types.MsgMultihopProofs, _ *connectiontypes.ConnectionEnd) (string, []byte, error) { | ||
key := host.ChannelPath(channel.Counterparty.PortId, channel.Counterparty.ChannelId) | ||
|
||
counterpartyHops, err := mProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
counterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.CLOSED, channel.Ordering, counterparty, | ||
counterpartyHops, channel.Version, | ||
) | ||
value, err := expectedChannel.Marshal() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
return key, value, nil | ||
} | ||
|
||
if err := k.connectionKeeper.VerifyMultihopMembership( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
channel.ConnectionHops, kvGenerator); err != nil { | ||
var ( | ||
err error | ||
multihopProof types.MsgMultihopProofs | ||
) | ||
if err = k.cdc.Unmarshal(proofInit, &multihopProof); err != nil { | ||
return err | ||
} | ||
|
||
} else { | ||
counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
counterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.CLOSED, channel.Ordering, counterparty, | ||
counterpartyHops, channel.Version) | ||
|
||
if err := k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
channel.Counterparty.PortId, channel.Counterparty.ChannelId, | ||
expectedChannel); err != nil { | ||
counterpartyHops, err = multihopProof.GetCounterpartyConnectionHops(k.cdc, &connectionEnd) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
counterpartyHops = []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
} | ||
|
||
// counterparty of the counterparty channel end (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, channelID) | ||
expectedChannel := types.NewChannel( | ||
types.CLOSED, | ||
channel.Ordering, | ||
expectedCounterparty, | ||
counterpartyHops, | ||
channel.Version, | ||
) | ||
|
||
if err := k.connectionKeeper.VerifyChannelState( | ||
ctx, connectionEnd, proofHeight, proofInit, | ||
channel.Counterparty.PortId, channel.Counterparty.ChannelId, | ||
expectedChannel); err != nil { | ||
return err | ||
} | ||
|
||
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State.String(), "new-state", types.CLOSED.String()) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is still some leakage of the multi hop abstraction at 04-channel, but I couldn't see any way around it. Open to ideas.
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.
I took a deeper look at this today and I think I see a path forward, but it is annoying due to import cycles. I am working on a branch to merge multi-hop and single hop logic into the existing VerifyXXX() functions by changing the single hop proof into a multi-hop proof. The multi-hop proof logic then degenerates into standard single hop proof logic.
However, I need to to some moderate refactoring to get this approach to work. I'll share a draft PR so you can see the approach hopefully next week. The main changes are to:
Hopefully I can do this in a way where I can pick just one verification function and show how it would look. This would also remove the key generator functions as well.