feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
package group
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
2021-12-10 03:02:11 -08:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/group/errors"
"github.com/cosmos/cosmos-sdk/x/group/internal/math"
"github.com/cosmos/cosmos-sdk/x/group/internal/orm"
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
)
2022-04-06 04:16:23 -07:00
// DecisionPolicyResult is the result of whether a proposal passes or not a
// decision policy.
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
type DecisionPolicyResult struct {
2022-04-06 04:16:23 -07:00
// Allow determines if the proposal is allowed to pass.
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
Allow bool
2022-04-06 04:16:23 -07:00
// Final determines if the tally result is final or not. If final, then
// votes are pruned, and the tally result is saved in the proposal's
// `FinalTallyResult` field.
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
Final bool
}
// DecisionPolicy is the persistent set of rules to determine the result of election on a proposal.
type DecisionPolicy interface {
codec . ProtoMarshaler
2022-03-02 04:00:59 -08:00
// GetVotingPeriod returns the duration after proposal submission where
// votes are accepted.
GetVotingPeriod ( ) time . Duration
// Allow defines policy-specific logic to allow a proposal to pass or not,
// based on its tally result, the group's total power and the time since
// the proposal was submitted.
Allow ( tallyResult TallyResult , totalPower string , sinceSubmission time . Duration ) ( DecisionPolicyResult , error )
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
ValidateBasic ( ) error
2022-03-02 04:00:59 -08:00
Validate ( g GroupInfo , config Config ) error
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
2021-12-10 03:02:11 -08:00
// Implements DecisionPolicy Interface
var _ DecisionPolicy = & ThresholdDecisionPolicy { }
// NewThresholdDecisionPolicy creates a threshold DecisionPolicy
2022-03-02 04:00:59 -08:00
func NewThresholdDecisionPolicy ( threshold string , votingPeriod time . Duration , minExecutionPeriod time . Duration ) DecisionPolicy {
return & ThresholdDecisionPolicy { threshold , & DecisionPolicyWindows { votingPeriod , minExecutionPeriod } }
}
func ( p ThresholdDecisionPolicy ) GetVotingPeriod ( ) time . Duration {
return p . Windows . VotingPeriod
2021-12-10 03:02:11 -08:00
}
func ( p ThresholdDecisionPolicy ) ValidateBasic ( ) error {
if _ , err := math . NewPositiveDecFromString ( p . Threshold ) ; err != nil {
return sdkerrors . Wrap ( err , "threshold" )
}
2022-03-02 04:00:59 -08:00
if p . Windows == nil || p . Windows . VotingPeriod == 0 {
return sdkerrors . Wrap ( errors . ErrInvalid , "voting period cannot be zero" )
2021-12-10 03:02:11 -08:00
}
2022-03-02 04:00:59 -08:00
2021-12-10 03:02:11 -08:00
return nil
}
// Allow allows a proposal to pass when the tally of yes votes equals or exceeds the threshold before the timeout.
2022-03-02 04:00:59 -08:00
func ( p ThresholdDecisionPolicy ) Allow ( tallyResult TallyResult , totalPower string , sinceSubmission time . Duration ) ( DecisionPolicyResult , error ) {
if sinceSubmission < p . Windows . MinExecutionPeriod {
return DecisionPolicyResult { } , errors . ErrUnauthorized . Wrapf ( "must wait %s after submission before execution, currently at %s" , p . Windows . MinExecutionPeriod , sinceSubmission )
2021-12-10 03:02:11 -08:00
}
threshold , err := math . NewPositiveDecFromString ( p . Threshold )
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "threshold" )
2021-12-10 03:02:11 -08:00
}
2022-02-14 05:48:17 -08:00
yesCount , err := math . NewNonNegativeDecFromString ( tallyResult . YesCount )
2021-12-10 03:02:11 -08:00
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "yes count" )
2021-12-10 03:02:11 -08:00
}
totalPowerDec , err := math . NewNonNegativeDecFromString ( totalPower )
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "total power" )
2021-12-10 03:02:11 -08:00
}
2022-03-08 08:32:54 -08:00
// the real threshold of the policy is `min(threshold,total_weight)`. If
// the group member weights changes (member leaving, member weight update)
// and the threshold doesn't, we can end up with threshold > total_weight.
// In this case, as long as everyone votes yes (in which case
// `yesCount`==`realThreshold`), then the proposal still passes.
realThreshold := min ( threshold , totalPowerDec )
if yesCount . Cmp ( realThreshold ) >= 0 {
return DecisionPolicyResult { Allow : true , Final : true } , nil
}
2022-02-14 05:48:17 -08:00
totalCounts , err := tallyResult . TotalCounts ( )
2021-12-10 03:02:11 -08:00
if err != nil {
return DecisionPolicyResult { } , err
}
undecided , err := math . SubNonNegative ( totalPowerDec , totalCounts )
if err != nil {
return DecisionPolicyResult { } , err
}
2022-03-08 08:32:54 -08:00
// maxYesCount is the max potential number of yes count, i.e the current yes count
// plus all undecided count (supposing they all vote yes).
maxYesCount , err := yesCount . Add ( undecided )
2021-12-10 03:02:11 -08:00
if err != nil {
return DecisionPolicyResult { } , err
}
2022-03-08 08:32:54 -08:00
if maxYesCount . Cmp ( realThreshold ) < 0 {
2021-12-10 03:02:11 -08:00
return DecisionPolicyResult { Allow : false , Final : true } , nil
}
return DecisionPolicyResult { Allow : false , Final : false } , nil
}
2022-03-08 08:32:54 -08:00
func min ( a , b math . Dec ) math . Dec {
if a . Cmp ( b ) < 0 {
return a
}
return b
}
// Validate validates the policy against the group. Note that the threshold
// can actually be greater than the group's total weight: in the Allow method
// we check the tally weight against `min(threshold,total_weight)`.
2022-03-02 04:00:59 -08:00
func ( p * ThresholdDecisionPolicy ) Validate ( g GroupInfo , config Config ) error {
2022-03-08 08:32:54 -08:00
_ , err := math . NewPositiveDecFromString ( p . Threshold )
2021-12-10 03:02:11 -08:00
if err != nil {
return sdkerrors . Wrap ( err , "threshold" )
}
2022-03-08 08:32:54 -08:00
_ , err = math . NewNonNegativeDecFromString ( g . TotalWeight )
2021-12-10 03:02:11 -08:00
if err != nil {
return sdkerrors . Wrap ( err , "group total weight" )
}
2022-03-08 08:32:54 -08:00
2022-03-02 04:00:59 -08:00
if p . Windows . MinExecutionPeriod > p . Windows . VotingPeriod + config . MaxExecutionPeriod {
return sdkerrors . Wrap ( errors . ErrInvalid , "min_execution_period should be smaller than voting_period + max_execution_period" )
}
2021-12-10 03:02:11 -08:00
return nil
}
2022-02-11 01:12:57 -08:00
// Implements DecisionPolicy Interface
var _ DecisionPolicy = & PercentageDecisionPolicy { }
// NewPercentageDecisionPolicy creates a new percentage DecisionPolicy
2022-03-02 04:00:59 -08:00
func NewPercentageDecisionPolicy ( percentage string , votingPeriod time . Duration , executionPeriod time . Duration ) DecisionPolicy {
return & PercentageDecisionPolicy { percentage , & DecisionPolicyWindows { votingPeriod , executionPeriod } }
}
func ( p PercentageDecisionPolicy ) GetVotingPeriod ( ) time . Duration {
return p . Windows . VotingPeriod
2022-02-11 01:12:57 -08:00
}
func ( p PercentageDecisionPolicy ) ValidateBasic ( ) error {
percentage , err := math . NewPositiveDecFromString ( p . Percentage )
if err != nil {
return sdkerrors . Wrap ( err , "percentage threshold" )
}
if percentage . Cmp ( math . NewDecFromInt64 ( 1 ) ) == 1 {
return sdkerrors . Wrap ( errors . ErrInvalid , "percentage must be > 0 and <= 1" )
}
2022-03-02 04:00:59 -08:00
if p . Windows == nil || p . Windows . VotingPeriod == 0 {
return sdkerrors . Wrap ( errors . ErrInvalid , "voting period cannot be 0" )
2022-02-11 01:12:57 -08:00
}
2022-03-02 04:00:59 -08:00
2022-02-11 01:12:57 -08:00
return nil
}
2022-03-02 04:00:59 -08:00
func ( p * PercentageDecisionPolicy ) Validate ( g GroupInfo , config Config ) error {
if p . Windows . MinExecutionPeriod > p . Windows . VotingPeriod + config . MaxExecutionPeriod {
return sdkerrors . Wrap ( errors . ErrInvalid , "min_execution_period should be smaller than voting_period + max_execution_period" )
}
2022-02-11 01:12:57 -08:00
return nil
}
// Allow allows a proposal to pass when the tally of yes votes equals or exceeds the percentage threshold before the timeout.
2022-03-02 04:00:59 -08:00
func ( p PercentageDecisionPolicy ) Allow ( tally TallyResult , totalPower string , sinceSubmission time . Duration ) ( DecisionPolicyResult , error ) {
if sinceSubmission < p . Windows . MinExecutionPeriod {
return DecisionPolicyResult { } , errors . ErrUnauthorized . Wrapf ( "must wait %s after submission before execution, currently at %s" , p . Windows . MinExecutionPeriod , sinceSubmission )
2022-02-11 01:12:57 -08:00
}
percentage , err := math . NewPositiveDecFromString ( p . Percentage )
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "percentage" )
2022-02-11 01:12:57 -08:00
}
yesCount , err := math . NewNonNegativeDecFromString ( tally . YesCount )
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "yes count" )
2022-02-11 01:12:57 -08:00
}
totalPowerDec , err := math . NewNonNegativeDecFromString ( totalPower )
if err != nil {
2022-04-15 03:35:45 -07:00
return DecisionPolicyResult { } , sdkerrors . Wrap ( err , "total power" )
2022-02-11 01:12:57 -08:00
}
yesPercentage , err := yesCount . Quo ( totalPowerDec )
if err != nil {
return DecisionPolicyResult { } , err
}
if yesPercentage . Cmp ( percentage ) >= 0 {
return DecisionPolicyResult { Allow : true , Final : true } , nil
}
totalCounts , err := tally . TotalCounts ( )
if err != nil {
return DecisionPolicyResult { } , err
}
undecided , err := math . SubNonNegative ( totalPowerDec , totalCounts )
if err != nil {
return DecisionPolicyResult { } , err
}
sum , err := yesCount . Add ( undecided )
if err != nil {
return DecisionPolicyResult { } , err
}
sumPercentage , err := sum . Quo ( totalPowerDec )
if err != nil {
return DecisionPolicyResult { } , err
}
if sumPercentage . Cmp ( percentage ) < 0 {
return DecisionPolicyResult { Allow : false , Final : true } , nil
}
return DecisionPolicyResult { Allow : false , Final : false } , nil
}
2022-01-05 00:54:52 -08:00
var _ orm . Validateable = GroupPolicyInfo { }
2021-12-10 03:02:11 -08:00
2022-01-05 00:54:52 -08:00
// NewGroupPolicyInfo creates a new GroupPolicyInfo instance
2022-03-01 00:35:37 -08:00
func NewGroupPolicyInfo ( address sdk . AccAddress , group uint64 , admin sdk . AccAddress , metadata string ,
2022-01-06 08:29:46 -08:00
version uint64 , decisionPolicy DecisionPolicy , createdAt time . Time ) ( GroupPolicyInfo , error ) {
2022-01-05 00:54:52 -08:00
p := GroupPolicyInfo {
2022-01-06 08:29:46 -08:00
Address : address . String ( ) ,
GroupId : group ,
Admin : admin . String ( ) ,
Metadata : metadata ,
Version : version ,
CreatedAt : createdAt ,
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
err := p . SetDecisionPolicy ( decisionPolicy )
if err != nil {
2022-01-05 00:54:52 -08:00
return GroupPolicyInfo { } , err
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
return p , nil
}
2022-01-05 00:54:52 -08:00
func ( g * GroupPolicyInfo ) SetDecisionPolicy ( decisionPolicy DecisionPolicy ) error {
2022-04-15 03:35:45 -07:00
any , err := codectypes . NewAnyWithValue ( decisionPolicy )
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
if err != nil {
return err
}
g . DecisionPolicy = any
return nil
}
2022-04-15 03:35:45 -07:00
func ( g GroupPolicyInfo ) GetDecisionPolicy ( ) ( DecisionPolicy , error ) {
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
decisionPolicy , ok := g . DecisionPolicy . GetCachedValue ( ) . ( DecisionPolicy )
if ! ok {
2022-04-15 03:35:45 -07:00
return nil , sdkerrors . ErrInvalidType . Wrapf ( "expected %T, got %T" , ( DecisionPolicy ) ( nil ) , g . DecisionPolicy . GetCachedValue ( ) )
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
2022-04-15 03:35:45 -07:00
return decisionPolicy , nil
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
2022-01-05 00:54:52 -08:00
func ( g GroupPolicyInfo ) UnpackInterfaces ( unpacker codectypes . AnyUnpacker ) error {
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
var decisionPolicy DecisionPolicy
return unpacker . UnpackAny ( g . DecisionPolicy , & decisionPolicy )
}
2022-01-11 00:56:35 -08:00
func ( g GroupInfo ) PrimaryKeyFields ( ) [ ] interface { } {
2022-02-14 05:48:17 -08:00
return [ ] interface { } { g . Id }
2022-01-11 00:56:35 -08:00
}
func ( g GroupInfo ) ValidateBasic ( ) error {
2022-02-14 05:48:17 -08:00
if g . Id == 0 {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( errors . ErrEmpty , "group's GroupId" )
}
_ , err := sdk . AccAddressFromBech32 ( g . Admin )
if err != nil {
return sdkerrors . Wrap ( err , "admin" )
}
if _ , err := math . NewNonNegativeDecFromString ( g . TotalWeight ) ; err != nil {
return sdkerrors . Wrap ( err , "total weight" )
}
if g . Version == 0 {
return sdkerrors . Wrap ( errors . ErrEmpty , "version" )
}
return nil
}
2022-01-05 00:54:52 -08:00
func ( g GroupPolicyInfo ) PrimaryKeyFields ( ) [ ] interface { } {
2021-12-10 03:02:11 -08:00
addr , err := sdk . AccAddressFromBech32 ( g . Address )
if err != nil {
panic ( err )
}
return [ ] interface { } { addr . Bytes ( ) }
}
2022-01-11 00:56:35 -08:00
func ( g Proposal ) PrimaryKeyFields ( ) [ ] interface { } {
2022-02-14 05:48:17 -08:00
return [ ] interface { } { g . Id }
2022-01-11 00:56:35 -08:00
}
2022-01-05 00:54:52 -08:00
func ( g GroupPolicyInfo ) ValidateBasic ( ) error {
2021-12-10 03:02:11 -08:00
_ , err := sdk . AccAddressFromBech32 ( g . Admin )
if err != nil {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( err , "group policy admin" )
2021-12-10 03:02:11 -08:00
}
_ , err = sdk . AccAddressFromBech32 ( g . Address )
if err != nil {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( err , "group policy account address" )
2021-12-10 03:02:11 -08:00
}
if g . GroupId == 0 {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( errors . ErrEmpty , "group policy's group id" )
2021-12-10 03:02:11 -08:00
}
if g . Version == 0 {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( errors . ErrEmpty , "group policy version" )
2021-12-10 03:02:11 -08:00
}
2022-04-15 03:35:45 -07:00
policy , err := g . GetDecisionPolicy ( )
if err != nil {
return sdkerrors . Wrap ( err , "group policy decision policy" )
2021-12-10 03:02:11 -08:00
}
2022-04-15 03:35:45 -07:00
2021-12-10 03:02:11 -08:00
if err := policy . ValidateBasic ( ) ; err != nil {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( err , "group policy's decision policy" )
2021-12-10 03:02:11 -08:00
}
return nil
}
func ( g GroupMember ) PrimaryKeyFields ( ) [ ] interface { } {
addr , err := sdk . AccAddressFromBech32 ( g . Member . Address )
if err != nil {
panic ( err )
}
return [ ] interface { } { g . GroupId , addr . Bytes ( ) }
}
func ( g GroupMember ) ValidateBasic ( ) error {
if g . GroupId == 0 {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( errors . ErrEmpty , "group member's group id" )
2021-12-10 03:02:11 -08:00
}
2022-04-29 05:01:20 -07:00
err := MemberToMemberRequest ( g . Member ) . ValidateBasic ( )
2021-12-10 03:02:11 -08:00
if err != nil {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( err , "group member" )
}
return nil
}
2022-04-29 05:01:20 -07:00
// MemberToMemberRequest converts a `Member` (used for storage)
// to a `MemberRequest` (used in requests). The only difference
// between the two is that `MemberRequest` doesn't have any `AddedAt` field
// since it cannot be set as part of requests.
func MemberToMemberRequest ( m * Member ) MemberRequest {
return MemberRequest {
Address : m . Address ,
Weight : m . Weight ,
Metadata : m . Metadata ,
}
}
2022-01-11 00:56:35 -08:00
func ( p Proposal ) ValidateBasic ( ) error {
2022-02-14 05:48:17 -08:00
if p . Id == 0 {
2022-01-11 00:56:35 -08:00
return sdkerrors . Wrap ( errors . ErrEmpty , "proposal id" )
}
2022-04-06 04:16:23 -07:00
_ , err := sdk . AccAddressFromBech32 ( p . GroupPolicyAddress )
2022-01-11 00:56:35 -08:00
if err != nil {
2022-01-21 02:52:26 -08:00
return sdkerrors . Wrap ( err , "proposal group policy address" )
2022-01-11 00:56:35 -08:00
}
if p . GroupVersion == 0 {
return sdkerrors . Wrap ( errors . ErrEmpty , "proposal group version" )
}
if p . GroupPolicyVersion == 0 {
return sdkerrors . Wrap ( errors . ErrEmpty , "proposal group policy version" )
}
2022-02-14 05:48:17 -08:00
_ , err = p . FinalTallyResult . GetYesCount ( )
2022-01-11 00:56:35 -08:00
if err != nil {
2022-02-14 05:48:17 -08:00
return sdkerrors . Wrap ( err , "proposal FinalTallyResult yes count" )
2022-01-11 00:56:35 -08:00
}
2022-02-14 05:48:17 -08:00
_ , err = p . FinalTallyResult . GetNoCount ( )
2022-01-11 00:56:35 -08:00
if err != nil {
2022-02-14 05:48:17 -08:00
return sdkerrors . Wrap ( err , "proposal FinalTallyResult no count" )
2022-01-11 00:56:35 -08:00
}
2022-02-14 05:48:17 -08:00
_ , err = p . FinalTallyResult . GetAbstainCount ( )
2022-01-11 00:56:35 -08:00
if err != nil {
2022-02-14 05:48:17 -08:00
return sdkerrors . Wrap ( err , "proposal FinalTallyResult abstain count" )
2022-01-11 00:56:35 -08:00
}
2022-02-14 05:48:17 -08:00
_ , err = p . FinalTallyResult . GetNoWithVetoCount ( )
2022-01-11 00:56:35 -08:00
if err != nil {
2022-02-14 05:48:17 -08:00
return sdkerrors . Wrap ( err , "proposal FinalTallyResult veto count" )
2021-12-10 03:02:11 -08:00
}
return nil
}
func ( v Vote ) PrimaryKeyFields ( ) [ ] interface { } {
addr , err := sdk . AccAddressFromBech32 ( v . Voter )
if err != nil {
panic ( err )
}
return [ ] interface { } { v . ProposalId , addr . Bytes ( ) }
}
2022-01-11 00:56:35 -08:00
var _ orm . Validateable = Vote { }
func ( v Vote ) ValidateBasic ( ) error {
_ , err := sdk . AccAddressFromBech32 ( v . Voter )
if err != nil {
return sdkerrors . Wrap ( err , "voter" )
}
if v . ProposalId == 0 {
return sdkerrors . Wrap ( errors . ErrEmpty , "voter ProposalId" )
}
2022-02-14 05:48:17 -08:00
if v . Option == VOTE_OPTION_UNSPECIFIED {
return sdkerrors . Wrap ( errors . ErrEmpty , "voter vote option" )
2022-01-11 00:56:35 -08:00
}
2022-02-14 05:48:17 -08:00
if _ , ok := VoteOption_name [ int32 ( v . Option ) ] ; ! ok {
return sdkerrors . Wrap ( errors . ErrInvalid , "vote option" )
2022-01-11 00:56:35 -08:00
}
return nil
}
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
2022-01-05 00:54:52 -08:00
func ( q QueryGroupPoliciesByGroupResponse ) UnpackInterfaces ( unpacker codectypes . AnyUnpacker ) error {
return unpackGroupPolicies ( unpacker , q . GroupPolicies )
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
2022-01-05 00:54:52 -08:00
func ( q QueryGroupPoliciesByAdminResponse ) UnpackInterfaces ( unpacker codectypes . AnyUnpacker ) error {
return unpackGroupPolicies ( unpacker , q . GroupPolicies )
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
}
2022-01-05 00:54:52 -08:00
func unpackGroupPolicies ( unpacker codectypes . AnyUnpacker , accs [ ] * GroupPolicyInfo ) error {
feat: Add group module proto definitions and basic types (#9631)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
ref: https://github.com/cosmos/cosmos-sdk/issues/7633
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
Following up on https://github.com/cosmos/cosmos-sdk/pull/9089, this PR is the first step towards the migration of [x/group](https://github.com/regen-network/regen-ledger/tree/master/x/group) into the SDK. It introduces the group module proto definitions (types, tx, query) and other types.
The rest of the code (module, server, client, genesis...) is dependent on various other discussions (https://github.com/cosmos/cosmos-sdk/issues/9238, https://github.com/cosmos/cosmos-sdk/discussions/9182, https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/issues/7773) and will be added in follow-up PRs incrementally.
---
### 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...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-07-09 00:36:16 -07:00
for _ , g := range accs {
err := g . UnpackInterfaces ( unpacker )
if err != nil {
return err
}
}
return nil
}
2021-12-10 03:02:11 -08:00
type operation func ( x , y math . Dec ) ( math . Dec , error )
2022-02-14 05:48:17 -08:00
func ( t * TallyResult ) operation ( vote Vote , weight string , op operation ) error {
2021-12-10 03:02:11 -08:00
weightDec , err := math . NewPositiveDecFromString ( weight )
if err != nil {
return err
}
yesCount , err := t . GetYesCount ( )
if err != nil {
return sdkerrors . Wrap ( err , "yes count" )
}
noCount , err := t . GetNoCount ( )
if err != nil {
return sdkerrors . Wrap ( err , "no count" )
}
abstainCount , err := t . GetAbstainCount ( )
if err != nil {
return sdkerrors . Wrap ( err , "abstain count" )
}
2022-02-14 05:48:17 -08:00
vetoCount , err := t . GetNoWithVetoCount ( )
2021-12-10 03:02:11 -08:00
if err != nil {
return sdkerrors . Wrap ( err , "veto count" )
}
2022-02-14 05:48:17 -08:00
switch vote . Option {
case VOTE_OPTION_YES :
2021-12-10 03:02:11 -08:00
yesCount , err := op ( yesCount , weightDec )
if err != nil {
return sdkerrors . Wrap ( err , "yes count" )
}
t . YesCount = yesCount . String ( )
2022-02-14 05:48:17 -08:00
case VOTE_OPTION_NO :
2021-12-10 03:02:11 -08:00
noCount , err := op ( noCount , weightDec )
if err != nil {
return sdkerrors . Wrap ( err , "no count" )
}
t . NoCount = noCount . String ( )
2022-02-14 05:48:17 -08:00
case VOTE_OPTION_ABSTAIN :
2021-12-10 03:02:11 -08:00
abstainCount , err := op ( abstainCount , weightDec )
if err != nil {
return sdkerrors . Wrap ( err , "abstain count" )
}
t . AbstainCount = abstainCount . String ( )
2022-02-14 05:48:17 -08:00
case VOTE_OPTION_NO_WITH_VETO :
2021-12-10 03:02:11 -08:00
vetoCount , err := op ( vetoCount , weightDec )
if err != nil {
return sdkerrors . Wrap ( err , "veto count" )
}
2022-02-14 05:48:17 -08:00
t . NoWithVetoCount = vetoCount . String ( )
2021-12-10 03:02:11 -08:00
default :
2022-02-14 05:48:17 -08:00
return sdkerrors . Wrapf ( errors . ErrInvalid , "unknown vote option %s" , vote . Option . String ( ) )
2021-12-10 03:02:11 -08:00
}
return nil
}
2022-02-14 05:48:17 -08:00
func ( t TallyResult ) GetYesCount ( ) ( math . Dec , error ) {
2021-12-10 03:02:11 -08:00
yesCount , err := math . NewNonNegativeDecFromString ( t . YesCount )
if err != nil {
return math . Dec { } , err
}
return yesCount , nil
}
2022-02-14 05:48:17 -08:00
func ( t TallyResult ) GetNoCount ( ) ( math . Dec , error ) {
2021-12-10 03:02:11 -08:00
noCount , err := math . NewNonNegativeDecFromString ( t . NoCount )
if err != nil {
return math . Dec { } , err
}
return noCount , nil
}
2022-02-14 05:48:17 -08:00
func ( t TallyResult ) GetAbstainCount ( ) ( math . Dec , error ) {
2021-12-10 03:02:11 -08:00
abstainCount , err := math . NewNonNegativeDecFromString ( t . AbstainCount )
if err != nil {
return math . Dec { } , err
}
return abstainCount , nil
}
2022-02-14 05:48:17 -08:00
func ( t TallyResult ) GetNoWithVetoCount ( ) ( math . Dec , error ) {
vetoCount , err := math . NewNonNegativeDecFromString ( t . NoWithVetoCount )
2021-12-10 03:02:11 -08:00
if err != nil {
return math . Dec { } , err
}
return vetoCount , nil
}
2022-02-14 05:48:17 -08:00
func ( t * TallyResult ) Add ( vote Vote , weight string ) error {
2021-12-10 03:02:11 -08:00
if err := t . operation ( vote , weight , math . Add ) ; err != nil {
return err
}
return nil
}
// TotalCounts is the sum of all weights.
2022-02-14 05:48:17 -08:00
func ( t TallyResult ) TotalCounts ( ) ( math . Dec , error ) {
2021-12-10 03:02:11 -08:00
yesCount , err := t . GetYesCount ( )
if err != nil {
return math . Dec { } , sdkerrors . Wrap ( err , "yes count" )
}
noCount , err := t . GetNoCount ( )
if err != nil {
return math . Dec { } , sdkerrors . Wrap ( err , "no count" )
}
abstainCount , err := t . GetAbstainCount ( )
if err != nil {
return math . Dec { } , sdkerrors . Wrap ( err , "abstain count" )
}
2022-02-14 05:48:17 -08:00
vetoCount , err := t . GetNoWithVetoCount ( )
2021-12-10 03:02:11 -08:00
if err != nil {
return math . Dec { } , sdkerrors . Wrap ( err , "veto count" )
}
totalCounts := math . NewDecFromInt64 ( 0 )
totalCounts , err = totalCounts . Add ( yesCount )
if err != nil {
return math . Dec { } , err
}
totalCounts , err = totalCounts . Add ( noCount )
if err != nil {
return math . Dec { } , err
}
totalCounts , err = totalCounts . Add ( abstainCount )
if err != nil {
return math . Dec { } , err
}
totalCounts , err = totalCounts . Add ( vetoCount )
if err != nil {
return math . Dec { } , err
}
return totalCounts , nil
}
2021-12-15 07:17:28 -08:00
2022-03-02 04:00:59 -08:00
// DefaultTallyResult returns a TallyResult with all counts set to 0.
func DefaultTallyResult ( ) TallyResult {
return TallyResult {
YesCount : "0" ,
NoCount : "0" ,
NoWithVetoCount : "0" ,
AbstainCount : "0" ,
}
}
2022-02-14 05:48:17 -08:00
// VoteOptionFromString returns a VoteOption from a string. It returns an error
2021-12-15 07:17:28 -08:00
// if the string is invalid.
2022-02-14 05:48:17 -08:00
func VoteOptionFromString ( str string ) ( VoteOption , error ) {
vo , ok := VoteOption_value [ str ]
2021-12-15 07:17:28 -08:00
if ! ok {
2022-02-14 05:48:17 -08:00
return VOTE_OPTION_UNSPECIFIED , fmt . Errorf ( "'%s' is not a valid vote option" , str )
2021-12-15 07:17:28 -08:00
}
2022-02-14 05:48:17 -08:00
return VoteOption ( vo ) , nil
2021-12-15 07:17:28 -08:00
}