-
Notifications
You must be signed in to change notification settings - Fork 396
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 simulation for gov #1064
Merged
Merged
Add simulation for gov #1064
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
436d2bc
add struct for sim gov
GNaD13 cee5615
add proposal for store contract
GNaD13 14a3395
change wasm file
GNaD13 4fd3365
add proposal instantiate contract
GNaD13 61bf669
change admin account
GNaD13 5f6dc63
add sim gov proposals Update Admin
GNaD13 42a72ce
add sim gov execute contract
GNaD13 b0948f4
fix some bug
GNaD13 98107f6
add sim gov clear admin
GNaD13 3139aa6
add comment
GNaD13 171f52a
fix lint
GNaD13 aab2a6d
Merge remote-tracking branch 'cosmwasm/main' into dang/add-sim-gov-prop
GNaD13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
package simulation | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/CosmWasm/wasmd/app/params" | ||
"github.com/CosmWasm/wasmd/x/wasm/keeper/testdata" | ||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
"github.com/cosmos/cosmos-sdk/x/simulation" | ||
) | ||
|
||
const ( | ||
WeightStoreCodeProposal = "weight_store_code_proposal" | ||
WeightInstantiateContractProposal = "weight_instantiate_contract_proposal" | ||
WeightUpdateAdminProposal = "weight_update_admin_proposal" | ||
WeightExeContractProposal = "weight_execute_contract_proposal" | ||
WeightClearAdminProposal = "weight_clear_admin_proposal" | ||
) | ||
|
||
func ProposalContents(bk BankKeeper, wasmKeeper WasmKeeper) []simtypes.WeightedProposalContent { | ||
return []simtypes.WeightedProposalContent{ | ||
// simulation.NewWeightedProposalContent( | ||
// WeightStoreCodeProposal, | ||
// params.DefaultWeightStoreCodeProposal, | ||
// SimulateStoreCodeProposal(wasmKeeper), | ||
// ), | ||
simulation.NewWeightedProposalContent( | ||
WeightInstantiateContractProposal, | ||
params.DefaultWeightInstantiateContractProposal, | ||
SimulateInstantiateContractProposal( | ||
bk, | ||
wasmKeeper, | ||
DefaultSimulationCodeIDSelector, | ||
), | ||
), | ||
simulation.NewWeightedProposalContent( | ||
WeightUpdateAdminProposal, | ||
params.DefaultWeightUpdateAdminProposal, | ||
SimulateUpdateAdminProposal( | ||
wasmKeeper, | ||
DefaultSimulateUpdateAdminProposalContractSelector, | ||
), | ||
), | ||
simulation.NewWeightedProposalContent( | ||
WeightExeContractProposal, | ||
params.DefaultWeightExecuteContractProposal, | ||
SimulateExecuteContractProposal( | ||
bk, | ||
wasmKeeper, | ||
DefaultSimulationExecuteContractSelector, | ||
DefaultSimulationExecuteSenderSelector, | ||
DefaultSimulationExecutePayloader, | ||
), | ||
), | ||
simulation.NewWeightedProposalContent( | ||
WeightClearAdminProposal, | ||
params.DefaultWeightClearAdminProposal, | ||
SimulateClearAdminProposal( | ||
wasmKeeper, | ||
DefaultSimulateClearAdminProposalContractSelector, | ||
), | ||
), | ||
} | ||
} | ||
|
||
// simulate store code proposal (unused now) | ||
// Current problem: out of gas (defaul gaswanted config of gov SimulateMsgSubmitProposal is 10_000_000) | ||
// but this proposal may need more than it | ||
func SimulateStoreCodeProposal(wasmKeeper WasmKeeper) simtypes.ContentSimulatorFn { | ||
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
|
||
wasmBz := testdata.ReflectContractWasm() | ||
|
||
permission := wasmKeeper.GetParams(ctx).InstantiateDefaultPermission.With(simAccount.Address) | ||
|
||
return types.NewStoreCodeProposal( | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandStringOfLength(r, 10), | ||
simAccount.Address.String(), | ||
wasmBz, | ||
&permission, | ||
false, | ||
) | ||
} | ||
} | ||
|
||
// Simulate instantiate contract proposal | ||
func SimulateInstantiateContractProposal(bk BankKeeper, wasmKeeper WasmKeeper, codeSelector CodeIDSelector) simtypes.ContentSimulatorFn { | ||
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
// admin | ||
adminAccount, _ := simtypes.RandomAcc(r, accs) | ||
// get codeID | ||
codeID := codeSelector(ctx, wasmKeeper) | ||
if codeID == 0 { | ||
return nil | ||
} | ||
|
||
return types.NewInstantiateContractProposal( | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandStringOfLength(r, 10), | ||
simAccount.Address.String(), | ||
adminAccount.Address.String(), | ||
codeID, | ||
simtypes.RandStringOfLength(r, 10), | ||
[]byte(`{}`), | ||
sdk.Coins{}, | ||
) | ||
} | ||
} | ||
|
||
// Simulate execute contract proposal | ||
func SimulateExecuteContractProposal( | ||
bk BankKeeper, | ||
wasmKeeper WasmKeeper, | ||
contractSelector MsgExecuteContractSelector, | ||
senderSelector MsgExecuteSenderSelector, | ||
payloader MsgExecutePayloader, | ||
) simtypes.ContentSimulatorFn { | ||
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { | ||
ctAddress := contractSelector(ctx, wasmKeeper) | ||
if ctAddress == nil { | ||
return nil | ||
} | ||
|
||
simAccount, err := senderSelector(wasmKeeper, ctx, ctAddress, accs) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
msg := types.MsgExecuteContract{ | ||
Sender: simAccount.Address.String(), | ||
Contract: ctAddress.String(), | ||
Funds: sdk.Coins{}, | ||
} | ||
|
||
if err := payloader(&msg); err != nil { | ||
return nil | ||
} | ||
|
||
return types.NewExecuteContractProposal( | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandStringOfLength(r, 10), | ||
simAccount.Address.String(), | ||
ctAddress.String(), | ||
msg.Msg, | ||
sdk.Coins{}, | ||
) | ||
} | ||
} | ||
|
||
type UpdateAdminContractSelector func(sdk.Context, WasmKeeper, string) (sdk.AccAddress, types.ContractInfo) | ||
|
||
func DefaultSimulateUpdateAdminProposalContractSelector( | ||
ctx sdk.Context, | ||
wasmKeeper WasmKeeper, | ||
adminAddress string, | ||
) (sdk.AccAddress, types.ContractInfo) { | ||
var contractAddr sdk.AccAddress | ||
var contractInfo types.ContractInfo | ||
wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool { | ||
if info.Admin != adminAddress { | ||
return false | ||
} | ||
contractAddr = address | ||
contractInfo = info | ||
return true | ||
}) | ||
return contractAddr, contractInfo | ||
} | ||
|
||
// Simulate update admin contract proposal | ||
func SimulateUpdateAdminProposal(wasmKeeper WasmKeeper, contractSelector UpdateAdminContractSelector) simtypes.ContentSimulatorFn { | ||
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
ctAddress, _ := contractSelector(ctx, wasmKeeper, simAccount.Address.String()) | ||
if ctAddress == nil { | ||
return nil | ||
} | ||
|
||
return types.NewUpdateAdminProposal( | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandomAccounts(r, 1)[0].Address.String(), | ||
ctAddress.String(), | ||
) | ||
} | ||
} | ||
|
||
type ClearAdminContractSelector func(sdk.Context, WasmKeeper) sdk.AccAddress | ||
|
||
func DefaultSimulateClearAdminProposalContractSelector( | ||
ctx sdk.Context, | ||
wasmKeeper WasmKeeper, | ||
) sdk.AccAddress { | ||
var contractAddr sdk.AccAddress | ||
wasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info types.ContractInfo) bool { | ||
contractAddr = address | ||
return true | ||
}) | ||
return contractAddr | ||
} | ||
|
||
// Simulate clear admin proposal | ||
func SimulateClearAdminProposal(wasmKeeper WasmKeeper, contractSelector ClearAdminContractSelector) simtypes.ContentSimulatorFn { | ||
return func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) simtypes.Content { | ||
ctAddress := contractSelector(ctx, wasmKeeper) | ||
if ctAddress == nil { | ||
return nil | ||
} | ||
|
||
return types.NewClearAdminProposal( | ||
simtypes.RandStringOfLength(r, 10), | ||
simtypes.RandStringOfLength(r, 10), | ||
ctAddress.String(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you have a proposal for a better gas value? We can open an issue/PR on the SDK to get this addressed
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.
I think we have 2 solution to solve this problem:
But the 2 solution quite difficult. Because this may change all the current sims test. One more thing, in the future, we need to implement sudo entry point for Sudo proposal (it will increase the size of .wasm contract)
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.
Store code and sudo gov operations are well covered by integration tests already. Simulations are just for regression and fuzzing. They don't add super much value here IMHO. Let's have a follow up task to revisit the store code when we are on SDK v0.47. There is some work on the gov module.