-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add restaking simulation tests (#212)
## Description Closes: MILK-150 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/milkyway-labs/milkyway/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html) - [ ] included the necessary unit and integration [tests](https://github.com/milkyway-labs/milkyway/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
Showing
14 changed files
with
1,138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package simulation | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
|
||
"github.com/milkyway-labs/milkyway/v3/x/pools/types" | ||
) | ||
|
||
// GetGenesisState returns the pools genesis state from the SimulationState | ||
func GetGenesisState(simState *module.SimulationState) types.GenesisState { | ||
operatorsGenesisJSON, found := simState.GenState[types.ModuleName] | ||
var operatorsGenesis types.GenesisState | ||
if found { | ||
simState.Cdc.MustUnmarshalJSON(operatorsGenesisJSON, &operatorsGenesis) | ||
} else { | ||
operatorsGenesis = *types.DefaultGenesis() | ||
} | ||
|
||
return operatorsGenesis | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package simulation | ||
|
||
import ( | ||
"math/rand" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/milkyway-labs/milkyway/v3/utils" | ||
"github.com/milkyway-labs/milkyway/v3/x/pools/keeper" | ||
"github.com/milkyway-labs/milkyway/v3/x/pools/types" | ||
) | ||
|
||
// GetRandomExistingPool returns a random existing pool | ||
func GetRandomExistingPool(r *rand.Rand, ctx sdk.Context, k *keeper.Keeper, filter func(s types.Pool) bool) (types.Pool, bool) { | ||
pools, err := k.GetPools(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if filter != nil { | ||
pools = utils.Filter(pools, filter) | ||
} | ||
|
||
if len(pools) == 0 { | ||
return types.Pool{}, false | ||
} | ||
|
||
randomServiceIndex := r.Intn(len(pools)) | ||
return pools[randomServiceIndex], true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package simulation | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
|
||
"github.com/milkyway-labs/milkyway/v3/x/restaking/keeper" | ||
"github.com/milkyway-labs/milkyway/v3/x/restaking/types" | ||
) | ||
|
||
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's | ||
// Value to the corresponding restaking type. | ||
func NewDecodeStore(cdc codec.BinaryCodec, keeper *keeper.Keeper) func(kvA kv.Pair, kvB kv.Pair) string { | ||
collectionsDecoder := simtypes.NewStoreDecoderFuncFromCollectionsSchema(keeper.Schema) | ||
|
||
return func(kvA, kvB kv.Pair) string { | ||
switch { | ||
|
||
case bytes.Equal(kvA.Key[:1], types.UnbondingIDKey): | ||
idA := binary.BigEndian.Uint64(kvA.Value) | ||
idB := binary.BigEndian.Uint64(kvB.Value) | ||
return fmt.Sprintf("%d\n%d", idA, idB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.UnbondingIndexKey): | ||
return fmt.Sprintf("%v\n%v", kvA.Value, kvB.Value) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.UnbondingTypeKey): | ||
typeA := binary.BigEndian.Uint32(kvA.Value) | ||
typeB := binary.BigEndian.Uint32(kvB.Value) | ||
return fmt.Sprintf("%d\n%d", typeA, typeB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.PoolDelegationPrefix): | ||
delegationA := types.MustUnmarshalDelegation(cdc, kvA.Value) | ||
delegationB := types.MustUnmarshalDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", delegationA, delegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.PoolUnbondingDelegationPrefix): | ||
undelegationA := types.MustUnmarshalUnbondingDelegation(cdc, kvA.Value) | ||
undelegationB := types.MustUnmarshalUnbondingDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", undelegationA, undelegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.OperatorDelegationPrefix): | ||
delegationA := types.MustUnmarshalDelegation(cdc, kvA.Value) | ||
delegationB := types.MustUnmarshalDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", delegationA, delegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.OperatorUnbondingDelegationPrefix): | ||
undelegationA := types.MustUnmarshalUnbondingDelegation(cdc, kvA.Value) | ||
undelegationB := types.MustUnmarshalUnbondingDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", undelegationA, undelegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.ServiceDelegationPrefix): | ||
delegationA := types.MustUnmarshalDelegation(cdc, kvA.Value) | ||
delegationB := types.MustUnmarshalDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", delegationA, delegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.ServiceUnbondingDelegationPrefix): | ||
undelegationA := types.MustUnmarshalUnbondingDelegation(cdc, kvA.Value) | ||
undelegationB := types.MustUnmarshalUnbondingDelegation(cdc, kvB.Value) | ||
return fmt.Sprintf("%v\n%v", undelegationA, undelegationB) | ||
|
||
case bytes.Equal(kvA.Key[:1], types.UnbondingQueueKey): | ||
var listA, listB types.DTDataList | ||
cdc.MustUnmarshal(kvA.Value, &listA) | ||
cdc.MustUnmarshal(kvB.Value, &listB) | ||
return fmt.Sprintf("%v\n%v", listA, listB) | ||
|
||
// Collections | ||
case bytes.Equal(kvA.Key[:1], types.OperatorJoinedServicesPrefix), | ||
bytes.Equal(kvA.Key[:1], types.ServiceOperatorsAllowListPrefix), | ||
bytes.Equal(kvA.Key[:1], types.ServiceSecuringPoolsPrefix), | ||
bytes.Equal(kvA.Key[:1], types.UserPreferencesPrefix): | ||
return collectionsDecoder(kvA, kvB) | ||
|
||
default: | ||
panic(fmt.Sprintf("invalid restaking key prefix %X", kvA.Key[:1])) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package simulation | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
|
||
operatorssimulation "github.com/milkyway-labs/milkyway/v3/x/operators/simulation" | ||
poolssimulation "github.com/milkyway-labs/milkyway/v3/x/pools/simulation" | ||
"github.com/milkyway-labs/milkyway/v3/x/restaking/types" | ||
servicessimulation "github.com/milkyway-labs/milkyway/v3/x/services/simulation" | ||
) | ||
|
||
// RandomizedGenState generates a random GenesisState for the restaking module | ||
func RandomizedGenState(simState *module.SimulationState) { | ||
poolsGenesis := poolssimulation.GetGenesisState(simState) | ||
operatorsGenesis := operatorssimulation.GetGenesisState(simState) | ||
servicesGenesis := servicessimulation.GetGenesisState(simState) | ||
|
||
genesis := types.NewGenesis( | ||
RandomOperatorJoinedServices(simState.Rand, operatorsGenesis.Operators, servicesGenesis.Services), | ||
RandomServiceAllowedOperators(simState.Rand, servicesGenesis.Services, operatorsGenesis.Operators), | ||
RandomServiceSecuringPools(simState.Rand, poolsGenesis.Pools, servicesGenesis.Services), | ||
// empty delegations and undelegations since we need to also perform side effects to other | ||
// modules to keep the shares consistent. | ||
nil, | ||
nil, | ||
RandomUserPreferencesEntries(simState.Rand, servicesGenesis.Services), | ||
RandomParams(simState.Rand), | ||
) | ||
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(genesis) | ||
} |
Oops, something went wrong.