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"
|
|
|
|
|
|
|
|
proto "github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/gogo/protobuf/types"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-12-10 03:02:11 -08:00
|
|
|
// MaxMetadataLength defines the max length of the metadata bytes field
|
|
|
|
// for various entities within the group module
|
|
|
|
// TODO: This could be used as params once x/params is upgraded to use protobuf
|
|
|
|
const MaxMetadataLength = 255
|
|
|
|
|
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 {
|
|
|
|
Allow bool
|
|
|
|
Final bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecisionPolicy is the persistent set of rules to determine the result of election on a proposal.
|
|
|
|
type DecisionPolicy interface {
|
|
|
|
codec.ProtoMarshaler
|
|
|
|
|
|
|
|
ValidateBasic() error
|
2021-12-10 03:02:11 -08:00
|
|
|
GetTimeout() time.Duration
|
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(tally Tally, totalPower string, votingDuration time.Duration) (DecisionPolicyResult, error)
|
|
|
|
Validate(g GroupInfo) error
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:02:11 -08:00
|
|
|
// Implements DecisionPolicy Interface
|
|
|
|
var _ DecisionPolicy = &ThresholdDecisionPolicy{}
|
|
|
|
|
|
|
|
// NewThresholdDecisionPolicy creates a threshold DecisionPolicy
|
|
|
|
func NewThresholdDecisionPolicy(threshold string, timeout time.Duration) DecisionPolicy {
|
|
|
|
return &ThresholdDecisionPolicy{threshold, timeout}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p ThresholdDecisionPolicy) ValidateBasic() error {
|
|
|
|
if _, err := math.NewPositiveDecFromString(p.Threshold); err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "threshold")
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := p.Timeout
|
|
|
|
|
|
|
|
if timeout <= time.Nanosecond {
|
|
|
|
return sdkerrors.Wrap(errors.ErrInvalid, "timeout")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow allows a proposal to pass when the tally of yes votes equals or exceeds the threshold before the timeout.
|
|
|
|
func (p ThresholdDecisionPolicy) Allow(tally Tally, totalPower string, votingDuration time.Duration) (DecisionPolicyResult, error) {
|
|
|
|
pTimeout := types.DurationProto(p.Timeout)
|
|
|
|
timeout, err := types.DurationFromProto(pTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionPolicyResult{}, err
|
|
|
|
}
|
|
|
|
if timeout <= votingDuration {
|
|
|
|
return DecisionPolicyResult{Allow: false, Final: true}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold, err := math.NewPositiveDecFromString(p.Threshold)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionPolicyResult{}, err
|
|
|
|
}
|
|
|
|
yesCount, err := math.NewNonNegativeDecFromString(tally.YesCount)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionPolicyResult{}, err
|
|
|
|
}
|
|
|
|
if yesCount.Cmp(threshold) >= 0 {
|
|
|
|
return DecisionPolicyResult{Allow: true, Final: true}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
totalPowerDec, err := math.NewNonNegativeDecFromString(totalPower)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionPolicyResult{}, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if sum.Cmp(threshold) < 0 {
|
|
|
|
return DecisionPolicyResult{Allow: false, Final: true}, nil
|
|
|
|
}
|
|
|
|
return DecisionPolicyResult{Allow: false, Final: false}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if policy threshold is greater than the total group weight
|
|
|
|
func (p *ThresholdDecisionPolicy) Validate(g GroupInfo) error {
|
|
|
|
threshold, err := math.NewPositiveDecFromString(p.Threshold)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "threshold")
|
|
|
|
}
|
|
|
|
totalWeight, err := math.NewNonNegativeDecFromString(g.TotalWeight)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "group total weight")
|
|
|
|
}
|
|
|
|
if threshold.Cmp(totalWeight) > 0 {
|
|
|
|
return sdkerrors.Wrap(errors.ErrInvalid, "policy threshold should not be greater than the total group weight")
|
|
|
|
}
|
|
|
|
return 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
|
|
|
|
func NewGroupPolicyInfo(address sdk.AccAddress, group uint64, admin sdk.AccAddress, metadata []byte,
|
|
|
|
version uint64, decisionPolicy DecisionPolicy) (GroupPolicyInfo, error) {
|
|
|
|
p := GroupPolicyInfo{
|
2021-12-10 03:02:11 -08:00
|
|
|
Address: address.String(),
|
|
|
|
GroupId: group,
|
|
|
|
Admin: admin.String(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Version: version,
|
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 {
|
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
|
|
|
msg, ok := decisionPolicy.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't proto marshal %T", msg)
|
|
|
|
}
|
|
|
|
any, err := codectypes.NewAnyWithValue(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.DecisionPolicy = any
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-05 00:54:52 -08:00
|
|
|
func (g GroupPolicyInfo) GetDecisionPolicy() 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
|
|
|
decisionPolicy, ok := g.DecisionPolicy.GetCachedValue().(DecisionPolicy)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return decisionPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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-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 {
|
|
|
|
return sdkerrors.Wrap(err, "admin")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = sdk.AccAddressFromBech32(g.Address)
|
|
|
|
if err != nil {
|
2022-01-05 00:54:52 -08:00
|
|
|
return sdkerrors.Wrap(err, "group policy")
|
2021-12-10 03:02:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if g.GroupId == 0 {
|
|
|
|
return sdkerrors.Wrap(errors.ErrEmpty, "group")
|
|
|
|
}
|
|
|
|
if g.Version == 0 {
|
|
|
|
return sdkerrors.Wrap(errors.ErrEmpty, "version")
|
|
|
|
}
|
|
|
|
policy := g.GetDecisionPolicy()
|
|
|
|
|
|
|
|
if policy == nil {
|
|
|
|
return sdkerrors.Wrap(errors.ErrEmpty, "policy")
|
|
|
|
}
|
|
|
|
if err := policy.ValidateBasic(); err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return sdkerrors.Wrap(errors.ErrEmpty, "group")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := g.Member.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "member")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Vote) PrimaryKeyFields() []interface{} {
|
|
|
|
addr, err := sdk.AccAddressFromBech32(v.Voter)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return []interface{}{v.ProposalId, addr.Bytes()}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
func (t *Tally) operation(vote Vote, weight string, op operation) error {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
vetoCount, err := t.GetVetoCount()
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "veto count")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vote.Choice {
|
|
|
|
case Choice_CHOICE_YES:
|
|
|
|
yesCount, err := op(yesCount, weightDec)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "yes count")
|
|
|
|
}
|
|
|
|
t.YesCount = yesCount.String()
|
|
|
|
case Choice_CHOICE_NO:
|
|
|
|
noCount, err := op(noCount, weightDec)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "no count")
|
|
|
|
}
|
|
|
|
t.NoCount = noCount.String()
|
|
|
|
case Choice_CHOICE_ABSTAIN:
|
|
|
|
abstainCount, err := op(abstainCount, weightDec)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "abstain count")
|
|
|
|
}
|
|
|
|
t.AbstainCount = abstainCount.String()
|
|
|
|
case Choice_CHOICE_VETO:
|
|
|
|
vetoCount, err := op(vetoCount, weightDec)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.Wrap(err, "veto count")
|
|
|
|
}
|
|
|
|
t.VetoCount = vetoCount.String()
|
|
|
|
default:
|
|
|
|
return sdkerrors.Wrapf(errors.ErrInvalid, "unknown choice %s", vote.Choice.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tally) GetYesCount() (math.Dec, error) {
|
|
|
|
yesCount, err := math.NewNonNegativeDecFromString(t.YesCount)
|
|
|
|
if err != nil {
|
|
|
|
return math.Dec{}, err
|
|
|
|
}
|
|
|
|
return yesCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tally) GetNoCount() (math.Dec, error) {
|
|
|
|
noCount, err := math.NewNonNegativeDecFromString(t.NoCount)
|
|
|
|
if err != nil {
|
|
|
|
return math.Dec{}, err
|
|
|
|
}
|
|
|
|
return noCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tally) GetAbstainCount() (math.Dec, error) {
|
|
|
|
abstainCount, err := math.NewNonNegativeDecFromString(t.AbstainCount)
|
|
|
|
if err != nil {
|
|
|
|
return math.Dec{}, err
|
|
|
|
}
|
|
|
|
return abstainCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tally) GetVetoCount() (math.Dec, error) {
|
|
|
|
vetoCount, err := math.NewNonNegativeDecFromString(t.VetoCount)
|
|
|
|
if err != nil {
|
|
|
|
return math.Dec{}, err
|
|
|
|
}
|
|
|
|
return vetoCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tally) Add(vote Vote, weight string) error {
|
|
|
|
if err := t.operation(vote, weight, math.Add); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalCounts is the sum of all weights.
|
|
|
|
func (t Tally) TotalCounts() (math.Dec, error) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
vetoCount, err := t.GetVetoCount()
|
|
|
|
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
|
|
|
|
|
|
|
// ChoiceFromString returns a Choice from a string. It returns an error
|
|
|
|
// if the string is invalid.
|
|
|
|
func ChoiceFromString(str string) (Choice, error) {
|
|
|
|
choice, ok := Choice_value[str]
|
|
|
|
if !ok {
|
|
|
|
return Choice_CHOICE_UNSPECIFIED, fmt.Errorf("'%s' is not a valid vote choice", str)
|
|
|
|
}
|
|
|
|
return Choice(choice), nil
|
|
|
|
}
|