add vote tallying and tests

This commit is contained in:
rhuairahrighairigh 2020-03-11 19:27:36 +00:00
parent f9dab88c16
commit e473d972ec
11 changed files with 446 additions and 63 deletions

View File

@ -249,6 +249,7 @@ func NewApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
app.committeeKeeper = committee.NewKeeper( app.committeeKeeper = committee.NewKeeper(
app.cdc, app.cdc,
keys[committee.StoreKey], keys[committee.StoreKey],
govRouter,
// TODO blacklist module addresses? // TODO blacklist module addresses?
) )

View File

@ -8,29 +8,43 @@ import (
) )
const ( const (
ModuleName = types.ModuleName DefaultNextProposalID = types.DefaultNextProposalID
StoreKey = types.StoreKey ModuleName = types.ModuleName
StoreKey = types.StoreKey
) )
var ( var (
// function aliases // function aliases
NewKeeper = keeper.NewKeeper NewKeeper = keeper.NewKeeper
RegisterCodec = types.RegisterCodec DefaultGenesisState = types.DefaultGenesisState
GetKeyFromID = types.GetKeyFromID
GetVoteKey = types.GetVoteKey
NewGenesisState = types.NewGenesisState
RegisterCodec = types.RegisterCodec
Uint64FromBytes = types.Uint64FromBytes
// variable aliases // variable aliases
ModuleCdc = types.ModuleCdc CommitteeKeyPrefix = types.CommitteeKeyPrefix
ModuleCdc = types.ModuleCdc
NextProposalIDKey = types.NextProposalIDKey
ProposalKeyPrefix = types.ProposalKeyPrefix
VoteKeyPrefix = types.VoteKeyPrefix
VoteThreshold = types.VoteThreshold
) )
type ( type (
Keeper = keeper.Keeper Keeper = keeper.Keeper
Committee = types.Committee Committee = types.Committee
GeneralShutdownPermission = types.GeneralShutdownPermission GeneralShutdownPermission = types.GeneralShutdownPermission
GenesisState = types.GenesisState
GodPermission = types.GodPermission
GroupChangeProposal = types.GroupChangeProposal GroupChangeProposal = types.GroupChangeProposal
InflationRateChangePermission = types.InflationRateChangePermission InflationRateChangePermission = types.InflationRateChangePermission
MsgSubmitProposal = types.MsgSubmitProposal MsgSubmitProposal = types.MsgSubmitProposal
MsgVote = types.MsgVote MsgVote = types.MsgVote
Permission = types.Permission Permission = types.Permission
Proposal = types.Proposal Proposal = types.Proposal
PubProposal = types.PubProposal
ShutdownCDPDepsitPermission = types.ShutdownCDPDepsitPermission ShutdownCDPDepsitPermission = types.ShutdownCDPDepsitPermission
Vote = types.Vote Vote = types.Vote
) )

24
x/committee/genesis.go Normal file
View File

@ -0,0 +1,24 @@
package committee
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// InitGenesis initializes the store state from a genesis state.
func InitGenesis(ctx sdk.Context, keeper Keeper, gs GenesisState) {
if err := gs.Validate(); err != nil {
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
}
keeper.SetNextProposalID(ctx, gs.NextProposalID)
// TODO set votes, committee, proposals
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
// TODO
return GenesisState{}
}

View File

@ -40,6 +40,16 @@ func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubm
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result { func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result {
err := keeper.AddVote(ctx, msg) err := keeper.AddVote(ctx, msg)
// Try closing proposal
_ = k.CloseOutProposal(ctx, proposalID)
// if err.Error() == "note enough votes to close proposal" { // TODO
// return nil // This is not a reason to error
// }
// if err != nil {
// return err
// }
if err != nil { if err != nil {
return err.Result() return err.Result()
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix" "github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
//govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/kava-labs/kava/x/committee/types" "github.com/kava-labs/kava/x/committee/types"
) )
@ -14,20 +14,28 @@ type Keeper struct {
cdc *codec.Codec cdc *codec.Codec
storeKey sdk.StoreKey storeKey sdk.StoreKey
// TODO Proposal router // Proposal router
//router govtypes.Router router govtypes.Router
} }
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey) Keeper { 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{ return Keeper{
cdc: cdc, cdc: cdc,
storeKey: storeKey, storeKey: storeKey,
router: router,
} }
} }
func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, proposal types.Proposal) (uint64, sdk.Error) { 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 // Limit proposals to only be submitted by committee members
com, found := k.GetCommittee(ctx, proposal.CommitteeID) com, found := k.GetCommittee(ctx, committeeID)
if !found { if !found {
return 0, sdk.ErrInternal("committee doesn't exist") return 0, sdk.ErrInternal("committee doesn't exist")
} }
@ -35,30 +43,29 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, proposa
return 0, sdk.ErrInternal("only member can propose proposals") return 0, sdk.ErrInternal("only member can propose proposals")
} }
// Check proposal is valid // Check committee has permissions to enact proposal.
if err := proposal.ValidateBasic(); err != nil { if !com.HasPermissionsFor(pubProposal) {
return 0, err
}
// Check group has permissions to enact proposal.
if !com.HasPermissionsFor(proposal) {
return 0, sdk.ErrInternal("committee does not have permissions to enact proposal") return 0, sdk.ErrInternal("committee does not have permissions to enact proposal")
} }
// TODO validate proposal by running it with cached context like how gov does it // Check proposal is valid
// what if it's not valid now but will be in the future? // 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 // Get a new ID and store the proposal
return k.StoreNewProposal(ctx, proposal) return k.StoreNewProposal(ctx, committeeID, pubProposal)
} }
func (k Keeper) AddVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) sdk.Error { func (k Keeper) AddVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) sdk.Error {
// Validate // Validate
proposal, found := k.GetProposal(ctx, proposalID) pr, found := k.GetProposal(ctx, proposalID)
if !found { if !found {
return sdk.ErrInternal("proposal not found") return sdk.ErrInternal("proposal not found")
} }
com, found := k.GetCommittee(ctx, proposal.CommitteeID) com, found := k.GetCommittee(ctx, pr.CommitteeID)
if !found { if !found {
return sdk.ErrInternal("committee disbanded") return sdk.ErrInternal("committee disbanded")
} }
@ -69,7 +76,70 @@ func (k Keeper) AddVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress
// Store vote, overwriting any prior vote // Store vote, overwriting any prior vote
k.SetVote(ctx, types.Vote{ProposalID: proposalID, Voter: voter}) k.SetVote(ctx, types.Vote{ProposalID: proposalID, Voter: voter})
// TODO close vote if tally has been reached 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 return nil
} }
@ -125,12 +195,16 @@ func (k Keeper) IncrementNextProposalID(ctx sdk.Context) sdk.Error {
} }
// StoreNewProposal stores a proposal, adding a new ID // StoreNewProposal stores a proposal, adding a new ID
func (k Keeper) StoreNewProposal(ctx sdk.Context, proposal types.Proposal) (uint64, sdk.Error) { func (k Keeper) StoreNewProposal(ctx sdk.Context, committeeID uint64, pubProposal types.PubProposal) (uint64, sdk.Error) {
newProposalID, err := k.GetNextProposalID(ctx) newProposalID, err := k.GetNextProposalID(ctx)
if err != nil { if err != nil {
return 0, err return 0, err
} }
proposal.ID = newProposalID proposal := types.Proposal{
PubProposal: pubProposal,
ID: newProposalID,
CommitteeID: committeeID,
}
k.SetProposal(ctx, proposal) k.SetProposal(ctx, proposal)
@ -166,6 +240,23 @@ func (k Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) {
store.Delete(types.GetKeyFromID(proposalID)) 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. // GetVote gets a vote from the store.
func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.Vote, bool) { func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.Vote, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)

View File

@ -1,16 +1,18 @@
package keeper_test package keeper_test
import ( import (
"reflect"
"testing" "testing"
"github.com/kava-labs/kava/x/committee/types"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app" "github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/committee/keeper" "github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
) )
type KeeperTestSuite struct { type KeeperTestSuite struct {
@ -27,25 +29,89 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.app = app.NewTestApp() suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper() suite.keeper = suite.app.GetCommitteeKeeper()
suite.ctx = suite.app.NewContext(true, abci.Header{}) suite.ctx = suite.app.NewContext(true, abci.Header{})
_, suite.addresses = app.GeneratePrivKeyAddressPairs(2) _, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
} }
func (suite *KeeperTestSuite) TestSubmitProposal() { func (suite *KeeperTestSuite) TestSubmitProposal() {
normalCom := types.Committee{
ID: 12,
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
}
noPermissionsCom := types.Committee{
ID: 12,
Members: suite.addresses[:2],
Permissions: []types.Permission{},
}
testcases := []struct { testcases := []struct {
name string name string
proposal types.Proposal committee types.Committee
proposer sdk.AccAddress pubProposal types.PubProposal
expectPass bool proposer sdk.AccAddress
committeeID uint64
expectPass bool
}{ }{
{name: "empty proposal", proposer: suite.addresses[0], expectPass: false}, {
name: "normal",
committee: normalCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: normalCom.Members[0],
committeeID: normalCom.ID,
expectPass: true,
},
{
name: "invalid proposal",
committee: normalCom,
pubProposal: nil,
proposer: normalCom.Members[0],
committeeID: normalCom.ID,
expectPass: false,
},
{
name: "missing committee",
// no committee
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: suite.addresses[0],
committeeID: 0,
expectPass: false,
},
{
name: "not a member",
committee: normalCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: suite.addresses[4],
committeeID: normalCom.ID,
expectPass: false,
},
{
name: "not enough permissions",
committee: noPermissionsCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: noPermissionsCom.Members[0],
committeeID: noPermissionsCom.ID,
expectPass: false,
},
} }
for _, tc := range testcases { for _, tc := range testcases {
suite.Run(tc.name, func() { suite.Run(tc.name, func() {
_, err := suite.keeper.SubmitProposal(suite.ctx, tc.proposer, tc.proposal) // Create local testApp because suite doesn't run the SetupTest function for subtests, which would mean the app state is not be reset between subtests.
tApp := app.NewTestApp()
keeper := tApp.GetCommitteeKeeper()
ctx := tApp.NewContext(true, abci.Header{})
tApp.InitializeFromGenesisStates()
// setup committee (if required)
if !(reflect.DeepEqual(tc.committee, types.Committee{})) {
keeper.SetCommittee(ctx, tc.committee)
}
id, err := keeper.SubmitProposal(ctx, tc.proposer, tc.committeeID, tc.pubProposal)
if tc.expectPass { if tc.expectPass {
suite.NoError(err) suite.NoError(err)
// TODO suite.keeper.GetProposal(suite.ctx, tc.proposal.ID) _, found := keeper.GetProposal(ctx, id)
suite.True(found)
} else { } else {
suite.NotNil(err) suite.NotNil(err)
} }
@ -54,21 +120,140 @@ func (suite *KeeperTestSuite) TestSubmitProposal() {
} }
func (suite *KeeperTestSuite) TestAddVote() { func (suite *KeeperTestSuite) TestAddVote() {
normalCom := types.Committee{
ID: 12,
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
}
testcases := []struct { testcases := []struct {
name string name string
proposalID uint64 proposalID uint64
voter sdk.AccAddress voter sdk.AccAddress
expectPass bool expectPass bool
}{ }{
{name: "no proposal", proposalID: 9999999, voter: suite.addresses[0], expectPass: false}, {
name: "normal",
proposalID: types.DefaultNextProposalID,
voter: normalCom.Members[0],
expectPass: true,
},
{
name: "nonexistent proposal",
proposalID: 9999999,
voter: normalCom.Members[0],
expectPass: false,
},
{
name: "voter not committee member",
proposalID: types.DefaultNextProposalID,
voter: suite.addresses[4],
expectPass: false,
},
} }
for _, tc := range testcases { for _, tc := range testcases {
suite.Run(tc.name, func() { suite.Run(tc.name, func() {
err := suite.keeper.AddVote(suite.ctx, tc.proposalID, tc.voter) // Create local testApp because suite doesn't run the SetupTest function for subtests, which would mean the app state is not be reset between subtests.
tApp := app.NewTestApp()
keeper := tApp.GetCommitteeKeeper()
ctx := tApp.NewContext(true, abci.Header{})
tApp.InitializeFromGenesisStates()
// setup the committee and proposal
keeper.SetCommittee(ctx, normalCom)
_, err := keeper.SubmitProposal(ctx, normalCom.Members[0], normalCom.ID, gov.NewTextProposal("A Title", "A description of this proposal."))
suite.NoError(err)
err = keeper.AddVote(ctx, tc.proposalID, tc.voter)
if tc.expectPass {
suite.NoError(err)
_, found := keeper.GetVote(ctx, tc.proposalID, tc.voter)
suite.True(found)
} else {
suite.NotNil(err)
}
})
}
}
func (suite *KeeperTestSuite) TestCloseOutProposal() {
// setup test
suite.app.InitializeFromGenesisStates()
// TODO replace below with genesis state
normalCom := types.Committee{
ID: 12,
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
}
suite.keeper.SetCommittee(suite.ctx, normalCom)
pprop := gov.NewTextProposal("A Title", "A description of this proposal.")
id, err := suite.keeper.SubmitProposal(suite.ctx, normalCom.Members[0], normalCom.ID, pprop)
suite.NoError(err)
err = suite.keeper.AddVote(suite.ctx, id, normalCom.Members[0])
suite.NoError(err)
err = suite.keeper.AddVote(suite.ctx, id, normalCom.Members[1])
suite.NoError(err)
// run test
err = suite.keeper.CloseOutProposal(suite.ctx, id)
// check
suite.NoError(err)
_, found := suite.keeper.GetProposal(suite.ctx, id)
suite.False(found)
suite.keeper.IterateVotes(suite.ctx, id, func(v types.Vote) bool {
suite.Fail("found vote when none should exist")
return false
})
}
type UnregisteredProposal struct {
gov.TextProposal
}
func (UnregisteredProposal) ProposalRoute() string { return "unregistered" }
func (UnregisteredProposal) ProposalType() string { return "unregistered" }
var _ types.PubProposal = UnregisteredProposal{}
func (suite *KeeperTestSuite) TestValidatePubProposal() {
testcases := []struct {
name string
pubProposal types.PubProposal
expectPass bool
}{
{
name: "valid",
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
expectPass: true,
},
{
name: "invalid (missing title)",
pubProposal: gov.TextProposal{Description: "A description of this proposal."},
expectPass: false,
},
{
name: "invalid (unregistered)",
pubProposal: UnregisteredProposal{gov.TextProposal{Title: "A Title", Description: "A description of this proposal."}},
expectPass: false,
},
{
name: "invalid (nil)",
pubProposal: nil,
expectPass: false,
},
// TODO test case when the handler fails
}
for _, tc := range testcases {
suite.Run(tc.name, func() {
err := suite.keeper.ValidatePubProposal(suite.ctx, tc.pubProposal)
if tc.expectPass { if tc.expectPass {
suite.NoError(err) suite.NoError(err)
// TODO GetVote
} else { } else {
suite.NotNil(err) suite.NotNil(err)
} }

View File

@ -22,7 +22,7 @@ var (
// AppModuleBasic app module basics object // AppModuleBasic app module basics object
type AppModuleBasic struct{} type AppModuleBasic struct{}
// Name get module name // Name gets the module name
func (AppModuleBasic) Name() string { func (AppModuleBasic) Name() string {
return ModuleName return ModuleName
} }
@ -34,19 +34,17 @@ func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
// DefaultGenesis default genesis state // DefaultGenesis default genesis state
func (AppModuleBasic) DefaultGenesis() json.RawMessage { func (AppModuleBasic) DefaultGenesis() json.RawMessage {
//return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
return nil
} }
// ValidateGenesis module validate genesis // ValidateGenesis module validate genesis
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
// var gs GenesisState var gs GenesisState
// err := ModuleCdc.UnmarshalJSON(bz, &gs) err := ModuleCdc.UnmarshalJSON(bz, &gs)
// if err != nil { if err != nil {
// return err return err
// } }
// return gs.Validate() return gs.Validate()
return nil
} }
// RegisterRESTRoutes registers the REST routes for the module. // RegisterRESTRoutes registers the REST routes for the module.
@ -137,18 +135,17 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
// InitGenesis module init-genesis // InitGenesis module init-genesis
func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
// var genesisState GenesisState var genesisState GenesisState
// ModuleCdc.MustUnmarshalJSON(data, &genesisState) ModuleCdc.MustUnmarshalJSON(data, &genesisState)
// InitGenesis(ctx, am.keeper, am.pricefeedKeeper, genesisState) InitGenesis(ctx, am.keeper, genesisState)
return []abci.ValidatorUpdate{} return []abci.ValidatorUpdate{}
} }
// ExportGenesis module export genesis // ExportGenesis module export genesis
func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
// gs := ExportGenesis(ctx, am.keeper) gs := ExportGenesis(ctx, am.keeper)
// return ModuleCdc.MustMarshalJSON(gs) return ModuleCdc.MustMarshalJSON(gs)
return nil
} }
// BeginBlock module begin-block // BeginBlock module begin-block

View File

@ -1,6 +1,8 @@
package types package types
import "github.com/cosmos/cosmos-sdk/codec" import (
"github.com/cosmos/cosmos-sdk/codec"
)
// ModuleCdc generic sealed codec to be used throughout module // ModuleCdc generic sealed codec to be used throughout module
var ModuleCdc *codec.Codec var ModuleCdc *codec.Codec
@ -13,10 +15,10 @@ func init() {
// RegisterCodec registers the necessary types for the module // RegisterCodec registers the necessary types for the module
func RegisterCodec(cdc *codec.Codec) { func RegisterCodec(cdc *codec.Codec) {
// TODO
// cdc.RegisterConcrete(MsgCreateCDP{}, "cdp/MsgCreateCDP", nil) // TODO need to register Content interface, however amino panics if you try and register it twice and helpfully doesn't provide a way to query registered types
// cdc.RegisterConcrete(MsgDeposit{}, "cdp/MsgDeposit", nil) //cdc.RegisterInterface((*gov.Content)(nil), nil)
// cdc.RegisterConcrete(MsgWithdraw{}, "cdp/MsgWithdraw", nil)
// cdc.RegisterConcrete(MsgDrawDebt{}, "cdp/MsgDrawDebt", nil) cdc.RegisterInterface((*Permission)(nil), nil)
// cdc.RegisterConcrete(MsgRepayDebt{}, "cdp/MsgRepayDebt", nil) cdc.RegisterConcrete(GodPermission{}, "kava/GodPermission", nil)
} }

View File

@ -0,0 +1,51 @@
package types
import (
"bytes"
)
// DefaultNextProposalID is the starting poiint for proposal IDs.
const DefaultNextProposalID uint64 = 1
// GenesisState is state that must be provided at chain genesis.
type GenesisState struct {
NextProposalID uint64
Votes []Vote
Proposals []Proposal
Committees []Committee
}
// NewGenesisState returns a new genesis state object for the module.
func NewGenesisState(nextProposalID uint64, votes []Vote, proposals []Proposal, committees []Committee) GenesisState {
return GenesisState{
NextProposalID: nextProposalID,
Votes: votes,
Proposals: proposals,
Committees: committees,
}
}
// DefaultGenesisState returns the default genesis state for the module.
func DefaultGenesisState() GenesisState {
return NewGenesisState(
DefaultNextProposalID,
[]Vote{},
[]Proposal{},
[]Committee{},
)
}
// Equal checks whether two gov GenesisState structs are equivalent
func (data GenesisState) Equal(data2 GenesisState) bool {
b1 := ModuleCdc.MustMarshalBinaryBare(data)
b2 := ModuleCdc.MustMarshalBinaryBare(data2)
return bytes.Equal(b1, b2)
}
// IsEmpty returns true if a GenesisState is empty
func (data GenesisState) IsEmpty() bool {
return data.Equal(GenesisState{})
}
// Validate performs basic validation of genesis data.
func (gs GenesisState) Validate() error { return nil }

View File

@ -8,6 +8,10 @@ import (
// EXAMPLE PERMISSIONS ------------------------------ // EXAMPLE PERMISSIONS ------------------------------
type GodPermission struct{}
func (GodPermission) Allows(gov.Content) bool { return true }
// Allow only changes to inflation_rate // Allow only changes to inflation_rate
type InflationRateChangePermission struct{} type InflationRateChangePermission struct{}

View File

@ -5,6 +5,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov"
) )
var VoteThreshold sdk.Dec = sdk.MustNewDecFromStr("0.75")
// A Committee is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions. // A Committee is a collection of addresses that are allowed to vote and enact any governance proposal that passes their permissions.
type Committee struct { type Committee struct {
ID uint64 // TODO or a name? ID uint64 // TODO or a name?
@ -39,10 +41,12 @@ type Permission interface {
// GOV STUFF -------------------------- // GOV STUFF --------------------------
// Should be much the same as in gov module, except Proposals are linked to a committee ID. // Should be much the same as in gov module, except Proposals are linked to a committee ID.
var _ gov.Content = Proposal{} // TODO not needed? var _ gov.Content = Proposal{}
type PubProposal = gov.Content // TODO better name
type Proposal struct { type Proposal struct {
gov.Content PubProposal
ID uint64 ID uint64
CommitteeID uint64 CommitteeID uint64
// TODO // TODO