-
Notifications
You must be signed in to change notification settings - Fork 636
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
feat: Reusability of Mock module for middleware integration tests #432
Changes from 2 commits
12c2d5b
ffc7c6f
be30a36
97f0907
c498c7a
b95dcb6
8edff5a
a74d134
dff6460
3fc2899
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package mock | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
|
||
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" | ||
"github.com/cosmos/ibc-go/v2/modules/core/exported" | ||
) | ||
|
||
type MockIBCApp struct { | ||
OnChanOpenInit func( | ||
ctx sdk.Context, | ||
order channeltypes.Order, | ||
connectionHops []string, | ||
portID string, | ||
channelID string, | ||
channelCap *capabilitytypes.Capability, | ||
counterparty channeltypes.Counterparty, | ||
version string, | ||
) error | ||
|
||
OnChanOpenTry func( | ||
ctx sdk.Context, | ||
order channeltypes.Order, | ||
connectionHops []string, | ||
portID, | ||
channelID string, | ||
channelCap *capabilitytypes.Capability, | ||
counterparty channeltypes.Counterparty, | ||
version, | ||
counterpartyVersion string, | ||
) error | ||
|
||
OnChanOpenAck func( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
counterpartyVersion string, | ||
) error | ||
|
||
OnChanOpenConfirm func( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error | ||
|
||
OnChanCloseInit func( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error | ||
|
||
OnChanCloseConfirm func( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) error | ||
|
||
// OnRecvPacket must return an acknowledgement that implements the Acknowledgement interface. | ||
// In the case of an asynchronous acknowledgement, nil should be returned. | ||
// If the acknowledgement returned is successful, the state changes on callback are written, | ||
// otherwise the application state changes are discarded. In either case the packet is received | ||
// and the acknowledgement is written (in synchronous cases). | ||
OnRecvPacket func( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) exported.Acknowledgement | ||
|
||
OnAcknowledgementPacket func( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
acknowledgement []byte, | ||
relayer sdk.AccAddress, | ||
) error | ||
|
||
OnTimeoutPacket func( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) error | ||
|
||
// NegotiateAppVersion performs application version negotiation given the provided channel ordering, connectionID, portID, counterparty and proposed version. | ||
// An error is returned if version negotiation cannot be performed. For example, an application module implementing this interface | ||
// may decide to return an error in the event of the proposed version being incompatible with it's own | ||
NegotiateAppVersion func( | ||
ctx sdk.Context, | ||
order channeltypes.Order, | ||
connectionID string, | ||
portID string, | ||
counterparty channeltypes.Counterparty, | ||
proposedVersion string, | ||
) (version string, err error) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ type AppModule struct { | |
AppModuleBasic | ||
scopedKeeper capabilitykeeper.ScopedKeeper | ||
portKeeper PortKeeper | ||
BaseApp MockIBCApp // base application of an IBC middleware stack | ||
AdityaSripal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// NewAppModule returns a mock AppModule instance. | ||
|
@@ -153,9 +154,14 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V | |
|
||
// OnChanOpenInit implements the IBCModule interface. | ||
func (am AppModule) OnChanOpenInit( | ||
ctx sdk.Context, _ channeltypes.Order, _ []string, portID string, | ||
channelID string, chanCap *capabilitytypes.Capability, _ channeltypes.Counterparty, _ string, | ||
ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string, | ||
channelID string, chanCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version string, | ||
) error { | ||
if am.BaseApp.OnChanOpenInit != nil { | ||
return am.BaseApp.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version) | ||
|
||
} | ||
|
||
// Claim channel capability passed back by IBC module | ||
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { | ||
return err | ||
|
@@ -166,9 +172,13 @@ func (am AppModule) OnChanOpenInit( | |
|
||
// OnChanOpenTry implements the IBCModule interface. | ||
func (am AppModule) OnChanOpenTry( | ||
ctx sdk.Context, _ channeltypes.Order, _ []string, portID string, | ||
channelID string, chanCap *capabilitytypes.Capability, _ channeltypes.Counterparty, _, _ string, | ||
ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string, | ||
channelID string, chanCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version, counterpartyVersion string, | ||
) error { | ||
if am.BaseApp.OnChanOpenTry != nil { | ||
return am.BaseApp.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion) | ||
|
||
} | ||
// Claim channel capability passed back by IBC module | ||
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { | ||
return err | ||
|
@@ -178,27 +188,47 @@ func (am AppModule) OnChanOpenTry( | |
} | ||
|
||
// OnChanOpenAck implements the IBCModule interface. | ||
func (am AppModule) OnChanOpenAck(sdk.Context, string, string, string) error { | ||
func (am AppModule) OnChanOpenAck(ctx sdk.Context, portID string, channelID string, counterpartyVersion string) error { | ||
if am.BaseApp.OnChanOpenAck != nil { | ||
return am.BaseApp.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnChanOpenConfirm implements the IBCModule interface. | ||
func (am AppModule) OnChanOpenConfirm(sdk.Context, string, string) error { | ||
func (am AppModule) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string) error { | ||
if am.BaseApp.OnChanOpenConfirm != nil { | ||
return am.BaseApp.OnChanOpenConfirm(ctx, portID, channelID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnChanCloseInit implements the IBCModule interface. | ||
func (am AppModule) OnChanCloseInit(sdk.Context, string, string) error { | ||
func (am AppModule) OnChanCloseInit(ctx sdk.Context, portID, channelID string) error { | ||
if am.BaseApp.OnChanCloseInit != nil { | ||
return am.BaseApp.OnChanCloseInit(ctx, portID, channelID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnChanCloseConfirm implements the IBCModule interface. | ||
func (am AppModule) OnChanCloseConfirm(sdk.Context, string, string) error { | ||
func (am AppModule) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string) error { | ||
if am.BaseApp.OnChanCloseConfirm != nil { | ||
return am.BaseApp.OnChanCloseConfirm(ctx, portID, channelID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnRecvPacket implements the IBCModule interface. | ||
func (am AppModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) exported.Acknowledgement { | ||
if am.BaseApp.OnRecvPacket != nil { | ||
return am.BaseApp.OnRecvPacket(ctx, packet, relayer) | ||
} | ||
|
||
// set state by claiming capability to check if revert happens return | ||
_, err := am.scopedKeeper.NewCapability(ctx, MockRecvCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) | ||
if err != nil { | ||
|
@@ -216,7 +246,11 @@ func (am AppModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re | |
} | ||
|
||
// OnAcknowledgementPacket implements the IBCModule interface. | ||
func (am AppModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, _ []byte, _ sdk.AccAddress) error { | ||
func (am AppModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) error { | ||
if am.BaseApp.OnAcknowledgementPacket != nil { | ||
return am.BaseApp.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) | ||
} | ||
|
||
_, err := am.scopedKeeper.NewCapability(ctx, MockAckCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) | ||
if err != nil { | ||
// application callback called twice on same packet sequence | ||
|
@@ -228,7 +262,11 @@ func (am AppModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes | |
} | ||
|
||
// OnTimeoutPacket implements the IBCModule interface. | ||
func (am AppModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, _ sdk.AccAddress) error { | ||
func (am AppModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error { | ||
if am.BaseApp.OnTimeoutPacket != nil { | ||
return am.BaseApp.OnTimeoutPacket(ctx, packet, relayer) | ||
} | ||
|
||
_, err := am.scopedKeeper.NewCapability(ctx, MockTimeoutCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) | ||
if err != nil { | ||
// application callback called twice on same packet sequence | ||
|
@@ -248,6 +286,10 @@ func (am AppModule) NegotiateAppVersion( | |
counterparty channeltypes.Counterparty, | ||
proposedVersion 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. haha nice call @damiannolan on not using 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 would be the reason not to use |
||
) (string, error) { | ||
if am.BaseApp.NegotiateAppVersion != nil { | ||
return am.BaseApp.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion) | ||
} | ||
|
||
if proposedVersion != Version { // allow testing of error scenarios | ||
return "", errors.New("failed to negotiate app version") | ||
} | ||
|
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.
typo:
avaliable