Skip to content
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

refactor: remove MinThreshold and MinPercentage from x/foundation config #686

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/collection) [\#639](https://github.com/line/lbm-sdk/pull/639) rename x/collection events
* (x/wasm) [\#661](https://github.com/line/lbm-sdk/pull/661) x/wasm refactoring - detaching the custom wasm proto part of lbm-sdk. (apply changes of [\#625](https://github.com/line/lbm-sdk/pull/625) and [\#655](https://github.com/line/lbm-sdk/pull/655))
* (refactor) [\#685](https://github.com/line/lbm-sdk/pull/685) remove x/foundation UpdateValidatorAuthsProposal
* (refactor) [\#686](https://github.com/line/lbm-sdk/pull/686) remove `Minthreshold` and `MinPercentage` from x/foundation config
0Tech marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes
* (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path
Expand Down
6 changes: 0 additions & 6 deletions x/foundation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package foundation

import (
"time"

sdk "github.com/line/lbm-sdk/types"
)

// Config is a config struct used for intialising the group module to avoid using globals.
Expand All @@ -12,15 +10,11 @@ type Config struct {
MaxExecutionPeriod time.Duration
// MaxMetadataLen defines the max length of the metadata bytes field for various entities within the foundation module. Defaults to 255 if not explicitly set.
MaxMetadataLen uint64
MinThreshold sdk.Dec
MinPercentage sdk.Dec
}

func DefaultConfig() Config {
return Config{
MaxExecutionPeriod: 2 * 7 * 24 * time.Hour, // two weeks
MaxMetadataLen: 255,
MinThreshold: sdk.NewDec(3),
MinPercentage: sdk.MustNewDecFromStr("0.8"),
}
}
14 changes: 3 additions & 11 deletions x/foundation/foundation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
sdkerrors "github.com/line/lbm-sdk/types/errors"
)

func DefaultDecisionPolicy(config Config) DecisionPolicy {
func DefaultDecisionPolicy() DecisionPolicy {
return &ThresholdDecisionPolicy{
Threshold: config.MinThreshold,
Threshold: sdk.OneDec(),
Windows: &DecisionPolicyWindows{
VotingPeriod: 24 * time.Hour,
},
Expand Down Expand Up @@ -275,7 +275,7 @@ func (p ThresholdDecisionPolicy) GetVotingPeriod() time.Duration {
}

func (p ThresholdDecisionPolicy) ValidateBasic() error {
if !p.Threshold.IsPositive() {
if p.Threshold.IsNil() || !p.Threshold.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrap("threshold must be a positive number")
}

Expand All @@ -286,10 +286,6 @@ func (p ThresholdDecisionPolicy) ValidateBasic() error {
}

func (p ThresholdDecisionPolicy) Validate(config Config) error {
if p.Threshold.LT(config.MinThreshold) {
return sdkerrors.ErrInvalidRequest.Wrap("threshold must be greater than or equal to min_threshold")
}
0Tech marked this conversation as resolved.
Show resolved Hide resolved

if err := validateDecisionPolicyWindows(*p.Windows, config); err != nil {
return err
}
Expand Down Expand Up @@ -343,10 +339,6 @@ func (p PercentageDecisionPolicy) ValidateBasic() error {
}

func (p PercentageDecisionPolicy) Validate(config Config) error {
if p.Percentage.LT(config.MinPercentage) {
return sdkerrors.ErrInvalidRequest.Wrap("percentage must be greater than or equal to min_percentage")
}

if err := validateDecisionPolicyWindows(*p.Windows, config); err != nil {
return err
}
Expand Down
42 changes: 19 additions & 23 deletions x/foundation/foundation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestDecisionPolicy(t *testing.T) {
config := foundation.DefaultConfig()
policy := foundation.DefaultDecisionPolicy(config)
policy := foundation.DefaultDecisionPolicy()

require.NoError(t, policy.ValidateBasic())
require.NoError(t, policy.Validate(config))
Expand Down Expand Up @@ -53,24 +53,22 @@ func TestThresholdDecisionPolicy(t *testing.T) {
valid bool
}{
"valid policy": {
threshold: config.MinThreshold,
threshold: sdk.OneDec(),
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond,
validBasic: true,
valid: true,
},
"invalid policy (basic)": {
threshold: config.MinThreshold,
minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond,
},
"invalid policy": {
threshold: config.MinThreshold.Sub(sdk.SmallestDec()),
"invalid threshold": {
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond,
validBasic: true,
},
"invalid policy (windows)": {
threshold: config.MinThreshold,
"invalid voting period": {
threshold: sdk.OneDec(),
minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond,
},
"invalid min execution period": {
threshold: sdk.OneDec(),
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour,
validBasic: true,
Expand Down Expand Up @@ -133,8 +131,8 @@ func TestThresholdDecisionPolicyAllow(t *testing.T) {
},
"allow (member size < threshold)": {
sinceSubmission: policy.Windows.MinExecutionPeriod,
totalWeight: config.MinThreshold,
tally: foundation.NewTallyResult(config.MinThreshold, sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
totalWeight: sdk.OneDec(),
tally: foundation.NewTallyResult(sdk.OneDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
valid: true,
final: true,
allow: true,
Expand Down Expand Up @@ -185,24 +183,22 @@ func TestPercentageDecisionPolicy(t *testing.T) {
valid bool
}{
"valid policy": {
percentage: config.MinPercentage,
percentage: sdk.OneDec(),
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond,
validBasic: true,
valid: true,
},
"invalid policy (basic)": {
percentage: config.MinPercentage,
minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond,
},
"invalid policy": {
percentage: config.MinPercentage.Sub(sdk.SmallestDec()),
"invalid percentage": {
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour - time.Nanosecond,
validBasic: true,
},
"invalid policy (windows)": {
percentage: config.MinPercentage,
"invalid voting period": {
percentage: sdk.OneDec(),
minExecutionPeriod: config.MaxExecutionPeriod - time.Nanosecond,
},
"invalid min execution period": {
percentage: sdk.OneDec(),
votingPeriod: time.Hour,
minExecutionPeriod: config.MaxExecutionPeriod + time.Hour,
validBasic: true,
Expand Down
2 changes: 1 addition & 1 deletion x/foundation/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, sk foundation.StakingKeeper, data *
if info.GetDecisionPolicy() == nil ||
info.GetDecisionPolicy().ValidateBasic() != nil ||
info.GetDecisionPolicy().Validate(k.config) != nil {
policy := foundation.DefaultDecisionPolicy(k.config)
policy := foundation.DefaultDecisionPolicy()
if err := info.SetDecisionPolicy(policy); err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions x/foundation/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.ZeroDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
},
},
"enabled with no create validator grantees": {
Expand All @@ -42,7 +42,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.ZeroDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
},
},
"members": {
Expand All @@ -61,7 +61,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.OneDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
Members: []foundation.Member{
{
Address: s.members[0].String(),
Expand Down Expand Up @@ -98,7 +98,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.ZeroDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
Proposals: []foundation.Proposal{
*foundation.Proposal{
Id: 1,
Expand Down Expand Up @@ -140,7 +140,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.ZeroDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
Authorizations: []foundation.GrantAuthorization{
*foundation.GrantAuthorization{
Grantee: s.stranger.String(),
Expand All @@ -165,7 +165,7 @@ func (s *KeeperTestSuite) TestImportExportGenesis() {
Operator: s.keeper.GetAdmin(s.ctx).String(),
Version: 1,
TotalWeight: sdk.OneDec(),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy(foundation.DefaultConfig())),
}.WithDecisionPolicy(foundation.DefaultDecisionPolicy()),
Authorizations: []foundation.GrantAuthorization{
*foundation.GrantAuthorization{
Grantee: s.stranger.String(),
Expand Down
2 changes: 1 addition & 1 deletion x/foundation/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *KeeperTestSuite) SetupTest() {
}

s.operator = s.keeper.GetOperator(s.ctx)
s.members = make([]sdk.AccAddress, foundation.DefaultConfig().MinThreshold.TruncateInt64())
s.members = make([]sdk.AccAddress, 10)
for i := range s.members {
s.members[i] = createAddress()
}
Expand Down
40 changes: 3 additions & 37 deletions x/foundation/keeper/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,18 @@ func (s *KeeperTestSuite) TestUpdateDecisionPolicy() {
policy foundation.DecisionPolicy
valid bool
}{
"threshold policy (valid)": {
"valid policy": {
policy: &foundation.ThresholdDecisionPolicy{
Threshold: config.MinThreshold,
Threshold: sdk.OneDec(),
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
},
},
valid: true,
},
"threshold policy (low threshold)": {
"invalid policy (invalid min execution period)": {
policy: &foundation.ThresholdDecisionPolicy{
Threshold: sdk.OneDec(),
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
},
},
},
"threshold policy (invalid min execution period)": {
policy: &foundation.ThresholdDecisionPolicy{
Threshold: config.MinThreshold,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
MinExecutionPeriod: time.Hour + config.MaxExecutionPeriod,
},
},
},
"percentage policy (valid)": {
policy: &foundation.PercentageDecisionPolicy{
Percentage: config.MinPercentage,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
},
},
valid: true,
},
"percentage policy (low percentage)": {
policy: &foundation.PercentageDecisionPolicy{
Percentage: sdk.MustNewDecFromStr("0.1"),
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
},
},
},
"percentage policy (invalid min execution period)": {
policy: &foundation.PercentageDecisionPolicy{
Percentage: config.MinPercentage,
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
MinExecutionPeriod: time.Hour + config.MaxExecutionPeriod,
Expand Down
13 changes: 9 additions & 4 deletions x/foundation/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
"time"

sdk "github.com/line/lbm-sdk/types"
"github.com/line/lbm-sdk/x/foundation"
"github.com/line/lbm-sdk/x/stakingplus"
Expand Down Expand Up @@ -98,23 +100,26 @@ func (s *KeeperTestSuite) TestMsgUpdateDecisionPolicy() {
"valid request": {
operator: s.operator,
policy: &foundation.ThresholdDecisionPolicy{
Threshold: foundation.DefaultConfig().MinThreshold,
Threshold: sdk.OneDec(),
Windows: &foundation.DecisionPolicyWindows{},
},
valid: true,
},
"not authorized": {
operator: s.stranger,
policy: &foundation.ThresholdDecisionPolicy{
Threshold: foundation.DefaultConfig().MinThreshold,
Threshold: sdk.OneDec(),
Windows: &foundation.DecisionPolicyWindows{},
},
},
"low threshold": {
"invalid policy": {
operator: s.operator,
policy: &foundation.ThresholdDecisionPolicy{
Threshold: sdk.OneDec(),
Windows: &foundation.DecisionPolicyWindows{},
Windows: &foundation.DecisionPolicyWindows{
VotingPeriod: time.Hour,
MinExecutionPeriod: foundation.DefaultConfig().MaxExecutionPeriod + time.Hour,
},
},
},
}
Expand Down