From 8c64fd375044a1f5aeb68e86d52c97f411eac408 Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Tue, 10 Mar 2020 23:28:25 +0000 Subject: [PATCH] add keeper delete methods --- x/committee/keeper/keeper.go | 24 ++++++++++++++++++++++++ x/committee/keeper/keeper_test.go | 23 ++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/x/committee/keeper/keeper.go b/x/committee/keeper/keeper.go index 3e403955..55b3c875 100644 --- a/x/committee/keeper/keeper.go +++ b/x/committee/keeper/keeper.go @@ -71,6 +71,7 @@ func (k Keeper) AddVote(ctx sdk.Context, msg types.MsgVote) sdk.Error { return nil } +// GetCommittee gets a committee from the store. func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix) bz := store.Get(types.GetKeyFromID(committeeID)) @@ -82,12 +83,20 @@ func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committ return committee, true } +// SetCommittee puts a committee into the store. func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix) bz := k.cdc.MustMarshalBinaryLengthPrefixed(committee) store.Set(types.GetKeyFromID(committee.ID), bz) } +// DeleteCommittee removes a committee from the store. +func (k Keeper) DeleteCommittee(ctx sdk.Context, committeeID uint64) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix) + store.Delete(types.GetKeyFromID(committeeID)) +} + +// GetProposal gets a proposal from the store. func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix) bz := store.Get(types.GetKeyFromID(proposalID)) @@ -99,12 +108,20 @@ func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, return proposal, true } +// SetProposal puts a proposal into the store. func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix) bz := k.cdc.MustMarshalBinaryLengthPrefixed(proposal) store.Set(types.GetKeyFromID(proposal.ID), bz) } +// DeleteProposal removes a proposal from the store. +func (k Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix) + store.Delete(types.GetKeyFromID(proposalID)) +} + +// GetVote gets a vote from the store. func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (types.Vote, bool) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix) bz := store.Get(types.GetVoteKey(proposalID, voter)) @@ -116,8 +133,15 @@ func (k Keeper) GetVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress return vote, true } +// SetVote puts a vote into the store. func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix) bz := k.cdc.MustMarshalBinaryLengthPrefixed(vote) store.Set(types.GetVoteKey(vote.ProposalID, vote.Voter), bz) } + +// DeleteVote removes a Vote from the store. +func (k Keeper) DeleteVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix) + store.Delete(types.GetVoteKey(proposalID, voter)) +} diff --git a/x/committee/keeper/keeper_test.go b/x/committee/keeper/keeper_test.go index 90f5556d..e4fec90a 100644 --- a/x/committee/keeper/keeper_test.go +++ b/x/committee/keeper/keeper_test.go @@ -31,7 +31,7 @@ func (suite *KeeperTestSuite) SetupTest() { _, suite.addresses = app.GeneratePrivKeyAddressPairs(2) } -func (suite *KeeperTestSuite) TestGetSetCommittee() { +func (suite *KeeperTestSuite) TestGetSetDeleteCommittee() { // test setup com := types.Committee{ ID: 12, @@ -45,6 +45,13 @@ func (suite *KeeperTestSuite) TestGetSetCommittee() { // check before and after match suite.True(found) suite.Equal(com, readCommittee) + + // delete from store + suite.keeper.DeleteCommittee(suite.ctx, com.ID) + + // check does not exist + _, found = suite.keeper.GetCommittee(suite.ctx, com.ID) + suite.False(found) } func (suite *KeeperTestSuite) TestGetSetProposal() { @@ -61,6 +68,13 @@ func (suite *KeeperTestSuite) TestGetSetProposal() { // check before and after match suite.True(found) suite.Equal(prop, readProposal) + + // delete from store + suite.keeper.DeleteProposal(suite.ctx, prop.ID) + + // check does not exist + _, found = suite.keeper.GetProposal(suite.ctx, prop.ID) + suite.False(found) } func (suite *KeeperTestSuite) TestGetSetVote() { @@ -78,6 +92,13 @@ func (suite *KeeperTestSuite) TestGetSetVote() { // check before and after match suite.True(found) suite.Equal(vote, readVote) + + // delete from store + suite.keeper.DeleteVote(suite.ctx, vote.ProposalID, vote.Voter) + + // check does not exist + _, found = suite.keeper.GetVote(suite.ctx, vote.ProposalID, vote.Voter) + suite.False(found) } func TestKeeperTestSuite(t *testing.T) {