-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Gov Keys and Iterators #4460
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
Merged
Merged
Gov Keys and Iterators #4460
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
84493fd
refactor gov keys
fedekunze 91e9c68
iterators and renamings
fedekunze de49818
invert queue key order
fedekunze 8194039
changelog
fedekunze 1df9b17
fix tests
fedekunze 0215c8a
update alias.go
fedekunze b9a7d88
Apply suggestions from code review
fedekunze 01fcfdf
rename keys
fedekunze 0e649f0
rename functions
fedekunze 02b69f0
Apply suggestions from code review
fedekunze c0d0c04
address Aleks' comments
fedekunze 476a606
fix test
fedekunze 5239919
address Karoly's comments
fedekunze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #4437 replace gov store keys to use `[]byte` instead of `string` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #4439 add governance iterators | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package gov | ||
|
|
||
| import ( | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
fedekunze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
| ) | ||
|
|
||
| // GetDeposit gets the deposit of a specific depositor on a specific proposal | ||
| func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) (deposit Deposit, found bool) { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| bz := store.Get(types.KeyProposalDeposit(proposalID, depositorAddr)) | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if bz == nil { | ||
| return deposit, false | ||
| } | ||
|
|
||
| keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &deposit) | ||
| return deposit, true | ||
| } | ||
|
|
||
| func (keeper Keeper) setDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress, deposit Deposit) { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(deposit) | ||
| store.Set(types.KeyProposalDeposit(proposalID, depositorAddr), bz) | ||
| } | ||
|
|
||
| // AddDeposit adds or updates a deposit of a specific depositor on a specific proposal | ||
| // Activates voting period when appropriate | ||
| func (keeper Keeper) AddDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress, depositAmount sdk.Coins) (sdk.Error, bool) { | ||
| // Checks to see if proposal exists | ||
| proposal, ok := keeper.GetProposal(ctx, proposalID) | ||
| if !ok { | ||
| return ErrUnknownProposal(keeper.codespace, proposalID), false | ||
| } | ||
|
|
||
| // Check if proposal is still depositable | ||
| if (proposal.Status != StatusDepositPeriod) && (proposal.Status != StatusVotingPeriod) { | ||
| return ErrAlreadyFinishedProposal(keeper.codespace, proposalID), false | ||
| } | ||
|
|
||
| // Send coins from depositor's account to DepositedCoinsAccAddr account | ||
| // TODO: Don't use an account for this purpose; it's clumsy and prone to misuse. | ||
| err := keeper.ck.SendCoins(ctx, depositorAddr, DepositedCoinsAccAddr, depositAmount) | ||
| if err != nil { | ||
| return err, false | ||
| } | ||
|
|
||
| // Update proposal | ||
| proposal.TotalDeposit = proposal.TotalDeposit.Add(depositAmount) | ||
| keeper.SetProposal(ctx, proposal) | ||
|
|
||
| // Check if deposit has provided sufficient total funds to transition the proposal into the voting period | ||
| activatedVotingPeriod := false | ||
| if proposal.Status == StatusDepositPeriod && proposal.TotalDeposit.IsAllGTE(keeper.GetDepositParams(ctx).MinDeposit) { | ||
| keeper.activateVotingPeriod(ctx, proposal) | ||
| activatedVotingPeriod = true | ||
| } | ||
|
|
||
| // Add or update deposit object | ||
| currDeposit, found := keeper.GetDeposit(ctx, proposalID, depositorAddr) | ||
| if !found { | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| newDeposit := NewDeposit(proposalID, depositorAddr, depositAmount) | ||
| keeper.setDeposit(ctx, proposalID, depositorAddr, newDeposit) | ||
| } else { | ||
| currDeposit.Amount = currDeposit.Amount.Add(depositAmount) | ||
| keeper.setDeposit(ctx, proposalID, depositorAddr, currDeposit) | ||
| } | ||
|
|
||
| return nil, activatedVotingPeriod | ||
| } | ||
|
|
||
| // GetDeposits returns all the deposits from the store | ||
| func (keeper Keeper) GetDeposits(ctx sdk.Context) (deposits Deposits) { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| iterator := sdk.KVStorePrefixIterator(store, types.DepositsKeyPrefix) | ||
|
|
||
| keeper.IterateDeposits(ctx, iterator, func(deposit Deposit) bool { | ||
| deposits = append(deposits, deposit) | ||
| return false | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| // GetProposalDeposits returns all the deposits from a proposal | ||
| func (keeper Keeper) GetProposalDeposits(ctx sdk.Context, proposalID uint64) (deposits Deposits) { | ||
| iterator := keeper.GetProposalDepositsIterator(ctx, proposalID) | ||
|
|
||
| keeper.IterateDeposits(ctx, iterator, func(deposit Deposit) bool { | ||
| deposits = append(deposits, deposit) | ||
| return false | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| // GetProposalDepositsIterator gets all the deposits on a specific proposal as an sdk.Iterator | ||
| func (keeper Keeper) GetProposalDepositsIterator(ctx sdk.Context, proposalID uint64) sdk.Iterator { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| return sdk.KVStorePrefixIterator(store, types.KeyProposalDeposits(proposalID)) | ||
| } | ||
|
|
||
| // RefundProposalDeposits refunds and deletes all the deposits on a specific proposal | ||
| func (keeper Keeper) RefundProposalDeposits(ctx sdk.Context, proposalID uint64) { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| iterator := keeper.GetProposalDepositsIterator(ctx, proposalID) | ||
|
|
||
| keeper.IterateDeposits(ctx, iterator, func(deposit Deposit) bool { | ||
| err := keeper.ck.SendCoins(ctx, DepositedCoinsAccAddr, deposit.Depositor, deposit.Amount) | ||
| if err != nil { | ||
| panic("should not happen") | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| store.Delete(iterator.Key()) | ||
| return false | ||
| }) | ||
| } | ||
|
|
||
| // DeleteProposalDeposits deletes all the deposits on a specific proposal without refunding them | ||
| func (keeper Keeper) DeleteProposalDeposits(ctx sdk.Context, proposalID uint64) { | ||
| store := ctx.KVStore(keeper.storeKey) | ||
| iterator := keeper.GetProposalDepositsIterator(ctx, proposalID) | ||
|
|
||
| keeper.IterateDeposits(ctx, iterator, func(deposit Deposit) bool { | ||
| err := keeper.ck.SendCoins(ctx, DepositedCoinsAccAddr, BurnedDepositCoinsAccAddr, deposit.Amount) | ||
| if err != nil { | ||
| panic("should not happen") | ||
fedekunze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| store.Delete(iterator.Key()) | ||
| return false | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.