Skip to content

Commit

Permalink
feat(claim): simulation for MsgClaimInitial (#886)
Browse files Browse the repository at this point in the history
* squeletton

* simulation

* format
  • Loading branch information
lumtis authored Jul 8, 2022
1 parent 1d22b30 commit 26a17f0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 30 deletions.
57 changes: 42 additions & 15 deletions x/claim/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,56 @@ package claim
import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
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/simulation"

"github.com/tendermint/spn/testutil/sample"
claimsimulation "github.com/tendermint/spn/x/claim/simulation"
"github.com/tendermint/spn/x/claim/types"
)

// avoid unused import issue
var (
_ = sample.AccAddress
_ = claimsimulation.FindAccount
_ = simappparams.StakePerAccount
_ = simulation.MsgEntryKind
_ = baseapp.Paramspace
)

const (
// this line is used by starport scaffolding # simapp/module/const
airdropDenom = "drop"

opWeightMsgClaimInitial = "op_weight_msg_claim_initial"
defaultWeightMsgClaimInitial int = 50

// this line is used by starport scaffolding # simapp/module/const
)

// GenerateGenesisState creates a randomized GenState of the module
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs := make([]string, len(simState.Accounts))
claimRecords := make([]types.ClaimRecord, len(simState.Accounts))
totalSupply := sdk.ZeroInt()
for i, acc := range simState.Accounts {
accs[i] = acc.Address.String()

// fill claim records from simulation accounts
accSupply := sdk.NewIntFromUint64(simState.Rand.Uint64() % 1000)
claimRecords[i] = types.ClaimRecord{
Claimable: accSupply,
Address: acc.Address.String(),
}
totalSupply = totalSupply.Add(accSupply)
}

claimGenesis := types.GenesisState{
//Params: types.DefaultParams(),
AirdropSupply: sdk.NewCoin("foo", sdk.ZeroInt()),
Params: types.DefaultParams(),
AirdropSupply: sdk.NewCoin(airdropDenom, totalSupply),
Missions: []types.Mission{
{
MissionID: 1,
Description: "initial claim",
Weight: sdk.OneDec(),
},
},
InitialClaim: types.InitialClaim{
Enabled: true,
MissionID: 1,
},
ClaimRecords: claimRecords,
// this line is used by starport scaffolding # simapp/module/genesisState
}
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&claimGenesis)
Expand All @@ -59,6 +75,17 @@ func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgClaimInitial int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgClaimInitial, &weightMsgClaimInitial, nil,
func(_ *rand.Rand) {
weightMsgClaimInitial = defaultWeightMsgClaimInitial
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgClaimInitial,
claimsimulation.SimulateMsgClaimInitial(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand Down
15 changes: 0 additions & 15 deletions x/claim/simulation/simap.go

This file was deleted.

61 changes: 61 additions & 0 deletions x/claim/simulation/simulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/simulation"
"github.com/tendermint/spn/x/claim/keeper"
"github.com/tendermint/spn/x/claim/types"
)

func SimulateMsgClaimInitial(
ak types.AccountKeeper,
bk types.BankKeeper,
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) {
msg := &types.MsgClaimInitial{}

// find an account
simAccount, _ := simtypes.RandomAcc(r, accs)

// check the account has a claim record and initial claim has not been completed
cr, found := k.GetClaimRecord(ctx, simAccount.Address.String())
if !found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "account has no claim record"), nil, nil
}
if cr.IsMissionCompleted(1) {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "account already completed initial claim"), nil, nil
}

// initialize basic message
msg = &types.MsgClaimInitial{
Claimer: simAccount.Address.String(),
}

txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: sdk.NewCoins(),
}

return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}
}

0 comments on commit 26a17f0

Please sign in to comment.