From 3929eb229db2c2e0f578dce3c2f6835cf2444980 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Tue, 26 Nov 2024 16:09:04 +1100 Subject: [PATCH] some tests --- testutil/datagen/btcstkconsumer.go | 15 ++++++ testutil/keeper/zoneconcierge.go | 49 +++++-------------- x/zoneconcierge/genesis_test.go | 10 +++- x/zoneconcierge/keeper/grpc_query_test.go | 6 ++- .../ibc_packet_btc_staking_consumer_event.go | 13 ++++- x/zoneconcierge/keeper/keeper_test.go | 3 +- x/zoneconcierge/keeper/params_test.go | 2 +- .../keeper/proof_btc_timestamp_test.go | 7 ++- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/testutil/datagen/btcstkconsumer.go b/testutil/datagen/btcstkconsumer.go index 6a49c0615..172b26c56 100644 --- a/testutil/datagen/btcstkconsumer.go +++ b/testutil/datagen/btcstkconsumer.go @@ -19,3 +19,18 @@ func GenRandomCosmosConsumerRegister(r *rand.Rand) *bsctypes.ConsumerRegister { }, } } + +func GenRandomETHL2Register(r *rand.Rand) *bsctypes.ConsumerRegister { + clientID := "test-" + GenRandomHexStr(r, 10) + contractAddress := GenRandomAccount().Address + return &bsctypes.ConsumerRegister{ + ConsumerId: clientID, + ConsumerName: GenRandomHexStr(r, 5), + ConsumerDescription: "Chain description: " + GenRandomHexStr(r, 15), + ConsumerMetadata: &bsctypes.ConsumerRegister_EthL2ConsumerMetadata{ + EthL2ConsumerMetadata: &bsctypes.ETHL2ConsumerMetadata{ + FinalityContractAddress: contractAddress, + }, + }, + } +} diff --git a/testutil/keeper/zoneconcierge.go b/testutil/keeper/zoneconcierge.go index cc5502ec6..a001c4424 100644 --- a/testutil/keeper/zoneconcierge.go +++ b/testutil/keeper/zoneconcierge.go @@ -18,45 +18,12 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" - capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" - channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" - ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" "github.com/stretchr/testify/require" "github.com/babylonlabs-io/babylon/x/zoneconcierge/keeper" "github.com/babylonlabs-io/babylon/x/zoneconcierge/types" ) -// zoneconciergeChannelKeeper is a stub of ChannelKeeper -type zoneconciergeChannelKeeper struct{} - -func (zoneconciergeChannelKeeper) GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) { - return channeltypes.Channel{}, false -} -func (zoneconciergeChannelKeeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) { - return 0, false -} -func (zoneconciergeChannelKeeper) SendPacket(ctx sdk.Context, channelCap *capabilitytypes.Capability, packet ibcexported.PacketI) error { - return nil -} -func (zoneconciergeChannelKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error { - return nil -} - -func (zoneconciergeChannelKeeper) GetAllChannels(ctx sdk.Context) []channeltypes.IdentifiedChannel { - return nil -} -func (zoneconciergeChannelKeeper) GetChannelClientState(ctx sdk.Context, portID, channelID string) (string, ibcexported.ClientState, error) { - return "", nil, nil -} - -// zoneconciergeportKeeper is a stub of PortKeeper -type zoneconciergePortKeeper struct{} - -func (zoneconciergePortKeeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { - return &capabilitytypes.Capability{} -} - type zoneconciergeStoreQuerier struct{} func (zoneconciergeStoreQuerier) Query(req *storetypes.RequestQuery) (*storetypes.ResponseQuery, error) { @@ -69,7 +36,17 @@ func (zoneconciergeStoreQuerier) Query(req *storetypes.RequestQuery) (*storetype }, nil } -func ZoneConciergeKeeper(t testing.TB, btclcKeeper types.BTCLightClientKeeper, checkpointingKeeper types.CheckpointingKeeper, btccKeeper types.BtcCheckpointKeeper, epochingKeeper types.EpochingKeeper, bsKeeper types.BTCStakingKeeper, btcStkKeeper types.BTCStkConsumerKeeper) (*keeper.Keeper, sdk.Context) { +func ZoneConciergeKeeper( + t testing.TB, + channelKeeper types.ChannelKeeper, + portKeeper types.PortKeeper, + btclcKeeper types.BTCLightClientKeeper, + checkpointingKeeper types.CheckpointingKeeper, + btccKeeper types.BtcCheckpointKeeper, + epochingKeeper types.EpochingKeeper, + bsKeeper types.BTCStakingKeeper, + btcStkKeeper types.BTCStkConsumerKeeper, +) (*keeper.Keeper, sdk.Context) { logger := log.NewTestLogger(t) storeKey := storetypes.NewKVStoreKey(types.StoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) @@ -88,8 +65,8 @@ func ZoneConciergeKeeper(t testing.TB, btclcKeeper types.BTCLightClientKeeper, c runtime.NewKVStoreService(storeKey), nil, // TODO: mock this keeper nil, // TODO: mock this keeper - zoneconciergeChannelKeeper{}, - zoneconciergePortKeeper{}, + channelKeeper, + portKeeper, nil, // TODO: mock this keeper nil, // TODO: mock this keeper btclcKeeper, diff --git a/x/zoneconcierge/genesis_test.go b/x/zoneconcierge/genesis_test.go index 54d0001b2..bb329a50e 100644 --- a/x/zoneconcierge/genesis_test.go +++ b/x/zoneconcierge/genesis_test.go @@ -7,6 +7,8 @@ import ( "github.com/babylonlabs-io/babylon/testutil/nullify" "github.com/babylonlabs-io/babylon/x/zoneconcierge" "github.com/babylonlabs-io/babylon/x/zoneconcierge/types" + capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" ) @@ -16,7 +18,13 @@ func TestGenesis(t *testing.T) { Params: types.Params{IbcPacketTimeoutSeconds: 100}, } - k, ctx := keepertest.ZoneConciergeKeeper(t, nil, nil, nil, nil, nil, nil) + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + portKeeper := types.NewMockPortKeeper(ctrl) + portKeeper.EXPECT().BindPort(gomock.Any(), gomock.Any()).Return(&capabilitytypes.Capability{}).AnyTimes() + + k, ctx := keepertest.ZoneConciergeKeeper(t, nil, portKeeper, nil, nil, nil, nil, nil, nil) zoneconcierge.InitGenesis(ctx, *k, genesisState) got := zoneconcierge.ExportGenesis(ctx, *k) require.NotNil(t, got) diff --git a/x/zoneconcierge/keeper/grpc_query_test.go b/x/zoneconcierge/keeper/grpc_query_test.go index d38fdfb60..bb59582d4 100644 --- a/x/zoneconcierge/keeper/grpc_query_test.go +++ b/x/zoneconcierge/keeper/grpc_query_test.go @@ -395,8 +395,12 @@ func FuzzFinalizedChainInfo(f *testing.F) { mockBTCHeaderInfo := datagen.GenRandomBTCHeaderInfo(r) btclcKeeper.EXPECT().GetMainChainFrom(gomock.Any(), gomock.Any()).Return([]*btclightclienttypes.BTCHeaderInfo{mockBTCHeaderInfo}).AnyTimes() btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(mockBTCHeaderInfo).AnyTimes() + // mock channel/port keeper + channelKeeper := zctypes.NewMockChannelKeeper(ctrl) + channelKeeper.EXPECT().GetAllChannels(gomock.Any()).Return(nil).AnyTimes() + portKeeper := zctypes.NewMockPortKeeper(ctrl) - zcKeeper, ctx := testkeeper.ZoneConciergeKeeper(t, btclcKeeper, checkpointingKeeper, btccKeeper, epochingKeeper, nil, nil) + zcKeeper, ctx := testkeeper.ZoneConciergeKeeper(t, channelKeeper, portKeeper, btclcKeeper, checkpointingKeeper, btccKeeper, epochingKeeper, nil, nil) hooks := zcKeeper.Hooks() var ( diff --git a/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go b/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go index a690bf014..3a708e7b5 100644 --- a/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go +++ b/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go @@ -5,6 +5,7 @@ import ( "fmt" bbn "github.com/babylonlabs-io/babylon/types" + bsctypes "github.com/babylonlabs-io/babylon/x/btcstkconsumer/types" finalitytypes "github.com/babylonlabs-io/babylon/x/finality/types" "github.com/babylonlabs-io/babylon/x/zoneconcierge/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -71,14 +72,22 @@ func (k Keeper) HandleIBCChannelCreation( return fmt.Errorf("client ID %s is not registered as a consumer: %w", clientID, err) } - // Update the consumer metadata with the new channel ID + // Ensure the consumer is a Cosmos consumer cosmosMetadata := consumerRegister.GetCosmosConsumerMetadata() if cosmosMetadata == nil { return fmt.Errorf("consumer %s is not a Cosmos consumer", clientID) } - // all good, update the channel ID + // Ensure the client ID hasn't integrated yet, i.e., the channel ID is not set + if len(cosmosMetadata.ChannelId) > 0 { + return fmt.Errorf("consumer %s has already integrated with channel %s", clientID, cosmosMetadata.ChannelId) + } + + // all good, update the channel ID in the consumer register cosmosMetadata.ChannelId = channelID + consumerRegister.ConsumerMetadata = &bsctypes.ConsumerRegister_CosmosConsumerMetadata{ + CosmosConsumerMetadata: cosmosMetadata, + } if err := k.btcStkKeeper.UpdateConsumer(ctx, consumerRegister); err != nil { return fmt.Errorf("failed to update consumer register: %w", err) } diff --git a/x/zoneconcierge/keeper/keeper_test.go b/x/zoneconcierge/keeper/keeper_test.go index a552219b8..ec079dbd6 100644 --- a/x/zoneconcierge/keeper/keeper_test.go +++ b/x/zoneconcierge/keeper/keeper_test.go @@ -4,10 +4,9 @@ import ( "context" "math/rand" - ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" - "github.com/babylonlabs-io/babylon/testutil/datagen" zckeeper "github.com/babylonlabs-io/babylon/x/zoneconcierge/keeper" + ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" ) // SimulateNewHeaders generates a non-zero number of canonical headers diff --git a/x/zoneconcierge/keeper/params_test.go b/x/zoneconcierge/keeper/params_test.go index 92746136e..8a7957d32 100644 --- a/x/zoneconcierge/keeper/params_test.go +++ b/x/zoneconcierge/keeper/params_test.go @@ -9,7 +9,7 @@ import ( ) func TestGetParams(t *testing.T) { - k, ctx := testkeeper.ZoneConciergeKeeper(t, nil, nil, nil, nil, nil, nil) + k, ctx := testkeeper.ZoneConciergeKeeper(t, nil, nil, nil, nil, nil, nil, nil, nil) params := types.DefaultParams() if err := k.SetParams(ctx, params); err != nil { diff --git a/x/zoneconcierge/keeper/proof_btc_timestamp_test.go b/x/zoneconcierge/keeper/proof_btc_timestamp_test.go index 91a291aa0..76d8d768d 100644 --- a/x/zoneconcierge/keeper/proof_btc_timestamp_test.go +++ b/x/zoneconcierge/keeper/proof_btc_timestamp_test.go @@ -136,8 +136,13 @@ func FuzzProofEpochSealed_BLSSig(f *testing.F) { epochingKeeper := zctypes.NewMockEpochingKeeper(ctrl) epochingKeeper.EXPECT().GetEpoch(gomock.Any()).Return(epoch).AnyTimes() epochingKeeper.EXPECT().GetHistoricalEpoch(gomock.Any(), gomock.Eq(epoch.EpochNumber)).Return(epoch, nil).AnyTimes() + // mock channel/port keeper + channelKeeper := zctypes.NewMockChannelKeeper(ctrl) + channelKeeper.EXPECT().GetAllChannels(gomock.Any()).Return(nil).AnyTimes() + portKeeper := zctypes.NewMockPortKeeper(ctrl) + // create zcKeeper and ctx - zcKeeper, ctx := testkeeper.ZoneConciergeKeeper(t, nil, checkpointingKeeper, nil, epochingKeeper, nil, nil) + zcKeeper, ctx := testkeeper.ZoneConciergeKeeper(t, channelKeeper, portKeeper, nil, checkpointingKeeper, nil, epochingKeeper, nil, nil) // prove proof, err := zcKeeper.ProveEpochSealed(ctx, epoch.EpochNumber)