-
Notifications
You must be signed in to change notification settings - Fork 402
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
Add Weighted Operations to simulation #825
Changes from all commits
e11bfe7
2a4ea51
bfb4d31
a872930
dd567ae
fd88b6a
283bde0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ const ( | |
DefaultWeightCommunitySpendProposal int = 5 | ||
DefaultWeightTextProposal int = 5 | ||
DefaultWeightParamChangeProposal int = 5 | ||
DefaultWeightMsgStoreCode int = 100 | ||
DefaultWeightMsgInstantiateContract int = 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this high as well to get some load on our module |
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package simulation | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"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/CosmWasm/wasmd/app/params" | ||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
) | ||
|
||
//go:embed testdata/reflect.wasm | ||
var reflectContract []byte | ||
|
||
// Simulation operation weights constants | ||
//nolint:gosec | ||
const ( | ||
OpWeightMsgStoreCode = "op_weight_msg_store_code" | ||
OpWeightMsgInstantiateContract = "op_weight_msg_instantiate_contract" | ||
) | ||
|
||
// WasmKeeper is a subset of the wasm keeper used by simulations | ||
type WasmKeeper interface { | ||
GetParams(ctx sdk.Context) types.Params | ||
IterateContractInfo(ctx sdk.Context, cb func(sdk.AccAddress, types.ContractInfo) bool) | ||
} | ||
|
||
// WeightedOperations returns all the operations from the module with their respective weights | ||
func WeightedOperations( | ||
simstate *module.SimulationState, | ||
ak types.AccountKeeper, | ||
bk simulation.BankKeeper, | ||
wasmKeeper WasmKeeper, | ||
) simulation.WeightedOperations { | ||
var ( | ||
weightMsgStoreCode int | ||
weightMsgInstantiateContract int | ||
) | ||
|
||
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgStoreCode, &weightMsgStoreCode, nil, | ||
func(_ *rand.Rand) { | ||
weightMsgStoreCode = params.DefaultWeightMsgStoreCode | ||
}, | ||
) | ||
|
||
simstate.AppParams.GetOrGenerate(simstate.Cdc, OpWeightMsgInstantiateContract, &weightMsgInstantiateContract, nil, | ||
func(_ *rand.Rand) { | ||
weightMsgInstantiateContract = params.DefaultWeightMsgInstantiateContract | ||
}, | ||
) | ||
|
||
return simulation.WeightedOperations{ | ||
simulation.NewWeightedOperation( | ||
weightMsgStoreCode, | ||
SimulateMsgStoreCode(ak, bk, wasmKeeper), | ||
), | ||
simulation.NewWeightedOperation( | ||
weightMsgInstantiateContract, | ||
SimulateMsgInstantiateContract(ak, bk, wasmKeeper), | ||
), | ||
} | ||
} | ||
|
||
// SimulateMsgStoreCode generates a MsgStoreCode with random values | ||
func SimulateMsgStoreCode(ak types.AccountKeeper, bk simulation.BankKeeper, wasmKeeper WasmKeeper) simtypes.Operation { | ||
return func( | ||
r *rand.Rand, | ||
app *baseapp.BaseApp, | ||
ctx sdk.Context, | ||
accs []simtypes.Account, | ||
chainID string, | ||
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { | ||
if wasmKeeper.GetParams(ctx).CodeUploadAccess.Permission != types.AccessTypeEverybody { | ||
return simtypes.NoOpMsg(types.ModuleName, types.MsgStoreCode{}.Type(), "no chain permission"), nil, nil | ||
} | ||
|
||
config := &types.AccessConfig{ | ||
Permission: types.AccessTypeEverybody, | ||
} | ||
|
||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
msg := types.MsgStoreCode{ | ||
Sender: simAccount.Address.String(), | ||
WASMByteCode: reflectContract, | ||
InstantiatePermission: config, | ||
} | ||
|
||
txCtx := simulation.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, | ||
} | ||
|
||
return simulation.GenAndDeliverTxWithRandFees(txCtx) | ||
} | ||
} | ||
|
||
// SimulateMsgInstantiateContract generates a MsgInstantiateContract with random values | ||
func SimulateMsgInstantiateContract(ak types.AccountKeeper, bk simulation.BankKeeper, wasmKeeper WasmKeeper) simtypes.Operation { | ||
return func( | ||
r *rand.Rand, | ||
app *baseapp.BaseApp, | ||
ctx sdk.Context, | ||
accs []simtypes.Account, | ||
chainID string, | ||
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
|
||
var contractInfo types.ContractInfo | ||
wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool { | ||
contractInfo = info | ||
return true | ||
}) | ||
|
||
if contractInfo.Equal(types.ContractInfo{}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to look for a CodeInfo here. The system won't have contract instances in the beginning and you abort here so that none will be created |
||
return simtypes.NoOpMsg(types.ModuleName, types.MsgInstantiateContract{}.Type(), "no contracts available"), nil, nil | ||
} | ||
|
||
spendable := bk.SpendableCoins(ctx, simAccount.Address) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice to send some tokens. 💸 |
||
|
||
msg := types.MsgInstantiateContract{ | ||
Sender: simAccount.Address.String(), | ||
Admin: contractInfo.Admin, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a random account address here. Empty string is valid too. |
||
CodeID: contractInfo.CodeID, | ||
Label: simtypes.RandStringOfLength(r, 10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
Msg: []byte(fmt.Sprintf(`{"%s": "%s"}`, simtypes.RandStringOfLength(r, 10), simtypes.RandStringOfLength(r, 10))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reflect contract expects an empty object on instantiation |
||
Funds: spendable, | ||
} | ||
|
||
txCtx := simulation.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, | ||
} | ||
|
||
return simulation.GenAndDeliverTxWithRandFees(txCtx) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 this is in
main
already. I think you PR was not rebased proper