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

581 lines
16 KiB
Go
Raw Normal View History

2020-03-11 19:52:25 +00:00
package keeper_test
import (
"reflect"
2020-03-12 17:05:40 +00:00
"time"
2020-03-11 19:52:25 +00:00
2020-03-26 20:17:49 +00:00
"github.com/cosmos/cosmos-sdk/codec"
2020-03-11 19:52:25 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
2020-03-30 13:06:31 +00:00
"github.com/cosmos/cosmos-sdk/x/params"
2020-04-30 14:23:41 +00:00
2020-03-11 19:52:25 +00:00
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
bep3types "github.com/kava-labs/kava/x/bep3/types"
2020-04-25 16:39:59 +00:00
cdptypes "github.com/kava-labs/kava/x/cdp/types"
2020-03-26 20:17:49 +00:00
"github.com/kava-labs/kava/x/committee"
2020-03-11 19:52:25 +00:00
"github.com/kava-labs/kava/x/committee/types"
"github.com/kava-labs/kava/x/pricefeed"
2020-03-11 19:52:25 +00:00
)
func newCDPGenesisState(params cdptypes.Params) app.GenesisState {
genesis := cdptypes.DefaultGenesisState()
genesis.Params = params
return app.GenesisState{cdptypes.ModuleName: cdptypes.ModuleCdc.MustMarshalJSON(genesis)}
}
func newBep3GenesisState(params bep3types.Params) app.GenesisState {
genesis := bep3types.DefaultGenesisState()
genesis.Params = params
return app.GenesisState{bep3types.ModuleName: bep3types.ModuleCdc.MustMarshalJSON(genesis)}
}
func newPricefeedGenState(assets []string, prices []sdk.Dec) app.GenesisState {
if len(assets) != len(prices) {
panic("assets and prices must be the same length")
}
pfGenesis := pricefeed.DefaultGenesisState()
for i := range assets {
pfGenesis.Params.Markets = append(
pfGenesis.Params.Markets,
pricefeed.Market{
MarketID: assets[i] + ":usd", BaseAsset: assets[i], QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true,
})
pfGenesis.PostedPrices = append(
pfGenesis.PostedPrices,
pricefeed.PostedPrice{
MarketID: assets[i] + ":usd",
OracleAddress: sdk.AccAddress{},
Price: prices[i],
Expiry: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
})
}
return app.GenesisState{pricefeed.ModuleName: pricefeed.ModuleCdc.MustMarshalJSON(pfGenesis)}
}
2020-03-11 19:52:25 +00:00
func (suite *KeeperTestSuite) TestSubmitProposal() {
normalCom := types.Committee{
2020-04-24 23:22:56 +00:00
ID: 12,
Description: "This committee is for testing.",
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
ProposalDuration: time.Hour * 24 * 7,
2020-03-11 19:52:25 +00:00
}
2020-03-27 18:34:03 +00:00
noPermissionsCom := normalCom
noPermissionsCom.Permissions = []types.Permission{}
2020-03-11 19:52:25 +00:00
paramChangePermissionsCom := normalCom
paramChangePermissionsCom.Permissions = []types.Permission{
types.SubParamChangePermission{
AllowedParams: types.AllowedParams{
{Subspace: cdptypes.ModuleName, Key: string(cdptypes.KeyDebtThreshold)},
{Subspace: cdptypes.ModuleName, Key: string(cdptypes.KeyCollateralParams)},
},
AllowedCollateralParams: types.AllowedCollateralParams{
types.AllowedCollateralParam{
Denom: "bnb",
DebtLimit: true,
StabilityFee: true,
},
},
},
}
testCP := cdptypes.CollateralParams{{
Denom: "bnb",
LiquidationRatio: d("1.5"),
DebtLimit: c("usdx", 1000000000000),
StabilityFee: d("1.000000001547125958"), // %5 apr
LiquidationPenalty: d("0.05"),
AuctionSize: i(100),
Prefix: 0x20,
ConversionFactor: i(6),
MarketID: "bnb:usd",
}}
testCDPParams := cdptypes.DefaultParams()
testCDPParams.CollateralParams = testCP
testCDPParams.GlobalDebtLimit = testCP[0].DebtLimit
newValidCP := make(cdptypes.CollateralParams, len(testCP))
copy(newValidCP, testCP)
newValidCP[0].DebtLimit = c("usdx", 500000000000)
newInvalidCP := make(cdptypes.CollateralParams, len(testCP))
copy(newInvalidCP, testCP)
newInvalidCP[0].MarketID = "btc:usd"
2020-03-11 19:52:25 +00:00
testcases := []struct {
name string
committee types.Committee
pubProposal types.PubProposal
proposer sdk.AccAddress
committeeID uint64
2020-04-25 16:39:59 +00:00
expectErr bool
2020-03-11 19:52:25 +00:00
}{
{
name: "normal text proposal",
2020-03-11 19:52:25 +00:00
committee: normalCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: normalCom.Members[0],
committeeID: normalCom.ID,
2020-04-25 16:39:59 +00:00
expectErr: false,
2020-03-11 19:52:25 +00:00
},
{
name: "normal param change proposal",
committee: normalCom,
pubProposal: params.NewParameterChangeProposal(
"A Title", "A description of this proposal.",
[]params.ParamChange{
{
Subspace: "cdp", Key: string(cdptypes.KeyDebtThreshold), Value: string(suite.app.Codec().MustMarshalJSON(i(1000000))),
},
},
),
proposer: normalCom.Members[0],
committeeID: normalCom.ID,
expectErr: false,
},
2020-03-11 19:52:25 +00:00
{
name: "invalid proposal",
committee: normalCom,
pubProposal: nil,
proposer: normalCom.Members[0],
committeeID: normalCom.ID,
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "missing committee",
// no committee
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: suite.addresses[0],
committeeID: 0,
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "not a member",
committee: normalCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: suite.addresses[4],
committeeID: normalCom.ID,
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "not enough permissions",
committee: noPermissionsCom,
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
proposer: noPermissionsCom.Members[0],
committeeID: noPermissionsCom.ID,
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "valid sub param change",
committee: paramChangePermissionsCom,
pubProposal: params.NewParameterChangeProposal(
"A Title", "A description of this proposal.",
[]params.ParamChange{
{
"cdp", string(cdptypes.KeyDebtThreshold), string(suite.app.Codec().MustMarshalJSON(i(1000000000))),
},
{
"cdp", string(cdptypes.KeyCollateralParams), string(suite.app.Codec().MustMarshalJSON(newValidCP)),
},
},
),
proposer: paramChangePermissionsCom.Members[0],
committeeID: paramChangePermissionsCom.ID,
expectErr: false,
},
{
name: "invalid sub param change permission",
committee: paramChangePermissionsCom,
pubProposal: params.NewParameterChangeProposal(
"A Title", "A description of this proposal.",
[]params.ParamChange{
{
"cdp", string(cdptypes.KeyDebtThreshold), string(suite.app.Codec().MustMarshalJSON(i(1000000000))),
},
{
"cdp", string(cdptypes.KeyCollateralParams), string(suite.app.Codec().MustMarshalJSON(newInvalidCP)),
},
},
),
proposer: paramChangePermissionsCom.Members[0],
committeeID: paramChangePermissionsCom.ID,
expectErr: true,
},
2020-03-11 19:52:25 +00:00
}
for _, tc := range testcases {
suite.Run(tc.name, func() {
2020-03-30 13:06:31 +00:00
// Create local testApp because suite doesn't run the SetupTest function for subtests
2020-03-11 19:52:25 +00:00
tApp := app.NewTestApp()
keeper := tApp.GetCommitteeKeeper()
ctx := tApp.NewContext(true, abci.Header{})
tApp.InitializeFromGenesisStates(
newPricefeedGenState([]string{"bnb"}, []sdk.Dec{d("15.01")}),
newCDPGenesisState(testCDPParams),
)
2020-03-11 19:52:25 +00:00
// 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)
2020-04-25 16:39:59 +00:00
if tc.expectErr {
suite.NotNil(err)
} else {
2020-03-11 19:52:25 +00:00
suite.NoError(err)
2020-03-12 17:05:40 +00:00
pr, found := keeper.GetProposal(ctx, id)
2020-03-11 19:52:25 +00:00
suite.True(found)
2020-03-12 17:05:40 +00:00
suite.Equal(tc.committeeID, pr.CommitteeID)
2020-04-24 23:22:56 +00:00
suite.Equal(ctx.BlockTime().Add(tc.committee.ProposalDuration), pr.Deadline)
2020-03-11 19:52:25 +00:00
}
})
}
}
func (suite *KeeperTestSuite) TestAddVote() {
normalCom := types.Committee{
ID: 12,
Members: suite.addresses[:2],
Permissions: []types.Permission{types.GodPermission{}},
}
2020-03-12 17:05:40 +00:00
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
2020-03-11 19:52:25 +00:00
testcases := []struct {
name string
proposalID uint64
voter sdk.AccAddress
2020-03-12 17:05:40 +00:00
voteTime time.Time
2020-04-25 16:39:59 +00:00
expectErr bool
2020-03-11 19:52:25 +00:00
}{
{
name: "normal",
proposalID: types.DefaultNextProposalID,
voter: normalCom.Members[0],
2020-04-25 16:39:59 +00:00
expectErr: false,
2020-03-11 19:52:25 +00:00
},
{
name: "nonexistent proposal",
proposalID: 9999999,
voter: normalCom.Members[0],
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "voter not committee member",
proposalID: types.DefaultNextProposalID,
voter: suite.addresses[4],
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
2020-03-12 17:05:40 +00:00
{
name: "proposal expired",
proposalID: types.DefaultNextProposalID,
voter: normalCom.Members[0],
2020-04-24 23:22:56 +00:00
voteTime: firstBlockTime.Add(normalCom.ProposalDuration),
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-12 17:05:40 +00:00
},
2020-03-11 19:52:25 +00:00
}
for _, tc := range testcases {
suite.Run(tc.name, func() {
2020-03-30 13:06:31 +00:00
// Create local testApp because suite doesn't run the SetupTest function for subtests
2020-03-11 19:52:25 +00:00
tApp := app.NewTestApp()
keeper := tApp.GetCommitteeKeeper()
2020-03-12 17:05:40 +00:00
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
2020-03-11 19:52:25 +00:00
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)
2020-03-12 17:05:40 +00:00
ctx = ctx.WithBlockTime(tc.voteTime)
2020-03-11 19:52:25 +00:00
err = keeper.AddVote(ctx, tc.proposalID, tc.voter)
2020-04-25 16:39:59 +00:00
if tc.expectErr {
suite.NotNil(err)
} else {
2020-03-11 19:52:25 +00:00
suite.NoError(err)
_, found := keeper.GetVote(ctx, tc.proposalID, tc.voter)
suite.True(found)
}
})
}
}
2020-03-26 20:17:49 +00:00
func (suite *KeeperTestSuite) TestGetProposalResult() {
2020-03-27 18:34:03 +00:00
normalCom := types.Committee{
2020-04-24 23:22:56 +00:00
ID: 12,
Description: "This committee is for testing.",
Members: suite.addresses[:5],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
ProposalDuration: time.Hour * 24 * 7,
2020-03-27 18:34:03 +00:00
}
2020-03-26 20:17:49 +00:00
var defaultID uint64 = 1
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
testcases := []struct {
name string
committee types.Committee
votes []types.Vote
proposalPasses bool
2020-04-25 16:39:59 +00:00
expectErr bool
2020-03-26 20:17:49 +00:00
}{
{
2020-03-27 18:34:03 +00:00
name: "enough votes",
committee: normalCom,
2020-03-26 20:17:49 +00:00
votes: []types.Vote{
{ProposalID: defaultID, Voter: suite.addresses[0]},
{ProposalID: defaultID, Voter: suite.addresses[1]},
{ProposalID: defaultID, Voter: suite.addresses[2]},
{ProposalID: defaultID, Voter: suite.addresses[3]},
},
proposalPasses: true,
2020-04-25 16:39:59 +00:00
expectErr: false,
2020-03-26 20:17:49 +00:00
},
{
2020-03-27 18:34:03 +00:00
name: "not enough votes",
committee: normalCom,
2020-03-26 20:17:49 +00:00
votes: []types.Vote{
{ProposalID: defaultID, Voter: suite.addresses[0]},
},
proposalPasses: false,
2020-04-25 16:39:59 +00:00
expectErr: false,
2020-03-26 20:17:49 +00:00
},
2020-03-11 19:52:25 +00:00
}
2020-03-26 20:17:49 +00:00
for _, tc := range testcases {
suite.Run(tc.name, func() {
2020-03-30 13:06:31 +00:00
// Create local testApp because suite doesn't run the SetupTest function for subtests
2020-03-26 20:17:49 +00:00
tApp := app.NewTestApp()
keeper := tApp.GetCommitteeKeeper()
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
tApp.InitializeFromGenesisStates(
committeeGenState(
tApp.Codec(),
[]types.Committee{tc.committee},
[]types.Proposal{{
PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
ID: defaultID,
CommitteeID: tc.committee.ID,
Deadline: firstBlockTime.Add(time.Hour * 24 * 7),
}},
tc.votes,
),
)
proposalPasses, err := keeper.GetProposalResult(ctx, defaultID)
2020-04-25 16:39:59 +00:00
if tc.expectErr {
suite.NotNil(err)
} else {
2020-03-26 20:17:49 +00:00
suite.NoError(err)
suite.Equal(tc.proposalPasses, proposalPasses)
}
})
}
}
func committeeGenState(cdc *codec.Codec, committees []types.Committee, proposals []types.Proposal, votes []types.Vote) app.GenesisState {
gs := types.NewGenesisState(
uint64(len(proposals)+1),
committees,
proposals,
votes,
)
return app.GenesisState{committee.ModuleName: cdc.MustMarshalJSON(gs)}
2020-03-11 19:52:25 +00:00
}
2020-04-27 12:57:47 +00:00
type UnregisteredPubProposal struct {
2020-03-11 19:52:25 +00:00
gov.TextProposal
}
2020-04-27 12:57:47 +00:00
func (UnregisteredPubProposal) ProposalRoute() string { return "unregistered" }
func (UnregisteredPubProposal) ProposalType() string { return "unregistered" }
2020-03-11 19:52:25 +00:00
2020-04-27 12:57:47 +00:00
var _ types.PubProposal = UnregisteredPubProposal{}
2020-03-11 19:52:25 +00:00
func (suite *KeeperTestSuite) TestValidatePubProposal() {
testcases := []struct {
name string
pubProposal types.PubProposal
2020-04-25 16:39:59 +00:00
expectErr bool
2020-03-11 19:52:25 +00:00
}{
{
2020-04-25 16:39:59 +00:00
name: "valid (text proposal)",
2020-03-11 19:52:25 +00:00
pubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
2020-04-25 16:39:59 +00:00
expectErr: false,
},
{
name: "valid (param change proposal)",
pubProposal: params.NewParameterChangeProposal(
"Change the debt limit",
"This proposal changes the debt limit of the cdp module.",
[]params.ParamChange{{
Subspace: cdptypes.ModuleName,
Key: string(cdptypes.KeyGlobalDebtLimit),
Value: string(types.ModuleCdc.MustMarshalJSON(c("usdx", 100000000000))),
2020-04-25 16:39:59 +00:00
}},
),
expectErr: false,
2020-03-11 19:52:25 +00:00
},
{
name: "invalid (missing title)",
pubProposal: gov.TextProposal{Description: "A description of this proposal."},
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "invalid (unregistered)",
2020-04-27 12:57:47 +00:00
pubProposal: UnregisteredPubProposal{gov.TextProposal{Title: "A Title", Description: "A description of this proposal."}},
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
{
name: "invalid (nil)",
pubProposal: nil,
2020-04-25 16:39:59 +00:00
expectErr: true,
2020-03-11 19:52:25 +00:00
},
2020-03-30 13:06:31 +00:00
{
name: "invalid (proposal handler fails)",
pubProposal: params.NewParameterChangeProposal(
"A Title",
"A description of this proposal.",
[]params.ParamChange{{
2020-04-25 16:39:59 +00:00
Subspace: "nonsense-subspace",
Key: "nonsense-key",
Value: "nonsense-value",
2020-03-30 13:06:31 +00:00
}},
),
2020-04-25 16:39:59 +00:00
expectErr: true,
},
{
name: "invalid (proposal handler panics)",
pubProposal: params.NewParameterChangeProposal(
"A Title",
"A description of this proposal.",
[]params.ParamChange{{
Subspace: cdptypes.ModuleName,
Key: "nonsense-key", // a valid Subspace but invalid Key will trigger a panic in the paramchange propsal handler
Value: "nonsense-value",
}},
),
expectErr: true,
2020-03-30 13:06:31 +00:00
},
2020-04-26 14:28:57 +00:00
{
name: "invalid (proposal handler fails - invalid json)",
pubProposal: params.NewParameterChangeProposal(
"A Title",
"A description of this proposal.",
[]params.ParamChange{{
Subspace: cdptypes.ModuleName,
Key: string(cdptypes.KeyGlobalDebtLimit),
Value: `{"denom": "usdx",`,
}},
),
expectErr: true,
},
2020-03-11 19:52:25 +00:00
}
for _, tc := range testcases {
suite.Run(tc.name, func() {
err := suite.keeper.ValidatePubProposal(suite.ctx, tc.pubProposal)
2020-04-25 16:39:59 +00:00
if tc.expectErr {
2020-03-11 19:52:25 +00:00
suite.NotNil(err)
2020-04-25 16:39:59 +00:00
} else {
suite.NoError(err)
2020-03-11 19:52:25 +00:00
}
})
}
}
2020-04-24 22:15:51 +00:00
func (suite *KeeperTestSuite) TestCloseExpiredProposals() {
// Setup test state
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
2020-04-24 23:22:56 +00:00
testGenesis := types.NewGenesisState(
2020-04-24 22:15:51 +00:00
3,
[]types.Committee{
{
2020-04-24 23:22:56 +00:00
ID: 1,
Description: "This committee is for testing.",
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
ProposalDuration: time.Hour * 24 * 7,
2020-04-24 22:15:51 +00:00
},
{
2020-04-24 23:22:56 +00:00
ID: 2,
Members: suite.addresses[2:],
Permissions: nil,
VoteThreshold: d("0.667"),
ProposalDuration: time.Hour * 24 * 7,
2020-04-24 22:15:51 +00:00
},
},
[]types.Proposal{
{
ID: 1,
CommitteeID: 1,
PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
Deadline: firstBlockTime.Add(7 * 24 * time.Hour),
},
{
ID: 2,
CommitteeID: 1,
PubProposal: gov.NewTextProposal("Another Title", "A description of this other proposal."),
Deadline: firstBlockTime.Add(21 * 24 * time.Hour),
},
},
[]types.Vote{
{ProposalID: 1, Voter: suite.addresses[0]},
{ProposalID: 1, Voter: suite.addresses[1]},
{ProposalID: 2, Voter: suite.addresses[2]},
},
)
suite.app.InitializeFromGenesisStates(
2020-04-24 23:22:56 +00:00
NewCommitteeGenesisState(suite.app.Codec(), testGenesis),
2020-04-24 22:15:51 +00:00
)
// close proposals
2020-04-24 23:22:56 +00:00
ctx := suite.app.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
2020-04-24 22:15:51 +00:00
suite.keeper.CloseExpiredProposals(ctx)
// check
for _, p := range testGenesis.Proposals {
2020-04-24 23:22:56 +00:00
_, found := suite.keeper.GetProposal(ctx, p.ID)
2020-04-24 22:15:51 +00:00
votes := getProposalVoteMap(suite.keeper, ctx)
if ctx.BlockTime().After(p.Deadline) {
suite.False(found)
suite.Empty(votes[p.ID])
} else {
suite.True(found)
suite.NotEmpty(votes[p.ID])
}
}
// close (later time)
2020-04-24 23:22:56 +00:00
ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime.Add(7 * 24 * time.Hour)})
2020-04-24 22:15:51 +00:00
suite.keeper.CloseExpiredProposals(ctx)
// check
for _, p := range testGenesis.Proposals {
2020-04-24 23:22:56 +00:00
_, found := suite.keeper.GetProposal(ctx, p.ID)
2020-04-24 22:15:51 +00:00
votes := getProposalVoteMap(suite.keeper, ctx)
2020-04-24 23:22:56 +00:00
if ctx.BlockTime().Equal(p.Deadline) || ctx.BlockTime().After(p.Deadline) {
2020-04-24 22:15:51 +00:00
suite.False(found)
suite.Empty(votes[p.ID])
} else {
suite.True(found)
suite.NotEmpty(votes[p.ID])
}
}
}