add keeper delete methods

This commit is contained in:
rhuairahrighairigh 2020-03-10 23:28:25 +00:00
parent a145846ed2
commit 8c64fd3750
2 changed files with 46 additions and 1 deletions

View File

@ -71,6 +71,7 @@ func (k Keeper) AddVote(ctx sdk.Context, msg types.MsgVote) sdk.Error {
return nil return nil
} }
// GetCommittee gets a committee from the store.
func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) { func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committee, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
bz := store.Get(types.GetKeyFromID(committeeID)) bz := store.Get(types.GetKeyFromID(committeeID))
@ -82,12 +83,20 @@ func (k Keeper) GetCommittee(ctx sdk.Context, committeeID uint64) (types.Committ
return committee, true return committee, true
} }
// SetCommittee puts a committee into the store.
func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) { func (k Keeper) SetCommittee(ctx sdk.Context, committee types.Committee) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.CommitteeKeyPrefix)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(committee) bz := k.cdc.MustMarshalBinaryLengthPrefixed(committee)
store.Set(types.GetKeyFromID(committee.ID), bz) 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) { func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
bz := store.Get(types.GetKeyFromID(proposalID)) bz := store.Get(types.GetKeyFromID(proposalID))
@ -99,12 +108,20 @@ func (k Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (types.Proposal,
return proposal, true return proposal, true
} }
// SetProposal puts a proposal into the store.
func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) { func (k Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProposalKeyPrefix)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(proposal) bz := k.cdc.MustMarshalBinaryLengthPrefixed(proposal)
store.Set(types.GetKeyFromID(proposal.ID), bz) 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) { 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)
bz := store.Get(types.GetVoteKey(proposalID, voter)) 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 return vote, true
} }
// SetVote puts a vote into the store.
func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) { func (k Keeper) SetVote(ctx sdk.Context, vote types.Vote) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.VoteKeyPrefix)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(vote) bz := k.cdc.MustMarshalBinaryLengthPrefixed(vote)
store.Set(types.GetVoteKey(vote.ProposalID, vote.Voter), bz) 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))
}

View File

@ -31,7 +31,7 @@ func (suite *KeeperTestSuite) SetupTest() {
_, suite.addresses = app.GeneratePrivKeyAddressPairs(2) _, suite.addresses = app.GeneratePrivKeyAddressPairs(2)
} }
func (suite *KeeperTestSuite) TestGetSetCommittee() { func (suite *KeeperTestSuite) TestGetSetDeleteCommittee() {
// test setup // test setup
com := types.Committee{ com := types.Committee{
ID: 12, ID: 12,
@ -45,6 +45,13 @@ func (suite *KeeperTestSuite) TestGetSetCommittee() {
// check before and after match // check before and after match
suite.True(found) suite.True(found)
suite.Equal(com, readCommittee) 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() { func (suite *KeeperTestSuite) TestGetSetProposal() {
@ -61,6 +68,13 @@ func (suite *KeeperTestSuite) TestGetSetProposal() {
// check before and after match // check before and after match
suite.True(found) suite.True(found)
suite.Equal(prop, readProposal) 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() { func (suite *KeeperTestSuite) TestGetSetVote() {
@ -78,6 +92,13 @@ func (suite *KeeperTestSuite) TestGetSetVote() {
// check before and after match // check before and after match
suite.True(found) suite.True(found)
suite.Equal(vote, readVote) 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) { func TestKeeperTestSuite(t *testing.T) {