Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of rand in simulation test #740

Merged
merged 3 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x/campaign/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func SimulateMsgBurnVouchers(
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
campID, simAccount, vouchers, found := GetAccountWithVouchers(ctx, bk, k, accs, false)
campID, simAccount, vouchers, found := GetAccountWithVouchers(r, ctx, bk, k, accs, false)
if !found {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgBurnVouchers, "skip burn vouchers"), nil, nil
}
Expand All @@ -292,7 +292,7 @@ func SimulateMsgRedeemVouchers(
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
campID, simAccount, vouchers, found := GetAccountWithVouchers(ctx, bk, k, accs, true)
campID, simAccount, vouchers, found := GetAccountWithVouchers(r, ctx, bk, k, accs, true)
if !found {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgRedeemVouchers, "skip redeem vouchers"), nil, nil
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func SimulateMsgSendVouchers(
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
_, simAccount, vouchers, found := GetAccountWithVouchers(ctx, bk, k, accs, false)
_, simAccount, vouchers, found := GetAccountWithVouchers(r, ctx, bk, k, accs, false)
if !found {
return simtypes.NoOpMsg(banktypes.ModuleName, banktypes.TypeMsgSend, "skip send vouchers"), nil, nil
}
Expand Down
10 changes: 6 additions & 4 deletions x/campaign/simulation/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func GetSharesFromCampaign(r *rand.Rand, ctx sdk.Context, k keeper.Keeper, campI

// GetAccountWithVouchers returns an account that has vouchers for a campaign
func GetAccountWithVouchers(
r *rand.Rand,
ctx sdk.Context,
bk types.BankKeeper,
k keeper.Keeper,
Expand Down Expand Up @@ -178,10 +179,11 @@ func GetAccountWithVouchers(
bk.IterateAccountBalances(ctx, accountAddr, func(coin sdk.Coin) bool {
coinCampID, err := types.VoucherCampaign(coin.Denom)
if err == nil && coinCampID == campID {
// retain a random portion of the balance in the range [0, coin.Amount)
retainAmt := sdk.NewInt(rand.Int63n(coin.Amount.Int64()))
coin.Amount = coin.Amount.Sub(retainAmt)
coins = append(coins, coin)
// fetch a part of each voucher hold by the account
amt, err := simtypes.RandPositiveInt(r, coin.Amount)
if err == nil {
coins = append(coins, sdk.NewCoin(coin.Denom, amt))
}
}
return false
})
Expand Down
6 changes: 3 additions & 3 deletions x/campaign/simulation/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestGetAccountWithVouchers(t *testing.T) {
}

t.Run("no account", func(t *testing.T) {
_, _, _, found := simcampaign.GetAccountWithVouchers(ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, false)
_, _, _, found := simcampaign.GetAccountWithVouchers(r, ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, false)
require.False(t, found)
})

Expand All @@ -236,7 +236,7 @@ func TestGetAccountWithVouchers(t *testing.T) {
campaign.MainnetID = tk.LaunchKeeper.AppendChain(ctx, chain)
campaign.CampaignID = tk.CampaignKeeper.AppendCampaign(ctx, campaign)
mint(acc.Address, sample.Vouchers(r, campaign.CampaignID))
campID, acc, coins, found := simcampaign.GetAccountWithVouchers(ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, false)
campID, acc, coins, found := simcampaign.GetAccountWithVouchers(r, ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, false)
require.True(t, found)
require.EqualValues(t, campaign.CampaignID, campID)
require.False(t, coins.Empty())
Expand All @@ -249,7 +249,7 @@ func TestGetAccountWithVouchers(t *testing.T) {
campaign.MainnetInitialized = false
campaign.CampaignID = tk.CampaignKeeper.AppendCampaign(ctx, campaign)
mint(acc.Address, sample.Vouchers(r, campaign.CampaignID))
campID, acc, coins, found := simcampaign.GetAccountWithVouchers(ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, true)
campID, acc, coins, found := simcampaign.GetAccountWithVouchers(r, ctx, tk.BankKeeper, *tk.CampaignKeeper, accs, true)
require.True(t, found)
require.EqualValues(t, campaign.CampaignID, campID)
require.False(t, coins.Empty())
Expand Down