cosmos-sdk/x/group/types_test.go

277 lines
5.7 KiB
Go
Raw Normal View History

feat: Add percentage decision policy to x/group (#11072) ## Description Closes: #10946 Add a percentage decision policy to group module. --- ### 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-02-11 01:12:57 -08:00
package group_test
import (
"testing"
"time"
"github.com/cosmos/cosmos-sdk/x/group"
"github.com/stretchr/testify/require"
)
func TestPercentageDecisionPolicyAllow(t *testing.T) {
testCases := []struct {
name string
policy *group.PercentageDecisionPolicy
tally *group.Tally
totalPower string
votingDuration time.Duration
result group.DecisionPolicyResult
}{
{
"YesCount percentage > decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "2",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: true,
Final: true,
},
},
{
"YesCount percentage == decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "2",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"4",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: true,
Final: true,
},
},
{
"YesCount percentage < decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: false,
},
},
{
"sum percentage (YesCount + undecided votes percentage) < decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "2",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: true,
},
},
{
"sum percentage = decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "2",
AbstainCount: "0",
VetoCount: "0",
},
"4",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: false,
},
},
{
"sum percentage > decision policy percentage",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: false,
},
},
{
"decision policy timeout <= voting duration",
&group.PercentageDecisionPolicy{
Percentage: "0.5",
Timeout: time.Second * 10,
},
&group.Tally{
YesCount: "2",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: true,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
policyResult, err := tc.policy.Allow(*tc.tally, tc.totalPower, tc.votingDuration)
require.NoError(t, err)
require.Equal(t, tc.result, policyResult)
})
}
}
func TestThresholdDecisionPolicyAllow(t *testing.T) {
testCases := []struct {
name string
policy *group.ThresholdDecisionPolicy
tally *group.Tally
totalPower string
votingDuration time.Duration
result group.DecisionPolicyResult
}{
{
"YesCount >= threshold decision policy",
&group.ThresholdDecisionPolicy{
Threshold: "3",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "3",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: true,
Final: true,
},
},
{
"YesCount < threshold decision policy",
&group.ThresholdDecisionPolicy{
Threshold: "3",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: false,
},
},
{
"sum votes < threshold decision policy",
&group.ThresholdDecisionPolicy{
Threshold: "3",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"2",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: true,
},
},
{
"sum votes >= threshold decision policy",
&group.ThresholdDecisionPolicy{
Threshold: "3",
Timeout: time.Second * 100,
},
&group.Tally{
YesCount: "1",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: false,
},
},
{
"decision policy timeout <= voting duration",
&group.ThresholdDecisionPolicy{
Threshold: "3",
Timeout: time.Second * 10,
},
&group.Tally{
YesCount: "3",
NoCount: "0",
AbstainCount: "0",
VetoCount: "0",
},
"3",
time.Duration(time.Second * 50),
group.DecisionPolicyResult{
Allow: false,
Final: true,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
policyResult, err := tc.policy.Allow(*tc.tally, tc.totalPower, tc.votingDuration)
require.NoError(t, err)
require.Equal(t, tc.result, policyResult)
})
}
}