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"
|
|
|
|
)
|
|
|
|
|
2022-03-02 04:00:59 -08:00
|
|
|
func TestThresholdDecisionPolicyValidate(t *testing.T) {
|
|
|
|
g := group.GroupInfo{
|
|
|
|
TotalWeight: "10",
|
|
|
|
}
|
|
|
|
config := group.DefaultConfig()
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
policy group.ThresholdDecisionPolicy
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"min exec period too big",
|
|
|
|
group.ThresholdDecisionPolicy{
|
|
|
|
Threshold: "5",
|
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second,
|
|
|
|
MinExecutionPeriod: time.Hour * 24 * 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"all good",
|
|
|
|
group.ThresholdDecisionPolicy{
|
|
|
|
Threshold: "5",
|
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Hour,
|
|
|
|
MinExecutionPeriod: time.Hour * 24,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.policy.Validate(g, config)
|
|
|
|
if tc.expErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPercentageDecisionPolicyValidate(t *testing.T) {
|
|
|
|
g := group.GroupInfo{}
|
|
|
|
config := group.DefaultConfig()
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
policy group.PercentageDecisionPolicy
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"min exec period too big",
|
|
|
|
group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second,
|
|
|
|
MinExecutionPeriod: time.Hour * 24 * 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"all good",
|
|
|
|
group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Hour,
|
|
|
|
MinExecutionPeriod: time.Hour * 24,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := tc.policy.Validate(g, config)
|
|
|
|
if tc.expErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 01:12:57 -08:00
|
|
|
func TestPercentageDecisionPolicyAllow(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
policy *group.PercentageDecisionPolicy
|
2022-02-14 05:48:17 -08:00
|
|
|
tally *group.TallyResult
|
2022-02-11 01:12:57 -08:00
|
|
|
totalPower string
|
|
|
|
votingDuration time.Duration
|
|
|
|
result group.DecisionPolicyResult
|
2022-03-02 04:00:59 -08:00
|
|
|
expErr bool
|
2022-02-11 01:12:57 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"YesCount percentage > decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "2",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: true,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"YesCount percentage == decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "2",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"4",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: true,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"YesCount percentage < decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: false,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sum percentage (YesCount + undecided votes percentage) < decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "2",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sum percentage = decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "2",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"4",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: false,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"sum percentage > decision policy percentage",
|
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: false,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
2022-03-02 04:00:59 -08:00
|
|
|
"time since submission < min execution period",
|
2022-02-11 01:12:57 -08:00
|
|
|
&group.PercentageDecisionPolicy{
|
|
|
|
Percentage: "0.5",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 10,
|
|
|
|
MinExecutionPeriod: time.Minute,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "2",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
2022-03-02 04:00:59 -08:00
|
|
|
group.DecisionPolicyResult{},
|
|
|
|
true,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
policyResult, err := tc.policy.Allow(*tc.tally, tc.totalPower, tc.votingDuration)
|
2022-03-02 04:00:59 -08:00
|
|
|
if tc.expErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.result, policyResult)
|
|
|
|
}
|
2022-02-11 01:12:57 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestThresholdDecisionPolicyAllow(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
policy *group.ThresholdDecisionPolicy
|
2022-02-14 05:48:17 -08:00
|
|
|
tally *group.TallyResult
|
2022-02-11 01:12:57 -08:00
|
|
|
totalPower string
|
|
|
|
votingDuration time.Duration
|
|
|
|
result group.DecisionPolicyResult
|
2022-03-02 04:00:59 -08:00
|
|
|
expErr bool
|
2022-02-11 01:12:57 -08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"YesCount >= threshold decision policy",
|
|
|
|
&group.ThresholdDecisionPolicy{
|
2022-03-08 08:32:54 -08:00
|
|
|
Threshold: "2",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
2022-03-08 08:32:54 -08:00
|
|
|
YesCount: "2",
|
2022-02-14 05:48:17 -08:00
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: true,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"YesCount < threshold decision policy",
|
|
|
|
&group.ThresholdDecisionPolicy{
|
2022-03-08 08:32:54 -08:00
|
|
|
Threshold: "2",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "1",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: false,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
2022-03-08 08:32:54 -08:00
|
|
|
"YesCount == group total weight < threshold",
|
2022-02-11 01:12:57 -08:00
|
|
|
&group.ThresholdDecisionPolicy{
|
2022-03-08 08:32:54 -08:00
|
|
|
Threshold: "20",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
2022-03-08 08:32:54 -08:00
|
|
|
YesCount: "3",
|
2022-02-14 05:48:17 -08:00
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-03-08 08:32:54 -08:00
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: true,
|
|
|
|
Final: true,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"maxYesCount < threshold decision policy",
|
|
|
|
&group.ThresholdDecisionPolicy{
|
|
|
|
Threshold: "2",
|
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "0",
|
|
|
|
NoCount: "1",
|
|
|
|
AbstainCount: "1",
|
|
|
|
NoWithVetoCount: "0",
|
|
|
|
},
|
|
|
|
"3",
|
2022-02-11 01:12:57 -08:00
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
2022-03-08 08:32:54 -08:00
|
|
|
"maxYesCount >= threshold decision policy",
|
2022-02-11 01:12:57 -08:00
|
|
|
&group.ThresholdDecisionPolicy{
|
2022-03-08 08:32:54 -08:00
|
|
|
Threshold: "2",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 100,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
2022-03-08 08:32:54 -08:00
|
|
|
YesCount: "0",
|
|
|
|
NoCount: "1",
|
2022-02-14 05:48:17 -08:00
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: false,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
false,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
{
|
2022-03-02 04:00:59 -08:00
|
|
|
"time since submission < min execution period",
|
2022-02-11 01:12:57 -08:00
|
|
|
&group.ThresholdDecisionPolicy{
|
|
|
|
Threshold: "3",
|
2022-03-02 04:00:59 -08:00
|
|
|
Windows: &group.DecisionPolicyWindows{
|
|
|
|
VotingPeriod: time.Second * 10,
|
|
|
|
MinExecutionPeriod: time.Minute,
|
|
|
|
},
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
2022-02-14 05:48:17 -08:00
|
|
|
&group.TallyResult{
|
|
|
|
YesCount: "3",
|
|
|
|
NoCount: "0",
|
|
|
|
AbstainCount: "0",
|
|
|
|
NoWithVetoCount: "0",
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
"3",
|
|
|
|
time.Duration(time.Second * 50),
|
|
|
|
group.DecisionPolicyResult{
|
|
|
|
Allow: false,
|
|
|
|
Final: true,
|
|
|
|
},
|
2022-03-02 04:00:59 -08:00
|
|
|
true,
|
2022-02-11 01:12:57 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
policyResult, err := tc.policy.Allow(*tc.tally, tc.totalPower, tc.votingDuration)
|
2022-03-02 04:00:59 -08:00
|
|
|
if tc.expErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.result, policyResult)
|
|
|
|
}
|
2022-02-11 01:12:57 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|