-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not allow to answer a poll after voting period ends (#964)
## Description This PR fixes a bug within the `x/posts` module that currently allows users to answer a poll even when it's already ended. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html) - [x] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
Showing
9 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changeset/entries/62bf6c9bac7b77295632276f7e66130725d7fec458bd73f5f195daea2905009f.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: fix | ||
module: x/posts | ||
pull_request: 964 | ||
description: Do not allow to answer a poll after voting period ends | ||
backward_compatible: false | ||
date: 2022-07-08T05:38:47.837745373Z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package v4 | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/desmos-labs/desmos/v4/x/posts/types" | ||
) | ||
|
||
// MigrateStore performs the migration from version 3 to version 4 of the store. | ||
// To do this, it iterates over all the polls, and removes from the store the user answers for | ||
// polls that are already ended (and thus should have not accepted new answers). | ||
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { | ||
store := ctx.KVStore(storeKey) | ||
return removeInvalidPollAnswers(store, cdc) | ||
} | ||
|
||
// removeInvalidPollAnswers iterates over all the polls and deletes the user answers for all the polls which | ||
// final results already been tallied. This is to delete all the answers that have been added after that. | ||
func removeInvalidPollAnswers(store sdk.KVStore, cdc codec.BinaryCodec) error { | ||
attachmentStore := prefix.NewStore(store, types.AttachmentPrefix) | ||
attachmentsIterator := attachmentStore.Iterator(nil, nil) | ||
defer attachmentsIterator.Close() | ||
|
||
for ; attachmentsIterator.Valid(); attachmentsIterator.Next() { | ||
// Get the attachment | ||
var attachment types.Attachment | ||
err := cdc.Unmarshal(attachmentsIterator.Value(), &attachment) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Check if the attachment represents a poll and the final results have already been tallied | ||
if poll, ok := attachment.Content.GetCachedValue().(*types.Poll); ok && poll.FinalTallyResults != nil { | ||
// Remove all the answers that might still be there | ||
err = removePollAnswers(store, attachment.SubspaceID, attachment.PostID, attachment.ID) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// removePollAnswers removes from the store the answers related to the given poll | ||
func removePollAnswers(store sdk.KVStore, subspaceID uint64, postID uint64, pollID uint32) error { | ||
answersStore := prefix.NewStore(store, types.PollAnswersPrefix(subspaceID, postID, pollID)) | ||
answersIterator := answersStore.Iterator(nil, nil) | ||
|
||
// Get the answers | ||
var keys [][]byte | ||
for ; answersIterator.Valid(); answersIterator.Next() { | ||
user := string(answersIterator.Key()) | ||
keys = append(keys, types.PollAnswerStoreKey(subspaceID, postID, pollID, user)) | ||
} | ||
|
||
// Close the iterator to avoid any conflict | ||
err := answersIterator.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Delete the various answers | ||
for _, key := range keys { | ||
store.Delete(key) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package v4_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
v4 "github.com/desmos-labs/desmos/v4/x/posts/legacy/v4" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/desmos-labs/desmos/v4/app" | ||
"github.com/desmos-labs/desmos/v4/testutil/storetesting" | ||
"github.com/desmos-labs/desmos/v4/x/posts/types" | ||
subspacestypes "github.com/desmos-labs/desmos/v4/x/subspaces/types" | ||
) | ||
|
||
func TestMigrateStore(t *testing.T) { | ||
cdc, _ := app.MakeCodecs() | ||
|
||
// Build all the necessary keys | ||
keys := sdk.NewKVStoreKeys(paramstypes.StoreKey, subspacestypes.StoreKey, types.StoreKey) | ||
tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) | ||
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey) | ||
|
||
testCases := []struct { | ||
name string | ||
store func(ctx sdk.Context) | ||
shouldErr bool | ||
check func(ctx sdk.Context) | ||
}{ | ||
{ | ||
name: "user answers are deleted properly for tallied poll", | ||
store: func(ctx sdk.Context) { | ||
poll := types.NewAttachment(1, 1, 1, types.NewPoll( | ||
"What animal is best?", | ||
[]types.Poll_ProvidedAnswer{ | ||
types.NewProvidedAnswer("Cat", nil), | ||
types.NewProvidedAnswer("Dog", nil), | ||
}, | ||
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), | ||
false, | ||
false, | ||
types.NewPollTallyResults([]types.PollTallyResults_AnswerResult{ | ||
types.NewAnswerResult(0, 1), | ||
}), | ||
)) | ||
|
||
userAnswer := types.NewUserAnswer( | ||
1, | ||
1, | ||
1, | ||
[]uint32{1}, | ||
"cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw", | ||
) | ||
|
||
store := ctx.KVStore(keys[types.StoreKey]) | ||
store.Set(types.AttachmentStoreKey(1, 1, 1), cdc.MustMarshal(&poll)) | ||
store.Set(types.PollAnswerStoreKey(1, 1, 1, "cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw"), cdc.MustMarshal(&userAnswer)) | ||
}, | ||
shouldErr: false, | ||
check: func(ctx sdk.Context) { | ||
store := ctx.KVStore(keys[types.StoreKey]) | ||
|
||
var stored types.Attachment | ||
err := cdc.Unmarshal(store.Get(types.AttachmentStoreKey(1, 1, 1)), &stored) | ||
require.NoError(t, err) | ||
require.Equal(t, types.NewAttachment(1, 1, 1, types.NewPoll( | ||
"What animal is best?", | ||
[]types.Poll_ProvidedAnswer{ | ||
types.NewProvidedAnswer("Cat", nil), | ||
types.NewProvidedAnswer("Dog", nil), | ||
}, | ||
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), | ||
false, | ||
false, | ||
types.NewPollTallyResults([]types.PollTallyResults_AnswerResult{ | ||
types.NewAnswerResult(0, 1), | ||
}), | ||
)), stored) | ||
|
||
require.False(t, store.Has(types.PollAnswerStoreKey(1, 1, 1, "cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw"))) | ||
}, | ||
}, | ||
{ | ||
name: "user answers are not deleted for not tallied poll", | ||
store: func(ctx sdk.Context) { | ||
poll := types.NewAttachment(1, 1, 1, types.NewPoll( | ||
"What animal is best?", | ||
[]types.Poll_ProvidedAnswer{ | ||
types.NewProvidedAnswer("Cat", nil), | ||
types.NewProvidedAnswer("Dog", nil), | ||
}, | ||
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), | ||
false, | ||
false, | ||
nil, | ||
)) | ||
|
||
userAnswer := types.NewUserAnswer( | ||
1, | ||
1, | ||
1, | ||
[]uint32{1}, | ||
"cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw", | ||
) | ||
|
||
store := ctx.KVStore(keys[types.StoreKey]) | ||
store.Set(types.AttachmentStoreKey(1, 1, 1), cdc.MustMarshal(&poll)) | ||
store.Set(types.PollAnswerStoreKey(1, 1, 1, "cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw"), cdc.MustMarshal(&userAnswer)) | ||
}, | ||
shouldErr: false, | ||
check: func(ctx sdk.Context) { | ||
store := ctx.KVStore(keys[types.StoreKey]) | ||
|
||
var stored types.Attachment | ||
err := cdc.Unmarshal(store.Get(types.AttachmentStoreKey(1, 1, 1)), &stored) | ||
require.NoError(t, err) | ||
require.Equal(t, types.NewAttachment(1, 1, 1, types.NewPoll( | ||
"What animal is best?", | ||
[]types.Poll_ProvidedAnswer{ | ||
types.NewProvidedAnswer("Cat", nil), | ||
types.NewProvidedAnswer("Dog", nil), | ||
}, | ||
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), | ||
false, | ||
false, | ||
nil, | ||
)), stored) | ||
|
||
require.True(t, store.Has(types.PollAnswerStoreKey(1, 1, 1, "cosmos1jseuux3pktht0kkhlcsv4kqff3mql65udqs4jw"))) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := storetesting.BuildContext(keys, tKeys, memKeys) | ||
if tc.store != nil { | ||
tc.store(ctx) | ||
} | ||
|
||
err := v4.MigrateStore(ctx, keys[types.StoreKey], cdc) | ||
if tc.shouldErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
if tc.check != nil { | ||
tc.check(ctx) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.