cosmos-sdk/x/group/simulation/genesis.go
Amaury da36c46f3a
refactor(group): Distinguish Voting period and Execution period for group policies (#11198)
## Description

Closes: #11092

## TODOs

I'm thinking to do the 2 todos in a separate PR, or else this PR is too big. WDYT?

- [ ] #11246 This involves adding a new index ProposalsByVotingPeriodEnd, so might be better to do in another PR
- [ ] #11245  Also should be done in a separate PR (as it needs the above index)

### Main change 1: Group policy proto defs have `voting_period` and `min_execution_period`

For group policies:

```diff
- // Within this times votes and exec messages can be submitted.
- // timeout is the duration from submission of a proposal to the end of voting period
- google.protobuf.Duration timeout = 2 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];

+  // voting_period is the duration from submission of a proposal to the end of voting period
+  // Within this times votes can be submitted with MsgVote.
+  google.protobuf.Duration voting_period = 2 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];

+  // min_execution_period is the minimum duration after the proposal submission
+  // where members can start sending MsgExec. This means that the window for
+  // sending a MsgExec transaction is:
+  // `[ submission + min_execution_period ; submission + voting_period + max_execution_period]`
+  // where max_execution_period is a app-specific config, defined in the keeper.
+  // If not set, min_execution_period will default to 0.
+  google.protobuf.Duration min_execution_period = 3 [(gogoproto.stdduration) = true, (gogoproto.nullable) = false];
```

### Main Change 2: We don't update proposal's FinalTallyResult result on MsgVote/MsgSubmitProposal

Unless the msg has TryExec set to true, in which case the FinalTallyResult is updated ONLY if the tally is final.

### Main Change 3: Add a keeper-level `MaxExecutionPeriod`

MsgExecs will be rejected if they are sent after `voting_period_end + MaxExecutionPeriod`



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-03-02 12:00:59 +00:00

200 lines
5.5 KiB
Go

package simulation
import (
"math/rand"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/group"
)
const (
GroupInfo = "group-info"
GroupMembers = "group-members"
GroupPolicyInfo = "group-policy-info"
GroupProposals = "group-proposals"
GroupVote = "group-vote"
)
func getGroups(r *rand.Rand, accounts []simtypes.Account) []*group.GroupInfo {
groups := make([]*group.GroupInfo, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, accounts)
groups[i] = &group.GroupInfo{
Id: uint64(i + 1),
Admin: acc.Address.String(),
Metadata: simtypes.RandStringOfLength(r, 10),
Version: 1,
TotalWeight: "10",
}
}
return groups
}
func getGroupMembers(r *rand.Rand, accounts []simtypes.Account) []*group.GroupMember {
groupMembers := make([]*group.GroupMember, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, accounts)
groupMembers[i] = &group.GroupMember{
GroupId: uint64(i + 1),
Member: &group.Member{
Address: acc.Address.String(),
Weight: "10",
Metadata: simtypes.RandStringOfLength(r, 10),
},
}
}
return groupMembers
}
func getGroupPolicies(r *rand.Rand, simState *module.SimulationState) []*group.GroupPolicyInfo {
groupPolicies := make([]*group.GroupPolicyInfo, 3)
for i := 0; i < 3; i++ {
acc, _ := simtypes.RandomAcc(r, simState.Accounts)
any, err := codectypes.NewAnyWithValue(group.NewThresholdDecisionPolicy("10", time.Second, 0))
if err != nil {
panic(err)
}
groupPolicies[i] = &group.GroupPolicyInfo{
GroupId: uint64(i + 1),
Admin: acc.Address.String(),
Address: acc.Address.String(),
Version: 1,
DecisionPolicy: any,
Metadata: simtypes.RandStringOfLength(r, 10),
}
}
return groupPolicies
}
func getProposals(r *rand.Rand, simState *module.SimulationState) []*group.Proposal {
proposals := make([]*group.Proposal, 3)
proposers := []string{simState.Accounts[0].Address.String(), simState.Accounts[1].Address.String()}
for i := 0; i < 3; i++ {
from, _ := simtypes.RandomAcc(r, simState.Accounts)
to, _ := simtypes.RandomAcc(r, simState.Accounts)
fromAddr := from.Address.String()
submittedAt := time.Unix(0, 0)
timeout := submittedAt.Add(time.Second * 1000).UTC()
proposal := &group.Proposal{
Id: uint64(i + 1),
Proposers: proposers,
Address: fromAddr,
GroupVersion: uint64(i + 1),
GroupPolicyVersion: uint64(i + 1),
Status: group.PROPOSAL_STATUS_SUBMITTED,
Result: group.PROPOSAL_RESULT_ACCEPTED,
FinalTallyResult: group.TallyResult{
YesCount: "1",
NoCount: "1",
AbstainCount: "1",
NoWithVetoCount: "0",
},
ExecutorResult: group.PROPOSAL_EXECUTOR_RESULT_NOT_RUN,
Metadata: simtypes.RandStringOfLength(r, 50),
SubmitTime: submittedAt,
VotingPeriodEnd: timeout,
}
err := proposal.SetMsgs([]sdk.Msg{&banktypes.MsgSend{
FromAddress: fromAddr,
ToAddress: to.Address.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)),
}})
if err != nil {
panic(err)
}
proposals[i] = proposal
}
return proposals
}
func getVotes(r *rand.Rand, simState *module.SimulationState) []*group.Vote {
votes := make([]*group.Vote, 3)
for i := 0; i < 3; i++ {
votes[i] = &group.Vote{
ProposalId: uint64(i + 1),
Voter: simState.Accounts[i].Address.String(),
Option: getVoteOption(i),
Metadata: simtypes.RandStringOfLength(r, 50),
SubmitTime: time.Unix(0, 0),
}
}
return votes
}
func getVoteOption(index int) group.VoteOption {
switch index {
case 0:
return group.VOTE_OPTION_YES
case 1:
return group.VOTE_OPTION_NO
case 2:
return group.VOTE_OPTION_ABSTAIN
default:
return group.VOTE_OPTION_NO_WITH_VETO
}
}
// RandomizedGenState generates a random GenesisState for the group module.
func RandomizedGenState(simState *module.SimulationState) {
// groups
var groups []*group.GroupInfo
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupInfo, &groups, simState.Rand,
func(r *rand.Rand) { groups = getGroups(r, simState.Accounts) },
)
// group members
var members []*group.GroupMember
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupMembers, &members, simState.Rand,
func(r *rand.Rand) { members = getGroupMembers(r, simState.Accounts) },
)
// group accounts
var groupPolicies []*group.GroupPolicyInfo
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupPolicyInfo, &groupPolicies, simState.Rand,
func(r *rand.Rand) { groupPolicies = getGroupPolicies(r, simState) },
)
// proposals
var proposals []*group.Proposal
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupProposals, &proposals, simState.Rand,
func(r *rand.Rand) { proposals = getProposals(r, simState) },
)
// votes
var votes []*group.Vote
simState.AppParams.GetOrGenerate(
simState.Cdc, GroupVote, &votes, simState.Rand,
func(r *rand.Rand) { votes = getVotes(r, simState) },
)
groupGenesis := group.GenesisState{
GroupSeq: 3,
Groups: groups,
GroupMembers: members,
GroupPolicySeq: 3,
GroupPolicies: groupPolicies,
ProposalSeq: 3,
Proposals: proposals,
Votes: votes,
}
simState.GenState[group.ModuleName] = simState.Cdc.MustMarshalJSON(&groupGenesis)
}