rename modules and tidy

This commit is contained in:
rhuairahrighairigh 2020-03-04 19:16:27 +00:00
parent 3e1b1b1b72
commit 2ab6c4669f
20 changed files with 275 additions and 213 deletions

View File

@ -1,22 +0,0 @@
package circuit-breaker
import (
"github.com/cosmos/cosmos-sdk/x/gov"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/circuit-breaker/types"
"github.com/kava-labs/kava/x/circuit-breaker/keeper"
)
func NewCircuitBreakerProposalHandler(k Keeper) gov.Handler {
return func(ctx sdk.Context, content gov.Content) sdk.Error {
switch c := content.(type) {
case types.CircuitBreakerProposal:
return keeper.HandleCircuitBreakerProposal(ctx, k, c)
default:
errMsg := fmt.Sprintf("unrecognized circuit-breaker proposal content type: %T", c)
return sdk.ErrUnknownRequest(errMsg)
}
}
}

12
x/committee/abci.go Normal file
View File

@ -0,0 +1,12 @@
package committee
func BeginBlocker() {
// TODO much the same as the current gov endblocker does
// Get all active proposals
// If voting periods are over, tally up the results
// If a proposal passes run it through the correct handler
// Handler need to be registered in app.go as they are for the current gov module
handler := keeper.Router().GetRoute(proposal.ProposalRoute())
err := handler(ctx, proposal.Content)
}

47
x/committee/handler.go Normal file
View File

@ -0,0 +1,47 @@
package committee
// committee, subcommittee, council, caucus, commission, synod, board
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
)
// NewHandler creates an sdk.Handler for committee messages
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case types.MsgSubmitProposal:
handleMsgSubmitProposal(ctx, k, msg)
case types.MsgVote:
handleMsgVote(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s msg type: %T", types.ModuleName, msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitProposal) sdk.Result {
err := keeper.SubmitProposal(ctx, msg)
if err != nil {
return err.Result()
}
return sdk.Result{}
}
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result {
err := keeper.AddVote(ctx, msg)
if err != nil {
return err.Result()
}
return sdk.Result{}
}

View File

@ -0,0 +1,61 @@
package types
import (
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 {
// TODO other stuff as needed
// Proposal router
router govtypes.Router
}
/* 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
*/
func (k Keeper) SubmitProposal(ctx sdk.Context, msg types.MsgSubmitProposal) sdk.Error {
// 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.
committee, _ := k.GetCommittee(ctx, msg.CommitteeID)
hasPermissions := false
for _, p := range committee.Permissions {
if p.Allows(msg.Proposal) {
hasPermissions = true
break
}
}
if !hasPermissions {
return sdk.ErrInternal("committee does not have permissions to enact proposal").Result()
}
// 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
}

View File

@ -1,4 +1,4 @@
package groupgov
package committee
// TODO create a GroupChangeProposalHandler, see params or distribution
// It will overwrite the Members of Permissions field of a group

View File

@ -1,15 +1,15 @@
# `groupgov`
# `committee`
## Table of Contents
## Overview
The `x/groupgov` module is an additional governance module to `cosmos-sdk/x/gov`.
The `x/committee` module is an additional governance module to `cosmos-sdk/x/gov`.
It allows groups of accounts to vote on and enact proposals, mainly to allow certain proposal types to be decided on quickly in emergency situations, or to delegate low risk parameter updates to a smaller group of individuals.
Groups have members and permissions.
Committees have members and permissions.
Members vote on proposals, with just simple one vote per member, no deposits or slashing. More sophisticated voting could be added.
@ -20,4 +20,5 @@ This allows permissions to be parameterized to allow fine grained control specif
Design Alternatives
- Should this define its own gov types, or reuse those from gov module?
- Should we push changes to sdk gov to make it more general purpose?
- Should we push changes to sdk gov to make it more general purpose?
- Could use params more instead of custom gov proposals

View File

@ -2,12 +2,12 @@ package types
// These msg types should be basically the same as for gov, but without deposits.
// MsgSubmitProposal is used by group members to create a new proposal that they can vote on.
// MsgSubmitProposal is used by committee members to create a new proposal that they can vote on.
type MsgSubmitProposal struct {
// TODO
}
// MsgVote is submitted by group members to vote on proposals.
// MsgVote is submitted by committee members to vote on proposals.
type MsgVote struct {
// TODO
}

View File

@ -0,0 +1,65 @@
package types
import (
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"
sdtypes "github.com/kava-labs/kava/x/shutdown/types"
)
// EXAMPLE PERMISSIONS ------------------------------
// Allow only changes to inflation_rate
type InflationRateChangePermission struct{}
var _ types.Permission = InflationRateChangePermission
func (InflationRateChangePermission) Allows(p gov.Proposal) bool {
pcp, ok := p.Content.(params.ParameterChangeProposal)
if !ok {
return false
}
for _, pc := range pcp.Changes {
if pc.Key == "inflation_rate" {
return true
}
}
return false
}
// Allow only shutdown of the CDP Deposit msg
type ShutdownCDPDepsitPermission struct{}
var _ types.Permission = ShutdownCDPDepsitPermission
func (ShutdownCDPDepsitPermission) Allows(p gov.Content) bool {
sdp, ok := p.(sdtypes.ShutdownProposal)
if !ok {
return false
}
for _, r := range sdp.MsgRoutes {
if r.Route == "cdp" && r.Msg == "MsgCDPDeposit" {
return true
}
}
return false
}
// Same as above but the route isn't static
type GeneralShutdownPermission struct {
MsgRoute cbtypes.MsgRoute
}
var _ types.Permission = GeneralShutdownPermission
func (perm GeneralShutdownPermission) Allows(p gov.Content) bool {
sdp, ok := p.Content.(sdtypes.ShutdownProposal)
if !ok {
return false
}
for _, r := range sdp.MsgRoutes {
if r == perm.MsgRoute {
return true
}
}
return false
}

View File

@ -5,7 +5,7 @@ import (
)
// A gov.Proposal to used to add/remove members from a group, or to add/remove permissions.
// Normally registered with standard gov. But could also be registed with groupgov to allow groups to be controlled by other groups.
// Normally registered with standard gov. But could also be registed with committee to allow groups to be controlled by other groups.
type GroupChangeProposal struct {
Members []sdk.AccAddress
Permissions []Permission

View File

@ -0,0 +1,33 @@
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
)
// A Committee is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions.
type Committee struct {
Members []sdk.AccAddress
Permissions []Permission
}
// Permission is anything with a method that validates whether a proposal is allowed by it or not.
type Permission interface {
Allows(gov.Content) bool
}
// GOV STUFF --------------------------
// Should be much the same as in gov module, except Proposals are linked to a committee ID.
type Proposal struct {
gov.Content
ID uint64
committeeID uint64
// TODO
}
type Vote struct {
ProposalID uint64
Voter sdk.AccAddress
Option byte
}

View File

@ -1,6 +0,0 @@
package groupgov
func BeginBlocker() {
// TODO do much the same as the current gov endblocker does
// if voting periods are over, collect votes and run proposals through proposal handlers
}

View File

@ -1,52 +0,0 @@
package cdp
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/groupgov/types"
)
// NewHandler creates an sdk.Handler for cdp messages
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case types.MsgSubmitProposal:
handleMsgSubmitProposal(ctx, k, msg)
case types.MsgVote:
handleMsgVote(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s msg type: %T", , types.ModuleName, msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSubmitProposal(ctx sdk.Context, k Keeper, msg types.MsgSubmitProposal) sdk.Result {
// TODO limit proposals to only be submitted by group members
// get group
group, _ := k.GetGroup(ctx, msg.GroupID)
// 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.
var hasPermissions := false
for p, _ := range group.Permissions {
if p.Allows(msg.Proposal) {
hasPermissions = true
break
}
}
if !hasPermissions {
return sdk.ErrInternal("group does not have permissions to enact proposal").Result()
}
// TODO validate proposal by running it with cached context like how gov does it
// TODO store the proposal, probably put it in a queue
}
func handleMsgVote(ctx sdk.Context, k Keeper, msg types.MsgVote) sdk.Result {
/* TODO
- validate vote
- store vote
*/
}

View File

@ -1,18 +0,0 @@
package types
import "github.com/cosmos/cosmos-sdk/types"
type Keeper struct {
// TODO other stuff as needed
// Proposal router
router types.Router
}
/* TODO methods - should be similar to gov
- GetGroup
- SetGroup
- AddVote
*/

View File

@ -1,50 +0,0 @@
package types
import (
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"
cbtypes "github.com/kava-labs/kava/x/circuit-breaker/types"
)
// EXAMPLE PERMISSIONS ------------------------------
// Allow only changes to inflation_rate
type InflationRateChangePermission struct{}
func (InflationRateChangePermission) Allows(p gov.Proposal) bool {
pcp, _ := p.Content.(params.ParameterChangeProposal)
for _, pc := range pcp.Changes {
if pc.Key == "inflation_rate" {
return true
}
}
return false
}
// Allow only circuit breaking of the CDP Deposit msg
type CircuitBreakCDPDepsitPermission struct{}
func (CircuitBreakCDPDepsitPermission) Allows(p gov.Proposal) bool {
cbp, _ := p.Content.(cbtypes.CircuitBreakProposal)
for _, r := range cbp.MsgRoutes {
if r.Route == "cdp" && r.Msg == "MsgCDPDeposit" {
return true
}
}
return false
}
// Same as above but the route the permssion allows can be set
type CircuitBreakPermission struct {
MsgRoute cbtypes.MsgRoute
}
func (perm CircuitBreakPermission) Allows(p gov.Proposal) bool {
cbp, _ := p.Content.(cbtypes.CircuitBreakProposal)
for _, r := range cbp.MsgRoutes {
if r == perm.MsgRoute {
return true
}
}
return false
}

View File

@ -1,37 +0,0 @@
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
)
// A Group is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions.
type Group struct {
Members []sdk.AccAddress
Permissions []Permission
}
// Permission is anything with a method that validates whether a proposal is allowed by it or not.
// Collectively, if one permission allows a proposal then the proposal is allowed through.
type Permission interface {
Allows(gov.Proposal) bool // maybe don't reuse gov's type here
}
// STANDARD GOV STUFF --------------------------
// Should be much the same as in gov module, except Proposals are linked to a group ID.
type Router struct {
// TODO
}
type Proposal struct {
ID uint64
groupID uint64
// TODO
}
type Vote struct {
proposalID uint64
option uint64
// TODO
}

View File

@ -3,7 +3,7 @@ package ante
import (
"fmt"
"github.com/kava-labs/kava/x/circuit-breaker/keeper"
"github.com/kava-labs/kava/x/shutdown/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
)

View File

@ -2,7 +2,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/circuit-breaker/types"
"github.com/kava-labs/kava/x/shutdown/types"
)
// Keeper stores routes that have been "broken"
@ -11,12 +11,9 @@ type Keeper struct {
func (k Keeper) GetMsgRoutes(ctx sdk.Context) []types.MsgRoute {
// TODO
return []types.MsgRoute{}
}
func (k Keeper) SetMsgRoutes(ctx sdk.Context, routes []types.MsgRoute) {
// TODO
}
func HandleCircuitBreakerProposal(ctx sdk.Context, k Keeper, c types.CircuitBreakProposal) {
k.SetMsgRoutes(ctx, c.MsgRoutes)
}

View File

@ -0,0 +1,30 @@
package shutdown
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/kava-labs/kava/x/shutdown/keeper"
"github.com/kava-labs/kava/x/shutdown/types"
)
func NewShutdownProposalHandler(k keeper.Keeper) gov.Handler {
return func(ctx sdk.Context, content gov.Content) sdk.Error {
switch c := content.(type) {
case types.ShutdownProposal:
return handleShutdownProposal(ctx, k, c)
default:
errMsg := fmt.Sprintf("unrecognized %s proposal content type: %T", types.ModuleName, c)
return sdk.ErrUnknownRequest(errMsg)
}
}
}
func handleShutdownProposal(ctx sdk.Context, k keeper.Keeper, c types.ShutdownProposal) sdk.Error {
// TODO validate proposal
k.SetMsgRoutes(ctx, c.MsgRoutes)
return nil
}

View File

@ -1,11 +1,11 @@
# `circuit-breaker`
# `shutdown`
## Table of Contents
## Overview
The `x/circuit-breaker` module allows certain message types to be disabled based on governance votes.
The `x/shutdown` module allows certain message types to be disabled based on governance votes.
Msgs and routes are disabled via an antehandler decorator. The decorator checks incoming all txs and rejects them if they contain a disallowed msg type.
Disallowed msg types are stored in a circuit breaker keeper.

View File

@ -11,33 +11,33 @@ type MsgRoute struct {
}
const (
ProposalTypeCircuitBreak = "CircuitBreak"
ProposalTypeShutdown = "Shutdown"
)
// Assert CircuitBreakProposal implements govtypes.Content at compile-time
var _ govtypes.Content = CircuitBreakProposal{}
// Assert ShutdownProposal implements govtypes.Content at compile-time
var _ govtypes.Content = ShutdownProposal{}
type CircuitBreakProposal struct {
type ShutdownProposal struct {
Title string
Description string
MsgRoutes []MsgRoute
}
// GetTitle returns the title of a community pool spend proposal.
func (cbp CircuitBreakProposal) GetTitle() string { return cbp.Title }
func (sp ShutdownProposal) GetTitle() string { return sp.Title }
// GetDescription returns the description of a community pool spend proposal.
func (cbp CircuitBreakProposal) GetDescription() string { return cbp.Description }
func (sp ShutdownProposal) GetDescription() string { return sp.Description }
// GetDescription returns the routing key of a community pool spend proposal.
func (cbp CircuitBreakProposal) ProposalRoute() string { return RouterKey }
func (sp ShutdownProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a community pool spend proposal.
func (cbp CircuitBreakProposal) ProposalType() string { return ProposalTypeCircuitBreak }
func (sp ShutdownProposal) ProposalType() string { return ProposalTypeShutdown }
// ValidateBasic runs basic stateless validity checks
func (cbp CircuitBreakProposal) ValidateBasic() sdk.Error {
err := govtypes.ValidateAbstract(DefaultCodespace, cbp)
func (sp ShutdownProposal) ValidateBasic() sdk.Error {
err := govtypes.ValidateAbstract(DefaultCodespace, sp)
if err != nil {
return err
}
@ -46,15 +46,16 @@ func (cbp CircuitBreakProposal) ValidateBasic() sdk.Error {
}
// String implements the Stringer interface.
func (cbp CircuitBreakProposal) String() string {
func (sp ShutdownProposal) String() string {
// TODO
return ""
}
const (
DefaultCodespace sdk.CodespaceType = ModuleName
// ModuleName is the module name constant used in many places
ModuleName = "circuit-breaker"
ModuleName = "shutdown"
// RouterKey is the message route for distribution
RouterKey = ModuleName