Skip to content

Commit

Permalink
fix: MsgDelegateService simulation test
Browse files Browse the repository at this point in the history
  • Loading branch information
manu0466 committed Dec 11, 2024
1 parent 30da3d5 commit c4be804
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions x/restaking/simulation/msg_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simulation

import (
"math/rand"
"slices"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -528,7 +529,7 @@ func SimulateMsgDelegateService(
}

// Get a random service
operator, found := servicessimulation.GetRandomExistingService(r, ctx, sk, func(s servicestypes.Service) bool {
service, found := servicessimulation.GetRandomExistingService(r, ctx, sk, func(s servicestypes.Service) bool {
return s.IsActive()
})
if !found {
Expand All @@ -537,6 +538,8 @@ func SimulateMsgDelegateService(

// Get a random delegator with a random amount
delegator, coins, skip := randomDelegatorAndAmount(r, ctx, accs, k, bk, ak)
// Filter the coins to only those that can be restaked toward the service
coins = filterServiceRestakableCoins(sk, ctx, service.ID, coins)

// If coins slice is empty, we can not create valid msg
if len(coins) == 0 {
Expand All @@ -547,7 +550,7 @@ func SimulateMsgDelegateService(
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "skip delegate"), nil, nil
}

msg = types.NewMsgDelegateService(operator.ID, coins, delegator.Address.String())
msg = types.NewMsgDelegateService(service.ID, coins, delegator.Address.String())

return simtesting.SendMsg(r, types.ModuleName, app, ak, bk, msg, ctx, delegator)
}
Expand Down Expand Up @@ -612,3 +615,23 @@ func randomDelegatorAndAmount(

return delegator, coins, false
}

func filterServiceRestakableCoins(sk *serviceskeeper.Keeper, ctx sdk.Context, serviceID uint32, coins sdk.Coins) sdk.Coins {
serviceParams, err := sk.GetServiceParams(ctx, serviceID)
if err != nil {
panic(err)
}
// If empty allows all denoms
if len(serviceParams.AllowedDenoms) == 0 {
return coins
}

filteredCoins := sdk.NewCoins()
for _, coin := range coins {
if slices.Contains(serviceParams.AllowedDenoms, coin.Denom) {
filteredCoins = filteredCoins.Add(coin)
}
}

return filteredCoins
}

0 comments on commit c4be804

Please sign in to comment.