Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation on governance proposals #9791

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
- [9751](https://github.com/vegaprotocol/vega/issues/9751) - Make sure that LP fee party accounts exists.
- [9762](https://github.com/vegaprotocol/vega/issues/9762) - Referral fees API not filtering by party correctly.
- [9775](https://github.com/vegaprotocol/vega/issues/9775) - Do not pay discount if set is not eligible
- [9788](https://github.com/vegaprotocol/vega/issues/9788) - Fix transfer account validation.

## 0.72.1

Expand Down
90 changes: 53 additions & 37 deletions commands/proposal_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,27 @@ import (

const ReferenceMaxLen int = 100

var (
validSources = map[protoTypes.AccountType]struct{}{
protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD: {},
protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE: {},
}
validDestinations = map[protoTypes.AccountType]struct{}{
var validTransfers = map[protoTypes.AccountType]map[protoTypes.AccountType]struct{}{
protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY: {
protoTypes.AccountType_ACCOUNT_TYPE_GENERAL: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_RECEIVED_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MARKET_PROPOSERS: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_AVERAGE_POSITION: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RELATIVE_RETURN: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RETURN_VOLATILITY: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_VALIDATOR_RANKING: {},
},
protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE: {
protoTypes.AccountType_ACCOUNT_TYPE_GENERAL: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_RECEIVED_FEES: {},
Expand All @@ -58,8 +66,22 @@ var (
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RELATIVE_RETURN: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RETURN_VOLATILITY: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_VALIDATOR_RANKING: {},
}
)
},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE: {
protoTypes.AccountType_ACCOUNT_TYPE_GENERAL: {},
protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE: {},
protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY: {},
protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_PAID_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_LP_RECEIVED_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MAKER_RECEIVED_FEES: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_MARKET_PROPOSERS: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_AVERAGE_POSITION: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RELATIVE_RETURN: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_RETURN_VOLATILITY: {},
protoTypes.AccountType_ACCOUNT_TYPE_REWARD_VALIDATOR_RANKING: {},
},
}

func CheckProposalSubmission(cmd *commandspb.ProposalSubmission) error {
return checkProposalSubmission(cmd).ErrorOrNil()
Expand Down Expand Up @@ -494,48 +516,42 @@ func checkNewTransferChanges(change *protoTypes.ProposalTerms_NewTransfer) Error
if changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_UNSPECIFIED {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.source_type", ErrIsRequired)
}

validDest, ok := validTransfers[changes.SourceType]
// source account type may be one of the following:
if _, ok := validSources[changes.SourceType]; !ok {
if !ok {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.source_type", ErrIsNotValid)
}

if changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_UNSPECIFIED {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination_type", ErrIsRequired)
}

if changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD && changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD {
if _, ok := validDest[changes.DestinationType]; !ok {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination_type", ErrIsNotValid)
}
dest := changes.DestinationType

if changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY && changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination_type", ErrIsNotValid)
}

// destination account type may be one of the following:
if _, ok := validDestinations[changes.DestinationType]; !ok {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination_type", ErrIsNotValid)
}

if changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_GENERAL && !IsVegaPublicKey(changes.Destination) {
// party accounts: check pubkey
if dest == protoTypes.AccountType_ACCOUNT_TYPE_GENERAL && !IsVegaPublicKey(changes.Destination) {
errs.AddForProperty("proposal_submission.terms.change.new_transfer.changes.destination", ErrShouldBeAValidVegaPublicKey)
}

if (changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD ||
changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY ||
changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE) &&
len(changes.Source) > 0 {
// insurance account type requires a source, other sources are global
if changes.SourceType == protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE {
if len(changes.Source) == 0 {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.source", ErrIsNotValid)
}
// destination == source
if dest == changes.SourceType && changes.Source == changes.Destination {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this apply to any source type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but because of the map, I believe the only one where this check is actually required is market insurance pools

return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination", ErrIsNotValid)
}
} else if len(changes.Source) > 0 {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.source", ErrIsNotValid)
}

if (changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD ||
changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY ||
changes.DestinationType == protoTypes.AccountType_ACCOUNT_TYPE_GLOBAL_INSURANCE) &&
len(changes.Destination) > 0 {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination", ErrIsNotValid)
}

if changes.SourceType == changes.DestinationType && changes.Source == changes.Destination {
// global destination accounts == no source
if (dest == protoTypes.AccountType_ACCOUNT_TYPE_GENERAL ||
dest == protoTypes.AccountType_ACCOUNT_TYPE_INSURANCE) &&
len(changes.Destination) == 0 {
return errs.FinalAddForProperty("proposal_submission.terms.change.new_transfer.changes.destination", ErrIsNotValid)
}

Expand Down
Loading
Loading