-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Param Proposal Simulation Messages; Minting Params Fix #4244
Changes from all commits
f2d53c4
ebc1e01
d6ccc2d
8895415
7e41f38
43e5cd6
f424894
6287af6
d986617
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
#4235 Add parameter change proposal messages to simulation. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#4235 Update the minting module params to implement params.ParamSet so | ||
individual keys can be set via proposals instead of passing a struct. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,18 @@ import ( | |
"github.com/cosmos/cosmos-sdk/x/simulation" | ||
) | ||
|
||
// ContentSimulator defines a function type alias for generating random proposal | ||
// content. | ||
type ContentSimulator func(r *rand.Rand) gov.Content | ||
|
||
// SimulateSubmittingVotingAndSlashingForProposal simulates creating a msg Submit Proposal | ||
// voting on the proposal, and subsequently slashing the proposal. It is implemented using | ||
// future operations. | ||
// TODO: Vote more intelligently, so we can actually do some checks regarding votes passing or failing | ||
// TODO: Actually check that validator slashings happened | ||
func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Operation { | ||
func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper, contentSim ContentSimulator) simulation.Operation { | ||
handler := gov.NewHandler(k) | ||
|
||
// The states are: | ||
// column 1: All validators vote | ||
// column 2: 90% vote | ||
|
@@ -36,38 +41,49 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope | |
{0, 0, 20, 30, 30, 30}, | ||
{0, 0, 0, 10, 10, 25}, | ||
}) | ||
|
||
statePercentageArray := []float64{1, .9, .75, .4, .15, 0} | ||
curNumVotesState := 1 | ||
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( | ||
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { | ||
|
||
return func( | ||
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, | ||
) (opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { | ||
|
||
// 1) submit proposal now | ||
sender := simulation.RandomAcc(r, accs) | ||
msg, err := simulationCreateMsgSubmitProposal(r, sender) | ||
content := contentSim(r) | ||
msg, err := simulationCreateMsgSubmitProposal(r, content, sender) | ||
if err != nil { | ||
return simulation.NoOpMsg(), nil, err | ||
} | ||
|
||
ok := simulateHandleMsgSubmitProposal(msg, handler, ctx) | ||
opMsg = simulation.NewOperationMsg(msg, ok, "") | ||
opMsg = simulation.NewOperationMsg(msg, ok, content.ProposalType()) | ||
// don't schedule votes if proposal failed | ||
if !ok { | ||
return opMsg, nil, nil | ||
} | ||
|
||
proposalID := k.GetLastProposalID(ctx) | ||
|
||
// 2) Schedule operations for votes | ||
// 2.1) first pick a number of people to vote. | ||
curNumVotesState = numVotesTransitionMatrix.NextState(r, curNumVotesState) | ||
numVotes := int(math.Ceil(float64(len(accs)) * statePercentageArray[curNumVotesState])) | ||
|
||
// 2.2) select who votes and when | ||
whoVotes := r.Perm(len(accs)) | ||
|
||
// didntVote := whoVotes[numVotes:] | ||
whoVotes = whoVotes[:numVotes] | ||
votingPeriod := k.GetVotingParams(ctx).VotingPeriod | ||
|
||
fops := make([]simulation.FutureOperation, numVotes+1) | ||
for i := 0; i < numVotes; i++ { | ||
whenVote := ctx.BlockHeader().Time.Add(time.Duration(r.Int63n(int64(votingPeriod.Seconds()))) * time.Second) | ||
fops[i] = simulation.FutureOperation{BlockTime: whenVote, Op: operationSimulateMsgVote(k, accs[whoVotes[i]], proposalID)} | ||
} | ||
|
||
// 3) Make an operation to ensure slashes were done correctly. (Really should be a future invariant) | ||
// TODO: Find a way to check if a validator was slashed other than just checking their balance a block | ||
// before and after. | ||
|
@@ -76,24 +92,6 @@ func SimulateSubmittingVotingAndSlashingForProposal(k gov.Keeper) simulation.Ope | |
} | ||
} | ||
|
||
// SimulateMsgSubmitProposal simulates a msg Submit Proposal | ||
// Note: Currently doesn't ensure that the proposal txt is in JSON form | ||
func SimulateMsgSubmitProposal(k gov.Keeper) simulation.Operation { | ||
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. dead code |
||
handler := gov.NewHandler(k) | ||
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account) ( | ||
opMsg simulation.OperationMsg, fOps []simulation.FutureOperation, err error) { | ||
|
||
sender := simulation.RandomAcc(r, accs) | ||
msg, err := simulationCreateMsgSubmitProposal(r, sender) | ||
if err != nil { | ||
return simulation.NoOpMsg(), nil, err | ||
} | ||
ok := simulateHandleMsgSubmitProposal(msg, handler, ctx) | ||
opMsg = simulation.NewOperationMsg(msg, ok, "") | ||
return opMsg, nil, nil | ||
} | ||
} | ||
|
||
func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Handler, ctx sdk.Context) (ok bool) { | ||
ctx, write := ctx.CacheContext() | ||
ok = handler(ctx, msg).IsOK() | ||
|
@@ -103,16 +101,16 @@ func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Hand | |
return ok | ||
} | ||
|
||
func simulationCreateMsgSubmitProposal(r *rand.Rand, sender simulation.Account) (msg gov.MsgSubmitProposal, err error) { | ||
deposit := randomDeposit(r) | ||
msg = gov.NewMsgSubmitProposal( | ||
gov.NewTextProposal( | ||
simulation.RandStringOfLength(r, 5), | ||
simulation.RandStringOfLength(r, 5), | ||
), | ||
deposit, | ||
sender.Address, | ||
// SimulateTextProposalContent returns random text proposal content. | ||
func SimulateTextProposalContent(r *rand.Rand) gov.Content { | ||
return gov.NewTextProposal( | ||
simulation.RandStringOfLength(r, 140), | ||
simulation.RandStringOfLength(r, 5000), | ||
) | ||
} | ||
|
||
func simulationCreateMsgSubmitProposal(r *rand.Rand, c gov.Content, s simulation.Account) (msg gov.MsgSubmitProposal, err error) { | ||
msg = gov.NewMsgSubmitProposal(c, randomDeposit(r), s.Address) | ||
if msg.ValidateBasic() != nil { | ||
err = fmt.Errorf("expected msg to pass ValidateBasic: %s", msg.GetSignBytes()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ import ( | |
"github.com/cosmos/cosmos-sdk/x/params" | ||
) | ||
|
||
var minterKey = []byte{0x00} // the one key to use for the keeper store | ||
|
||
const ( | ||
// ModuleName is the name of the module | ||
ModuleName = "minting" | ||
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. this differs with the module name (?) 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. indeed. I didn't write this module -- it should be changed. Separate PR though. |
||
|
@@ -42,23 +44,6 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, | |
return keeper | ||
} | ||
|
||
//____________________________________________________________________ | ||
// Keys | ||
|
||
var ( | ||
minterKey = []byte{0x00} // the one key to use for the keeper store | ||
|
||
// params store for inflation params | ||
ParamStoreKeyParams = []byte("params") | ||
) | ||
|
||
// ParamTable for staking module | ||
func ParamKeyTable() params.KeyTable { | ||
return params.NewKeyTable( | ||
ParamStoreKeyParams, Params{}, | ||
) | ||
} | ||
|
||
//______________________________________________________________________ | ||
|
||
// get the minter | ||
|
@@ -81,14 +66,13 @@ func (k Keeper) SetMinter(ctx sdk.Context, minter Minter) { | |
|
||
//______________________________________________________________________ | ||
|
||
// get inflation params from the global param store | ||
func (k Keeper) GetParams(ctx sdk.Context) Params { | ||
var params Params | ||
k.paramSpace.Get(ctx, ParamStoreKeyParams, ¶ms) | ||
// GetParams returns the total set of slashing parameters. | ||
func (k Keeper) GetParams(ctx sdk.Context) (params Params) { | ||
k.paramSpace.GetParamSet(ctx, ¶ms) | ||
return params | ||
} | ||
|
||
// set inflation params from the global param store | ||
func (k Keeper) SetParams(ctx sdk.Context, params Params) { | ||
k.paramSpace.Set(ctx, ParamStoreKeyParams, ¶ms) | ||
k.paramSpace.SetParamSet(ctx, ¶ms) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,17 @@ import ( | |
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/params" | ||
) | ||
|
||
// Parameter store keys | ||
var ( | ||
KeyMintDenom = []byte("MintDenom") | ||
KeyInflationRateChange = []byte("InflationRateChange") | ||
KeyInflationMax = []byte("InflationMax") | ||
KeyInflationMin = []byte("InflationMin") | ||
KeyGoalBonded = []byte("GoalBonded") | ||
KeyBlocksPerYear = []byte("BlocksPerYear") | ||
) | ||
|
||
// mint parameters | ||
|
@@ -16,6 +27,11 @@ type Params struct { | |
BlocksPerYear uint64 `json:"blocks_per_year"` // expected blocks per year | ||
} | ||
|
||
// ParamTable for minting module. | ||
func ParamKeyTable() params.KeyTable { | ||
return params.NewKeyTable().RegisterParamSet(&Params{}) | ||
} | ||
|
||
func NewParams(mintDenom string, inflationRateChange, inflationMax, | ||
inflationMin, goalBonded sdk.Dec, blocksPerYear uint64) Params { | ||
|
||
|
@@ -70,3 +86,15 @@ func (p Params) String() string { | |
p.InflationMin, p.GoalBonded, p.BlocksPerYear, | ||
) | ||
} | ||
|
||
// Implements params.ParamSet | ||
func (p *Params) ParamSetPairs() params.ParamSetPairs { | ||
return params.ParamSetPairs{ | ||
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. it'd be cool to use 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. I don't think we need |
||
{KeyMintDenom, &p.MintDenom}, | ||
{KeyInflationRateChange, &p.InflationRateChange}, | ||
{KeyInflationMax, &p.InflationMax}, | ||
{KeyInflationMin, &p.InflationMin}, | ||
{KeyGoalBonded, &p.GoalBonded}, | ||
{KeyBlocksPerYear, &p.BlocksPerYear}, | ||
} | ||
} |
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.
ditto
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.
Unfortunately, slashing has no such function. Separate PR.