mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
284 lines
9.5 KiB
Go
284 lines
9.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
)
|
|
|
|
type Keeper struct {
|
|
cdc *codec.Codec
|
|
storeKey sdk.StoreKey
|
|
|
|
// Proposal router
|
|
router govtypes.Router
|
|
}
|
|
|
|
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, router govtypes.Router) Keeper {
|
|
// It is vital to seal the governance proposal router here as to not allow
|
|
// further handlers to be registered after the keeper is created since this
|
|
// could create invalid or non-deterministic behavior.
|
|
// TODO why?
|
|
// Not sealing the router because for some reason the function panics if it has already been sealed and there is no way to tell if has already been called.
|
|
// router.Seal()
|
|
|
|
return Keeper{
|
|
cdc: cdc,
|
|
storeKey: storeKey,
|
|
router: router,
|
|
}
|
|
}
|
|
|
|
func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committeeID uint64, pubProposal types.PubProposal) (uint64, sdk.Error) {
|
|
// Limit proposals to only be submitted by committee members
|
|
com, found := k.GetCommittee(ctx, committeeID)
|
|
if !found {
|
|
return 0, sdk.ErrInternal("committee doesn't exist")
|
|
}
|
|
if !com.HasMember(proposer) {
|
|
return 0, sdk.ErrInternal("only member can propose proposals")
|
|
}
|
|
|
|
// Check committee has permissions to enact proposal.
|
|
if !com.HasPermissionsFor(pubProposal) {
|
|
return 0, sdk.ErrInternal("committee does not have permissions to enact proposal")
|
|
}
|
|
|
|
// Check proposal is valid
|
|
// TODO what if it's not valid now but will be in the future?
|
|
// TODO does this need to be before permission check?
|
|
if err := k.ValidatePubProposal(ctx, pubProposal); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// Get a new ID and store the proposal
|
|
return k.StoreNewProposal(ctx, committeeID, pubProposal)
|
|
}
|
|
|
|
func (k Keeper) AddVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) sdk.Error {
|
|
// Validate
|
|
pr, found := k.GetProposal(ctx, proposalID)
|
|
if !found {
|
|
return sdk.ErrInternal("proposal not found")
|
|
}
|
|
com, found := k.GetCommittee(ctx, pr.CommitteeID)
|
|
if !found {
|
|
return sdk.ErrInternal("committee disbanded")
|
|
}
|
|
if !com.HasMember(voter) {
|
|
return sdk.ErrInternal("not authorized to vote on proposal")
|
|
}
|
|
|
|
// Store vote, overwriting any prior vote
|
|
k.SetVote(ctx, types.Vote{ProposalID: proposalID, Voter: voter})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (k Keeper) CloseOutProposal(ctx sdk.Context, proposalID uint64) sdk.Error {
|
|
pr, found := k.GetProposal(ctx, proposalID)
|
|
if !found {
|
|
return sdk.ErrInternal("proposal not found")
|
|
}
|
|
com, found := k.GetCommittee(ctx, pr.CommitteeID)
|
|
if !found {
|
|
return sdk.ErrInternal("committee disbanded")
|
|
}
|
|
|
|
var votes []types.Vote
|
|
k.IterateVotes(ctx, proposalID, func(vote types.Vote) bool {
|
|
votes = append(votes, vote)
|
|
return false
|
|
})
|
|
if sdk.NewDec(int64(len(votes))).GTE(types.VoteThreshold.MulInt64(int64(len(com.Members)))) { // TODO move vote counting stuff to committee methods // TODO add timeout check here - close if expired regardless of votes
|
|
// eneact vote
|
|
// The proposal handler may execute state mutating logic depending
|
|
// on the proposal content. If the handler fails, no state mutation
|
|
// is written and the error message is logged.
|
|
handler := k.router.GetRoute(pr.ProposalRoute())
|
|
cacheCtx, writeCache := ctx.CacheContext()
|
|
err := handler(cacheCtx, pr.PubProposal) // need to pass pubProposal as the handlers type assert it into the concrete types
|
|
if err == nil {
|
|
// write state to the underlying multi-store
|
|
writeCache()
|
|
} // if handler returns error, then still delete the proposal - it's still over, but send an event
|
|
|
|
// delete proposal and votes
|
|
k.DeleteProposal(ctx, proposalID)
|
|
for _, v := range votes {
|
|
k.DeleteVote(ctx, v.ProposalID, v.Voter)
|
|
}
|
|
} else {
|
|
return sdk.ErrInternal("note enough votes to close proposal")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (k Keeper) ValidatePubProposal(ctx sdk.Context, pubProposal types.PubProposal) sdk.Error {
|
|
// TODO not sure if the basic validation is required - should be run in msg.ValidateBasic
|
|
if pubProposal == nil {
|
|
return sdk.ErrInternal("proposal is empty")
|
|
}
|
|
if err := pubProposal.ValidateBasic(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !k.router.HasRoute(pubProposal.ProposalRoute()) {
|
|
return sdk.ErrInternal("no handler found for proposal")
|
|
}
|
|
|
|
// Execute the proposal content in a cache-wrapped context to validate the
|
|
// actual parameter changes before the proposal proceeds through the
|
|
// governance process. State is not persisted.
|
|
cacheCtx, _ := ctx.CacheContext()
|
|
handler := k.router.GetRoute(pubProposal.ProposalRoute())
|
|
if err := handler(cacheCtx, pubProposal); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetCommittee gets a committee from the store.
|
|
func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) {
|
|
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
|
|
}
|
|
|
|
// SetCommittee puts a committee into the store.
|
|
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)
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// 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 {
|
|
return 0, sdk.ErrInternal("proposal ID not set at genesis")
|
|
}
|
|
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
|
|
func (k Keeper) StoreNewProposal(ctx sdk.Context, committeeID uint64, pubProposal types.PubProposal) (uint64, sdk.Error) {
|
|
newProposalID, err := k.GetNextProposalID(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
proposal := types.Proposal{
|
|
PubProposal: pubProposal,
|
|
ID: newProposalID,
|
|
CommitteeID: committeeID,
|
|
}
|
|
|
|
k.SetProposal(ctx, proposal)
|
|
|
|
err = k.IncrementNextProposalID(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return newProposalID, nil
|
|
}
|
|
|
|
// GetProposal gets a proposal from the store.
|
|
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
|
|
}
|
|
|
|
// SetProposal puts a proposal into the store.
|
|
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)
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// IterateVotes provides an iterator over all stored votes for a given proposal.
|
|
// For each vote, cb will be called. If cb returns true, the iterator will close and stop.
|
|
func (k Keeper) IterateVotes(ctx sdk.Context, proposalID uint64, cb func(vote types.Vote) (stop bool)) {
|
|
// iterate over the section of the votes store that has all votes for a particular proposal
|
|
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), append(types.VoteKeyPrefix, types.GetKeyFromID(proposalID)...))
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
var vote types.Vote
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &vote)
|
|
|
|
if cb(vote) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetVote gets a vote from the store.
|
|
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
|
|
}
|
|
|
|
// SetVote puts a vote into the store.
|
|
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)
|
|
}
|
|
|
|
// 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))
|
|
}
|