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

     1  package mock
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"strconv"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    14  	capabilitykeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/keeper"
    15  	capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types"
    16  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    17  	"github.com/gorilla/mux"
    18  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    19  	"github.com/spf13/cobra"
    20  
    21  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    22  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    23  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/exported"
    24  )
    25  
    26  const (
    27  	ModuleName = "mock"
    28  
    29  	Version = "mock-version"
    30  )
    31  
    32  var (
    33  	MockAcknowledgement             = channeltypes.NewResultAcknowledgement([]byte("mock acknowledgement"))
    34  	MockFailAcknowledgement         = channeltypes.NewErrorAcknowledgement("mock failed acknowledgement")
    35  	MockPacketData                  = []byte("mock packet data")
    36  	MockFailPacketData              = []byte("mock failed packet data")
    37  	MockAsyncPacketData             = []byte("mock async packet data")
    38  	MockRecvCanaryCapabilityName    = "mock receive canary capability name"
    39  	MockAckCanaryCapabilityName     = "mock acknowledgement canary capability name"
    40  	MockTimeoutCanaryCapabilityName = "mock timeout canary capability name"
    41  )
    42  
    43  var (
    44  	_ module.AppModule      = AppModule{}
    45  	_ module.AppModuleBasic = AppModuleBasic{}
    46  )
    47  
    48  //var _ porttypes.IBCModule = AppModule{}
    49  
    50  // Expected Interface
    51  // PortKeeper defines the expected IBC port keeper
    52  type PortKeeper interface {
    53  	BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability
    54  	IsBound(ctx sdk.Context, portID string) bool
    55  }
    56  
    57  // AppModuleBasic is the mock AppModuleBasic.
    58  type AppModuleBasic struct{}
    59  
    60  func (a AppModuleBasic) RegisterCodec(c *codec.Codec) {
    61  	return
    62  }
    63  
    64  // Name implements AppModuleBasic interface.
    65  func (AppModuleBasic) Name() string {
    66  	return ModuleName
    67  }
    68  
    69  // RegisterLegacyAminoCodec implements AppModuleBasic interface.
    70  //func (AppModuleBasic) RegisterLegacyAminoCodec(*codec.LegacyAmino) {}
    71  
    72  // RegisterInterfaces implements AppModuleBasic interface.
    73  func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {}
    74  
    75  // DefaultGenesis implements AppModuleBasic interface.
    76  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    77  	r, _ := json.Marshal([]byte{})
    78  	return r
    79  }
    80  
    81  // ValidateGenesis implements the AppModuleBasic interface.
    82  func (AppModuleBasic) ValidateGenesis(json.RawMessage) error {
    83  	return nil
    84  }
    85  
    86  // RegisterRESTRoutes implements AppModuleBasic interface.
    87  func (AppModuleBasic) RegisterRESTRoutes(clientCtx context.CLIContext, rtr *mux.Router) {}
    88  
    89  // RegisterGRPCGatewayRoutes implements AppModuleBasic interface.
    90  func (a AppModuleBasic) RegisterGRPCGatewayRoutes(_ context.CLIContext, _ *runtime.ServeMux) {}
    91  
    92  // GetTxCmd implements AppModuleBasic interface.
    93  func (AppModuleBasic) GetTxCmd(proxy *codec.Codec) *cobra.Command {
    94  	return nil
    95  }
    96  
    97  // GetQueryCmd implements AppModuleBasic interface.
    98  func (AppModuleBasic) GetQueryCmd(codec *codec.Codec) *cobra.Command {
    99  	return nil
   100  }
   101  
   102  // AppModule represents the AppModule for the mock module.
   103  type AppModule struct {
   104  	AppModuleBasic
   105  	scopedKeeper capabilitykeeper.ScopedKeeper
   106  	portKeeper   PortKeeper
   107  	ibcApps      []*MockIBCApp
   108  }
   109  
   110  func (am AppModule) NewHandler() sdk.Handler {
   111  	return nil
   112  }
   113  
   114  func (am AppModule) NewQuerierHandler() sdk.Querier {
   115  	return nil
   116  }
   117  
   118  // NewAppModule returns a mock AppModule instance.
   119  func NewAppModule(sk capabilitykeeper.ScopedKeeper, pk PortKeeper) AppModule {
   120  	return AppModule{
   121  		scopedKeeper: sk,
   122  		portKeeper:   pk,
   123  	}
   124  }
   125  
   126  // RegisterInvariants implements the AppModule interface.
   127  func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}
   128  
   129  // Route implements the AppModule interface.
   130  func (am AppModule) Route() string {
   131  	return sdk.NewRoute(ModuleName, nil).Path()
   132  }
   133  
   134  // QuerierRoute implements the AppModule interface.
   135  func (AppModule) QuerierRoute() string {
   136  	return ""
   137  }
   138  
   139  // LegacyQuerierHandler implements the AppModule interface.
   140  // func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
   141  // 	return nil
   142  // }
   143  
   144  // RegisterServices implements the AppModule interface.
   145  func (am AppModule) RegisterServices(module.Configurator) {}
   146  
   147  // InitGenesis implements the AppModule interface.
   148  func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
   149  	// bind mock port ID
   150  	//cap := am.portKeeper.BindPort(ctx, ModuleName)
   151  	//am.scopedKeeper.ClaimCapability(ctx, cap, host.PortPath(ModuleName))
   152  
   153  	for _, ibcApp := range am.ibcApps {
   154  		if ibcApp.PortID != "" && !am.portKeeper.IsBound(ctx, ibcApp.PortID) {
   155  			// bind mock portID
   156  			cap := am.portKeeper.BindPort(ctx, ibcApp.PortID)
   157  			ibcApp.ScopedKeeper.ClaimCapability(ctx, cap, host.PortPath(ibcApp.PortID))
   158  		}
   159  	}
   160  
   161  	return []abci.ValidatorUpdate{}
   162  }
   163  
   164  // ExportGenesis implements the AppModule interface.
   165  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   166  	return nil
   167  }
   168  
   169  // ConsensusVersion implements AppModule/ConsensusVersion.
   170  func (AppModule) ConsensusVersion() uint64 { return 1 }
   171  
   172  // BeginBlock implements the AppModule interface
   173  func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
   174  }
   175  
   176  // EndBlock implements the AppModule interface
   177  func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
   178  	return []abci.ValidatorUpdate{}
   179  }
   180  
   181  // OnChanOpenInit implements the IBCModule interface.
   182  func (am AppModule) OnChanOpenInit(
   183  	ctx sdk.Context, _ channeltypes.Order, _ []string, portID string,
   184  	channelID string, chanCap *capabilitytypes.Capability, _ channeltypes.Counterparty, v string,
   185  ) (string, error) {
   186  	// Claim channel capability passed back by IBC module
   187  	if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
   188  		return "", err
   189  	}
   190  
   191  	return "", nil
   192  }
   193  
   194  // OnChanOpenTry implements the IBCModule interface.
   195  func (am AppModule) OnChanOpenTry(
   196  	ctx sdk.Context, _ channeltypes.Order, _ []string, portID string,
   197  	channelID string, chanCap *capabilitytypes.Capability, _ channeltypes.Counterparty, _, _ string,
   198  ) (string, error) {
   199  	// Claim channel capability passed back by IBC module
   200  	if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
   201  		return "", err
   202  	}
   203  
   204  	return "", nil
   205  }
   206  
   207  // OnChanOpenAck implements the IBCModule interface.
   208  func (am AppModule) OnChanOpenAck(sdk.Context, string, string, string, string) error {
   209  	return nil
   210  }
   211  
   212  // OnChanOpenConfirm implements the IBCModule interface.
   213  func (am AppModule) OnChanOpenConfirm(sdk.Context, string, string) error {
   214  	return nil
   215  }
   216  
   217  // OnChanCloseInit implements the IBCModule interface.
   218  func (am AppModule) OnChanCloseInit(sdk.Context, string, string) error {
   219  	return nil
   220  }
   221  
   222  // OnChanCloseConfirm implements the IBCModule interface.
   223  func (am AppModule) OnChanCloseConfirm(sdk.Context, string, string) error {
   224  	return nil
   225  }
   226  
   227  // OnRecvPacket implements the IBCModule interface.
   228  func (am AppModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) exported.Acknowledgement {
   229  	// set state by claiming capability to check if revert happens return
   230  	_, err := am.scopedKeeper.NewCapability(ctx, MockRecvCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence())))
   231  	if err != nil {
   232  		// application callback called twice on same packet sequence
   233  		// must never occur
   234  		panic(err)
   235  	}
   236  	if bytes.Equal(MockPacketData, packet.GetData()) {
   237  		return MockAcknowledgement
   238  	} else if bytes.Equal(MockAsyncPacketData, packet.GetData()) {
   239  		return nil
   240  	}
   241  
   242  	return MockFailAcknowledgement
   243  }
   244  
   245  // OnAcknowledgementPacket implements the IBCModule interface.
   246  func (am AppModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, _ []byte, _ sdk.AccAddress) error {
   247  	_, err := am.scopedKeeper.NewCapability(ctx, MockAckCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence())))
   248  	if err != nil {
   249  		// application callback called twice on same packet sequence
   250  		// must never occur
   251  		panic(err)
   252  	}
   253  
   254  	return nil
   255  }
   256  
   257  // OnTimeoutPacket implements the IBCModule interface.
   258  func (am AppModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, _ sdk.AccAddress) error {
   259  	_, err := am.scopedKeeper.NewCapability(ctx, MockTimeoutCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence())))
   260  	if err != nil {
   261  		// application callback called twice on same packet sequence
   262  		// must never occur
   263  		panic(err)
   264  	}
   265  
   266  	return nil
   267  }
   268  
   269  // NegotiateAppVersion implements the IBCModule interface.
   270  func (am AppModule) NegotiateAppVersion(
   271  	ctx sdk.Context,
   272  	order channeltypes.Order,
   273  	connectionID string,
   274  	portID string,
   275  	counterparty channeltypes.Counterparty,
   276  	proposedVersion string,
   277  ) (string, error) {
   278  	if proposedVersion != Version { // allow testing of error scenarios
   279  		return "", errors.New("failed to negotiate app version")
   280  	}
   281  
   282  	return Version, nil
   283  }