Skip to content

Commit

Permalink
Add more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Warehime committed Dec 19, 2024
1 parent 8c9573c commit 66dfe2d
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 202 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,6 @@ proto-gen:
@echo "Generating Protobuf files"
@$(protoImage) sh ./proto/scripts/protocgen.sh

proto-pulsar-gen:
@echo "Generating Dep-Inj Protobuf files"
@$(protoImage) sh ./scripts/protocgen-pulsar.sh

proto-swagger-gen:
@echo "Generating Protobuf Swagger"
@$(protoImage) sh ./proto/scripts/protoc-swagger-gen.sh
Expand Down
19 changes: 0 additions & 19 deletions proto/buf.gen.pulsar.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions proto/gaia/lsm/v1beta1/lsm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ message Params {
option (amino.name) = "gaia/x/lsm/Params";
option (gogoproto.equal) = true;

// bond_denom defines the bondable coin denomination.
string bond_denom = 5;

// validator_bond_factor is required as a safety check for tokenizing shares
// and delegations from liquid staking providers
string validator_bond_factor = 7 [
Expand Down
74 changes: 74 additions & 0 deletions x/lsm/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package keeper

import (
"context"
"fmt"

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

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

// InitGenesis sets lsm information for genesis
func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) {
//
// Set the total liquid staked tokens
k.SetTotalLiquidStakedTokens(ctx, data.TotalLiquidStakedTokens)

// Set each tokenize share record, as well as the last tokenize share record ID
latestID := uint64(0)
for _, tokenizeShareRecord := range data.TokenizeShareRecords {
if err := k.AddTokenizeShareRecord(ctx, tokenizeShareRecord); err != nil {
panic(err)
}
if tokenizeShareRecord.Id > latestID {
latestID = tokenizeShareRecord.Id
}
}
if data.LastTokenizeShareRecordId < latestID {
panic("Tokenize share record specified with ID greater than the latest ID")
}
k.SetLastTokenizeShareRecordID(ctx, data.LastTokenizeShareRecordId)

// Set the tokenize shares locks for accounts that have disabled tokenizing shares
// The lock can either be in status LOCKED or LOCK_EXPIRING
// If it is in status LOCK_EXPIRING, the unlocking must also be queued
for _, tokenizeShareLock := range data.TokenizeShareLocks {
address, err := k.authKeeper.AddressCodec().StringToBytes(tokenizeShareLock.Address)
if err != nil {
panic(err)
}

switch tokenizeShareLock.Status {
case types.TOKENIZE_SHARE_LOCK_STATUS_LOCKED.String():
k.AddTokenizeSharesLock(ctx, address)

case types.TOKENIZE_SHARE_LOCK_STATUS_LOCK_EXPIRING.String():
completionTime := tokenizeShareLock.CompletionTime

authorizations := k.GetPendingTokenizeShareAuthorizations(ctx, completionTime)
authorizations.Addresses = append(authorizations.Addresses, sdk.AccAddress(address).String())

k.SetPendingTokenizeShareAuthorizations(ctx, completionTime, authorizations)
k.SetTokenizeSharesUnlockTime(ctx, address, completionTime)

default:
panic(fmt.Sprintf("Unsupported tokenize share lock status %s", tokenizeShareLock.Status))
}
}
}

func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
params, err := k.GetParams(ctx)
if err != nil {
panic(err)
}

return &types.GenesisState{
Params: params,
TokenizeShareRecords: k.GetAllTokenizeShareRecords(ctx),
LastTokenizeShareRecordId: k.GetLastTokenizeShareRecordID(ctx),
TotalLiquidStakedTokens: k.GetTotalLiquidStakedTokens(ctx),
TokenizeShareLocks: k.GetAllTokenizeSharesLocks(ctx),
}
}
59 changes: 13 additions & 46 deletions x/lsm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@ package lsm

import (
"context"
modulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"encoding/json"
"fmt"
abci "github.com/cometbft/cometbft/abci/types"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"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"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking/exported"

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

Expand All @@ -35,7 +31,7 @@ var (
// _ module.AppModuleSimulation = AppModule{}
_ module.HasServices = AppModule{}
// _ module.HasInvariants = AppModule{}
_ module.HasABCIGenesis = AppModule{}
_ module.HasGenesis = AppModule{}

_ appmodule.AppModule = AppModule{}
)
Expand All @@ -61,7 +57,7 @@ func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}

// DefaultGenesis returns default genesis state as raw bytes for the staking
// DefaultGenesis returns default genesis state as raw bytes for the lsm
// module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
Expand Down Expand Up @@ -126,7 +122,7 @@ func (am AppModule) IsAppModule() {}

// TODO eric -- replace lsm invariants
/*
// RegisterInvariants registers the staking module invariants.
// RegisterInvariants registers the lsm module invariants.
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
keeper.RegisterInvariants(ir, am.keeper)
}
Expand All @@ -140,12 +136,12 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}

// InitGenesis performs genesis initialization for the lsm module.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
var genesisState types.GenesisState

cdc.MustUnmarshalJSON(data, &genesisState)

return am.keeper.InitGenesis(ctx, &genesisState)
am.keeper.InitGenesis(ctx, &genesisState)
}

// ExportGenesis returns the exported genesis state as raw bytes for the lsm
Expand All @@ -157,37 +153,8 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return consensusVersion }

func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}

type ModuleInputs struct {
depinject.In

Config *modulev1.Module
ValidatorAddressCodec runtime.ValidatorAddressCodec
ConsensusAddressCodec runtime.ConsensusAddressCodec
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
Cdc codec.Codec
StoreService store.KVStoreService

// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace `optional:"true"`
}

// Dependency Injection Outputs
type ModuleOutputs struct {
depinject.Out

LsmKeeper *keeper.Keeper
Module appmodule.AppModule
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
// TODO eric -- fix this
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)

Check failure on line 159 in x/lsm/module.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body (typecheck)

Check failure on line 159 in x/lsm/module.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected declaration, found authority (typecheck)

Check failure on line 159 in x/lsm/module.go

View workflow job for this annotation

GitHub Actions / Analyze

syntax error: non-declaration statement outside function body
if in.Config.Authority != "" {
Expand All @@ -207,15 +174,15 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
return ModuleOutputs{LsmKeeper: k, Module: m}
}

// TODO eric replace simulation
// AppModuleSimulation functions

/*
// GenerateGenesisState creates a randomized GenState of the lsm module.
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 {
return simulation.ProposalMsgs()
Expand All @@ -233,4 +200,4 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
am.accountKeeper, am.bankKeeper, am.keeper,
)
}
*/
*/
66 changes: 66 additions & 0 deletions x/lsm/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package simulation

import (
"encoding/json"
"fmt"
"math/rand"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"

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

// Simulation parameter constants
const (
ValidatorBondFactor = "validator_bond_factor"
GlobalLiquidStakingCap = "global_liquid_staking_cap"
ValidatorLiquidStakingCap = "validator_liquid_staking_cap"
)

// getGlobalLiquidStakingCap returns randomized GlobalLiquidStakingCap between 0-1.
func getGlobalLiquidStakingCap(r *rand.Rand) sdkmath.LegacyDec {
return simulation.RandomDecAmount(r, sdkmath.LegacyOneDec())
}

// getValidatorLiquidStakingCap returns randomized ValidatorLiquidStakingCap between 0-1.
func getValidatorLiquidStakingCap(r *rand.Rand) sdkmath.LegacyDec {
return simulation.RandomDecAmount(r, sdkmath.LegacyOneDec())
}

// getValidatorBondFactor returns randomized ValidatorBondCap between -1 and 300.
func getValidatorBondFactor(r *rand.Rand) sdkmath.LegacyDec {
return sdkmath.LegacyNewDec(int64(simulation.RandIntBetween(r, -1, 300)))
}

// RandomizedGenState generates a random GenesisState for lsm
func RandomizedGenState(simState *module.SimulationState) {
// params
var (
validatorBondFactor sdkmath.LegacyDec
globalLiquidStakingCap sdkmath.LegacyDec
validatorLiquidStakingCap sdkmath.LegacyDec
)

simState.AppParams.GetOrGenerate(ValidatorBondFactor, &validatorBondFactor, simState.Rand, func(r *rand.Rand) { validatorBondFactor = getValidatorBondFactor(r) })

simState.AppParams.GetOrGenerate(GlobalLiquidStakingCap, &globalLiquidStakingCap, simState.Rand, func(r *rand.Rand) { globalLiquidStakingCap = getGlobalLiquidStakingCap(r) })

simState.AppParams.GetOrGenerate(ValidatorLiquidStakingCap, &validatorLiquidStakingCap, simState.Rand, func(r *rand.Rand) { validatorLiquidStakingCap = getValidatorLiquidStakingCap(r) })

params := types.NewParams(
validatorBondFactor,
globalLiquidStakingCap,
validatorLiquidStakingCap,
)

lsmGenesis := types.NewGenesisState(params, nil, 0, sdkmath.ZeroInt(), nil)

bz, err := json.MarshalIndent(&lsmGenesis.Params, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated lsm parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(lsmGenesis)
}
25 changes: 25 additions & 0 deletions x/lsm/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

import "cosmossdk.io/math"

func NewGenesisState(
params Params,
tsr []TokenizeShareRecord,
recordId uint64,
liquidStakeTokens math.Int,
locks []TokenizeShareLock,
) *GenesisState {
return &GenesisState{
Params: params,
TokenizeShareRecords: tsr,
LastTokenizeShareRecordId: recordId,
TotalLiquidStakedTokens: liquidStakeTokens,
TokenizeShareLocks: locks,
}
}

func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
}
}
Loading

0 comments on commit 66dfe2d

Please sign in to comment.