-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(gov)!: use collections for Vote state management. #16164
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b545c09
remove SetVote
15ce593
remove GetAllVotes
8df65e0
remove GetVote
8263ab5
remove deleteVote api
1f37086
remove GetVotes api
7d2c151
remove IterateVotes and IterateAllVotes API
c6f800f
do not error when no results are found
31af9b7
remove keys
704d165
chore: adjust doc
0573841
chore: lint
12ca750
chore: CHANGELOG.md
4d507a8
remove more unused functions
fa1f803
Merge branch 'main' into tip/gov/coll-votes
testinginprod 5d3601f
lint
3f1fa20
Merge remote-tracking branch 'origin/tip/gov/coll-votes' into tip/gov…
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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -2,12 +2,10 @@ package keeper | |
|
||
import ( | ||
"context" | ||
"cosmossdk.io/collections" | ||
"fmt" | ||
|
||
"cosmossdk.io/errors" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/runtime" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/gov/types" | ||
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
|
@@ -38,7 +36,7 @@ func (keeper Keeper) AddVote(ctx context.Context, proposalID uint64, voterAddr s | |
} | ||
|
||
vote := v1.NewVote(proposalID, voterAddr, options, metadata) | ||
err = keeper.SetVote(ctx, vote) | ||
err = keeper.Votes.Set(ctx, collections.Join(proposalID, voterAddr), vote) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -58,118 +56,8 @@ func (keeper Keeper) AddVote(ctx context.Context, proposalID uint64, voterAddr s | |
return nil | ||
} | ||
|
||
// GetAllVotes returns all the votes from the store | ||
func (keeper Keeper) GetAllVotes(ctx context.Context) (votes v1.Votes, err error) { | ||
err = keeper.IterateAllVotes(ctx, func(vote v1.Vote) error { | ||
votes = append(votes, &vote) | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
// GetVotes returns all the votes from a proposal | ||
func (keeper Keeper) GetVotes(ctx context.Context, proposalID uint64) (votes v1.Votes, err error) { | ||
err = keeper.IterateVotes(ctx, proposalID, func(vote v1.Vote) error { | ||
votes = append(votes, &vote) | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
// GetVote gets the vote from an address on a specific proposal | ||
func (keeper Keeper) GetVote(ctx context.Context, proposalID uint64, voterAddr sdk.AccAddress) (vote v1.Vote, err error) { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
bz, err := store.Get(types.VoteKey(proposalID, voterAddr)) | ||
if err != nil { | ||
return vote, err | ||
} | ||
|
||
if bz == nil { | ||
return vote, types.ErrVoteNotFound | ||
} | ||
|
||
err = keeper.cdc.Unmarshal(bz, &vote) | ||
if err != nil { | ||
return vote, err | ||
} | ||
|
||
return vote, nil | ||
} | ||
|
||
// SetVote sets a Vote to the gov store | ||
func (keeper Keeper) SetVote(ctx context.Context, vote v1.Vote) error { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
bz, err := keeper.cdc.Marshal(&vote) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
addr, err := keeper.authKeeper.StringToBytes(vote.Voter) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return store.Set(types.VoteKey(vote.ProposalId, addr), bz) | ||
} | ||
|
||
// IterateAllVotes iterates over all the stored votes and performs a callback function | ||
func (keeper Keeper) IterateAllVotes(ctx context.Context, cb func(vote v1.Vote) error) error { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
iterator := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), types.VotesKeyPrefix) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
var vote v1.Vote | ||
err := keeper.cdc.Unmarshal(iterator.Value(), &vote) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cb(vote) | ||
// exit early without error if cb returns ErrStopIterating | ||
if errors.IsOf(err, errors.ErrStopIterating) { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IterateVotes iterates over all the proposals votes and performs a callback function | ||
func (keeper Keeper) IterateVotes(ctx context.Context, proposalID uint64, cb func(vote v1.Vote) error) error { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
iterator := storetypes.KVStorePrefixIterator(runtime.KVStoreAdapter(store), types.VotesKey(proposalID)) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
var vote v1.Vote | ||
err := keeper.cdc.Unmarshal(iterator.Value(), &vote) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cb(vote) | ||
// exit early without error if cb returns ErrStopIterating | ||
if errors.IsOf(err, errors.ErrStopIterating) { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// deleteVotes deletes the all votes from a given proposalID. | ||
func (keeper Keeper) deleteVotes(ctx context.Context, proposalID uint64) error { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
return store.Delete(types.VotesKey(proposalID)) | ||
} | ||
|
||
// deleteVote deletes a vote from a given proposalID and voter from the store | ||
func (keeper Keeper) deleteVote(ctx context.Context, proposalID uint64, voterAddr sdk.AccAddress) error { | ||
store := keeper.storeService.OpenKVStore(ctx) | ||
return store.Delete(types.VoteKey(proposalID, voterAddr)) | ||
// TODO(tip): fix https://github.com/cosmos/cosmos-sdk/issues/16162 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixing this in a separate PR in order to not increase PR scope. |
||
return nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated, but can we do them each in a go routine?
Maybe the speed-up is irrelevant however 🤷🏾♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, this is something I would love to experiment on as soon as we move modules to use core genesis.