Skip to content

Commit

Permalink
Merge pull request #175 from crescent-network/amm-module-fix-simulation
Browse files Browse the repository at this point in the history
fix: disable deprecated modules and fix simulation
  • Loading branch information
crypin authored Aug 9, 2023
2 parents 57a4d81 + 352d813 commit a413eaa
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 32 deletions.
30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,30 @@ benchmark:
test-sim-nondeterminism:
@echo "Running non-determinism test..."
@go test -mod=readonly $(SIMAPP) -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=100 -BlockSize=50 -Commit=true -Period=0 -v -timeout 1h
-NumBlocks=20 -BlockSize=100 -Commit=true -Seed=42 -Period=1 -v -timeout 10m

test-sim-nondeterminism-long:
@echo "Running non-determinism-long test..."
@go test -mod=readonly $(SIMAPP) -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=100 -BlockSize=200 -Commit=true -Seed=42 -Period=1 -v -timeout 10h

test-sim-import-export: runsim
@echo "Running application import/export simulation. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 2 2 TestAppImportExport
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds=1,10,100,1000 -ExitOnFail 50 5 TestAppImportExport

test-sim-import-export-long: runsim
@echo "Running application simulation-import-export-long. This may take several minutes..."
$(eval SEED := $(shell awk 'BEGIN{srand(); for (i=1; i<=50; i++) {n=int(10000*rand())+1; printf "%d%s", n, (i==50 ? "" : ",")}}'))
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds="$(SEED)" -ExitOnFail 500 5 TestAppImportExport

test-sim-after-import: runsim
@echo "Running application simulation-after-import. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 2 2 TestAppSimulationAfterImport

test-sim-nondeterminism-long:
@echo "Running non-determinism test..."
@go test -mod=readonly $(SIMAPP) -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=100 -BlockSize=100 -Commit=true -Period=0 -v -timeout 1h

test-sim-import-export-long: runsim
@echo "Running application import/export simulation. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 5 5 TestAppImportExport
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds=1,10,100,1000 -ExitOnFail 50 5 TestAppSimulationAfterImport

test-sim-after-import-long: runsim
@echo "Running application simulation-after-import. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 5 5 TestAppSimulationAfterImport
@echo "Running application simulation-after-import-long. This may take several minutes..."
$(eval SEED := $(shell awk 'BEGIN{srand(); for (i=1; i<=50; i++) {n=int(10000*rand())+1; printf "%d%s", n, (i==50 ? "" : ",")}}'))
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -Seeds="$(SEED)" -ExitOnFail 500 5 TestAppSimulationAfterImport

.PHONY: \
test-sim-nondeterminism \
Expand Down
2 changes: 2 additions & 0 deletions app/ante/msg_ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
claimtypes "github.com/crescent-network/crescent/v5/x/claim/types"
exchangetypes "github.com/crescent-network/crescent/v5/x/exchange/types"
farmingtypes "github.com/crescent-network/crescent/v5/x/farming/types"
liquidfarmingtypes "github.com/crescent-network/crescent/v5/x/liquidfarming/types"
liquiditytypes "github.com/crescent-network/crescent/v5/x/liquidity/types"
)

Expand Down Expand Up @@ -85,6 +86,7 @@ func (d MsgFilterDecorator) ValidateMsgs(ctx sdk.Context, msgs []sdk.Msg) error
if legacyMsg, ok := msg.(legacytx.LegacyMsg); ok {
switch legacyMsg.Route() {
case liquiditytypes.RouterKey,
liquidfarmingtypes.RouterKey,
farmingtypes.RouterKey,
claimtypes.RouterKey:
return fmt.Errorf("%s is deprecated msg type", sdk.MsgTypeURL(msg))
Expand Down
10 changes: 6 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,16 +856,12 @@ func NewApp(
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
budget.NewAppModule(appCodec, app.BudgetKeeper, app.AccountKeeper, app.BankKeeper),
farming.NewAppModule(appCodec, app.FarmingKeeper, app.AccountKeeper, app.BankKeeper),
staking.NewAppModule(appCodec, *app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
params.NewAppModule(app.ParamsKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
liquidity.NewAppModule(appCodec, app.LiquidityKeeper, app.AccountKeeper, app.BankKeeper),
liquidstaking.NewAppModule(appCodec, app.LiquidStakingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GovKeeper),
liquidfarming.NewAppModule(appCodec, app.LiquidFarmingKeeper, app.AccountKeeper, app.BankKeeper),
claim.NewAppModule(appCodec, app.ClaimKeeper, app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.GovKeeper, app.LiquidityKeeper, app.LiquidStakingKeeper),
liquidamm.NewAppModule(appCodec, app.LiquidAMMKeeper, app.AccountKeeper, app.BankKeeper),
marketmaker.NewAppModule(appCodec, app.MarketMakerKeeper, app.AccountKeeper, app.BankKeeper),
lpfarm.NewAppModule(appCodec, app.LPFarmKeeper, app.AccountKeeper, app.BankKeeper, app.LiquidityKeeper),
Expand All @@ -874,6 +870,12 @@ func NewApp(
amm.NewAppModule(appCodec, app.AMMKeeper, app.AccountKeeper, app.BankKeeper, app.ExchangeKeeper),
ibc.NewAppModule(app.IBCKeeper),
app.transferModule,

// disable simulation for deprecated modules
//claim.NewAppModule(appCodec, app.ClaimKeeper, app.AccountKeeper, app.BankKeeper, app.DistrKeeper, app.GovKeeper, app.LiquidityKeeper, app.LiquidStakingKeeper),
//farming.NewAppModule(appCodec, app.FarmingKeeper, app.AccountKeeper, app.BankKeeper),
//liquidity.NewAppModule(appCodec, app.LiquidityKeeper, app.AccountKeeper, app.BankKeeper),
//liquidfarming.NewAppModule(appCodec, app.LiquidFarmingKeeper, app.AccountKeeper, app.BankKeeper),
)

app.sm.RegisterStoreDecoders()
Expand Down
11 changes: 6 additions & 5 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const (
// Deprecated claim module
DefaultWeightMsgClaim int = 0

DefaultWeightMsgLiquidFarm int = 50
DefaultWeightMsgLiquidUnfarm int = 10
DefaultWeightMsgPlaceBid int = 20
DefaultWeightMsgRefundBid int = 5
// Deprecated liquidfarming module
DefaultWeightMsgLiquidFarm int = 0
DefaultWeightMsgLiquidUnfarm int = 0
DefaultWeightMsgPlaceBid int = 0
DefaultWeightMsgRefundBid int = 0

DefaultWeightAddPublicPlanProposal int = 5
DefaultWeightUpdatePublicPlanProposal int = 5
Expand All @@ -41,7 +42,7 @@ const (
DefaultWeightAddWhitelistValidatorsProposal int = 50
DefaultWeightUpdateWhitelistValidatorsProposal int = 5
DefaultWeightDeleteWhitelistValidatorsProposal int = 5
DefaultWeightCompleteRedelegationUnbonding int = 30
DefaultWeightCompleteRedelegationUnbonding int = 2
DefaultWeightTallyWithLiquidStaking int = 30

DefaultWeightMsgApplyMarketMaker int = 20
Expand Down
7 changes: 3 additions & 4 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package app
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"testing"

Expand Down Expand Up @@ -312,12 +311,12 @@ func TestAppStateDeterminism(t *testing.T) {
config.AllInvariants = false
config.ChainID = helpers.SimAppChainID

numSeeds := 3
numTimesToRunPerSeed := 5
numSeeds := config.NumBlocks / 10
numTimesToRunPerSeed := 2
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)

for i := 0; i < numSeeds; i++ {
config.Seed = rand.Int63()
config.Seed = config.Seed + int64(i)

for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
Expand Down
2 changes: 1 addition & 1 deletion x/liquidstaking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
// WeightedOperations returns the all the liquidstaking module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper,
simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.stakingKeeper, am.keeper,
)
}
12 changes: 9 additions & 3 deletions x/liquidstaking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
// WeightedOperations returns all the operations from the module with their respective weights.
func WeightedOperations(
appParams simtypes.AppParams, cdc codec.JSONCodec, ak types.AccountKeeper,
bk types.BankKeeper, k keeper.Keeper,
bk types.BankKeeper, sk types.StakingKeeper, k keeper.Keeper,
) simulation.WeightedOperations {

var weightMsgLiquidStake int
Expand All @@ -59,7 +59,7 @@ func WeightedOperations(
),
simulation.NewWeightedOperation(
weightMsgLiquidUnstake,
SimulateMsgLiquidUnstake(ak, bk, k),
SimulateMsgLiquidUnstake(ak, bk, sk, k),
),
}
}
Expand Down Expand Up @@ -127,11 +127,17 @@ func SimulateMsgLiquidStake(ak types.AccountKeeper, bk types.BankKeeper, k keepe

// SimulateMsgLiquidUnstake generates a SimulateMsgLiquidUnstake with random values
// nolint: interfacer
func SimulateMsgLiquidUnstake(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgLiquidUnstake(ak types.AccountKeeper, bk types.BankKeeper, sk types.StakingKeeper, k keeper.Keeper) simtypes.Operation {
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {

liquidVals := k.GetAllLiquidValidators(ctx)
totalLiquidTokens, _ := liquidVals.TotalLiquidTokens(ctx, sk, false)
if !totalLiquidTokens.IsPositive() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgLiquidUnstake, "insufficient total liquid tokens"), nil, nil
}

simAccount, _ := simtypes.RandomAcc(r, accs)
var delegator sdk.AccAddress
var unstakingCoin sdk.Coin
Expand Down
2 changes: 1 addition & 1 deletion x/liquidstaking/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestWeightedOperations(t *testing.T) {
cdc := types.ModuleCdc
appParams := make(simtypes.AppParams)

weightedOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper, app.BankKeeper, app.LiquidStakingKeeper)
weightedOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.LiquidStakingKeeper)

s := rand.NewSource(2)
r := rand.New(s)
Expand Down

0 comments on commit a413eaa

Please sign in to comment.