Skip to content

Commit

Permalink
fix UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Oct 17, 2024
1 parent 8c346ba commit 58862ca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
6 changes: 3 additions & 3 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func SetupForDeleteConsumerChain(t *testing.T, ctx sdk.Context,
t.Helper()

expectations := GetMocksForCreateConsumerClient(ctx, &mocks,
"chainID", clienttypes.NewHeight(4, 5))
"chainID", clienttypes.NewHeight(0, 5))
expectations = append(expectations, GetMocksForSetConsumerChain(ctx, &mocks, "chainID")...)

gomock.InOrder(expectations...)
Expand Down Expand Up @@ -286,7 +286,7 @@ func GetTestConsumerMetadata() providertypes.ConsumerMetadata {

func GetTestInitializationParameters() providertypes.ConsumerInitializationParameters {
return providertypes.ConsumerInitializationParameters{
InitialHeight: clienttypes.NewHeight(4, 5),
InitialHeight: clienttypes.NewHeight(0, 5),
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: time.Now().UTC(),
Expand Down Expand Up @@ -323,7 +323,7 @@ func GetTestMsgUpdateConsumer() providertypes.MsgUpdateConsumer {
func GetTestMsgConsumerAddition() providertypes.MsgConsumerAddition {
return providertypes.MsgConsumerAddition{
ChainId: "a ChainId",
InitialHeight: clienttypes.NewHeight(4, 5),
InitialHeight: clienttypes.NewHeight(0, 5),
GenesisHash: []byte(base64.StdEncoding.EncodeToString([]byte("gen_hash"))),
BinaryHash: []byte(base64.StdEncoding.EncodeToString([]byte("bin_hash"))),
SpawnTime: time.Now(),
Expand Down
28 changes: 16 additions & 12 deletions x/ccv/provider/keeper/consumer_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func TestPrepareConsumerForLaunch(t *testing.T) {

func TestInitializeConsumer(t *testing.T) {
now := time.Now().UTC()
consumerId := "13"
consumerId := CONSUMER_ID
chainId := CONSUMER_CHAIN_ID

testCases := []struct {
name string
Expand All @@ -69,6 +70,7 @@ func TestInitializeConsumer(t *testing.T) {
spawnTime: now,
setup: func(pk *providerkeeper.Keeper, ctx sdk.Context, spawnTime time.Time) {
pk.SetConsumerPhase(ctx, consumerId, providertypes.CONSUMER_PHASE_REGISTERED)
pk.SetConsumerChainId(ctx, consumerId, chainId)
err := pk.SetConsumerInitializationParameters(ctx, consumerId,
providertypes.ConsumerInitializationParameters{
SpawnTime: spawnTime,
Expand All @@ -89,6 +91,7 @@ func TestInitializeConsumer(t *testing.T) {
spawnTime: now,
setup: func(pk *providerkeeper.Keeper, ctx sdk.Context, spawnTime time.Time) {
pk.SetConsumerPhase(ctx, consumerId, providertypes.CONSUMER_PHASE_LAUNCHED)
pk.SetConsumerChainId(ctx, consumerId, chainId)
err := pk.SetConsumerInitializationParameters(ctx, consumerId,
providertypes.ConsumerInitializationParameters{
SpawnTime: spawnTime,
Expand All @@ -110,6 +113,7 @@ func TestInitializeConsumer(t *testing.T) {
spawnTime: now,
setup: func(pk *providerkeeper.Keeper, ctx sdk.Context, spawnTime time.Time) {
pk.SetConsumerPhase(ctx, consumerId, providertypes.CONSUMER_PHASE_REGISTERED)
pk.SetConsumerChainId(ctx, consumerId, chainId)
err := pk.SetConsumerInitializationParameters(ctx, consumerId,
providertypes.ConsumerInitializationParameters{
SpawnTime: time.Time{},
Expand Down Expand Up @@ -150,7 +154,7 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {

initializationParameters := []providertypes.ConsumerInitializationParameters{
{
InitialHeight: clienttypes.NewHeight(3, 4),
InitialHeight: clienttypes.NewHeight(0, 4),
GenesisHash: []byte{},
BinaryHash: []byte{},
SpawnTime: now.Add(-time.Hour * 2).UTC(),
Expand All @@ -163,7 +167,7 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {
DistributionTransmissionChannel: "",
},
{
InitialHeight: clienttypes.NewHeight(3, 4),
InitialHeight: clienttypes.NewHeight(0, 4),
GenesisHash: []byte{},
BinaryHash: []byte{},
SpawnTime: now.Add(-time.Hour).UTC(),
Expand All @@ -176,7 +180,7 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {
DistributionTransmissionChannel: "",
},
{
InitialHeight: clienttypes.NewHeight(3, 4),
InitialHeight: clienttypes.NewHeight(0, 4),
GenesisHash: []byte{},
BinaryHash: []byte{},
SpawnTime: now.Add(time.Hour).UTC(),
Expand All @@ -189,7 +193,7 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {
DistributionTransmissionChannel: "",
},
{
InitialHeight: clienttypes.NewHeight(3, 4),
InitialHeight: clienttypes.NewHeight(0, 4),
GenesisHash: []byte{},
BinaryHash: []byte{},
SpawnTime: now.Add(-time.Hour).UTC(),
Expand All @@ -202,7 +206,7 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {
DistributionTransmissionChannel: "",
},
{
InitialHeight: clienttypes.NewHeight(3, 4),
InitialHeight: clienttypes.NewHeight(0, 4),
GenesisHash: []byte{},
BinaryHash: []byte{},
SpawnTime: now.Add(-time.Minute).UTC(),
Expand Down Expand Up @@ -283,11 +287,11 @@ func TestBeginBlockLaunchConsumers(t *testing.T) {

// Expect genesis and client creation for only the first, second, and fifth chains (spawn time already passed and valid)
expectedCalls := testkeeper.GetMocksForMakeConsumerGenesis(ctx, &mocks, time.Hour)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain0", clienttypes.NewHeight(3, 4))...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain0", clienttypes.NewHeight(0, 4))...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForMakeConsumerGenesis(ctx, &mocks, time.Hour)...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain1", clienttypes.NewHeight(3, 4))...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain1", clienttypes.NewHeight(0, 4))...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForMakeConsumerGenesis(ctx, &mocks, time.Hour)...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain3", clienttypes.NewHeight(3, 4))...)
expectedCalls = append(expectedCalls, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks, "chain3", clienttypes.NewHeight(0, 4))...)

gomock.InOrder(expectedCalls...)

Expand Down Expand Up @@ -479,7 +483,7 @@ func TestCreateConsumerClient(t *testing.T) {

// Valid client creation is asserted with mock expectations here
gomock.InOrder(
testkeeper.GetMocksForCreateConsumerClient(ctx, mocks, CONSUMER_CHAIN_ID, clienttypes.NewHeight(4, 5))...,
testkeeper.GetMocksForCreateConsumerClient(ctx, mocks, CONSUMER_CHAIN_ID, clienttypes.NewHeight(0, 5))...,
)
},
expClientCreated: true,
Expand Down Expand Up @@ -774,7 +778,7 @@ func TestBeginBlockStopConsumers(t *testing.T) {
chainId := chainIds[i]
// A consumer chain is setup corresponding to each consumerId, making these mocks necessary
expectations = append(expectations, testkeeper.GetMocksForCreateConsumerClient(ctx, &mocks,
chainId, clienttypes.NewHeight(2, 3))...)
chainId, clienttypes.NewHeight(0, 3))...)
expectations = append(expectations, testkeeper.GetMocksForSetConsumerChain(ctx, &mocks, chainId)...)
}
// Only first two consumer chains should be stopped
Expand All @@ -789,7 +793,7 @@ func TestBeginBlockStopConsumers(t *testing.T) {
for i, consumerId := range consumerIds {
// Setup a valid consumer chain for each consumerId
initializationRecord := testkeeper.GetTestInitializationParameters()
initializationRecord.InitialHeight = clienttypes.NewHeight(2, 3)
initializationRecord.InitialHeight = clienttypes.NewHeight(0, 3)
registrationRecord := testkeeper.GetTestConsumerMetadata()

providerKeeper.SetConsumerChainId(ctx, consumerId, chainIds[i])
Expand Down
22 changes: 15 additions & 7 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,15 @@ func TestQueryConsumerChainsValidatorHasToValidate(t *testing.T) {

// set up some consumer chains
for i := 0; i < consumerNum; i++ {
chainID := "consumer-" + strconv.Itoa(i)
revisionNumber := i + 1
chainID := "consumer-" + strconv.Itoa(revisionNumber)
metadata := types.ConsumerMetadata{Name: chainID}
initializationParameters := types.DefaultConsumerInitializationParameters()
initializationParameters.InitialHeight.RevisionNumber = uint64(revisionNumber)
msg := types.MsgCreateConsumer{
ChainId: chainID,
Metadata: metadata,
ChainId: chainID,
Metadata: metadata,
InitializationParameters: &initializationParameters,
}
resp, err := msgServer.CreateConsumer(ctx, &msg)
require.NoError(t, err)
Expand Down Expand Up @@ -535,7 +539,7 @@ func TestQueryConsumerChain(t *testing.T) {
defer ctrl.Finish()

consumerId := "0"
chainId := "consumer-1"
chainId := "consumer"

req := types.QueryConsumerChainRequest{
ConsumerId: consumerId,
Expand Down Expand Up @@ -644,11 +648,15 @@ func TestQueryConsumerChains(t *testing.T) {

// create four consumer chains in different phase
for i := 0; i < consumerNum; i++ {
chainID := "consumer-" + strconv.Itoa(i)
revisionNumber := i + 1
chainID := "consumer-" + strconv.Itoa(revisionNumber)
metadata := types.ConsumerMetadata{Name: chainID}
initializationParameters := types.DefaultConsumerInitializationParameters()
initializationParameters.InitialHeight.RevisionNumber = uint64(revisionNumber)
msg := types.MsgCreateConsumer{
ChainId: chainID,
Metadata: metadata,
ChainId: chainID,
Metadata: metadata,
InitializationParameters: &initializationParameters,
}
resp, err := msgServer.CreateConsumer(ctx, &msg)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion x/ccv/provider/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestUpdateConsumer(t *testing.T) {
// create a chain before updating it
createConsumerResponse, err := msgServer.CreateConsumer(ctx,
&providertypes.MsgCreateConsumer{
Submitter: "submitter", ChainId: "chainId",
Submitter: "submitter", ChainId: "chainId-1",
Metadata: providertypes.ConsumerMetadata{
Name: "name",
Description: "description",
Expand Down Expand Up @@ -113,6 +113,7 @@ func TestUpdateConsumer(t *testing.T) {
}

expectedInitializationParameters := testkeeper.GetTestInitializationParameters()
expectedInitializationParameters.InitialHeight.RevisionNumber = 1
expectedPowerShapingParameters := testkeeper.GetTestPowerShapingParameters()

expectedOwnerAddress := "cosmos1dkas8mu4kyhl5jrh4nzvm65qz588hy9qcz08la"
Expand Down
6 changes: 4 additions & 2 deletions x/ccv/provider/keeper/permissionless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ func TestConsumerInitializationParameters(t *testing.T) {
HistoricalEntries: 456,
DistributionTransmissionChannel: "distribution_transmission_channel",
}
providerKeeper.SetConsumerInitializationParameters(ctx, CONSUMER_ID, expectedInitializationParameters)
providerKeeper.SetConsumerChainId(ctx, CONSUMER_ID, "chain-1")
err = providerKeeper.SetConsumerInitializationParameters(ctx, CONSUMER_ID, expectedInitializationParameters)
require.NoError(t, err)
actualInitializationParameters, err := providerKeeper.GetConsumerInitializationParameters(ctx, CONSUMER_ID)
require.NoError(t, err)
require.Equal(t, expectedInitializationParameters, actualInitializationParameters)

// assert that overwriting the current initialization record works
expectedInitializationParameters = providertypes.ConsumerInitializationParameters{
InitialHeight: types.Height{RevisionNumber: 2, RevisionHeight: 3},
InitialHeight: types.Height{RevisionNumber: 1, RevisionHeight: 3},
GenesisHash: []byte{2, 3},
BinaryHash: []byte{4, 5},
SpawnTime: time.Unix(2, 3).UTC(),
Expand Down

0 comments on commit 58862ca

Please sign in to comment.