Skip to content

Commit

Permalink
Add simulation, fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Jan 3, 2025
1 parent a2653d6 commit 9612d78
Show file tree
Hide file tree
Showing 16 changed files with 757 additions and 37 deletions.
3 changes: 2 additions & 1 deletion app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
lsmtypes "github.com/cosmos/gaia/v22/x/lsm/types"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

lsmtypes "github.com/cosmos/gaia/v22/x/lsm/types"
)

func (appKeepers *AppKeepers) GenerateKeys() {
Expand Down
4 changes: 3 additions & 1 deletion x/lsm/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
goerrors "errors"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -86,7 +87,8 @@ func (k Keeper) WithdrawSingleShareRecordReward(ctx context.Context, recordID ui

// WithdrawTokenizeShareRecordReward withdraws rewards for owning a TokenizeShareRecord
func (k Keeper) WithdrawTokenizeShareRecordReward(ctx context.Context, ownerAddr sdk.AccAddress,
recordID uint64) (sdk.Coins, error) {
recordID uint64,
) (sdk.Coins, error) {
record, err := k.GetTokenizeShareRecord(ctx, recordID)
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions x/lsm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
goerrors "errors"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"cosmossdk.io/math"
"cosmossdk.io/store/prefix"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/gaia/v22/x/lsm/types"
)
Expand Down
1 change: 1 addition & 0 deletions x/lsm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down
13 changes: 9 additions & 4 deletions x/lsm/keeper/liquid_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
Expand Down Expand Up @@ -107,7 +108,8 @@ func (k Keeper) CheckExceedsGlobalLiquidStakingCap(ctx context.Context, tokens m
// A liquid delegation is defined as either tokenized shares, or a delegation from an ICA Account
// Returns true if the cap is exceeded
func (k Keeper) CheckExceedsValidatorBondCap(ctx context.Context, validator stakingtypes.Validator,
shares math.LegacyDec) (bool, error) {
shares math.LegacyDec,
) (bool, error) {
validatorBondFactor, err := k.ValidatorBondFactor(ctx)
if err != nil {
return false, err
Expand All @@ -130,7 +132,8 @@ func (k Keeper) CheckExceedsValidatorBondCap(ctx context.Context, validator stak
// we need to add the shares to the current validator's delegator shares to get the total shares
// Returns true if the cap is exceeded
func (k Keeper) CheckExceedsValidatorLiquidStakingCap(ctx context.Context, validator stakingtypes.Validator,
shares math.LegacyDec, sharesAlreadyBonded bool) (bool, error) {
shares math.LegacyDec, sharesAlreadyBonded bool,
) (bool, error) {
// TODO: eric -- move LiquidShares outside of staking module validator type
updatedLiquidShares := validator.LiquidShares.Add(shares)

Expand Down Expand Up @@ -185,7 +188,8 @@ func (k Keeper) DecreaseTotalLiquidStakedTokens(ctx context.Context, amount math
// 1. (TotalLiquidStakedTokens / TotalStakedTokens) <= ValidatorLiquidStakingCap
// 2. LiquidShares <= (ValidatorBondShares * ValidatorBondFactor)
func (k Keeper) SafelyIncreaseValidatorLiquidShares(ctx context.Context, valAddress sdk.ValAddress,
shares math.LegacyDec, sharesAlreadyBonded bool) (stakingtypes.Validator, error) {
shares math.LegacyDec, sharesAlreadyBonded bool,
) (stakingtypes.Validator, error) {
validator, err := k.stakingKeeper.GetValidator(ctx, valAddress)
if err != nil {
return validator, err
Expand Down Expand Up @@ -222,7 +226,8 @@ func (k Keeper) SafelyIncreaseValidatorLiquidShares(ctx context.Context, valAddr

// DecreaseValidatorLiquidShares decrements the liquid shares on a validator
func (k Keeper) DecreaseValidatorLiquidShares(ctx context.Context, valAddress sdk.ValAddress,
shares math.LegacyDec) (stakingtypes.Validator, error) {
shares math.LegacyDec,
) (stakingtypes.Validator, error) {
validator, err := k.stakingKeeper.GetValidator(ctx, valAddress)
if err != nil {
return validator, err
Expand Down
1 change: 1 addition & 0 deletions x/lsm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
Expand Down
6 changes: 4 additions & 2 deletions x/lsm/keeper/tokenize_share_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"

gogotypes "github.com/cosmos/gogoproto/types"

errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
gogotypes "github.com/cosmos/gogoproto/types"

"github.com/cosmos/gaia/v22/x/lsm/types"
)
Expand Down Expand Up @@ -53,7 +55,7 @@ func (k Keeper) GetTokenizeShareRecord(ctx context.Context, id uint64) (tokenize
func (k Keeper) GetTokenizeShareRecordsByOwner(ctx context.Context, owner sdk.AccAddress) (tokenizeShareRecords []types.TokenizeShareRecord) {
store := k.storeService.OpenKVStore(ctx)

it := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), types.GetTokenizeShareRecordIdsByOwnerPrefix(owner))
it := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), types.GetTokenizeShareRecordIDsByOwnerPrefix(owner))
defer it.Close()

for ; it.Valid(); it.Next() {
Expand Down
23 changes: 12 additions & 11 deletions x/lsm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/spf13/cobra"

"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking/exported"

Expand All @@ -27,11 +29,10 @@ const (
)

var (
_ module.AppModuleBasic = AppModuleBasic{}
// _ module.AppModuleSimulation = AppModule{}
_ module.HasServices = AppModule{}
// _ module.HasInvariants = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
_ module.HasServices = AppModule{}
_ module.HasGenesis = AppModule{}

_ appmodule.AppModule = AppModule{}
)
Expand Down Expand Up @@ -92,6 +93,7 @@ type AppModule struct {
keeper *keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper

// legacySubspace is used solely for migration of x/params managed parameters
legacySubspace exported.Subspace
Expand All @@ -103,13 +105,15 @@ func NewAppModule(
keeper *keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.StakingKeeper,
ls exported.Subspace,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc, ak: ak},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
stakingKeeper: sk,
legacySubspace: ls,
}
}
Expand Down Expand Up @@ -160,10 +164,8 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// TODO eric-fix these
/*
// ProposalMsgs returns msgs used for governance proposals for simulations.
func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
func (AppModule) ProposalMsgs(_ module.SimulationState) []simtypes.WeightedProposalMsg {
return simulation.ProposalMsgs()
}

Expand All @@ -175,8 +177,7 @@ func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
// WeightedOperations returns the all the lsm module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
simState.AppParams, simState.Cdc, simState.TxConfig,
am.accountKeeper, am.bankKeeper, am.keeper,
simState.AppParams, simState.TxConfig,
am.accountKeeper, am.bankKeeper, am.stakingKeeper, am.keeper,
)
}
*/
60 changes: 60 additions & 0 deletions x/lsm/simulation/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package simulation

import (
"bytes"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"

"github.com/cosmos/gaia/v22/x/lsm/types"
)

// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding lsm type.
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.TokenizeShareRecordPrefix):
var recordA, recordB types.TokenizeShareRecord

cdc.MustUnmarshal(kvA.Value, &recordA)
cdc.MustUnmarshal(kvB.Value, &recordB)

return fmt.Sprintf("%v\n%v", recordA, recordB)
case bytes.Equal(kvA.Key[:1], types.TokenizeShareRecordIDByOwnerPrefix),
bytes.Equal(kvA.Key[:1], types.TokenizeShareRecordIDByDenomPrefix),
bytes.Equal(kvA.Key[:1], types.LastTokenizeShareRecordIDKey):
var idA, idB uint64

idA = sdk.BigEndianToUint64(kvA.Value)
idB = sdk.BigEndianToUint64(kvB.Value)

return fmt.Sprintf("%v\n%v", idA, idB)
case bytes.Equal(kvA.Key[:1], types.TotalLiquidStakedTokensKey):
var tokensA, tokensB sdk.IntProto

cdc.MustUnmarshal(kvA.Value, &tokensA)
cdc.MustUnmarshal(kvB.Value, &tokensB)

return fmt.Sprintf("%v\n%v", tokensA, tokensB)
case bytes.Equal(kvA.Key[:1], types.TokenizeSharesLockPrefix):
var lockA, lockB types.TokenizeShareLock

cdc.MustUnmarshal(kvA.Value, &lockA)
cdc.MustUnmarshal(kvB.Value, &lockB)

return fmt.Sprintf("%v\n%v", lockA, lockB)
case bytes.Equal(kvA.Key[:1], types.TokenizeSharesUnlockQueuePrefix):
var authsA, authsB types.PendingTokenizeShareAuthorizations

cdc.MustUnmarshal(kvA.Value, &authsA)
cdc.MustUnmarshal(kvB.Value, &authsB)

return fmt.Sprintf("%v\n%v", authsA, authsB)
default:
panic(fmt.Sprintf("invalid lsm key prefix %X", kvA.Key[:1]))
}
}
}
1 change: 1 addition & 0 deletions x/lsm/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"

Expand Down
Loading

0 comments on commit 9612d78

Please sign in to comment.