diff --git a/x/circuit-breaker/proposal_handler.go b/x/circuit-breaker/proposal_handler.go deleted file mode 100644 index 0d5dd79c..00000000 --- a/x/circuit-breaker/proposal_handler.go +++ /dev/null @@ -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) - } - } -} \ No newline at end of file diff --git a/x/committee/abci.go b/x/committee/abci.go new file mode 100644 index 00000000..50bb9dc2 --- /dev/null +++ b/x/committee/abci.go @@ -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) +} diff --git a/x/committee/handler.go b/x/committee/handler.go new file mode 100644 index 00000000..bc23eb5a --- /dev/null +++ b/x/committee/handler.go @@ -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{} +} diff --git a/x/committee/keeper/keeper.go b/x/committee/keeper/keeper.go new file mode 100644 index 00000000..8dc5158d --- /dev/null +++ b/x/committee/keeper/keeper.go @@ -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 +} diff --git a/x/groupgov/proposal_handler.go b/x/committee/proposal_handler.go similarity index 88% rename from x/groupgov/proposal_handler.go rename to x/committee/proposal_handler.go index 12598aea..852a704f 100644 --- a/x/groupgov/proposal_handler.go +++ b/x/committee/proposal_handler.go @@ -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 diff --git a/x/groupgov/spec/README.md b/x/committee/spec/README.md similarity index 84% rename from x/groupgov/spec/README.md rename to x/committee/spec/README.md index db9f0c5b..ac8103d5 100644 --- a/x/groupgov/spec/README.md +++ b/x/committee/spec/README.md @@ -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? \ No newline at end of file +- Should we push changes to sdk gov to make it more general purpose? +- Could use params more instead of custom gov proposals diff --git a/x/groupgov/types/msg.go b/x/committee/types/msg.go similarity index 51% rename from x/groupgov/types/msg.go rename to x/committee/types/msg.go index 06cc211c..508d4a4a 100644 --- a/x/groupgov/types/msg.go +++ b/x/committee/types/msg.go @@ -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 } diff --git a/x/committee/types/permissions.go b/x/committee/types/permissions.go new file mode 100644 index 00000000..d560296d --- /dev/null +++ b/x/committee/types/permissions.go @@ -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 +} diff --git a/x/groupgov/types/proposal.go b/x/committee/types/proposal.go similarity index 81% rename from x/groupgov/types/proposal.go rename to x/committee/types/proposal.go index 611e5671..58403a17 100644 --- a/x/groupgov/types/proposal.go +++ b/x/committee/types/proposal.go @@ -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 diff --git a/x/committee/types/types.go b/x/committee/types/types.go new file mode 100644 index 00000000..2994a179 --- /dev/null +++ b/x/committee/types/types.go @@ -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 +} diff --git a/x/groupgov/abci.go b/x/groupgov/abci.go deleted file mode 100644 index 736efcd9..00000000 --- a/x/groupgov/abci.go +++ /dev/null @@ -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 -} diff --git a/x/groupgov/handler.go b/x/groupgov/handler.go deleted file mode 100644 index 5b1a959c..00000000 --- a/x/groupgov/handler.go +++ /dev/null @@ -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 - */ -} diff --git a/x/groupgov/keeper/keeper.go b/x/groupgov/keeper/keeper.go deleted file mode 100644 index 909d53bd..00000000 --- a/x/groupgov/keeper/keeper.go +++ /dev/null @@ -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 - -*/ diff --git a/x/groupgov/types/permissions.go b/x/groupgov/types/permissions.go deleted file mode 100644 index 4024d8d3..00000000 --- a/x/groupgov/types/permissions.go +++ /dev/null @@ -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 -} diff --git a/x/groupgov/types/types.go b/x/groupgov/types/types.go deleted file mode 100644 index 50940abd..00000000 --- a/x/groupgov/types/types.go +++ /dev/null @@ -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 -} diff --git a/x/circuit-breaker/ante/ante.go b/x/shutdown/ante/ante.go similarity index 94% rename from x/circuit-breaker/ante/ante.go rename to x/shutdown/ante/ante.go index 0ad9e78c..9a748340 100644 --- a/x/circuit-breaker/ante/ante.go +++ b/x/shutdown/ante/ante.go @@ -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" ) diff --git a/x/circuit-breaker/keeper/keeper.go b/x/shutdown/keeper/keeper.go similarity index 62% rename from x/circuit-breaker/keeper/keeper.go rename to x/shutdown/keeper/keeper.go index 0a1b650a..50118606 100644 --- a/x/circuit-breaker/keeper/keeper.go +++ b/x/shutdown/keeper/keeper.go @@ -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) -} diff --git a/x/shutdown/proposal_handler.go b/x/shutdown/proposal_handler.go new file mode 100644 index 00000000..97d2a251 --- /dev/null +++ b/x/shutdown/proposal_handler.go @@ -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 +} diff --git a/x/circuit-breaker/spec/README.md b/x/shutdown/spec/README.md similarity index 83% rename from x/circuit-breaker/spec/README.md rename to x/shutdown/spec/README.md index 5c53f051..96a98410 100644 --- a/x/circuit-breaker/spec/README.md +++ b/x/shutdown/spec/README.md @@ -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. diff --git a/x/circuit-breaker/types/types.go b/x/shutdown/types/types.go similarity index 56% rename from x/circuit-breaker/types/types.go rename to x/shutdown/types/types.go index 8f700025..110b5349 100644 --- a/x/circuit-breaker/types/types.go +++ b/x/shutdown/types/types.go @@ -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