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

148 lines
4.5 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 (
"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"
//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 {
cdc *codec.Codec
storeKey sdk.StoreKey
// TODO Proposal router
//router govtypes.Router
}
2020-03-04 19:16:27 +00:00
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
}
2020-03-04 19:16:27 +00:00
}
/* TODO keeper methods - very similar to gov
- SubmitProposal validate and store a proposal, additionally setting things like timeout
- GetProposal
- SetProposal
- AddVote - add a vote to a particular proposal from a member
- GetVote
- SetVote
- GetCommittee
- SetCommittee
*/
2020-03-10 21:41:10 +00:00
func (k Keeper) SubmitProposal(ctx sdk.Context, proposal types.Proposal) sdk.Error {
2020-03-04 19:16:27 +00:00
// TODO Limit proposals to only be submitted by group members
// Check group has permissions to enact proposal. As long as one permission allows the proposal then it goes through. Its the OR of all permissions.
2020-03-10 21:41:10 +00:00
committee, _ := k.GetCommittee(ctx, proposal.CommitteeID)
2020-03-04 19:16:27 +00:00
hasPermissions := false
for _, p := range committee.Permissions {
2020-03-10 21:41:10 +00:00
if p.Allows(proposal) {
2020-03-04 19:16:27 +00:00
hasPermissions = true
break
}
}
if !hasPermissions {
2020-03-10 21:41:10 +00:00
return sdk.ErrInternal("committee does not have permissions to enact proposal")
2020-03-04 19:16:27 +00:00
}
// TODO validate proposal by running it with cached context like how gov does it
// TODO store the proposal, probably put it in a queue
return nil
}
func (k Keeper) AddVote(ctx sdk.Context, msg types.MsgVote) sdk.Error {
/* TODO
- validate vote
- store vote
*/
return nil
}
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.MustUnmarshalBinaryLengthPrefixed(bz, &committee)
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.MustMarshalBinaryLengthPrefixed(committee)
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))
}
// 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.MustUnmarshalBinaryLengthPrefixed(bz, &proposal)
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.MustMarshalBinaryLengthPrefixed(proposal)
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))
}
// 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.MustUnmarshalBinaryLengthPrefixed(bz, &vote)
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.MustMarshalBinaryLengthPrefixed(vote)
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))
}