github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/mock/ibc_module.go (about)

     1  package mock
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types"
    12  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    13  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    14  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported"
    15  )
    16  
    17  // IBCModule implements the ICS26 callbacks for testing/mock.
    18  type IBCModule struct {
    19  	appModule *AppModule
    20  	IBCApp    *MockIBCApp // base application of an IBC middleware stack
    21  }
    22  
    23  //func (im IBCModule) OnChanOpenTry(ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version, counterpartyVersion string) (string, error) {
    24  //	//TODO implement me
    25  //	panic("implement me")
    26  //}
    27  
    28  func (im IBCModule) NegotiateAppVersion(ctx sdk.Context, order channeltypes.Order, connectionID string, portID string, counterparty channeltypes.Counterparty, proposedVersion string) (version string, err error) {
    29  	if proposedVersion != Version { // allow testing of error scenarios
    30  		return "", errors.New("failed to negotiate app version")
    31  	}
    32  
    33  	return Version, nil
    34  }
    35  
    36  // NewIBCModule creates a new IBCModule given the underlying mock IBC application and scopedKeeper.
    37  func NewIBCModule(appModule *AppModule, app *MockIBCApp) IBCModule {
    38  	appModule.ibcApps = append(appModule.ibcApps, app)
    39  	return IBCModule{
    40  		appModule: appModule,
    41  		IBCApp:    app,
    42  	}
    43  }
    44  
    45  // OnChanOpenInit implements the IBCModule interface.
    46  func (im IBCModule) OnChanOpenInit(
    47  	ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string,
    48  	channelID string, chanCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version string,
    49  ) (string, error) {
    50  	if strings.TrimSpace(version) == "" {
    51  		version = Version
    52  	}
    53  
    54  	if im.IBCApp.OnChanOpenInit != nil {
    55  		return im.IBCApp.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
    56  	}
    57  
    58  	// Claim channel capability passed back by IBC module
    59  	if err := im.IBCApp.ScopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
    60  		return "", err
    61  	}
    62  
    63  	return version, nil
    64  }
    65  
    66  // OnChanOpenTry implements the IBCModule interface.
    67  func (im IBCModule) OnChanOpenTry(ctx sdk.Context,
    68  	order channeltypes.Order, connectionHops []string, portID, channelID string,
    69  	channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version, counterpartyVersion string) (string, error) {
    70  	if im.IBCApp.OnChanOpenTry != nil {
    71  		return im.IBCApp.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion)
    72  	}
    73  
    74  	// Claim channel capability passed back by IBC module
    75  	if err := im.IBCApp.ScopedKeeper.ClaimCapability(ctx, channelCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
    76  		return "", err
    77  	}
    78  
    79  	return Version, nil
    80  }
    81  
    82  // OnChanOpenAck implements the IBCModule interface.
    83  func (im IBCModule) OnChanOpenAck(ctx sdk.Context, portID string, channelID string, counterpartyChannelID string, counterpartyVersion string) error {
    84  	if im.IBCApp.OnChanOpenAck != nil {
    85  		return im.IBCApp.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // OnChanOpenConfirm implements the IBCModule interface.
    92  func (im IBCModule) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string) error {
    93  	if im.IBCApp.OnChanOpenConfirm != nil {
    94  		return im.IBCApp.OnChanOpenConfirm(ctx, portID, channelID)
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  // OnChanCloseInit implements the IBCModule interface.
   101  func (im IBCModule) OnChanCloseInit(ctx sdk.Context, portID, channelID string) error {
   102  	if im.IBCApp.OnChanCloseInit != nil {
   103  		return im.IBCApp.OnChanCloseInit(ctx, portID, channelID)
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  // OnChanCloseConfirm implements the IBCModule interface.
   110  func (im IBCModule) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string) error {
   111  	if im.IBCApp.OnChanCloseConfirm != nil {
   112  		return im.IBCApp.OnChanCloseConfirm(ctx, portID, channelID)
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  // OnRecvPacket implements the IBCModule interface.
   119  func (im IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) exported.Acknowledgement {
   120  	if im.IBCApp.OnRecvPacket != nil {
   121  		return im.IBCApp.OnRecvPacket(ctx, packet, relayer)
   122  	}
   123  
   124  	// set state by claiming capability to check if revert happens return
   125  	capName := GetMockRecvCanaryCapabilityName(packet)
   126  	if _, err := im.IBCApp.ScopedKeeper.NewCapability(ctx, capName); err != nil {
   127  		// application callback called twice on same packet sequence
   128  		// must never occur
   129  		panic(err)
   130  	}
   131  
   132  	if bytes.Equal(MockPacketData, packet.GetData()) {
   133  		return MockAcknowledgement
   134  	} else if bytes.Equal(MockAsyncPacketData, packet.GetData()) {
   135  		return nil
   136  	}
   137  
   138  	return MockFailAcknowledgement
   139  }
   140  
   141  // OnAcknowledgementPacket implements the IBCModule interface.
   142  func (im IBCModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) error {
   143  	if im.IBCApp.OnAcknowledgementPacket != nil {
   144  		return im.IBCApp.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
   145  	}
   146  
   147  	capName := GetMockAckCanaryCapabilityName(packet)
   148  	if _, err := im.IBCApp.ScopedKeeper.NewCapability(ctx, capName); err != nil {
   149  		// application callback called twice on same packet sequence
   150  		// must never occur
   151  		panic(err)
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  // OnTimeoutPacket implements the IBCModule interface.
   158  func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error {
   159  	if im.IBCApp.OnTimeoutPacket != nil {
   160  		return im.IBCApp.OnTimeoutPacket(ctx, packet, relayer)
   161  	}
   162  
   163  	capName := GetMockTimeoutCanaryCapabilityName(packet)
   164  	if _, err := im.IBCApp.ScopedKeeper.NewCapability(ctx, capName); err != nil {
   165  		// application callback called twice on same packet sequence
   166  		// must never occur
   167  		panic(err)
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  // GetMockRecvCanaryCapabilityName generates a capability name for testing OnRecvPacket functionality.
   174  func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string {
   175  	return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence())))
   176  }
   177  
   178  // GetMockAckCanaryCapabilityName generates a capability name for OnAcknowledgementPacket functionality.
   179  func GetMockAckCanaryCapabilityName(packet channeltypes.Packet) string {
   180  	return fmt.Sprintf("%s%s%s%s", MockAckCanaryCapabilityName, packet.GetSourcePort(), packet.GetSourceChannel(), strconv.Itoa(int(packet.GetSequence())))
   181  }
   182  
   183  // GetMockTimeoutCanaryCapabilityName generates a capability name for OnTimeoutacket functionality.
   184  func GetMockTimeoutCanaryCapabilityName(packet channeltypes.Packet) string {
   185  	return fmt.Sprintf("%s%s%s%s", MockTimeoutCanaryCapabilityName, packet.GetSourcePort(), packet.GetSourceChannel(), strconv.Itoa(int(packet.GetSequence())))
   186  }