0g-chain/x/committee/keeper/keeper.go

283 lines
8.6 KiB
Go
Raw Normal View History

2020-03-10 21:41:10 +00:00
package keeper
2020-03-04 19:16:27 +00:00
import (
2020-03-12 17:05:40 +00:00
"time"
"github.com/cosmos/cosmos-sdk/codec"
2020-03-10 23:16:22 +00:00
"github.com/cosmos/cosmos-sdk/store/prefix"
2020-03-04 19:16:27 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
2020-03-11 19:27:36 +00:00
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
2020-03-04 19:16:27 +00:00
"github.com/kava-labs/kava/x/committee/types"
)
type Keeper struct {
2020-03-29 19:43:25 +00:00
cdc *codec.Codec
storeKey sdk.StoreKey
codespace sdk.CodespaceType
2020-03-11 19:27:36 +00:00
// Proposal router
router govtypes.Router
}
2020-03-04 19:16:27 +00:00
2020-03-29 19:43:25 +00:00
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, router govtypes.Router, codespace sdk.CodespaceType) Keeper {
2020-03-11 23:52:54 +00:00
// Logic in the keeper methods assume the set of gov handlers is fixed.
// So the gov router must be sealed so no handlers can be added or removed after the keeper is created.
2020-03-23 14:32:50 +00:00
router.Seal()
2020-03-11 19:27:36 +00:00
return Keeper{
2020-03-29 19:43:25 +00:00
cdc: cdc,
storeKey: storeKey,
codespace: codespace,
router: router,
}
2020-03-04 19:16:27 +00:00
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Committees
// ------------------------------------------
2020-03-10 21:41:10 +00:00
2020-03-10 23:28:25 +00:00
// GetCommittee gets a committee from the store.
2020-03-10 21:41:10 +00:00
func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) {
2020-03-10 23:16:22 +00:00
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
bz := store.Get(types.GetKeyFromID(committeeID))
if bz == nil {
return types.Committee{}, false
}
var committee types.Committee
k.cdc.MustUnmarshalBinaryBare(bz, &committee)
2020-03-10 23:16:22 +00:00
return committee, true
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// SetCommittee puts a committee into the store.
2020-03-10 23:16:22 +00:00
func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
bz := k.cdc.MustMarshalBinaryBare(committee)
2020-03-10 23:16:22 +00:00
store.Set(types.GetKeyFromID(committee.ID), bz)
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// DeleteCommittee removes a committee from the store.
func (k Keeper) DeleteCommittee(ctx sdk.Context, committeeID uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
store.Delete(types.GetKeyFromID(committeeID))
}
2020-03-13 15:11:31 +00:00
// IterateCommittees provides an iterator over all stored committees.
// For each committee, cb will be called. If cb returns true, the iterator will close and stop.
func (k Keeper) IterateCommittees(ctx sdk.Context, cb func(committee types.Committee) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var committee types.Committee
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &committee)
2020-03-13 15:11:31 +00:00
if cb(committee) {
break
}
}
}
2020-04-27 14:04:47 +00:00
// GetCommittees returns all stored committees.
func (k Keeper) GetCommittees(ctx sdk.Context) []types.Committee {
results := []types.Committee{}
k.IterateCommittees(ctx, func(com types.Committee) bool {
results = append(results, com)
return false
})
return results
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Proposals
// ------------------------------------------
2020-03-11 19:52:25 +00:00
2020-03-11 00:58:42 +00:00
// SetNextProposalID stores an ID to be used for the next created proposal
func (k Keeper) SetNextProposalID(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.NextProposalIDKey, types.GetKeyFromID(id))
}
// GetNextProposalID reads the next available global ID from store
func (k Keeper) GetNextProposalID(ctx sdk.Context) (uint64, sdk.Error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.NextProposalIDKey)
if bz == nil {
2020-03-29 19:43:25 +00:00
return 0, types.ErrInvalidGenesis(k.codespace, "next proposal ID not set at genesis")
2020-03-11 00:58:42 +00:00
}
return types.Uint64FromBytes(bz), nil
}
// IncrementNextProposalID increments the next proposal ID in the store by 1.
func (k Keeper) IncrementNextProposalID(ctx sdk.Context) sdk.Error {
id, err := k.GetNextProposalID(ctx)
if err != nil {
return err
}
k.SetNextProposalID(ctx, id+1)
return nil
}
// StoreNewProposal stores a proposal, adding a new ID
2020-03-12 17:05:40 +00:00
func (k Keeper) StoreNewProposal(ctx sdk.Context, pubProposal types.PubProposal, committeeID uint64, deadline time.Time) (uint64, sdk.Error) {
2020-03-11 00:58:42 +00:00
newProposalID, err := k.GetNextProposalID(ctx)
if err != nil {
return 0, err
}
proposal := types.NewProposal(
pubProposal,
newProposalID,
committeeID,
deadline,
)
2020-03-11 00:58:42 +00:00
k.SetProposal(ctx, proposal)
err = k.IncrementNextProposalID(ctx)
if err != nil {
return 0, err
}
return newProposalID, nil
}
2020-03-10 23:28:25 +00:00
// GetProposal gets a proposal from the store.
2020-03-10 23:16:22 +00:00
func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
bz := store.Get(types.GetKeyFromID(proposalID))
if bz == nil {
return types.Proposal{}, false
}
var proposal types.Proposal
k.cdc.MustUnmarshalBinaryBare(bz, &proposal)
2020-03-10 23:16:22 +00:00
return proposal, true
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// SetProposal puts a proposal into the store.
2020-03-10 23:16:22 +00:00
func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
bz := k.cdc.MustMarshalBinaryBare(proposal)
2020-03-10 23:16:22 +00:00
store.Set(types.GetKeyFromID(proposal.ID), bz)
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// DeleteProposal removes a proposal from the store.
func (k Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
store.Delete(types.GetKeyFromID(proposalID))
}
2020-03-12 17:05:40 +00:00
// IterateProposals provides an iterator over all stored proposals.
// For each proposal, cb will be called. If cb returns true, the iterator will close and stop.
func (k Keeper) IterateProposals(ctx sdk.Context, cb func(proposal types.Proposal) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var proposal types.Proposal
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &proposal)
2020-03-12 17:05:40 +00:00
if cb(proposal) {
break
}
}
}
2020-04-27 14:04:47 +00:00
// GetProposals returns all stored proposals.
func (k Keeper) GetProposals(ctx sdk.Context) []types.Proposal {
results := []types.Proposal{}
k.IterateProposals(ctx, func(prop types.Proposal) bool {
results = append(results, prop)
return false
})
return results
}
// GetProposalsByCommittee returns all proposals for one committee.
func (k Keeper) GetProposalsByCommittee(ctx sdk.Context, committeeID uint64) []types.Proposal {
results := []types.Proposal{}
k.IterateProposals(ctx, func(prop types.Proposal) bool {
if prop.CommitteeID == committeeID {
results = append(results, prop)
}
return false
})
return results
}
// DeleteProposalAndVotes removes a proposal and its associated votes.
func (k Keeper) DeleteProposalAndVotes(ctx sdk.Context, proposalID uint64) {
votes := k.GetVotesByProposal(ctx, proposalID)
k.DeleteProposal(ctx, proposalID)
for _, v := range votes {
k.DeleteVote(ctx, v.ProposalID, v.Voter)
}
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Votes
// ------------------------------------------
2020-03-11 19:27:36 +00:00
2020-03-10 23:28:25 +00:00
// GetVote gets a vote from the store.
2020-03-10 23:16:22 +00:00
func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.Vote, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
bz := store.Get(types.GetVoteKey(proposalID, voter))
if bz == nil {
return types.Vote{}, false
}
var vote types.Vote
k.cdc.MustUnmarshalBinaryBare(bz, &vote)
2020-03-10 23:16:22 +00:00
return vote, true
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// SetVote puts a vote into the store.
2020-03-10 23:16:22 +00:00
func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
bz := k.cdc.MustMarshalBinaryBare(vote)
2020-03-10 23:16:22 +00:00
store.Set(types.GetVoteKey(vote.ProposalID, vote.Voter), bz)
2020-03-10 21:41:10 +00:00
}
2020-03-10 23:28:25 +00:00
// DeleteVote removes a Vote from the store.
func (k Keeper) DeleteVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
store.Delete(types.GetVoteKey(proposalID, voter))
}
2020-03-11 19:52:25 +00:00
2020-04-27 14:04:47 +00:00
// IterateVotes provides an iterator over all stored votes.
2020-03-11 19:52:25 +00:00
// For each vote, cb will be called. If cb returns true, the iterator will close and stop.
2020-04-27 14:04:47 +00:00
func (k Keeper) IterateVotes(ctx sdk.Context, cb func(vote types.Vote) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
2020-03-11 19:52:25 +00:00
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var vote types.Vote
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &vote)
2020-03-11 19:52:25 +00:00
if cb(vote) {
break
}
}
}
2020-04-27 14:04:47 +00:00
// GetVotes returns all stored votes.
func (k Keeper) GetVotes(ctx sdk.Context) []types.Vote {
results := []types.Vote{}
k.IterateVotes(ctx, func(vote types.Vote) bool {
results = append(results, vote)
return false
})
return results
}
// GetVotesByProposal returns all votes for one proposal.
func (k Keeper) GetVotesByProposal(ctx sdk.Context, proposalID uint64) []types.Vote {
results := []types.Vote{}
k.IterateVotes(ctx, func(vote types.Vote) bool {
if vote.ProposalID == proposalID {
results = append(results, vote)
}
return false
})
return results
}