0g-chain/x/committee/handler_test.go

165 lines
4.5 KiB
Go
Raw Normal View History

2020-04-26 14:28:57 +00:00
package committee_test
import (
"testing"
"time"
2020-04-30 14:23:41 +00:00
"github.com/stretchr/testify/suite"
2020-04-26 14:28:57 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/upgrade"
2020-04-30 14:23:41 +00:00
2020-04-26 14:28:57 +00:00
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
cdptypes "github.com/kava-labs/kava/x/cdp/types"
"github.com/kava-labs/kava/x/committee"
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
)
// NewDistributionGenesisWithPool creates a default distribution genesis state with some coins in the community pool.
func NewDistributionGenesisWithPool(communityPoolCoins sdk.Coins) app.GenesisState {
gs := distribution.DefaultGenesisState()
2020-04-27 18:19:05 +00:00
gs.FeePool = distribution.FeePool{CommunityPool: sdk.NewDecCoinsFromCoins(communityPoolCoins...)}
2020-04-26 14:28:57 +00:00
return app.GenesisState{distribution.ModuleName: distribution.ModuleCdc.MustMarshalJSON(gs)}
}
type HandlerTestSuite struct {
suite.Suite
app app.TestApp
keeper keeper.Keeper
handler sdk.Handler
ctx sdk.Context
addresses []sdk.AccAddress
communityPoolAmt sdk.Coins
}
func (suite *HandlerTestSuite) SetupTest() {
_, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper()
suite.handler = committee.NewHandler(suite.keeper)
firstBlockTime := time.Date(1998, time.January, 1, 1, 0, 0, 0, time.UTC)
testGenesis := types.NewGenesisState(
3,
[]types.Committee{
Token holder governance (#917) * Committee types (#899) * committee types * refactor to committee interface * include tokencommitee stringer method * add members to BaseCommittee * address revisions * update querier * update querier * fix compilation errors, tests, etc. * Update MsgVote with vote type (#900) * add vote to msg * update querier/rest * update example cli vote msg * remove incorrect comments * address revisions * update handler, stub keeper method * add vote type to vote struct * Committee module keeper logic for token holder governance (#902) * fix keeper/test compilation errors * fix keeper/test compilation errors pt 2 * add setters to committee interface * fix sims compilation errors * fix incentive tests compilation errors * update types, expected keepers * core keeper logic * don't allow bond denom * implement vote tallying * query proposal polling status * update module keepers in app.go * register committee interface * fix failing incentive test * commitee types tests * refactor GetProposalResult by committee types * update invariants * implement most proposal keeper tests * add nulls to custom enums * remove abstain vote type * add test for close proposal * remove outdated TODOs * update ProcessProposals * switch on committee type directly * reintroduce Abstain votes and update vote tallying * don't allow divide by 0 panics * delete unused setters on committee interface * clean up tally methods return values for querier * update enum validation to catch negative ints * reintroduce setters for sims compilation * address revisions * remove commented out test * implement ProcessProposals test * additional revisions * Committee migrations (#909) * add committee v14 legacy types * update migration imports for compile * addRegisterCodec() to committee v14 legacy types * migrate committee genesis state from v14 to v15 * set stability committee permissions properly * fix committee allowed params * migration test, kava-7 sample data * add concrete types to committees (#911) * revisions: migrate + tests * register msgs on legacy codec * Prepare Committee module for migrations (#906) * remove invariants * edits * fix abci test * fix keeper querier tests * add committee interface registration * use codec.Codec * don't allow null vote types * don't allow null tally option * minor spelling fixes * update example cli proposal * fix cli tally query * enable vote abstain from cli * include vote options in cli help text * call CloseProposal from handler * custom enum marshaling * committee: fix failing tests (#921) * fix failing tests * fix: spelling Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
2021-06-07 16:08:03 +00:00
types.MemberCommittee{
BaseCommittee: types.BaseCommittee{
ID: 1,
Description: "This committee is for testing.",
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.5"),
ProposalDuration: time.Hour * 24 * 7,
TallyOption: types.FirstPastThePost,
},
2020-04-26 14:28:57 +00:00
},
},
[]types.Proposal{},
[]types.Vote{},
)
suite.communityPoolAmt = cs(c("ukava", 1000))
suite.app.InitializeFromGenesisStates(
NewCommitteeGenesisState(suite.app.Codec(), testGenesis),
NewDistributionGenesisWithPool(suite.communityPoolAmt),
)
suite.ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: firstBlockTime})
}
func (suite *HandlerTestSuite) TestSubmitProposalMsg_Valid() {
msg := committee.NewMsgSubmitProposal(
params.NewParameterChangeProposal(
"A Title",
"A description of this proposal.",
[]params.ParamChange{{
Subspace: cdptypes.ModuleName,
Key: string(cdptypes.KeyDebtThreshold),
Value: string(types.ModuleCdc.MustMarshalJSON(i(1000000))),
}},
),
suite.addresses[0],
1,
)
2020-04-27 18:19:05 +00:00
res, err := suite.handler(suite.ctx, msg)
2020-04-26 14:28:57 +00:00
2020-04-27 18:19:05 +00:00
suite.NoError(err)
2020-04-26 14:28:57 +00:00
_, found := suite.keeper.GetProposal(suite.ctx, types.Uint64FromBytes(res.Data))
suite.True(found)
}
func (suite *HandlerTestSuite) TestSubmitProposalMsg_Invalid() {
2020-04-27 14:04:47 +00:00
var committeeID uint64 = 1
2020-04-26 14:28:57 +00:00
msg := types.NewMsgSubmitProposal(
params.NewParameterChangeProposal(
"A Title",
"A description of this proposal.",
[]params.ParamChange{{
Subspace: cdptypes.ModuleName,
Key: "nonsense-key",
Value: "nonsense-value",
}},
),
suite.addresses[0],
2020-04-27 14:04:47 +00:00
committeeID,
2020-04-26 14:28:57 +00:00
)
2020-04-27 18:19:05 +00:00
_, err := suite.handler(suite.ctx, msg)
2020-04-26 14:28:57 +00:00
2020-04-27 18:19:05 +00:00
suite.Error(err)
2020-04-27 14:04:47 +00:00
suite.Empty(
suite.keeper.GetProposalsByCommittee(suite.ctx, committeeID),
"proposal found when none should exist",
)
2020-04-26 14:28:57 +00:00
}
func (suite *HandlerTestSuite) TestSubmitProposalMsg_ValidUpgrade() {
msg := committee.NewMsgSubmitProposal(
upgrade.NewSoftwareUpgradeProposal(
2020-04-26 14:28:57 +00:00
"A Title",
"A description of this proposal.",
upgrade.Plan{
Name: "emergency-shutdown-1", // identifier for the upgrade
Time: suite.ctx.BlockTime().Add(time.Minute * 10), // time after which to implement plan
Info: "Some information about the shutdown.",
},
2020-04-26 14:28:57 +00:00
),
suite.addresses[0],
1,
)
res, err := suite.handler(suite.ctx, msg)
2020-04-26 14:28:57 +00:00
2020-04-27 18:19:05 +00:00
suite.NoError(err)
_, found := suite.keeper.GetProposal(suite.ctx, types.Uint64FromBytes(res.Data))
suite.True(found)
2020-04-26 14:28:57 +00:00
}
func (suite *HandlerTestSuite) TestSubmitProposalMsg_Unregistered() {
var committeeID uint64 = 1
2020-04-26 14:28:57 +00:00
msg := types.NewMsgSubmitProposal(
UnregisteredPubProposal{},
2020-04-26 14:28:57 +00:00
suite.addresses[0],
committeeID,
2020-04-26 14:28:57 +00:00
)
_, err := suite.handler(suite.ctx, msg)
2020-04-26 14:28:57 +00:00
suite.Error(err)
2020-04-27 14:04:47 +00:00
suite.Empty(
suite.keeper.GetProposalsByCommittee(suite.ctx, committeeID),
"proposal found when none should exist",
2020-04-27 14:04:47 +00:00
)
2020-04-26 14:28:57 +00:00
}
func TestHandlerTestSuite(t *testing.T) {
suite.Run(t, new(HandlerTestSuite))
}