Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Nov 26, 2024
1 parent 5a3862d commit 3929eb2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 44 deletions.
15 changes: 15 additions & 0 deletions testutil/datagen/btcstkconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
49 changes: 13 additions & 36 deletions testutil/keeper/zoneconcierge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion x/zoneconcierge/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion x/zoneconcierge/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
13 changes: 11 additions & 2 deletions x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions x/zoneconcierge/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/zoneconcierge/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion x/zoneconcierge/keeper/proof_btc_timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3929eb2

Please sign in to comment.