Skip to content

Commit

Permalink
added some additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Aug 26, 2024
1 parent 29d790c commit a400de1
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 97 deletions.
10 changes: 8 additions & 2 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ func (k msgServer) CreateConsumer(goCtx context.Context, msg *types.MsgCreateCon

if spawnTime, canLaunch := k.Keeper.CanLaunch(ctx, consumerId); canLaunch {
k.Keeper.SetConsumerPhase(ctx, consumerId, Initialized)
k.Keeper.PrepareConsumerForLaunch(ctx, consumerId, time.Time{}, spawnTime)
if err := k.Keeper.PrepareConsumerForLaunch(ctx, consumerId, time.Time{}, spawnTime); err != nil {
return &types.MsgCreateConsumerResponse{}, errorsmod.Wrapf(types.ErrCannotPrepareForLaunch,
"cannot prepare chain with consumer id (%s) for launch", consumerId)
}
}

return &types.MsgCreateConsumerResponse{ConsumerId: consumerId}, nil
Expand Down Expand Up @@ -442,7 +445,10 @@ func (k msgServer) UpdateConsumer(goCtx context.Context, msg *types.MsgUpdateCon

if spawnTime, canLaunch := k.Keeper.CanLaunch(ctx, consumerId); canLaunch {
k.Keeper.SetConsumerPhase(ctx, consumerId, Initialized)
k.Keeper.PrepareConsumerForLaunch(ctx, consumerId, previousSpawnTime, spawnTime)
if err := k.Keeper.PrepareConsumerForLaunch(ctx, consumerId, previousSpawnTime, spawnTime); err != nil {
return &types.MsgUpdateConsumerResponse{}, errorsmod.Wrapf(types.ErrCannotPrepareForLaunch,
"cannot prepare chain with consumer id (%s) for launch", consumerId)
}
}

return &types.MsgUpdateConsumerResponse{}, nil
Expand Down
1 change: 0 additions & 1 deletion x/ccv/provider/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,4 @@ func TestUpdateConsumer(t *testing.T) {
require.Equal(t, providertypes.ConsumerIds{
Ids: []string{consumerId},
}, consumerIds)

}
105 changes: 52 additions & 53 deletions x/ccv/provider/keeper/permissionless.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,48 @@ func (k Keeper) GetInitializedConsumersReadyToLaunch(ctx sdk.Context, limit uint
return result
}

// GetLaunchedConsumersReadyToStop returns the consumer ids of the pending launched consumer chains
// that are ready to stop
func (k Keeper) GetLaunchedConsumersReadyToStop(ctx sdk.Context, limit uint32) []string {
store := ctx.KVStore(k.storeKey)

stopTimeToConsumerIdsKeyPrefix := types.StopTimeToConsumerIdsKeyPrefix()
iterator := storetypes.KVStorePrefixIterator(store, []byte{stopTimeToConsumerIdsKeyPrefix})
defer iterator.Close()

result := []string{}
for ; iterator.Valid(); iterator.Next() {
stopTime, err := types.ParseTime(types.StopTimeToConsumerIdsKeyPrefix(), iterator.Key())
if err != nil {
k.Logger(ctx).Error("failed to parse stop time",
"error", err)
continue
}
if stopTime.After(ctx.BlockTime()) {
return result
}

consumers, err := k.GetConsumersToBeStopped(ctx, stopTime)
if err != nil {
k.Logger(ctx).Error("failed to retrieve consumers to stop",
"stop time", stopTime,
"error", err)
continue
}
if len(result)+len(consumers.Ids) >= int(limit) {
remainingConsumerIds := len(result) + len(consumers.Ids) - int(limit)
if len(consumers.Ids[:len(consumers.Ids)-remainingConsumerIds]) == 0 {
return result
}
return append(result, consumers.Ids[:len(consumers.Ids)-remainingConsumerIds]...)
} else {
result = append(result, consumers.Ids...)
}
}

return result
}

// LaunchConsumer launches the chain with the provided consumer id by creating the consumer client and the respective
// consumer genesis file
func (k Keeper) LaunchConsumer(ctx sdk.Context, consumerId string) error {
Expand All @@ -518,16 +560,13 @@ func (k Keeper) LaunchConsumer(ctx sdk.Context, consumerId string) error {

consumerGenesis, found := k.GetConsumerGenesis(ctx, consumerId)
if !found {
return errorsmod.Wrapf(types.ErrNoConsumerGenesis, "consumer genesis could not be found")
return errorsmod.Wrapf(types.ErrNoConsumerGenesis, "consumer genesis could not be found for consumer id: %s", consumerId)
}

if len(consumerGenesis.Provider.InitialValSet) == 0 {
return errorsmod.Wrapf(types.ErrInvalidConsumerGenesis, "consumer genesis initial validator set is empty - no validators opted in")
return errorsmod.Wrapf(types.ErrInvalidConsumerGenesis, "consumer genesis initial validator set is empty - no validators opted in consumer id: %s", consumerId)
}

// The cached context is created with a new EventManager so we merge the event
// into the original context
ctx.EventManager().EmitEvents(ctx.EventManager().Events())
return nil
}

Expand Down Expand Up @@ -582,48 +621,6 @@ func (k Keeper) UpdateMinimumPowerInTopN(ctx sdk.Context, consumerId string, old
return nil
}

// GetLaunchedConsumersReadyToStop returns the consumer ids of the pending launched consumer chains
// that are ready to stop
func (k Keeper) GetLaunchedConsumersReadyToStop(ctx sdk.Context, limit uint32) []string {
store := ctx.KVStore(k.storeKey)

stopTimeToConsumerIdsKeyPrefix := types.StopTimeToConsumerIdsKeyPrefix()
iterator := storetypes.KVStorePrefixIterator(store, []byte{stopTimeToConsumerIdsKeyPrefix})
defer iterator.Close()

result := []string{}
for ; iterator.Valid(); iterator.Next() {
stopTime, err := types.ParseTime(types.StopTimeToConsumerIdsKeyPrefix(), iterator.Key())
if err != nil {
k.Logger(ctx).Error("failed to parse stop time",
"error", err)
continue
}
if stopTime.After(ctx.BlockTime()) {
return result
}

consumerIds, err := k.GetConsumersToBeStopped(ctx, stopTime)
if err != nil {
k.Logger(ctx).Error("failed to retrieve consumers to stop",
"stop time", stopTime,
"error", err)
continue
}
if len(result)+len(consumerIds.Ids) >= int(limit) {
remainingConsumerIds := len(result) + len(consumerIds.Ids) - int(limit)
if len(consumerIds.Ids[:len(consumerIds.Ids)-remainingConsumerIds]) == 0 {
return result
}
return append(result, consumerIds.Ids[:len(consumerIds.Ids)-remainingConsumerIds]...)
} else {
result = append(result, consumerIds.Ids...)
}
}

return result
}

// IsValidatorOptedInToChainId checks if the validator with `providerAddr` is opted into the chain with the specified `chainId`.
// It returns `found == true` and the corresponding chain's `consumerId` if the validator is opted in. Otherwise, it returns an empty string
// for `consumerId` and `found == false`.
Expand Down Expand Up @@ -653,16 +650,18 @@ func (k Keeper) IsValidatorOptedInToChainId(ctx sdk.Context, providerAddr types.
return "", false
}

func (k Keeper) PrepareConsumerForLaunch(ctx sdk.Context, consumerId string, previousSpawnTime time.Time, spawnTime time.Time) {
fmt.Printf("previousSpawnTime: \n(%+v)\n", previousSpawnTime)
fmt.Printf("time.Time: \n(%+v)\n", time.Time{})
// PrepareConsumerForLaunch prepares to move the launch of a consumer chain from the previous spawn time to spawn time.
// Previous spawn time can correspond to its zero value if the validator was not previously set for launch.
func (k Keeper) PrepareConsumerForLaunch(ctx sdk.Context, consumerId string, previousSpawnTime time.Time, spawnTime time.Time) error {
if !previousSpawnTime.Equal(time.Time{}) {
fmt.Println("mpika edo")
// if this is not the first initialization and hence `previousSpawnTime` does not contain the zero value of `Time`
// remove the consumer id from the previous spawn time
k.RemoveConsumerToBeLaunchedFromSpawnTime(ctx, consumerId, previousSpawnTime)
err := k.RemoveConsumerToBeLaunchedFromSpawnTime(ctx, consumerId, previousSpawnTime)
if err != nil {
return err
}
}
k.AppendConsumerToBeLaunchedOnSpawnTime(ctx, consumerId, spawnTime)
return k.AppendConsumerToBeLaunchedOnSpawnTime(ctx, consumerId, spawnTime)
}

// CanLaunch checks on whether the consumer with `consumerId` has set all the initialization parameters set and hence
Expand Down
Loading

0 comments on commit a400de1

Please sign in to comment.