From 2ada36ecd4f700d028b0df960e3822d909331cef Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 14:45:14 +0800 Subject: [PATCH 01/26] Revise post reaction type --- proto/desmos/posts/v1beta1/genesis.proto | 15 +- proto/desmos/posts/v1beta1/reactions.proto | 24 +- x/staging/posts/keeper/common_functions.go | 36 ++ x/staging/posts/keeper/genesis.go | 17 +- x/staging/posts/keeper/invariants.go | 8 +- x/staging/posts/keeper/keeper_reactions.go | 88 +--- x/staging/posts/keeper/msgs_server.go | 8 +- x/staging/posts/keeper/msgs_server_test.go | 102 +++-- x/staging/posts/simulation/decoder.go | 4 +- x/staging/posts/types/genesis.go | 26 +- x/staging/posts/types/genesis.pb.go | 295 ++------------ x/staging/posts/types/genesis_test.go | 4 +- x/staging/posts/types/keys.go | 25 +- x/staging/posts/types/reactions.go | 92 ++--- x/staging/posts/types/reactions.pb.go | 444 ++++----------------- x/staging/posts/types/reactions_test.go | 328 ++------------- 16 files changed, 319 insertions(+), 1197 deletions(-) diff --git a/proto/desmos/posts/v1beta1/genesis.proto b/proto/desmos/posts/v1beta1/genesis.proto index 05a8a82eb4..37f61642b8 100644 --- a/proto/desmos/posts/v1beta1/genesis.proto +++ b/proto/desmos/posts/v1beta1/genesis.proto @@ -14,8 +14,8 @@ option go_package = "github.com/desmos-labs/desmos/x/staging/posts/types"; message GenesisState { repeated desmos.posts.v1beta1.Post posts = 1 [ (gogoproto.nullable) = false ]; - repeated UserAnswer users_poll_answers = 2 [ (gogoproto.nullable) = false ]; - repeated PostReactionsEntry posts_reactions = 3 + repeated desmos.posts.v1beta1.UserAnswer users_poll_answers = 2 [ (gogoproto.nullable) = false ]; + repeated desmos.posts.v1beta1.PostReaction posts_reactions = 3 [ (gogoproto.nullable) = false ]; repeated desmos.posts.v1beta1.RegisteredReaction registered_reactions = 4 [ (gogoproto.nullable) = false ]; @@ -25,14 +25,3 @@ message GenesisState { ]; desmos.posts.v1beta1.Params params = 6 [ (gogoproto.nullable) = false ]; } - -// PostReactionEntry represents an entry containing all the reactions to a post -message PostReactionsEntry { - string post_id = 1 [ - (gogoproto.customname) = "PostID", - (gogoproto.jsontag) = "post_id", - (gogoproto.moretags) = "yaml:\"post_id\"" - ]; - repeated desmos.posts.v1beta1.PostReaction reactions = 2 - [ (gogoproto.nullable) = false ]; -} diff --git a/proto/desmos/posts/v1beta1/reactions.proto b/proto/desmos/posts/v1beta1/reactions.proto index 7055ff3be4..482721fe2f 100644 --- a/proto/desmos/posts/v1beta1/reactions.proto +++ b/proto/desmos/posts/v1beta1/reactions.proto @@ -23,12 +23,6 @@ message RegisteredReaction { string creator = 4 [ (gogoproto.moretags) = "yaml:\"creator\"" ]; } -// RegisteredReactions wraps a list of registered reactions -message RegisteredReactions { - repeated desmos.posts.v1beta1.RegisteredReaction reactions = 1 - [ (gogoproto.nullable) = false ]; -} - // ___________________________________________________________________________________________________________________ // PostReaction is a struct of a user reaction to a post @@ -36,18 +30,18 @@ message PostReaction { option (gogoproto.equal) = true; option (gogoproto.goproto_stringer) = true; - string short_code = 1 [ + string post_id = 1 [ + (gogoproto.customname) = "PostID", + (gogoproto.jsontag) = "post_id", + (gogoproto.moretags) = "yaml:\"post_id\"" + ]; + + string short_code = 2 [ (gogoproto.moretags) = "yaml:\"short_code\"", (gogoproto.jsontag) = "short_code" ]; - string value = 2 [ (gogoproto.moretags) = "yaml:\"value\"" ]; - - string owner = 3 [ (gogoproto.moretags) = "yaml:\"owner\"" ]; -} + string value = 3 [ (gogoproto.moretags) = "yaml:\"value\"" ]; -// PostReactions wraps a list of post reactions -message PostReactions { - repeated desmos.posts.v1beta1.PostReaction reactions = 1 - [ (gogoproto.nullable) = false ]; + string owner = 4 [ (gogoproto.moretags) = "yaml:\"owner\"" ]; } diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 53313c7182..1caadef347 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -135,3 +135,39 @@ func (k Keeper) IterateUserAnswersByPost(ctx sdk.Context, postID string, fn func i++ } } + +func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, reaction types.RegisteredReaction) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.RegisteredReactionsStorePrefix) + defer iterator.Close() + + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + reaction := types.MustUnmarshalRegisteredReaction(k.cdc, iterator.Value()) + + stop := fn(i, reaction) + if stop { + break + } + } + + i++ +} + +func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, reaction types.PostReaction) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsStorePrefix) + defer iterator.Close() + + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + reaction := types.MustUnmarshalPostReaction(k.cdc, iterator.Value()) + + stop := fn(i, reaction) + if stop { + break + } + } + + i++ +} diff --git a/x/staging/posts/keeper/genesis.go b/x/staging/posts/keeper/genesis.go index 375c2f088a..6f9dcc46b8 100644 --- a/x/staging/posts/keeper/genesis.go +++ b/x/staging/posts/keeper/genesis.go @@ -13,7 +13,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return types.NewGenesisState( k.GetPosts(ctx), k.GetAllUserAnswers(ctx), - k.GetPostReactionsEntries(ctx), + k.GetAllPostReactions(ctx), k.GetRegisteredReactions(ctx), k.GetAllReports(ctx), k.GetParams(ctx), @@ -43,16 +43,13 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { } // Save post reactions - for _, entry := range data.PostsReactions { - if !types.IsValidPostID(entry.PostID) { - panic(fmt.Errorf("invalid post id: %s", entry.PostID)) + for _, reaction := range data.PostsReactions { + if !types.IsValidPostID(reaction.PostID) { + panic(fmt.Errorf("invalid post id: %s", reaction.PostID)) } - - for _, reaction := range entry.Reactions { - err := k.SavePostReaction(ctx, entry.PostID, reaction) - if err != nil { - panic(err) - } + err := k.SavePostReaction(ctx, reaction) + if err != nil { + panic(err) } } diff --git a/x/staging/posts/keeper/invariants.go b/x/staging/posts/keeper/invariants.go index 04c559407b..110a54f5cf 100644 --- a/x/staging/posts/keeper/invariants.go +++ b/x/staging/posts/keeper/invariants.go @@ -106,10 +106,10 @@ func formatOutputReactions(reactions []types.PostReaction) (outputReactions stri func ValidPostForReactionsInvariant(k Keeper) sdk.Invariant { return func(ctx sdk.Context) (string, bool) { var invalidReactions []types.PostReaction - reactions := k.GetPostReactionsEntries(ctx) - for _, entry := range reactions { - if !k.DoesPostExist(ctx, entry.PostID) { - invalidReactions = append(invalidReactions, entry.Reactions...) + reactions := k.GetAllPostReactions(ctx) + for _, reaction := range reactions { + if !k.DoesPostExist(ctx, reaction.PostID) { + invalidReactions = append(invalidReactions, reaction) } } diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index a1ce914f73..0ff63e03ae 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -1,8 +1,6 @@ package keeper import ( - "bytes" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/desmos-labs/desmos/x/staging/posts/types" @@ -13,25 +11,19 @@ import ( // SavePostReaction allows to save the given reaction inside the store. // It assumes that the given reaction is valid and already registered. // If another reaction from the same user for the same post and with the same value exists, returns an expError. -func (k Keeper) SavePostReaction(ctx sdk.Context, postID string, reaction types.PostReaction) error { +func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) error { store := ctx.KVStore(k.storeKey) - key := types.PostReactionsStoreKey(postID) - - // Get the existent reactions - var reactions types.PostReactions - k.cdc.MustUnmarshalBinaryBare(store.Get(key), &reactions) + key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) // Check for double reactions - if reactions.ContainsReactionFrom(reaction.Owner, reaction.ShortCode) { + if store.Has(key) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s has already reacted with %s to the post with id %s", - reaction.Owner, reaction.ShortCode, postID) + reaction.Owner, reaction.ShortCode, reaction.PostID) } // Save the new reaction - store.Set(key, k.cdc.MustMarshalBinaryBare(&types.PostReactions{ - Reactions: append(reactions.Reactions, reaction), - })) + store.Set(key, k.cdc.MustMarshalBinaryBare(&reaction)) return nil } @@ -39,60 +31,28 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, postID string, reaction types. // DeletePostReaction removes the reaction from the given user from the post having the // given postID. If no reaction with the same value was previously added from the given user, an expError // is returned. -func (k Keeper) DeletePostReaction(ctx sdk.Context, postID string, reaction types.PostReaction) error { +func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) error { store := ctx.KVStore(k.storeKey) - key := types.PostReactionsStoreKey(postID) - - // Get the existing reactions - var wrapped types.PostReactions - k.cdc.MustUnmarshalBinaryBare(store.Get(key), &wrapped) + key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) - // Check if the user exists - reactions := types.NewPostReactions(wrapped.Reactions...) - if !reactions.ContainsReactionFrom(reaction.Owner, reaction.ShortCode) { + if !store.Has(key) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot remove the reaction with value %s from user %s as it does not exist", reaction.ShortCode, reaction.Owner) } - // Remove and save the reactions list - if appendedReactions, edited := reactions.RemoveReaction(reaction.Owner, reaction.ShortCode); edited { - if len(appendedReactions.Reactions) == 0 { - store.Delete(key) - } else { - store.Set(key, k.cdc.MustMarshalBinaryBare(&appendedReactions)) - } - } - + store.Delete(key) return nil } -// GetPostReactions returns the list of reactions that has been associated to the post having the given id -func (k Keeper) GetPostReactions(ctx sdk.Context, postID string) []types.PostReaction { - store := ctx.KVStore(k.storeKey) - - var reactions types.PostReactions - k.cdc.MustUnmarshalBinaryBare(store.Get(types.PostReactionsStoreKey(postID)), &reactions) - - return reactions.Reactions -} - -// GetPostReactionsEntries allows to returns the list of reactions that have been stored inside the given context -func (k Keeper) GetPostReactionsEntries(ctx sdk.Context) []types.PostReactionsEntry { - store := ctx.KVStore(k.storeKey) - - iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsStorePrefix) - defer iterator.Close() - - var reactionsData []types.PostReactionsEntry - for ; iterator.Valid(); iterator.Next() { - var wrapped types.PostReactions - k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &wrapped) - idBytes := bytes.TrimPrefix(iterator.Key(), types.PostReactionsStorePrefix) - reactionsData = append(reactionsData, types.NewPostReactionsEntry(string(idBytes), wrapped.Reactions)) - } - - return reactionsData +// GetAllPostReactions returns the list of reactions that has been associated to the post having the given id +func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { + var reactions []types.PostReaction + k.IteratePostReactions(ctx, func(_ int64, reaction types.PostReaction) bool { + reactions = append(reactions, reaction) + return false + }) + return reactions } // ___________________________________________________________________________________________________________________ @@ -123,17 +83,13 @@ func (k Keeper) GetRegisteredReaction( } // GetRegisteredReactions returns all the registered reactions -func (k Keeper) GetRegisteredReactions(ctx sdk.Context) (reactions []types.RegisteredReaction) { - store := ctx.KVStore(k.storeKey) +func (k Keeper) GetRegisteredReactions(ctx sdk.Context) []types.RegisteredReaction { + var reactions []types.RegisteredReaction - iterator := sdk.KVStorePrefixIterator(store, types.ReactionsStorePrefix) - defer iterator.Close() - - for ; iterator.Valid(); iterator.Next() { - var reaction types.RegisteredReaction - k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &reaction) + k.IterateRegisteredReactions(ctx, func(_ int64, reaction types.RegisteredReaction) bool { reactions = append(reactions, reaction) - } + return false + }) return reactions } diff --git a/x/staging/posts/keeper/msgs_server.go b/x/staging/posts/keeper/msgs_server.go index 9560b9d4c8..2af13a2408 100644 --- a/x/staging/posts/keeper/msgs_server.go +++ b/x/staging/posts/keeper/msgs_server.go @@ -189,8 +189,8 @@ func (k msgServer) AddPostReaction(goCtx context.Context, msg *types.MsgAddPostR return nil, err } - postReaction := types.NewPostReaction(reactionShortcode, reactionValue, msg.User) - if err := k.SavePostReaction(ctx, post.PostID, postReaction); err != nil { + postReaction := types.NewPostReaction(msg.PostID, reactionShortcode, reactionValue, msg.User) + if err := k.SavePostReaction(ctx, postReaction); err != nil { return nil, err } @@ -226,8 +226,8 @@ func (k msgServer) RemovePostReaction(goCtx context.Context, msg *types.MsgRemov } // Remove the registeredReactions - reaction := types.NewPostReaction(reactionShortcode, reactionValue, msg.User) - if err := k.DeletePostReaction(ctx, post.PostID, reaction); err != nil { + reaction := types.NewPostReaction(msg.PostID, reactionShortcode, reactionValue, msg.User) + if err := k.DeletePostReaction(ctx, reaction); err != nil { return nil, err } diff --git a/x/staging/posts/keeper/msgs_server_test.go b/x/staging/posts/keeper/msgs_server_test.go index 5299fcd6a3..4aa462834a 100644 --- a/x/staging/posts/keeper/msgs_server_test.go +++ b/x/staging/posts/keeper/msgs_server_test.go @@ -459,13 +459,13 @@ func (suite *KeeperTestSuite) TestMsgServer_EditPost() { func (suite *KeeperTestSuite) TestMsgServer_AddPostReaction() { tests := []struct { - name string - storedPosts []types.Post - registeredReactions []types.RegisteredReaction - msg *types.MsgAddPostReaction - expError bool - expEvents sdk.Events - expPostReactionEntries []types.PostReactionsEntry + name string + storedPosts []types.Post + registeredReactions []types.RegisteredReaction + msg *types.MsgAddPostReaction + expError bool + expEvents sdk.Events + expPostReactions []types.PostReaction }{ { name: "Post not found", @@ -511,16 +511,12 @@ func (suite *KeeperTestSuite) TestMsgServer_AddPostReaction() { sdk.NewAttribute(types.AttributeKeyReactionShortCode, ":smile:"), ), }, - expPostReactionEntries: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + expPostReactions: []types.PostReaction{ + types.NewPostReaction( suite.testData.post.PostID, - []types.PostReaction{ - types.NewPostReaction( - ":smile:", - "😄", - "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", - ), - }, + ":smile:", + "😄", + "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", ), }, }, @@ -544,16 +540,13 @@ func (suite *KeeperTestSuite) TestMsgServer_AddPostReaction() { sdk.NewAttribute(types.AttributeKeyReactionShortCode, ":slightly_smiling_face:"), ), }, - expPostReactionEntries: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + expPostReactions: []types.PostReaction{ + types.NewPostReaction( suite.testData.post.PostID, - []types.PostReaction{ - types.NewPostReaction( - ":slightly_smiling_face:", - "🙂", - "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", - ), - }), + ":slightly_smiling_face:", + "🙂", + "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", + ), }, }, } @@ -582,7 +575,7 @@ func (suite *KeeperTestSuite) TestMsgServer_AddPostReaction() { } else { suite.Require().NoError(err) suite.Require().Len(suite.ctx.EventManager().Events(), 1) - suite.Require().Equal(test.expPostReactionEntries, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expPostReactions, suite.k.GetPostReactionsEntries(suite.ctx)) } }) } @@ -593,11 +586,11 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { name string storedPosts []types.Post registeredReactions []types.RegisteredReaction - existingReactions []types.PostReactionsEntry + existingReactions []types.PostReaction msg *types.MsgRemovePostReaction expError bool expEvents sdk.Events - expReactions []types.PostReactionsEntry + expReactions []types.PostReaction }{ { name: "Post not found", @@ -639,14 +632,13 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { Creator: suite.testData.post.Creator, }, }, - existingReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry(suite.testData.postID, []types.PostReaction{ - types.NewPostReaction( - ":registeredReactions:", - "react", - "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", - ), - }), + existingReactions: []types.PostReaction{ + types.NewPostReaction( + suite.testData.postID, + ":registeredReactions:", + "react", + "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", + ), }, registeredReactions: []types.RegisteredReaction{ types.NewRegisteredReaction( @@ -684,14 +676,13 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { Creator: suite.testData.post.Creator, }, }, - existingReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry(suite.testData.postID, []types.PostReaction{ - types.NewPostReaction( - ":smile:", - "😄", - "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", - ), - }), + existingReactions: []types.PostReaction{ + types.NewPostReaction( + suite.testData.postID, + ":smile:", + "😄", + "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", + ), }, msg: types.NewMsgRemovePostReaction( suite.testData.postID, @@ -721,14 +712,13 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { Creator: suite.testData.post.Creator, }, }, - existingReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry(suite.testData.postID, []types.PostReaction{ - types.NewPostReaction( - ":+1:", - "👍", - "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", - ), - }), + existingReactions: []types.PostReaction{ + types.NewPostReaction( + suite.testData.postID, + ":+1:", + "👍", + "cosmos1q4hx350dh0843wr3csctxr87at3zcvd9qehqvg", + ), }, msg: types.NewMsgRemovePostReaction( suite.testData.postID, @@ -764,11 +754,9 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { suite.k.SavePost(suite.ctx, post) } - for _, entry := range test.existingReactions { - for _, reaction := range entry.Reactions { - err := suite.k.SavePostReaction(suite.ctx, entry.PostID, reaction) - suite.Require().NoError(err) - } + for _, reaction := range test.existingReactions { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) } handler := keeper.NewMsgServerImpl(suite.k) diff --git a/x/staging/posts/simulation/decoder.go b/x/staging/posts/simulation/decoder.go index 7a7b760787..18eda74812 100644 --- a/x/staging/posts/simulation/decoder.go +++ b/x/staging/posts/simulation/decoder.go @@ -28,12 +28,12 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { return fmt.Sprintf("CommentsA: %s\nCommentsB: %s\n", commentsA, commentsB) case bytes.HasPrefix(kvA.Key, types.PostReactionsStorePrefix): - var postReactionsA, postReactionsB types.PostReactions + var postReactionsA, postReactionsB types.PostReaction cdc.MustUnmarshalBinaryBare(kvA.Value, &postReactionsA) cdc.MustUnmarshalBinaryBare(kvB.Value, &postReactionsB) return fmt.Sprintf("PostReactionsA: %s\nPostReactionsB: %s\n", postReactionsA, postReactionsB) - case bytes.HasPrefix(kvA.Key, types.ReactionsStorePrefix): + case bytes.HasPrefix(kvA.Key, types.RegisteredReactionsStorePrefix): var reactionA, reactionB types.RegisteredReaction cdc.MustUnmarshalBinaryBare(kvA.Value, &reactionA) cdc.MustUnmarshalBinaryBare(kvB.Value, &reactionB) diff --git a/x/staging/posts/types/genesis.go b/x/staging/posts/types/genesis.go index cab6b0f872..962695db7a 100644 --- a/x/staging/posts/types/genesis.go +++ b/x/staging/posts/types/genesis.go @@ -2,19 +2,10 @@ package types import "fmt" -func NewPostReactionsEntry(postID string, reactions []PostReaction) PostReactionsEntry { - return PostReactionsEntry{ - PostID: postID, - Reactions: reactions, - } -} - -// ___________________________________________________________________________________________________________________ - // NewGenesisState creates a new genesis state func NewGenesisState( posts []Post, userAnswers []UserAnswer, - postReactions []PostReactionsEntry, registeredReactions []RegisteredReaction, reports []Report, params Params, + postReactions []PostReaction, registeredReactions []RegisteredReaction, reports []Report, params Params, ) *GenesisState { return &GenesisState{ Posts: posts, @@ -60,16 +51,13 @@ func ValidateGenesis(data *GenesisState) error { } } - for _, postReaction := range data.PostsReactions { - if _, ok := postMap[postReaction.PostID]; !ok { - return fmt.Errorf("invalid reactions; post with id %s does not exist", postReaction.PostID) + for _, reaction := range data.PostsReactions { + if _, ok := postMap[reaction.PostID]; !ok { + return fmt.Errorf("invalid reactions; post with id %s does not exist", reaction.PostID) } - - for _, record := range postReaction.Reactions { - err := record.Validate() - if err != nil { - return err - } + err := reaction.Validate() + if err != nil { + return err } } diff --git a/x/staging/posts/types/genesis.pb.go b/x/staging/posts/types/genesis.pb.go index 2a8b848cd3..ff2570656e 100644 --- a/x/staging/posts/types/genesis.pb.go +++ b/x/staging/posts/types/genesis.pb.go @@ -27,7 +27,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type GenesisState struct { Posts []Post `protobuf:"bytes,1,rep,name=posts,proto3" json:"posts"` UsersPollAnswers []UserAnswer `protobuf:"bytes,2,rep,name=users_poll_answers,json=usersPollAnswers,proto3" json:"users_poll_answers"` - PostsReactions []PostReactionsEntry `protobuf:"bytes,3,rep,name=posts_reactions,json=postsReactions,proto3" json:"posts_reactions"` + PostsReactions []PostReaction `protobuf:"bytes,3,rep,name=posts_reactions,json=postsReactions,proto3" json:"posts_reactions"` RegisteredReactions []RegisteredReaction `protobuf:"bytes,4,rep,name=registered_reactions,json=registeredReactions,proto3" json:"registered_reactions"` Reports []Report `protobuf:"bytes,5,rep,name=reports,proto3" json:"reports" yaml:"reports"` Params Params `protobuf:"bytes,6,opt,name=params,proto3" json:"params"` @@ -80,7 +80,7 @@ func (m *GenesisState) GetUsersPollAnswers() []UserAnswer { return nil } -func (m *GenesisState) GetPostsReactions() []PostReactionsEntry { +func (m *GenesisState) GetPostsReactions() []PostReaction { if m != nil { return m.PostsReactions } @@ -108,62 +108,8 @@ func (m *GenesisState) GetParams() Params { return Params{} } -// PostReactionEntry represents an entry containing all the reactions to a post -type PostReactionsEntry struct { - PostID string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"post_id"` - Reactions []PostReaction `protobuf:"bytes,2,rep,name=reactions,proto3" json:"reactions"` -} - -func (m *PostReactionsEntry) Reset() { *m = PostReactionsEntry{} } -func (m *PostReactionsEntry) String() string { return proto.CompactTextString(m) } -func (*PostReactionsEntry) ProtoMessage() {} -func (*PostReactionsEntry) Descriptor() ([]byte, []int) { - return fileDescriptor_e358c996c23f0348, []int{1} -} -func (m *PostReactionsEntry) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *PostReactionsEntry) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_PostReactionsEntry.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *PostReactionsEntry) XXX_Merge(src proto.Message) { - xxx_messageInfo_PostReactionsEntry.Merge(m, src) -} -func (m *PostReactionsEntry) XXX_Size() int { - return m.Size() -} -func (m *PostReactionsEntry) XXX_DiscardUnknown() { - xxx_messageInfo_PostReactionsEntry.DiscardUnknown(m) -} - -var xxx_messageInfo_PostReactionsEntry proto.InternalMessageInfo - -func (m *PostReactionsEntry) GetPostID() string { - if m != nil { - return m.PostID - } - return "" -} - -func (m *PostReactionsEntry) GetReactions() []PostReaction { - if m != nil { - return m.Reactions - } - return nil -} - func init() { proto.RegisterType((*GenesisState)(nil), "desmos.posts.v1beta1.GenesisState") - proto.RegisterType((*PostReactionsEntry)(nil), "desmos.posts.v1beta1.PostReactionsEntry") } func init() { @@ -171,36 +117,32 @@ func init() { } var fileDescriptor_e358c996c23f0348 = []byte{ - // 456 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0xcf, 0x8a, 0xd3, 0x40, - 0x1c, 0xc7, 0x3b, 0x76, 0x37, 0xcb, 0x8e, 0xb2, 0xca, 0x58, 0x24, 0x14, 0x49, 0x63, 0x10, 0xec, - 0xc5, 0x84, 0x75, 0xc1, 0xc3, 0x9e, 0xb4, 0xf8, 0x87, 0x3d, 0x28, 0x25, 0x2a, 0x82, 0x97, 0x30, - 0xd9, 0x0c, 0x31, 0x90, 0x64, 0xc2, 0xfc, 0x66, 0xd5, 0xbe, 0x85, 0x4f, 0xe0, 0xf3, 0xec, 0x71, - 0x8f, 0x9e, 0x82, 0xa4, 0x9e, 0x3c, 0xfa, 0x04, 0x92, 0x99, 0x49, 0x2b, 0x34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, 0xe4, 0x5f, 0x00, 0x00, 0x00, 0xff, 0xff, - 0x9c, 0x63, 0xad, 0x4b, 0xdf, 0x03, 0x00, 0x00, + // 390 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0x4f, 0x4b, 0xe3, 0x40, + 0x18, 0xc6, 0x93, 0xed, 0x9f, 0x85, 0xd9, 0xa5, 0xbb, 0xcc, 0x96, 0x25, 0x94, 0x25, 0xcd, 0x06, + 0x0f, 0xbd, 0x98, 0x50, 0x0b, 0x1e, 0x7a, 0xb3, 0x17, 0x4f, 0x4a, 0x8d, 0x7a, 0xf1, 0x52, 0x26, + 0xed, 0x10, 0x03, 0x49, 0x26, 0xcc, 0x3b, 0x55, 0xfb, 0x2d, 0xfc, 0x58, 0x3d, 0xf6, 0xe8, 0xa9, + 0x48, 0xfb, 0x0d, 0xfc, 0x04, 0x92, 0x99, 0x49, 0x11, 0x4d, 0x7b, 0x1b, 0x26, 0xbf, 0xe7, 0xc7, + 0xfb, 0x64, 0x5e, 0xe4, 0xce, 0x28, 0xa4, 0x0c, 0xfc, 0x9c, 0x81, 0x00, 0xff, 0xa1, 0x1f, 0x52, + 0x41, 0xfa, 0x7e, 0x44, 0x33, 0x0a, 0x31, 0x78, 0x39, 0x67, 0x82, 0xe1, 0xb6, 0x62, 0x3c, 0xc9, + 0x78, 0x9a, 0xe9, 0xb4, 0x23, 0x16, 0x31, 0x09, 0xf8, 0xc5, 0x49, 0xb1, 0x1d, 0xa7, 0xd2, 0xa7, + 0x92, 0x87, 0x89, 0x24, 0x29, 0x89, 0xa3, 0x4a, 0x82, 0x53, 0x32, 0x15, 0x31, 0xcb, 0x4a, 0xea, + 0x7f, 0xb5, 0x87, 0x70, 0x92, 0x1e, 0x46, 0x38, 0xcd, 0x19, 0x17, 0x0a, 0x71, 0xd7, 0x35, 0xf4, + 0xf3, 0x5c, 0xb5, 0xbd, 0x16,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7, 0xa1, 0x37, 0x65, 0xa9, + 0xaf, 0x7c, 0xc7, 0x09, 0x09, 0x41, 0x9f, 0xfd, 0x27, 0x1f, 0x04, 0x89, 0xe2, 0x2c, 0xd2, 0xeb, + 0x23, 0x16, 0x39, 0x85, 0xb0, 0x29, 0xd7, 0x66, 0xf0, 0x1e, 0x00, 0x00, 0xff, 0xff, 0xf1, 0xa9, + 0x69, 0x5e, 0x38, 0x03, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -306,50 +248,6 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *PostReactionsEntry) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *PostReactionsEntry) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PostReactionsEntry) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Reactions) > 0 { - for iNdEx := len(m.Reactions) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Reactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintGenesis(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - } - if len(m.PostID) > 0 { - i -= len(m.PostID) - copy(dAtA[i:], m.PostID) - i = encodeVarintGenesis(dAtA, i, uint64(len(m.PostID))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int { offset -= sovGenesis(v) base := offset @@ -402,25 +300,6 @@ func (m *GenesisState) Size() (n int) { return n } -func (m *PostReactionsEntry) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.PostID) - if l > 0 { - n += 1 + l + sovGenesis(uint64(l)) - } - if len(m.Reactions) > 0 { - for _, e := range m.Reactions { - l = e.Size() - n += 1 + l + sovGenesis(uint64(l)) - } - } - return n -} - func sovGenesis(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -553,7 +432,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.PostsReactions = append(m.PostsReactions, PostReactionsEntry{}) + m.PostsReactions = append(m.PostsReactions, PostReaction{}) if err := m.PostsReactions[len(m.PostsReactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } @@ -680,122 +559,6 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } return nil } -func (m *PostReactionsEntry) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenesis - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: PostReactionsEntry: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: PostReactionsEntry: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field PostID", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenesis - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthGenesis - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthGenesis - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.PostID = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Reactions", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenesis - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthGenesis - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthGenesis - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Reactions = append(m.Reactions, PostReaction{}) - if err := m.Reactions[len(m.Reactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipGenesis(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthGenesis - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipGenesis(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/staging/posts/types/genesis_test.go b/x/staging/posts/types/genesis_test.go index 8c303fc0db..7c90095304 100644 --- a/x/staging/posts/types/genesis_test.go +++ b/x/staging/posts/types/genesis_test.go @@ -37,8 +37,8 @@ func TestValidateGenesis(t *testing.T) { genesis: types.NewGenesisState( []types.Post{}, nil, - []types.PostReactionsEntry{ - types.NewPostReactionsEntry("1", []types.PostReaction{{Owner: ""}}), + []types.PostReaction{ + {PostID: "1", Owner: ""}, }, nil, nil, diff --git a/x/staging/posts/types/keys.go b/x/staging/posts/types/keys.go index 48a0b6e295..600b69ee94 100644 --- a/x/staging/posts/types/keys.go +++ b/x/staging/posts/types/keys.go @@ -37,13 +37,13 @@ var ( ModuleAddress = authtypes.NewModuleAddress(ModuleName) - PostStorePrefix = []byte("post") - PostCommentsStorePrefix = []byte("comments") - SubspacePostPrefix = []byte("subspace") - PostReactionsStorePrefix = []byte("p_reactions") - ReactionsStorePrefix = []byte("reactions") - UserAnswersStorePrefix = []byte("user_answers") - ReportsStorePrefix = []byte("reports") + PostStorePrefix = []byte("post") + PostCommentsStorePrefix = []byte("comments") + SubspacePostPrefix = []byte("subspace") + PostReactionsStorePrefix = []byte("p_reactions") + RegisteredReactionsStorePrefix = []byte("reactions") + UserAnswersStorePrefix = []byte("user_answers") + ReportsStorePrefix = []byte("reports") ) // IsValidPostID tells whether the given value represents a valid post id or not @@ -76,14 +76,19 @@ func SubspacePostKey(subspace string, id string) []byte { return append(SubspacePostsPrefix(subspace), []byte(id)...) } -// PostCommentsStoreKey turns an id to a key used to store a post's reactions into the posts store -func PostReactionsStoreKey(id string) []byte { +// PostReactionsPrefix returns the prefix used to store all the reactions for the post having the given id +func PostReactionsPrefix(id string) []byte { return append(PostReactionsStorePrefix, []byte(id)...) } +// PostReactionsStoreKey returns the key used to store the reaction containing the given data +func PostReactionsStoreKey(id, user, shortcode string) []byte { + return append(PostReactionsPrefix(id), []byte(user+shortcode)...) +} + // RegisteredReactionsPrefix returns the prefix used to store all the reactions for the subspace having the given id func RegisteredReactionsPrefix(subspace string) []byte { - return append(ReactionsStorePrefix, []byte(subspace)...) + return append(RegisteredReactionsStorePrefix, []byte(subspace)...) } // RegisteredReactionsStoreKey returns the key used to store the registered reaction having the given short code for the given subspace diff --git a/x/staging/posts/types/reactions.go b/x/staging/posts/types/reactions.go index 1146e0cb28..3902c88569 100644 --- a/x/staging/posts/types/reactions.go +++ b/x/staging/posts/types/reactions.go @@ -4,11 +4,10 @@ import ( "fmt" "strings" + "github.com/cosmos/cosmos-sdk/codec" subspacestypes "github.com/desmos-labs/desmos/x/staging/subspaces/types" "github.com/desmos-labs/desmos/x/commons" - - emoji "github.com/desmos-labs/Go-Emoji-Utils" ) // NewRegisteredReaction returns a new RegisteredReaction @@ -46,11 +45,25 @@ func (reaction RegisteredReaction) Validate() error { return nil } +// MustMarshalRegisteredReaction serializes the given registered reaction using the provided BinaryMarshaler +func MustMarshalRegisteredReaction(cdc codec.BinaryMarshaler, reaction RegisteredReaction) []byte { + return cdc.MustMarshalBinaryBare(&reaction) +} + +// MustUnmarshalRegisteredReaction deserializes the given byte array as a registered reaction using +// the provided BinaryMarshaler +func MustUnmarshalRegisteredReaction(cdc codec.BinaryMarshaler, bz []byte) RegisteredReaction { + var reaction RegisteredReaction + cdc.MustUnmarshalBinaryBare(bz, &reaction) + return reaction +} + // ___________________________________________________________________________________________________________________ // NewPostReaction returns a new PostReaction -func NewPostReaction(shortcode, value string, owner string) PostReaction { +func NewPostReaction(postID, shortcode, value, owner string) PostReaction { return PostReaction{ + PostID: postID, ShortCode: shortcode, Value: value, Owner: owner, @@ -59,6 +72,10 @@ func NewPostReaction(shortcode, value string, owner string) PostReaction { // Validate implements validator func (reaction PostReaction) Validate() error { + if !IsValidPostID(reaction.PostID) { + return fmt.Errorf("invalid post id: %s", reaction.PostID) + } + if reaction.Owner == "" { return fmt.Errorf("invalid reaction owner: %s", reaction.Owner) } @@ -74,66 +91,15 @@ func (reaction PostReaction) Validate() error { return nil } -// ___________________________________________________________________________________________________________________ - -// NewPostReactions allows to create a new PostReactions object from the given reactions -func NewPostReactions(reactions ...PostReaction) PostReactions { - return PostReactions{Reactions: reactions} -} - -// ContainsReactionFrom returns true if the reactions slice contain -// a reaction from the given user having the given value, false otherwise. -// NOTE: The value can be either an emoji or a shortcode. -func (reactions PostReactions) ContainsReactionFrom(user string, value string) bool { - return reactions.IndexOfByUserAndValue(user, value) != -1 +// MustMarshalPostReaction serializes the given post reaction using the provided BinaryMarshaler +func MustMarshalPostReaction(cdc codec.BinaryMarshaler, reaction PostReaction) []byte { + return cdc.MustMarshalBinaryBare(&reaction) } -// IndexOfByUserAndValue returns the index of the reaction from the -// given user with the specified code inside the reactions slice. -// NOTE: The value can be either an emoji or a shortcode. -func (reactions PostReactions) IndexOfByUserAndValue(owner string, value string) int { - var reactEmoji *emoji.Emoji - if ej, found := GetEmojiByShortCodeOrValue(value); found { - reactEmoji = ej - } - - for index, reaction := range reactions.Reactions { - if reaction.Owner == owner { - if reactEmoji != nil { - // Check the emoji value - if reaction.Value == reactEmoji.Value { - return index - } - - // Check the emoji shortcodes - for _, code := range reactEmoji.Shortcodes { - if reaction.ShortCode == code { - return index - } - } - } - - if reactEmoji == nil { - if value == reaction.ShortCode { - return index - } - } - } - } - return -1 -} - -// RemoveReaction returns a new PostReactions slice not containing the -// reaction of the given user with the given value. -// If the reaction was removed properly, true is also returned. Otherwise, -// if no reaction was found, false is returned instead. -func (reactions PostReactions) RemoveReaction(user string, value string) (PostReactions, bool) { - index := reactions.IndexOfByUserAndValue(user, value) - if index == -1 { - return reactions, false - } - - return PostReactions{ - Reactions: append(reactions.Reactions[:index], reactions.Reactions[index+1:]...), - }, true +// MustUnmarshalPostReaction deserializes the given byte array as a post reaction using +// the provided BinaryMarshaler +func MustUnmarshalPostReaction(cdc codec.BinaryMarshaler, bz []byte) PostReaction { + var reaction PostReaction + cdc.MustUnmarshalBinaryBare(bz, &reaction) + return reaction } diff --git a/x/staging/posts/types/reactions.pb.go b/x/staging/posts/types/reactions.pb.go index 96220d93d8..36eb19f0d3 100644 --- a/x/staging/posts/types/reactions.pb.go +++ b/x/staging/posts/types/reactions.pb.go @@ -93,63 +93,19 @@ func (m *RegisteredReaction) GetCreator() string { return "" } -// RegisteredReactions wraps a list of registered reactions -type RegisteredReactions struct { - Reactions []RegisteredReaction `protobuf:"bytes,1,rep,name=reactions,proto3" json:"reactions"` -} - -func (m *RegisteredReactions) Reset() { *m = RegisteredReactions{} } -func (m *RegisteredReactions) String() string { return proto.CompactTextString(m) } -func (*RegisteredReactions) ProtoMessage() {} -func (*RegisteredReactions) Descriptor() ([]byte, []int) { - return fileDescriptor_279d343e0105421c, []int{1} -} -func (m *RegisteredReactions) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *RegisteredReactions) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_RegisteredReactions.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *RegisteredReactions) XXX_Merge(src proto.Message) { - xxx_messageInfo_RegisteredReactions.Merge(m, src) -} -func (m *RegisteredReactions) XXX_Size() int { - return m.Size() -} -func (m *RegisteredReactions) XXX_DiscardUnknown() { - xxx_messageInfo_RegisteredReactions.DiscardUnknown(m) -} - -var xxx_messageInfo_RegisteredReactions proto.InternalMessageInfo - -func (m *RegisteredReactions) GetReactions() []RegisteredReaction { - if m != nil { - return m.Reactions - } - return nil -} - // PostReaction is a struct of a user reaction to a post type PostReaction struct { - ShortCode string `protobuf:"bytes,1,opt,name=short_code,json=shortCode,proto3" json:"short_code" yaml:"short_code"` - Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty" yaml:"value"` - Owner string `protobuf:"bytes,3,opt,name=owner,proto3" json:"owner,omitempty" yaml:"owner"` + PostID string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"post_id"` + ShortCode string `protobuf:"bytes,2,opt,name=short_code,json=shortCode,proto3" json:"short_code" yaml:"short_code"` + Value string `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty" yaml:"value"` + Owner string `protobuf:"bytes,4,opt,name=owner,proto3" json:"owner,omitempty" yaml:"owner"` } func (m *PostReaction) Reset() { *m = PostReaction{} } func (m *PostReaction) String() string { return proto.CompactTextString(m) } func (*PostReaction) ProtoMessage() {} func (*PostReaction) Descriptor() ([]byte, []int) { - return fileDescriptor_279d343e0105421c, []int{2} + return fileDescriptor_279d343e0105421c, []int{1} } func (m *PostReaction) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -178,6 +134,13 @@ func (m *PostReaction) XXX_DiscardUnknown() { var xxx_messageInfo_PostReaction proto.InternalMessageInfo +func (m *PostReaction) GetPostID() string { + if m != nil { + return m.PostID + } + return "" +} + func (m *PostReaction) GetShortCode() string { if m != nil { return m.ShortCode @@ -199,56 +162,9 @@ func (m *PostReaction) GetOwner() string { return "" } -// PostReactions wraps a list of post reactions -type PostReactions struct { - Reactions []PostReaction `protobuf:"bytes,1,rep,name=reactions,proto3" json:"reactions"` -} - -func (m *PostReactions) Reset() { *m = PostReactions{} } -func (m *PostReactions) String() string { return proto.CompactTextString(m) } -func (*PostReactions) ProtoMessage() {} -func (*PostReactions) Descriptor() ([]byte, []int) { - return fileDescriptor_279d343e0105421c, []int{3} -} -func (m *PostReactions) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *PostReactions) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_PostReactions.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *PostReactions) XXX_Merge(src proto.Message) { - xxx_messageInfo_PostReactions.Merge(m, src) -} -func (m *PostReactions) XXX_Size() int { - return m.Size() -} -func (m *PostReactions) XXX_DiscardUnknown() { - xxx_messageInfo_PostReactions.DiscardUnknown(m) -} - -var xxx_messageInfo_PostReactions proto.InternalMessageInfo - -func (m *PostReactions) GetReactions() []PostReaction { - if m != nil { - return m.Reactions - } - return nil -} - func init() { proto.RegisterType((*RegisteredReaction)(nil), "desmos.posts.v1beta1.RegisteredReaction") - proto.RegisterType((*RegisteredReactions)(nil), "desmos.posts.v1beta1.RegisteredReactions") proto.RegisterType((*PostReaction)(nil), "desmos.posts.v1beta1.PostReaction") - proto.RegisterType((*PostReactions)(nil), "desmos.posts.v1beta1.PostReactions") } func init() { @@ -256,32 +172,32 @@ func init() { } var fileDescriptor_279d343e0105421c = []byte{ - // 394 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x92, 0xbd, 0xae, 0xd3, 0x30, - 0x18, 0x86, 0x63, 0x4e, 0x0b, 0xad, 0x29, 0x7f, 0x6e, 0x87, 0x88, 0x21, 0xae, 0x0c, 0xaa, 0x3a, - 0x40, 0xac, 0xd2, 0xad, 0x63, 0x90, 0x98, 0x40, 0x42, 0x5e, 0x90, 0x58, 0x90, 0x93, 0x58, 0x69, - 0xa4, 0xb6, 0x8e, 0x62, 0xb7, 0xd0, 0xbb, 0x60, 0x64, 0xec, 0x05, 0x70, 0x21, 0x1d, 0x3b, 0x32, - 0x45, 0xa8, 0x5d, 0x80, 0x31, 0x57, 0x80, 0x70, 0x4c, 0x1b, 0x29, 0xdd, 0xcf, 0xe6, 0x7c, 0xdf, - 0xf3, 0xbe, 0x8a, 0x1f, 0x19, 0x3e, 0x8f, 0x85, 0x5a, 0x4a, 0x45, 0x33, 0xa9, 0xb4, 0xa2, 0x9b, - 0x49, 0x28, 0x34, 0x9f, 0xd0, 0x5c, 0xf0, 0x48, 0xa7, 0x72, 0xa5, 0xfc, 0x2c, 0x97, 0x5a, 0xa2, - 0x41, 0x45, 0xf9, 0x86, 0xf2, 0x2d, 0xf5, 0x74, 0x90, 0xc8, 0x44, 0x1a, 0x80, 0xfe, 0x3b, 0x55, - 0x2c, 0xf9, 0x0d, 0x20, 0x62, 0x22, 0x49, 0x95, 0x16, 0xb9, 0x88, 0x99, 0x6d, 0x42, 0x01, 0x84, - 0x6a, 0x2e, 0x73, 0xfd, 0x29, 0x92, 0xb1, 0x70, 0xc1, 0x10, 0x8c, 0xbb, 0xc1, 0xb3, 0x3f, 0x05, - 0xae, 0x4d, 0xcb, 0x02, 0x3f, 0xd9, 0xf2, 0xe5, 0x62, 0x46, 0x2e, 0x33, 0xc2, 0xba, 0xe6, 0xe3, - 0xb5, 0x8c, 0x05, 0x1a, 0xc1, 0xf6, 0x86, 0x2f, 0xd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, 0x9e, 0xa4, - 0xab, 0xc4, 0xbe, 0x72, 0xbd, 0xcd, 0x84, 0x0a, 0xef, 0x9a, 0xe7, 0x3a, 0xfd, 0x1b, 0x00, 0x00, - 0xff, 0xff, 0x8f, 0x06, 0x1b, 0xea, 0x02, 0x03, 0x00, 0x00, + // 386 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x92, 0xbb, 0x6e, 0xdb, 0x30, + 0x18, 0x85, 0x4d, 0xbb, 0xbe, 0x11, 0x46, 0x2f, 0xaa, 0x07, 0xa1, 0x83, 0x58, 0xb0, 0x85, 0xdb, + 0xa1, 0x15, 0x61, 0x78, 0xf3, 0x54, 0xb8, 0x5d, 0x3c, 0x14, 0x28, 0x38, 0x76, 0x31, 0x28, 0x89, + 0x90, 0x05, 0xd8, 0xa6, 0x20, 0xd2, 0x4e, 0xfc, 0x16, 0x19, 0x33, 0xfa, 0x71, 0x32, 0x7a, 0xcc, + 0x24, 0x04, 0xf2, 0x92, 0x78, 0xc8, 0xa0, 0x27, 0x08, 0x44, 0xd1, 0x17, 0x64, 0xc8, 0x92, 0xed, + 0xe7, 0x39, 0x1f, 0x7f, 0xe0, 0x1c, 0xfc, 0xf0, 0x6b, 0xc0, 0xe5, 0x5c, 0x48, 0x12, 0x0b, 0xa9, + 0x24, 0x59, 0xf5, 0x3d, 0xae, 0x58, 0x9f, 0x24, 0x9c, 0xf9, 0x2a, 0x12, 0x0b, 0xe9, 0xc6, 0x89, + 0x50, 0xc2, 0xea, 0x96, 0x94, 0xab, 0x29, 0xd7, 0x50, 0x9f, 0xba, 0xa1, 0x08, 0x85, 0x06, 0x48, + 0x31, 0x95, 0x2c, 0x7e, 0x00, 0xd0, 0xa2, 0x3c, 0x8c, 0xa4, 0xe2, 0x09, 0x0f, 0xa8, 0xd9, 0x64, + 0x8d, 0x20, 0x94, 0x53, 0x91, 0xa8, 0x89, 0x2f, 0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, 0xdb, 0x9d, 0x53, + 0xf9, 0x3f, 0x08, 0x23, 0x35, 0x5d, 0x7a, 0xae, 0x2f, 0xe6, 0xa4, 0xbc, 0x96, 0x9f, 0x33, 0xe6, + 0x49, 0x33, 0x93, 0x4b, 0x22, 0x15, 0x0b, 0xa3, 0x45, 0x68, 0x2e, 0x4d, 0xad, 0x63, 0x2e, 0xbd, + 0x86, 0x3e, 0x99, 0xc1, 0x53, 0x00, 0x00, 0x00, 0xff, 0xff, 0xef, 0x9c, 0xf9, 0xf8, 0x86, 0x02, + 0x00, 0x00, } func (this *RegisteredReaction) Equal(that interface{}) bool { @@ -336,6 +252,9 @@ func (this *PostReaction) Equal(that interface{}) bool { } else if this == nil { return false } + if this.PostID != that1.PostID { + return false + } if this.ShortCode != that1.ShortCode { return false } @@ -398,43 +317,6 @@ func (m *RegisteredReaction) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *RegisteredReactions) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *RegisteredReactions) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *RegisteredReactions) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Reactions) > 0 { - for iNdEx := len(m.Reactions) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Reactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintReactions(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } - } - return len(dAtA) - i, nil -} - func (m *PostReaction) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -460,58 +342,28 @@ func (m *PostReaction) MarshalToSizedBuffer(dAtA []byte) (int, error) { copy(dAtA[i:], m.Owner) i = encodeVarintReactions(dAtA, i, uint64(len(m.Owner))) i-- - dAtA[i] = 0x1a + dAtA[i] = 0x22 } if len(m.Value) > 0 { i -= len(m.Value) copy(dAtA[i:], m.Value) i = encodeVarintReactions(dAtA, i, uint64(len(m.Value))) i-- - dAtA[i] = 0x12 + dAtA[i] = 0x1a } if len(m.ShortCode) > 0 { i -= len(m.ShortCode) copy(dAtA[i:], m.ShortCode) i = encodeVarintReactions(dAtA, i, uint64(len(m.ShortCode))) i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *PostReactions) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err + dAtA[i] = 0x12 } - return dAtA[:n], nil -} - -func (m *PostReactions) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *PostReactions) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Reactions) > 0 { - for iNdEx := len(m.Reactions) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Reactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintReactions(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } + if len(m.PostID) > 0 { + i -= len(m.PostID) + copy(dAtA[i:], m.PostID) + i = encodeVarintReactions(dAtA, i, uint64(len(m.PostID))) + i-- + dAtA[i] = 0xa } return len(dAtA) - i, nil } @@ -552,27 +404,16 @@ func (m *RegisteredReaction) Size() (n int) { return n } -func (m *RegisteredReactions) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if len(m.Reactions) > 0 { - for _, e := range m.Reactions { - l = e.Size() - n += 1 + l + sovReactions(uint64(l)) - } - } - return n -} - func (m *PostReaction) Size() (n int) { if m == nil { return 0 } var l int _ = l + l = len(m.PostID) + if l > 0 { + n += 1 + l + sovReactions(uint64(l)) + } l = len(m.ShortCode) if l > 0 { n += 1 + l + sovReactions(uint64(l)) @@ -588,21 +429,6 @@ func (m *PostReaction) Size() (n int) { return n } -func (m *PostReactions) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if len(m.Reactions) > 0 { - for _, e := range m.Reactions { - l = e.Size() - n += 1 + l + sovReactions(uint64(l)) - } - } - return n -} - func sovReactions(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -787,7 +613,7 @@ func (m *RegisteredReaction) Unmarshal(dAtA []byte) error { } return nil } -func (m *RegisteredReactions) Unmarshal(dAtA []byte) error { +func (m *PostReaction) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -810,17 +636,17 @@ func (m *RegisteredReactions) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: RegisteredReactions: wiretype end group for non-group") + return fmt.Errorf("proto: PostReaction: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: RegisteredReactions: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: PostReaction: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Reactions", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field PostID", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowReactions @@ -830,77 +656,25 @@ func (m *RegisteredReactions) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthReactions } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthReactions } if postIndex > l { return io.ErrUnexpectedEOF } - m.Reactions = append(m.Reactions, RegisteredReaction{}) - if err := m.Reactions[len(m.Reactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } + m.PostID = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipReactions(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthReactions - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *PostReaction) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowReactions - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: PostReaction: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: PostReaction: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: + case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field ShortCode", wireType) } @@ -932,7 +706,7 @@ func (m *PostReaction) Unmarshal(dAtA []byte) error { } m.ShortCode = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - case 2: + case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Value", wireType) } @@ -964,7 +738,7 @@ func (m *PostReaction) Unmarshal(dAtA []byte) error { } m.Value = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex - case 3: + case 4: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Owner", wireType) } @@ -1017,90 +791,6 @@ func (m *PostReaction) Unmarshal(dAtA []byte) error { } return nil } -func (m *PostReactions) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowReactions - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: PostReactions: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: PostReactions: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Reactions", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowReactions - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthReactions - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthReactions - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Reactions = append(m.Reactions, PostReaction{}) - if err := m.Reactions[len(m.Reactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipReactions(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthReactions - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipReactions(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/staging/posts/types/reactions_test.go b/x/staging/posts/types/reactions_test.go index 6aeb693bf6..8fae0b7091 100644 --- a/x/staging/posts/types/reactions_test.go +++ b/x/staging/posts/types/reactions_test.go @@ -4,6 +4,7 @@ import ( "errors" "testing" + "github.com/desmos-labs/desmos/app" "github.com/desmos-labs/desmos/x/staging/posts/types" "github.com/stretchr/testify/require" @@ -117,6 +118,19 @@ func TestReaction_Validate(t *testing.T) { } } +func TestRegisteredReactionsMarshaling(t *testing.T) { + cdc, _ := app.MakeCodecs() + reaction := types.NewRegisteredReaction( + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ":smile-jpg:", + "https://smile.jpg", + "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", + ) + marshaled := types.MustMarshalRegisteredReaction(cdc, reaction) + unmarshaled := types.MustUnmarshalRegisteredReaction(cdc, marshaled) + require.Equal(t, reaction, unmarshaled) +} + // ___________________________________________________________________________________________________________________ func TestPostReaction_Validate(t *testing.T) { @@ -128,15 +142,27 @@ func TestPostReaction_Validate(t *testing.T) { { name: "Valid reaction returns no error", reaction: types.NewPostReaction( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", ":smile:", "reaction", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ), error: nil, }, + { + name: "Invalid post id returns error", + reaction: types.NewPostReaction( + "", + ":smile:", + "reaction", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), + error: errors.New("invalid post id: "), + }, { name: "Missing owner returns error", reaction: types.NewPostReaction( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", ":smile:", "reaction", "", @@ -146,6 +172,7 @@ func TestPostReaction_Validate(t *testing.T) { { name: "Missing value returns error", reaction: types.NewPostReaction( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", ":smile:", "", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", @@ -155,6 +182,7 @@ func TestPostReaction_Validate(t *testing.T) { { name: "Invalid shortcode returns error", reaction: types.NewPostReaction( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "invalid", "reaction", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", @@ -171,293 +199,15 @@ func TestPostReaction_Validate(t *testing.T) { } } -// ___________________________________________________________________________________________________________________ - -func TestPostReactions_ContainsOwnerLike(t *testing.T) { - tests := []struct { - name string - reactions types.PostReactions - owner string - shortcode string - expContains bool - }{ - { - name: "Non-empty list returns true with valid address", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - shortcode: ":smile:", - expContains: true, - }, - { - name: "Empty list returns false", - reactions: types.NewPostReactions(), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - shortcode: ":smile:", - expContains: false, - }, - { - name: "Non-empty list returns false with not found address", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - shortcode: ":smile:", - expContains: false, - }, - { - name: "Non-empty list returns false with not found value", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - shortcode: ":like:", - expContains: false, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - require.Equal(t, test.expContains, test.reactions.ContainsReactionFrom(test.owner, test.shortcode)) - }) - } -} - -func TestPostReactions_IndexOfByUserAndValue(t *testing.T) { - tests := []struct { - name string - reactions types.PostReactions - owner string - value string - expIndex int - }{ - { - name: "Non-empty list returns proper index with valid value (shortcode)", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":+1:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: ":+1:", - expIndex: 0, - }, - { - name: "Non-empty list returns proper index with valid value (emoji - one code)", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":+1:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: "👍", - expIndex: 0, - }, - { - name: "Non-empty list returns proper index with valid value (emoji - another code)", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":thumbsup:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: "👍", - expIndex: 0, - }, - { - name: "Empty list returns -1", - reactions: types.NewPostReactions(), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: "reaction", - expIndex: -1, - }, - { - name: "Non-empty list returns -1 with not found address", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - value: "reaction", - expIndex: -1, - }, - { - name: "Non-empty list returns -1 with not found value", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - value: "reaction-2", - expIndex: -1, - }, - { - name: "Existing reaction search by code", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":reaction:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: ":reaction:", - expIndex: 0, - }, - { - name: "Exiting emoji reaction stored by value search by code", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":fire:", - "🔥", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: ":fire:", - expIndex: 0, - }, - { - name: "Exiting emoji reaction stored by code search by code", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":fire:", - "🔥", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: ":fire:", - expIndex: 0, - }, - { - name: "Exiting emoji reaction stored by code search by value", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":fire:", - "🔥", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - value: "🔥", - expIndex: 0, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - require.Equal(t, test.expIndex, test.reactions.IndexOfByUserAndValue(test.owner, test.value)) - }) - } -} - -func TestPostReactions_RemoveReaction(t *testing.T) { - tests := []struct { - name string - reactions types.PostReactions - owner string - shortcode string - expResult types.PostReactions - expEdited bool - }{ - { - name: "PostReaction is removed from non-empty list", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - shortcode: ":smile:", - expResult: types.NewPostReactions([]types.PostReaction{}...), - expEdited: true, - }, - { - name: "Empty list is not edited", - reactions: types.NewPostReactions(), - owner: "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - shortcode: ":smile:", - expResult: types.NewPostReactions(), - expEdited: false, - }, - { - name: "Non-empty list with not found address is not edited", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - shortcode: ":smile:", - expResult: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - expEdited: false, - }, - { - name: "Non-empty list with not found value is not edited", - reactions: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - shortcode: ":like:", - expResult: types.NewPostReactions( - types.NewPostReaction( - ":smile:", - "reaction", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - ), - expEdited: false, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - result, edited := test.reactions.RemoveReaction(test.owner, test.shortcode) - require.Equal(t, test.expEdited, edited) - require.Equal(t, test.expResult, result) - }) - } +func TestPostReactionsMarshaling(t *testing.T) { + cdc, _ := app.MakeCodecs() + reaction := types.NewPostReaction( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", + ":smile:", + "reaction", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ) + marshaled := types.MustMarshalPostReaction(cdc, reaction) + unmarshaled := types.MustUnmarshalPostReaction(cdc, marshaled) + require.Equal(t, reaction, unmarshaled) } From 066f431edb0f70dd60573a41f024523b55174312 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 16:32:21 +0800 Subject: [PATCH 02/26] Revise keeper funcitons of post reaction --- x/staging/posts/keeper/common_functions.go | 18 ++ x/staging/posts/keeper/genesis_test.go | 150 ++++----- x/staging/posts/keeper/invariants_test.go | 38 +-- x/staging/posts/keeper/keeper_bench_test.go | 40 ++- x/staging/posts/keeper/keeper_reactions.go | 26 +- .../posts/keeper/keeper_reactions_test.go | 290 +++++++----------- x/staging/posts/keeper/msgs_server_test.go | 4 +- x/staging/posts/simulation/decoder.go | 2 +- x/staging/posts/simulation/decoder_test.go | 16 +- x/staging/posts/simulation/genesis.go | 29 +- .../posts/simulation/operations_reactions.go | 4 +- x/staging/posts/simulation/utils.go | 2 +- 12 files changed, 271 insertions(+), 348 deletions(-) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 1caadef347..5be0903c7b 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -171,3 +171,21 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react i++ } + +func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn func(index int64, reaction types.PostReaction) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsPrefix(postID)) + defer iterator.Close() + + i := int64(0) + for ; iterator.Valid(); iterator.Next() { + reaction := types.MustUnmarshalPostReaction(k.cdc, iterator.Value()) + + stop := fn(i, reaction) + if stop { + break + } + } + + i++ +} diff --git a/x/staging/posts/keeper/genesis_test.go b/x/staging/posts/keeper/genesis_test.go index c0ba75d4f9..8fa82cd58b 100644 --- a/x/staging/posts/keeper/genesis_test.go +++ b/x/staging/posts/keeper/genesis_test.go @@ -10,31 +10,31 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { tests := []struct { name string data struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params } expected *types.GenesisState }{ { name: "Default expected state", data: struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params }{ - posts: nil, - userAnswers: nil, - postReactionsEntries: nil, - registeredReactions: nil, - reports: nil, - params: types.DefaultParams(), + posts: nil, + userAnswers: nil, + postReactions: nil, + registeredReactions: nil, + reports: nil, + params: types.DefaultParams(), }, expected: &types.GenesisState{ Params: types.DefaultParams(), @@ -43,12 +43,12 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { { name: "Genesis is exported fully", data: struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params }{ posts: []types.Post{ types.NewPost( @@ -69,10 +69,8 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { types.NewUserAnswer("post_id_1", "user", []string{"1", "2"}), types.NewUserAnswer("post_id_2", "user_@", []string{"2", "4"}), }, - postReactionsEntries: []types.PostReactionsEntry{ - types.NewPostReactionsEntry("post_id", []types.PostReaction{ - types.NewPostReaction(":emoji:", "post_id", "creator"), - }), + postReactions: []types.PostReaction{ + types.NewPostReaction("post_id", ":emoji:", "post_id", "creator"), }, registeredReactions: []types.RegisteredReaction{ types.NewRegisteredReaction("creator", ":emoji:", "value", "subspace"), @@ -113,10 +111,8 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { types.NewUserAnswer("post_id_1", "user", []string{"1", "2"}), types.NewUserAnswer("post_id_2", "user_@", []string{"2", "4"}), }, - []types.PostReactionsEntry{ - types.NewPostReactionsEntry("post_id", []types.PostReaction{ - types.NewPostReaction(":emoji:", "post_id", "creator"), - }), + []types.PostReaction{ + types.NewPostReaction("post_id", ":emoji:", "post_id", "creator"), }, []types.RegisteredReaction{ types.NewRegisteredReaction("creator", ":emoji:", "value", "subspace"), @@ -158,11 +154,9 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { suite.k.SaveUserAnswer(suite.ctx, answer) } - for _, entry := range test.data.postReactionsEntries { - for _, reaction := range entry.Reactions { - err := suite.k.SavePostReaction(suite.ctx, entry.PostID, reaction) - suite.Require().NoError(err) - } + for _, reaction := range test.data.postReactions { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) } for _, report := range test.data.reports { @@ -182,12 +176,12 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { genesis *types.GenesisState expError bool expState struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params } }{ { @@ -195,19 +189,19 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { genesis: types.DefaultGenesisState(), expError: false, expState: struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params }{ - posts: nil, - userAnswers: nil, - postReactionsEntries: nil, - registeredReactions: nil, - reports: nil, - params: types.DefaultParams(), + posts: nil, + userAnswers: nil, + postReactions: nil, + registeredReactions: nil, + reports: nil, + params: types.DefaultParams(), }, }, { @@ -240,16 +234,12 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { []string{"2", "4"}, ), }, - []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + []types.PostReaction{ + types.NewPostReaction( "a56145270ce6b3bebd1dd012b73948677dd618d496488bc608a3cb43ce3547dd", - []types.PostReaction{ - types.NewPostReaction( - ":emoji:", - "post_id", - "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", - ), - }, + ":emoji:", + "post_id", + "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", ), }, []types.RegisteredReaction{ @@ -278,12 +268,12 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { ), expError: false, expState: struct { - posts []types.Post - userAnswers []types.UserAnswer - postReactionsEntries []types.PostReactionsEntry - registeredReactions []types.RegisteredReaction - reports []types.Report - params types.Params + posts []types.Post + userAnswers []types.UserAnswer + postReactions []types.PostReaction + registeredReactions []types.RegisteredReaction + reports []types.Report + params types.Params }{ posts: []types.Post{ types.NewPost( @@ -304,16 +294,12 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { types.NewUserAnswer("a56145270ce6b3bebd1dd012b73948677dd618d496488bc608a3cb43ce3547dd", "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", []string{"1", "2"}), types.NewUserAnswer("b459afddb3a09621ee29b78b3968e566d7fb0001d96395d54030eb703b0337a9", "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", []string{"2", "4"}), }, - postReactionsEntries: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + postReactions: []types.PostReaction{ + types.NewPostReaction( "a56145270ce6b3bebd1dd012b73948677dd618d496488bc608a3cb43ce3547dd", - []types.PostReaction{ - types.NewPostReaction( - ":emoji:", - "post_id", - "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", - ), - }, + ":emoji:", + "post_id", + "cosmos1u3cjgn7t7v6edpy2szvydxucarzkyjj26az3k8", ), }, registeredReactions: []types.RegisteredReaction{ @@ -372,8 +358,8 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { genesis: types.NewGenesisState( nil, nil, - []types.PostReactionsEntry{ - types.NewPostReactionsEntry("post_id", []types.PostReaction{}), + []types.PostReaction{ + types.NewPostReaction("post_id", "", "", ""), }, nil, nil, @@ -425,7 +411,7 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() { suite.Require().Equal(test.expState.posts, suite.k.GetPosts(suite.ctx)) suite.Require().Equal(test.expState.registeredReactions, suite.k.GetRegisteredReactions(suite.ctx)) - suite.Require().Equal(test.expState.postReactionsEntries, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expState.postReactions, suite.k.GetAllPostReactions(suite.ctx)) suite.Require().Equal(test.expState.userAnswers, suite.k.GetAllUserAnswers(suite.ctx)) suite.Require().Equal(test.expState.params, suite.k.GetParams(suite.ctx)) } diff --git a/x/staging/posts/keeper/invariants_test.go b/x/staging/posts/keeper/invariants_test.go index 3c9635b648..7f24185688 100644 --- a/x/staging/posts/keeper/invariants_test.go +++ b/x/staging/posts/keeper/invariants_test.go @@ -12,7 +12,7 @@ func (suite *KeeperTestSuite) TestInvariants() { name string posts []types.Post answers []types.UserAnswer - postReactions []types.PostReactionsEntry + postReactions []types.PostReaction registeredReactions []types.RegisteredReaction expStop bool }{ @@ -44,16 +44,12 @@ func (suite *KeeperTestSuite) TestInvariants() { answers: []types.UserAnswer{ types.NewUserAnswer("19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", suite.testData.post.Creator, []string{"1", "2"}), }, - postReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + postReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "+1", - "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", - ), - }, + ":like:", + "+1", + "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", ), }, registeredReactions: []types.RegisteredReaction{ @@ -114,16 +110,12 @@ func (suite *KeeperTestSuite) TestInvariants() { name: "ValidPostForReactions Invariants violated", posts: []types.Post{}, answers: nil, - postReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + postReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "+1", - "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", - ), - }, + ":like:", + "+1", + "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", ), }, registeredReactions: []types.RegisteredReaction{ @@ -173,11 +165,9 @@ func (suite *KeeperTestSuite) TestInvariants() { suite.k.SaveRegisteredReaction(suite.ctx, reaction) } - for _, entry := range test.postReactions { - for _, reaction := range entry.Reactions { - err := suite.k.SavePostReaction(suite.ctx, entry.PostID, reaction) - suite.Require().NoError(err) - } + for _, reaction := range test.postReactions { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) } for _, answer := range test.answers { diff --git a/x/staging/posts/keeper/keeper_bench_test.go b/x/staging/posts/keeper/keeper_bench_test.go index 0dede2140b..8580eff51d 100644 --- a/x/staging/posts/keeper/keeper_bench_test.go +++ b/x/staging/posts/keeper/keeper_bench_test.go @@ -69,36 +69,34 @@ func (suite *KeeperTestSuite) BenchmarkKeeper_SavePostReaction(b *testing.B) { suite.k.SavePost(suite.ctx, RandomPost()) } - posts := suite.k.GetPosts(suite.ctx) - post := posts[r.Intn(len(posts))] reaction := postssim.RandomEmojiPostReaction(r) b.ResetTimer() for i := 0; i < b.N; i++ { - err := suite.k.SavePostReaction(suite.ctx, post.PostID, reaction) + err := suite.k.SavePostReaction(suite.ctx, reaction) suite.Require().NoError(err) } } -func (suite *KeeperTestSuite) BenchmarkKeeper_GetPostReactions(b *testing.B) { - fmt.Println("Benchmark Get a post registeredReactions") - r := rand.New(rand.NewSource(100)) +// func (suite *KeeperTestSuite) BenchmarkKeeper_GetPostReactions(b *testing.B) { +// fmt.Println("Benchmark Get a post registeredReactions") +// r := rand.New(rand.NewSource(100)) - for i := 0; i < b.N; i++ { - suite.k.SavePost(suite.ctx, RandomPost()) - } +// for i := 0; i < b.N; i++ { +// suite.k.SavePost(suite.ctx, RandomPost()) +// } - posts := suite.k.GetPosts(suite.ctx) - post := posts[r.Intn(len(posts))] - reaction := postssim.RandomEmojiPostReaction(r) +// posts := suite.k.GetPosts(suite.ctx) +// post := posts[r.Intn(len(posts))] +// reaction := postssim.RandomEmojiPostReaction(r) - for i := 0; i < b.N; i++ { - err := suite.k.SavePostReaction(suite.ctx, post.PostID, reaction) - suite.Require().NoError(err) - } +// for i := 0; i < b.N; i++ { +// err := suite.k.SavePostReaction(suite.ctx, post.PostID, reaction) +// suite.Require().NoError(err) +// } - b.ResetTimer() - for i := 0; i < b.N; i++ { - suite.k.GetPostReactions(suite.ctx, post.PostID) - } -} +// b.ResetTimer() +// for i := 0; i < b.N; i++ { +// suite.k.GetPostReactions(suite.ctx, post.PostID) +// } +// } diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index 0ff63e03ae..7431df273d 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -13,8 +13,8 @@ import ( // If another reaction from the same user for the same post and with the same value exists, returns an expError. func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) error { store := ctx.KVStore(k.storeKey) - key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) + key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) // Check for double reactions if store.Has(key) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, @@ -24,7 +24,6 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) e // Save the new reaction store.Set(key, k.cdc.MustMarshalBinaryBare(&reaction)) - return nil } @@ -33,8 +32,8 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) e // is returned. func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) error { store := ctx.KVStore(k.storeKey) - key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) + key := types.PostReactionsStoreKey(reaction.PostID, reaction.Owner, reaction.ShortCode) if !store.Has(key) { return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot remove the reaction with value %s from user %s as it does not exist", @@ -45,7 +44,16 @@ func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) return nil } -// GetAllPostReactions returns the list of reactions that has been associated to the post having the given id +// GetPostReactions returns the list of reactions that has been associated to the post having the given id +func (k Keeper) GetPostReactions(ctx sdk.Context, postID string) []types.PostReaction { + var reactions []types.PostReaction + k.IteratePostReactionsByPost(ctx, postID, func(_ int64, reaction types.PostReaction) bool { + reactions = append(reactions, reaction) + return false + }) + return reactions +} + func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { var reactions []types.PostReaction k.IteratePostReactions(ctx, func(_ int64, reaction types.PostReaction) bool { @@ -55,6 +63,16 @@ func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { return reactions } +func (k Keeper) GetPostReaction(ctx sdk.Context, postID, owner, shortCode string) (types.PostReaction, bool) { + store := ctx.KVStore(k.storeKey) + + key := types.PostReactionsStoreKey(postID, owner, shortCode) + if !store.Has(key) { + return types.PostReaction{}, false + } + return types.MustUnmarshalPostReaction(k.cdc, store.Get(key)), true +} + // ___________________________________________________________________________________________________________________ // SaveRegisteredReaction allows to register a new reaction for later reference diff --git a/x/staging/posts/keeper/keeper_reactions_test.go b/x/staging/posts/keeper/keeper_reactions_test.go index 512a973bab..57497a6975 100644 --- a/x/staging/posts/keeper/keeper_reactions_test.go +++ b/x/staging/posts/keeper/keeper_reactions_test.go @@ -8,11 +8,10 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { tests := []struct { name string storedPosts []types.Post - storedReactions []types.PostReactionsEntry - postID string + storedReactions []types.PostReaction reaction types.PostReaction expError bool - expectedStored []types.PostReactionsEntry + expectedStored []types.PostReaction }{ { name: "Reaction from same user already present returns error", @@ -29,31 +28,28 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { Creator: suite.testData.post.Creator, }, }, - storedReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + storedReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }, + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ), }, - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction(":like:", "👍", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4"), + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), expError: true, - expectedStored: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + expectedStored: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }), + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), }, }, { @@ -71,19 +67,20 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { Creator: suite.testData.post.Creator, }, }, - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction(":like:", "👍", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4"), + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), expError: false, - expectedStored: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + expectedStored: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }), + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), }, }, { @@ -101,36 +98,34 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { Creator: suite.testData.post.Creator, }, }, - storedReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + storedReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }, + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ), }, - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction(":like:", "👍", "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae"), + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + ), expError: false, - expectedStored: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + expectedStored: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - types.NewPostReaction( - ":like:", - "👍", - "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - ), - }), + ":like:", + "👍", + "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + ), + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), }, }, } @@ -144,20 +139,18 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { suite.k.SavePost(suite.ctx, post) } - for _, entry := range test.storedReactions { - for _, reaction := range entry.Reactions { - err := suite.k.SavePostReaction(suite.ctx, entry.PostID, reaction) - suite.Require().NoError(err) - } + for _, reaction := range test.storedReactions { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) } - err := suite.k.SavePostReaction(suite.ctx, test.postID, test.reaction) + err := suite.k.SavePostReaction(suite.ctx, test.reaction) if test.expError { suite.Require().Error(err) } else { suite.Require().NoError(err) - suite.Require().Equal(test.expectedStored, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expectedStored, suite.k.GetAllPostReactions(suite.ctx)) } }) } @@ -166,81 +159,56 @@ func (suite *KeeperTestSuite) TestKeeper_SavePostReaction() { func (suite *KeeperTestSuite) TestKeeper_DeletePostReaction() { tests := []struct { name string - storedReactions []types.PostReactionsEntry - data struct { - postID string - reaction types.PostReaction - } - expError bool - expReactions []types.PostReactionsEntry + storedReactions []types.PostReaction + reaction types.PostReaction + expError bool + expReactions []types.PostReaction }{ { name: "Exiting reaction is removed properly", - storedReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + storedReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }, - ), - }, - data: struct { - postID string - reaction types.PostReaction - }{ - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction( ":like:", "👍", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ), }, + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), expError: false, expReactions: nil, }, { name: "Non existing reaction returns error (different creator)", - data: struct { - postID string - reaction types.PostReaction - }{ - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }, + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":like:", + "👍", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), expError: true, }, { name: "Non existing reaction returns error (different reaction)", - storedReactions: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + storedReactions: []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":like:", - "👍", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }), - }, - data: struct { - postID string - reaction types.PostReaction - }{ - postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - reaction: types.NewPostReaction( - ":smile:", - "😊", + ":like:", + "👍", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ), }, + reaction: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "😊", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), expError: true, }, } @@ -250,20 +218,18 @@ func (suite *KeeperTestSuite) TestKeeper_DeletePostReaction() { suite.Run(test.name, func() { suite.SetupTest() - for _, entry := range test.storedReactions { - for _, reaction := range entry.Reactions { - err := suite.k.SavePostReaction(suite.ctx, entry.PostID, reaction) - suite.Require().NoError(err) - } + for _, reaction := range test.storedReactions { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) } - err := suite.k.DeletePostReaction(suite.ctx, test.data.postID, test.data.reaction) + err := suite.k.DeletePostReaction(suite.ctx, test.reaction) if test.expError { suite.Require().Error(err) } else { suite.Require().NoError(err) - suite.Require().Equal(test.expReactions, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expReactions, suite.k.GetAllPostReactions(suite.ctx)) } }) } @@ -286,8 +252,18 @@ func (suite *KeeperTestSuite) TestKeeper_GetPostReactions() { { name: "Valid list of reactions is returned properly", reactions: []types.PostReaction{ - types.NewPostReaction(":smile:", "😊", "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae"), - types.NewPostReaction(":smile:", "😊", "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4"), + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "😊", + "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + ), + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "😊", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), }, postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", storedPost: suite.testData.post, @@ -301,7 +277,7 @@ func (suite *KeeperTestSuite) TestKeeper_GetPostReactions() { for _, l := range test.reactions { suite.k.SavePost(suite.ctx, test.storedPost) suite.k.SaveRegisteredReaction(suite.ctx, test.registeredReaction) - err := suite.k.SavePostReaction(suite.ctx, test.postID, l) + err := suite.k.SavePostReaction(suite.ctx, l) suite.Require().NoError(err) } @@ -315,62 +291,6 @@ func (suite *KeeperTestSuite) TestKeeper_GetPostReactions() { } } -func (suite *KeeperTestSuite) TestKeeper_GetPostReactionsEntries() { - tests := []struct { - name string - entries []types.PostReactionsEntry - }{ - { - name: "Empty reactions data are returned correctly", - entries: nil, - }, - { - name: "Non empty reactions data are returned correcly", - entries: []types.PostReactionsEntry{ - types.NewPostReactionsEntry( - "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":smile:", - "😊", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - types.NewPostReaction( - ":smile:", - "😊", - "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", - ), - }, - ), - types.NewPostReactionsEntry( - "f1b909289cd23188c19da17ae5d5a05ad65623b0fad756e5e03c8c936ca876fd", - []types.PostReaction{ - types.NewPostReaction( - ":smile:", - "😊", - "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", - ), - }, - ), - }, - }, - } - - for _, test := range tests { - test := test - suite.Run(test.name, func() { - store := suite.ctx.KVStore(suite.storeKey) - for _, entry := range test.entries { - wrapped := types.PostReactions{Reactions: entry.Reactions} - store.Set(types.PostReactionsStoreKey(entry.PostID), suite.cdc.MustMarshalBinaryBare(&wrapped)) - } - - likesData := suite.k.GetPostReactionsEntries(suite.ctx) - suite.Require().Equal(test.entries, likesData) - }) - } -} - // ___________________________________________________________________________________________________________________ func (suite *KeeperTestSuite) TestKeeper_SaveRegisteredReaction() { diff --git a/x/staging/posts/keeper/msgs_server_test.go b/x/staging/posts/keeper/msgs_server_test.go index 4aa462834a..ed98163e42 100644 --- a/x/staging/posts/keeper/msgs_server_test.go +++ b/x/staging/posts/keeper/msgs_server_test.go @@ -575,7 +575,7 @@ func (suite *KeeperTestSuite) TestMsgServer_AddPostReaction() { } else { suite.Require().NoError(err) suite.Require().Len(suite.ctx.EventManager().Events(), 1) - suite.Require().Equal(test.expPostReactions, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expPostReactions, suite.k.GetAllPostReactions(suite.ctx)) } }) } @@ -767,7 +767,7 @@ func (suite *KeeperTestSuite) TestMsgServer_RemovePostReaction() { } else { suite.Require().NoError(err) suite.Require().Equal(test.expEvents, suite.ctx.EventManager().Events()) - suite.Require().Equal(test.expReactions, suite.k.GetPostReactionsEntries(suite.ctx)) + suite.Require().Equal(test.expReactions, suite.k.GetAllPostReactions(suite.ctx)) } }) } diff --git a/x/staging/posts/simulation/decoder.go b/x/staging/posts/simulation/decoder.go index 18eda74812..52adca23a1 100644 --- a/x/staging/posts/simulation/decoder.go +++ b/x/staging/posts/simulation/decoder.go @@ -31,7 +31,7 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { var postReactionsA, postReactionsB types.PostReaction cdc.MustUnmarshalBinaryBare(kvA.Value, &postReactionsA) cdc.MustUnmarshalBinaryBare(kvB.Value, &postReactionsB) - return fmt.Sprintf("PostReactionsA: %s\nPostReactionsB: %s\n", postReactionsA, postReactionsB) + return fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReactionsA, postReactionsB) case bytes.HasPrefix(kvA.Key, types.RegisteredReactionsStorePrefix): var reactionA, reactionB types.RegisteredReaction diff --git a/x/staging/posts/simulation/decoder_test.go b/x/staging/posts/simulation/decoder_test.go index c34c2461db..df7c889be9 100644 --- a/x/staging/posts/simulation/decoder_test.go +++ b/x/staging/posts/simulation/decoder_test.go @@ -57,10 +57,12 @@ func TestDecodeStore(t *testing.T) { "f1b909289cd23188c19da17ae5d5a05ad65623b0fad756e5e03c8c936ca876fd", "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", }} - postReactions := types.PostReactions{Reactions: []types.PostReaction{ - types.NewPostReaction(":thumbsup:", "👍", address), - types.NewPostReaction("blue_heart:", "💙", address), - }} + postReaction := types.NewPostReaction( + "e1ba4807a15d8579f79cfd90a07fc015e6125565c9271eb94aded0b2ebf86163", + "blue_heart:", + "💙", + address, + ) registeredReaction := types.NewRegisteredReaction( address, @@ -95,8 +97,8 @@ func TestDecodeStore(t *testing.T) { Value: cdc.MustMarshalBinaryBare(&comments), }, { - Key: types.PostReactionsStoreKey(post.PostID), - Value: cdc.MustMarshalBinaryBare(&postReactions), + Key: types.PostReactionsStoreKey(postReaction.PostID, postReaction.Owner, postReaction.ShortCode), + Value: cdc.MustMarshalBinaryBare(&postReaction), }, { Key: types.RegisteredReactionsStoreKey(registeredReaction.Subspace, registeredReaction.ShortCode), @@ -114,7 +116,7 @@ func TestDecodeStore(t *testing.T) { }{ {"Post", fmt.Sprintf("PostA: %s\nPostB: %s\n", post.String(), post.String())}, {"Comments", fmt.Sprintf("CommentsA: %s\nCommentsB: %s\n", comments, comments)}, - {"PostReactions", fmt.Sprintf("PostReactionsA: %s\nPostReactionsB: %s\n", postReactions, postReactions)}, + {"PostReactions", fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReaction, postReaction)}, {"Reactions", fmt.Sprintf("ReactionA: %s\nReactionB: %s\n", registeredReaction, registeredReaction)}, {"Report", fmt.Sprintf("ReportsA: %s\nReportsB: %s\n", reports, reports)}, {"other", ""}, diff --git a/x/staging/posts/simulation/genesis.go b/x/staging/posts/simulation/genesis.go index 6d2541fecb..c03604f0b5 100644 --- a/x/staging/posts/simulation/genesis.go +++ b/x/staging/posts/simulation/genesis.go @@ -27,7 +27,7 @@ func RandomizedGenState(simState *module.SimulationState) { postsGenesis := types.NewGenesisState( posts, nil, - randomPostReactionsEntries(simState.Rand, posts, reactionsData), + randomPostReactions(simState.Rand, posts, reactionsData), registeredReactions(reactionsData), randomReports(simState), randomParams(simState), @@ -55,31 +55,22 @@ func randomPosts(simState *module.SimulationState) (posts []types.Post) { return posts } -// randomPostReactionsEntries returns a randomly generated list of reactions entries -func randomPostReactionsEntries(r *rand.Rand, posts []types.Post, reactionsData []ReactionData) []types.PostReactionsEntry { +// randomPostReactions returns a randomly generated list of reactions +func randomPostReactions(r *rand.Rand, posts []types.Post, reactionsData []ReactionData) []types.PostReaction { if len(posts) == 0 { return nil } - reactionsNumber := r.Intn(len(posts)) - - entries := make([]types.PostReactionsEntry, reactionsNumber) - for i := 0; i < reactionsNumber; i++ { - reactionsLen := r.Intn(20) - reactions := make([]types.PostReaction, reactionsLen) - - for j := 0; j < reactionsLen; j++ { - privKey := ed25519.GenPrivKey().PubKey() - data := reactionsData[r.Intn(len(reactionsData))] - - reactions[j] = types.NewPostReaction(data.ShortCode, data.Value, sdk.AccAddress(privKey.Address()).String()) - } + postsNumber := r.Intn(len(posts)) + reactions := make([]types.PostReaction, postsNumber*20) + for i := 0; i < postsNumber; i++ { id := RandomPostIDFromPosts(r, posts) - entries[i] = types.NewPostReactionsEntry(id, reactions) + privKey := ed25519.GenPrivKey().PubKey() + data := reactionsData[r.Intn(len(reactionsData))] + reactions[i] = types.NewPostReaction(id, data.ShortCode, data.Value, sdk.AccAddress(privKey.Address()).String()) } - - return entries + return reactions } // registeredReactions returns all the possible registered reactions diff --git a/x/staging/posts/simulation/operations_reactions.go b/x/staging/posts/simulation/operations_reactions.go index bf2ce00471..44b3b11476 100644 --- a/x/staging/posts/simulation/operations_reactions.go +++ b/x/staging/posts/simulation/operations_reactions.go @@ -111,8 +111,8 @@ func randomAddPostReactionFields( } // Skip if the reaction already exists - reactions := types.NewPostReactions(k.GetPostReactions(ctx, post.PostID)...) - if reactions.ContainsReactionFrom(reactionData.User.Address.String(), reactionData.Value) { + _, found := k.GetPostReaction(ctx, post.PostID, reactionData.User.Address.String(), reaction.ShortCode) + if found { return nil, true } diff --git a/x/staging/posts/simulation/utils.go b/x/staging/posts/simulation/utils.go index 3e0132fcfd..dfbbe01778 100644 --- a/x/staging/posts/simulation/utils.go +++ b/x/staging/posts/simulation/utils.go @@ -253,7 +253,7 @@ func RandomEmojiPostReaction(r *rand.Rand) types.PostReaction { creator := accounts[r.Intn(len(accounts))].Address rEmoji := emoji.EmojisList[r.Intn(len(emoji.EmojisList))] - return types.NewPostReaction(rEmoji.Shortcodes[0], rEmoji.Value, creator.String()) + return types.NewPostReaction(RandomPostID(r), rEmoji.Shortcodes[0], rEmoji.Value, creator.String()) } func RandomParams(r *rand.Rand) types.Params { From 795df304982c830b33a1ea9cfdd87dec34d65c91 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 16:33:38 +0800 Subject: [PATCH 03/26] Fix genesis of cli test --- x/staging/posts/client/cli/cli_test.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/x/staging/posts/client/cli/cli_test.go b/x/staging/posts/client/cli/cli_test.go index e2bde12acc..ebcbc0c97e 100644 --- a/x/staging/posts/client/cli/cli_test.go +++ b/x/staging/posts/client/cli/cli_test.go @@ -102,16 +102,13 @@ func (s *IntegrationTestSuite) SetupSuite() { []string{"1"}, ), } - postsData.PostsReactions = []types.PostReactionsEntry{ - types.NewPostReactionsEntry( + postsData.PostsReactions = []types.PostReaction{ + types.NewPostReaction( "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", - []types.PostReaction{ - types.NewPostReaction( - ":broken_heart:", - "💔", - "cosmos12t08qkk4dm2pqgyy8hmq5hx92y2m29zedmdw7f", - ), - }), + ":broken_heart:", + "💔", + "cosmos12t08qkk4dm2pqgyy8hmq5hx92y2m29zedmdw7f", + ), } postsData.Reports = []types.Report{ From d960fe62d3aab791c496284f822b9f4f283d654f Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 16:45:15 +0800 Subject: [PATCH 04/26] Fix bugs oiterate functions --- x/staging/posts/keeper/common_functions.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 5be0903c7b..131d7a6b47 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -149,9 +149,9 @@ func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, if stop { break } - } - i++ + i++ + } } func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, reaction types.PostReaction) (stop bool)) { @@ -167,9 +167,10 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react if stop { break } - } - i++ + i++ + + } } func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn func(index int64, reaction types.PostReaction) (stop bool)) { @@ -185,7 +186,7 @@ func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn fu if stop { break } - } - i++ + i++ + } } From 81a764c2a2186f705370ea8e32faea800081b67c Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 19:08:52 +0800 Subject: [PATCH 05/26] Add post reactions query method --- proto/desmos/posts/v1beta1/query.proto | 30 +- x/staging/posts/client/cli/cli_test.go | 6 +- x/staging/posts/keeper/grpc_query.go | 35 +- x/staging/posts/keeper/grpc_query_test.go | 95 +++- x/staging/posts/types/query.pb.go | 651 +++++++++++++++++++--- x/staging/posts/types/query.pb.gw.go | 116 ++++ 6 files changed, 859 insertions(+), 74 deletions(-) diff --git a/proto/desmos/posts/v1beta1/query.proto b/proto/desmos/posts/v1beta1/query.proto index 8e8a2d542b..dde61583ed 100644 --- a/proto/desmos/posts/v1beta1/query.proto +++ b/proto/desmos/posts/v1beta1/query.proto @@ -47,6 +47,12 @@ service Query { rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { option (google.api.http).get = "/desmos/posts/v1beta1/posts/params"; } + + // PostReactions queries all the reactions of the post having the given id + rpc PostReactions(QueryPostReactionsRequest) + returns (QueryPostReactionsResponse) { + option (google.api.http).get = "/desmos/posts/v1beta1/posts/{post_id}/reactions"; + } } // ___________________________________________________________________________________________________________________ @@ -92,7 +98,7 @@ message QueryUserAnswersRequest { option (gogoproto.goproto_getters) = false; string post_id = 1 - [ (gogoproto.jsontag) = "post_id", (gogoproto.moretags) = "yaml:\"id\"" ]; + [ (gogoproto.jsontag) = "post_id", (gogoproto.moretags) = "yaml:\"post_id\"" ]; string user = 2; @@ -125,7 +131,7 @@ message QueryRegisteredReactionsRequest { // QueryRegisteredReactionsResponse is the response type for the // Query/RegisteredReactions RPC method message QueryRegisteredReactionsResponse { - repeated desmos.posts.v1beta1.RegisteredReaction registered_reactions = 1 + repeated desmos.posts.v1beta1.RegisteredReaction reactions = 1 [ (gogoproto.nullable) = false ]; cosmos.base.query.v1beta1.PageResponse pagination = 2; @@ -158,3 +164,23 @@ message QueryReportsResponse { repeated desmos.posts.v1beta1.Report reports = 1 [ (gogoproto.nullable) = false ]; } + +// ___________________________________________________________________________________________________________________ + + +// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC method. +message QueryPostReactionsRequest { + string post_id = 1 + [ (gogoproto.jsontag) = "post_id", (gogoproto.moretags) = "yaml:\"post_id\"" ]; + + // pagination defines an optional pagination for the request. + cosmos.base.query.v1beta1.PageRequest pagination = 2; +} + +// QueryPostReactionsResponse is the response type for the Query/PostReactions RPC method +message QueryPostReactionsResponse { + repeated desmos.posts.v1beta1.PostReaction reactions = 1 + [ (gogoproto.nullable) = false ]; + + cosmos.base.query.v1beta1.PageResponse pagination = 2; +} \ No newline at end of file diff --git a/x/staging/posts/client/cli/cli_test.go b/x/staging/posts/client/cli/cli_test.go index ebcbc0c97e..01573f7a92 100644 --- a/x/staging/posts/client/cli/cli_test.go +++ b/x/staging/posts/client/cli/cli_test.go @@ -377,7 +377,7 @@ func (s *IntegrationTestSuite) TestCmdQueryRegisteredReactions() { args: []string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, expectErr: false, expectedOutput: types.QueryRegisteredReactionsResponse{ - RegisteredReactions: []types.RegisteredReaction{ + Reactions: []types.RegisteredReaction{ types.NewRegisteredReaction( "cosmos1lhhkerae9cu3fa442vt50t32grlajun5lmrv3g", ":reaction:", @@ -402,7 +402,7 @@ func (s *IntegrationTestSuite) TestCmdQueryRegisteredReactions() { args: []string{"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}, expectErr: false, expectedOutput: types.QueryRegisteredReactionsResponse{ - RegisteredReactions: []types.RegisteredReaction{ + Reactions: []types.RegisteredReaction{ types.NewRegisteredReaction( "cosmos1lhhkerae9cu3fa442vt50t32grlajun5lmrv3g", ":reaction:", @@ -425,7 +425,7 @@ func (s *IntegrationTestSuite) TestCmdQueryRegisteredReactions() { }, expectErr: false, expectedOutput: types.QueryRegisteredReactionsResponse{ - RegisteredReactions: []types.RegisteredReaction{ + Reactions: []types.RegisteredReaction{ types.NewRegisteredReaction( "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", ":smile-jpg:", diff --git a/x/staging/posts/keeper/grpc_query.go b/x/staging/posts/keeper/grpc_query.go index 0736fcd9bf..227370b01f 100644 --- a/x/staging/posts/keeper/grpc_query.go +++ b/x/staging/posts/keeper/grpc_query.go @@ -120,7 +120,7 @@ func (k Keeper) RegisteredReactions(goCtx context.Context, req *types.QueryRegis if err != nil { return nil, status.Error(codes.Internal, err.Error()) } - return &types.QueryRegisteredReactionsResponse{RegisteredReactions: reactions, Pagination: pageRes}, nil + return &types.QueryRegisteredReactionsResponse{Reactions: reactions, Pagination: pageRes}, nil } // Reports implements the Query/Reports gRPC method @@ -138,3 +138,36 @@ func (k Keeper) Params(goCtx context.Context, _ *types.QueryParamsRequest) (*typ params := k.GetParams(ctx) return &types.QueryParamsResponse{Params: params}, nil } + +// PostReactions implements the Query/PostReactions gRPC method +func (k Keeper) PostReactions(goCtx context.Context, req *types.QueryPostReactionsRequest) (*types.QueryPostReactionsResponse, error) { + if !types.IsValidPostID(req.PostId) { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid post id: %s", req.PostId) + } + + ctx := sdk.UnwrapSDKContext(goCtx) + + _, found := k.GetPost(ctx, req.PostId) + if !found { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post with id %s not found", req.PostId) + } + + var reactions []types.PostReaction + + store := ctx.KVStore(k.storeKey) + reactionsStore := prefix.NewStore(store, types.PostReactionsPrefix(req.PostId)) + + pageRes, err := query.FilteredPaginate(reactionsStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { + reaction := types.MustUnmarshalPostReaction(k.cdc, value) + if accumulate { + reactions = append(reactions, reaction) + } + return true, nil + }) + + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + + return &types.QueryPostReactionsResponse{Reactions: reactions, Pagination: pageRes}, nil +} diff --git a/x/staging/posts/keeper/grpc_query_test.go b/x/staging/posts/keeper/grpc_query_test.go index 7d7dddba00..67be8a0377 100644 --- a/x/staging/posts/keeper/grpc_query_test.go +++ b/x/staging/posts/keeper/grpc_query_test.go @@ -297,7 +297,7 @@ func (suite *KeeperTestSuite) Test_RegisteredReactions() { res, err := suite.k.RegisteredReactions(sdk.WrapSDKContext(suite.ctx), uc.req) suite.Require().NoError(err) suite.Require().NotNil(res) - suite.Require().Equal(uc.expLen, len(res.RegisteredReactions)) + suite.Require().Equal(uc.expLen, len(res.Reactions)) }) } } @@ -475,3 +475,96 @@ func (suite *KeeperTestSuite) Test_UserAnswers() { }) } } + +func (suite *KeeperTestSuite) Test_PostReactions() { + + creationDate, err := time.Parse(time.RFC3339, "2020-01-01T15:15:00.000Z") + suite.Require().NoError(err) + + post := types.Post{ + PostID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + Message: "Post message", + Created: creationDate, + LastEdited: creationDate.Add(1), + Subspace: "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", + Creator: "cosmos1y54exmx84cqtasvjnskf9f63djuuj68p7hqf47", + } + + reactions := []types.PostReaction{ + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "reaction", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + ), + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "reaction", + "cosmos1y54exmx84cqtasvjnskf9f63djuuj68p7hqf47", + ), + } + + usecases := []struct { + name string + store func(ctx sdk.Context) + req *types.QueryPostReactionsRequest + shouldErr bool + expLen int + }{ + { + name: "invalid post id returns error", + req: &types.QueryPostReactionsRequest{}, + shouldErr: true, + }, + { + name: "non existent post return error", + req: &types.QueryPostReactionsRequest{PostId: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af"}, + shouldErr: true, + }, + { + name: "valid request returns properly", + store: func(ctx sdk.Context) { + suite.k.SavePost(ctx, post) + for _, reaction := range reactions { + suite.k.SavePostReaction(ctx, reaction) + } + }, + req: &types.QueryPostReactionsRequest{PostId: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af"}, + shouldErr: false, + expLen: 2, + }, + { + name: "valid request with pagination returns properly", + store: func(ctx sdk.Context) { + suite.k.SavePost(ctx, post) + for _, reaction := range reactions { + suite.k.SavePostReaction(ctx, reaction) + } + }, + req: &types.QueryPostReactionsRequest{ + PostId: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + Pagination: &query.PageRequest{Limit: 1}, + }, + shouldErr: false, + expLen: 1, + }, + } + suite.SetupTest() + for _, uc := range usecases { + suite.Run(uc.name, func() { + ctx, _ := suite.ctx.CacheContext() + if uc.store != nil { + uc.store(ctx) + } + res, err := suite.k.PostReactions(sdk.WrapSDKContext(ctx), uc.req) + if uc.shouldErr { + suite.Require().Error(err) + } else { + suite.Require().NoError(err) + suite.Require().NotNil(res) + suite.Require().Equal(uc.expLen, len(res.Reactions)) + } + }) + } +} diff --git a/x/staging/posts/types/query.pb.go b/x/staging/posts/types/query.pb.go index 2556365a48..3dfd2fd2c7 100644 --- a/x/staging/posts/types/query.pb.go +++ b/x/staging/posts/types/query.pb.go @@ -226,7 +226,7 @@ func (m *QueryPostResponse) GetPost() Post { // QueryUserAnswersRequest is the request type for the Query/UserAnswers RPC // method. type QueryUserAnswersRequest struct { - PostId string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"id"` + PostId string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"post_id"` User string `protobuf:"bytes,2,opt,name=user,proto3" json:"user,omitempty"` // pagination defines an optional pagination for the request. Pagination *query.PageRequest `protobuf:"bytes,3,opt,name=pagination,proto3" json:"pagination,omitempty"` @@ -378,8 +378,8 @@ func (m *QueryRegisteredReactionsRequest) GetPagination() *query.PageRequest { // QueryRegisteredReactionsResponse is the response type for the // Query/RegisteredReactions RPC method type QueryRegisteredReactionsResponse struct { - RegisteredReactions []RegisteredReaction `protobuf:"bytes,1,rep,name=registered_reactions,json=registeredReactions,proto3" json:"registered_reactions"` - Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` + Reactions []RegisteredReaction `protobuf:"bytes,1,rep,name=reactions,proto3" json:"reactions"` + Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` } func (m *QueryRegisteredReactionsResponse) Reset() { *m = QueryRegisteredReactionsResponse{} } @@ -415,9 +415,9 @@ func (m *QueryRegisteredReactionsResponse) XXX_DiscardUnknown() { var xxx_messageInfo_QueryRegisteredReactionsResponse proto.InternalMessageInfo -func (m *QueryRegisteredReactionsResponse) GetRegisteredReactions() []RegisteredReaction { +func (m *QueryRegisteredReactionsResponse) GetReactions() []RegisteredReaction { if m != nil { - return m.RegisteredReactions + return m.Reactions } return nil } @@ -596,6 +596,113 @@ func (m *QueryReportsResponse) GetReports() []Report { return nil } +// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC method. +type QueryPostReactionsRequest struct { + PostId string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"post_id"` + // pagination defines an optional pagination for the request. + Pagination *query.PageRequest `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` +} + +func (m *QueryPostReactionsRequest) Reset() { *m = QueryPostReactionsRequest{} } +func (m *QueryPostReactionsRequest) String() string { return proto.CompactTextString(m) } +func (*QueryPostReactionsRequest) ProtoMessage() {} +func (*QueryPostReactionsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_b6258583fb7a3a9d, []int{12} +} +func (m *QueryPostReactionsRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryPostReactionsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryPostReactionsRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryPostReactionsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryPostReactionsRequest.Merge(m, src) +} +func (m *QueryPostReactionsRequest) XXX_Size() int { + return m.Size() +} +func (m *QueryPostReactionsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QueryPostReactionsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryPostReactionsRequest proto.InternalMessageInfo + +func (m *QueryPostReactionsRequest) GetPostId() string { + if m != nil { + return m.PostId + } + return "" +} + +func (m *QueryPostReactionsRequest) GetPagination() *query.PageRequest { + if m != nil { + return m.Pagination + } + return nil +} + +// QueryPostReactionsResponse is the response type for the Query/PostReactions RPC method +type QueryPostReactionsResponse struct { + Reactions []PostReaction `protobuf:"bytes,1,rep,name=reactions,proto3" json:"reactions"` + Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` +} + +func (m *QueryPostReactionsResponse) Reset() { *m = QueryPostReactionsResponse{} } +func (m *QueryPostReactionsResponse) String() string { return proto.CompactTextString(m) } +func (*QueryPostReactionsResponse) ProtoMessage() {} +func (*QueryPostReactionsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_b6258583fb7a3a9d, []int{13} +} +func (m *QueryPostReactionsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryPostReactionsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryPostReactionsResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryPostReactionsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryPostReactionsResponse.Merge(m, src) +} +func (m *QueryPostReactionsResponse) XXX_Size() int { + return m.Size() +} +func (m *QueryPostReactionsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QueryPostReactionsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryPostReactionsResponse proto.InternalMessageInfo + +func (m *QueryPostReactionsResponse) GetReactions() []PostReaction { + if m != nil { + return m.Reactions + } + return nil +} + +func (m *QueryPostReactionsResponse) GetPagination() *query.PageResponse { + if m != nil { + return m.Pagination + } + return nil +} + func init() { proto.RegisterType((*QueryPostsRequest)(nil), "desmos.posts.v1beta1.QueryPostsRequest") proto.RegisterType((*QueryPostsResponse)(nil), "desmos.posts.v1beta1.QueryPostsResponse") @@ -609,65 +716,72 @@ func init() { proto.RegisterType((*QueryParamsResponse)(nil), "desmos.posts.v1beta1.QueryParamsResponse") proto.RegisterType((*QueryReportsRequest)(nil), "desmos.posts.v1beta1.QueryReportsRequest") proto.RegisterType((*QueryReportsResponse)(nil), "desmos.posts.v1beta1.QueryReportsResponse") + proto.RegisterType((*QueryPostReactionsRequest)(nil), "desmos.posts.v1beta1.QueryPostReactionsRequest") + proto.RegisterType((*QueryPostReactionsResponse)(nil), "desmos.posts.v1beta1.QueryPostReactionsResponse") } func init() { proto.RegisterFile("desmos/posts/v1beta1/query.proto", fileDescriptor_b6258583fb7a3a9d) } var fileDescriptor_b6258583fb7a3a9d = []byte{ - // 840 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x56, 0xc1, 0x6e, 0xd3, 0x4a, - 0x14, 0xcd, 0xb4, 0x69, 0xf2, 0x3a, 0xd9, 0xbc, 0x37, 0x8d, 0xd4, 0xc8, 0xaf, 0xc4, 0xc1, 0x94, - 0x26, 0xad, 0x14, 0x9b, 0xa6, 0xb4, 0xaa, 0x2a, 0x16, 0xd0, 0x05, 0xa8, 0x0b, 0x44, 0x6b, 0xc1, - 0x86, 0x4d, 0x35, 0x4e, 0x06, 0x63, 0x29, 0x89, 0x5d, 0x8f, 0x03, 0x54, 0x80, 0x90, 0x58, 0x20, - 0x60, 0x05, 0x42, 0x48, 0x2c, 0x2b, 0x21, 0x7e, 0x80, 0x0f, 0x60, 0xdd, 0x15, 0xaa, 0xc4, 0x86, - 0x55, 0x85, 0x5a, 0x16, 0x88, 0x25, 0x5f, 0x80, 0x3c, 0x33, 0x76, 0x9d, 0xc6, 0x71, 0x42, 0x55, - 0xb1, 0x73, 0x66, 0xce, 0xbd, 0xf7, 0xdc, 0x73, 0x7d, 0x8f, 0x03, 0x4b, 0x0d, 0x42, 0x5b, 0x36, - 0xd5, 0x1c, 0x9b, 0x7a, 0x54, 0xbb, 0x37, 0x6f, 0x10, 0x0f, 0xcf, 0x6b, 0x5b, 0x1d, 0xe2, 0x6e, - 0xab, 0x8e, 0x6b, 0x7b, 0x36, 0xca, 0x73, 0x84, 0xca, 0x10, 0xaa, 0x40, 0x48, 0x79, 0xd3, 0x36, - 0x6d, 0x06, 0xd0, 0xfc, 0x27, 0x8e, 0x95, 0xa6, 0x4c, 0xdb, 0x36, 0x9b, 0x44, 0xc3, 0x8e, 0xa5, - 0xe1, 0x76, 0xdb, 0xf6, 0xb0,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x9c, 0x88, 0xb1, 0x04, 0xb4, 0x98, 0x28, 0x4d, 0x3f, - 0x3f, 0x93, 0x96, 0xfe, 0x34, 0x4c, 0xb0, 0x5f, 0x66, 0xec, 0x6b, 0xe8, 0x42, 0x12, 0xfb, 0x23, - 0x3f, 0xa9, 0x86, 0xde, 0x84, 0x5e, 0x00, 0x98, 0xe1, 0x7b, 0x8b, 0x12, 0xdf, 0xee, 0xa8, 0x4d, - 0x48, 0xb3, 0x43, 0x20, 0x05, 0xb3, 0x39, 0xc6, 0x6c, 0x1a, 0x29, 0x49, 0xcc, 0xb8, 0x55, 0xac, - 0x5e, 0xdf, 0x3d, 0x28, 0x82, 0xbd, 0x83, 0x22, 0xf8, 0x76, 0x50, 0x04, 0xaf, 0x0e, 0x8b, 0xa9, - 0xbd, 0xc3, 0x62, 0xea, 0xeb, 0x61, 0x31, 0x75, 0x7b, 0xc1, 0xb4, 0xbc, 0xbb, 0x1d, 0x43, 0xad, - 0xdb, 0x2d, 0x91, 0xa7, 0xda, 0xc4, 0x06, 0x0d, 0x72, 0x3e, 0xd0, 0xa8, 0xe7, 0x5b, 0x9b, 0x29, - 0xf2, 0x79, 0xdb, 0x0e, 0xa1, 0x46, 0x86, 0xfd, 0xad, 0x5a, 0xf8, 0x1d, 0x00, 0x00, 0xff, 0xff, - 0x95, 0x02, 0xc6, 0xff, 0xc1, 0x0a, 0x00, 0x00, + // 916 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x96, 0xb1, 0x8f, 0x1b, 0x45, + 0x14, 0xc6, 0x3d, 0x89, 0xcf, 0x8e, 0x9f, 0x05, 0x82, 0x89, 0xa5, 0x98, 0xe5, 0xe2, 0x35, 0x4b, + 0x88, 0x7d, 0x27, 0x79, 0x27, 0x76, 0xc8, 0x11, 0x9d, 0x28, 0xe0, 0x8a, 0xa0, 0x93, 0x40, 0x24, + 0x2b, 0x68, 0x68,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4, 0xef, 0xd1, 0x6b, 0x79, 0xf4, 0x41, 0x92, 0xa0, + 0x33, 0xeb, 0xab, 0xcf, 0x08, 0x94, 0xd4, 0x14, 0xa4, 0xb9, 0xb7, 0x3b, 0x3d, 0x74, 0xb5, 0x9d, + 0x0d, 0x94, 0x48, 0xb6, 0x2b, 0xc9, 0xae, 0x50, 0x23, 0x8f, 0x4c, 0x0d, 0x5e, 0xfa, 0x3d, 0x81, + 0x47, 0xe6, 0xa6, 0x08, 0x65, 0x6b, 0x2f, 0xfc, 0x82, 0x81, 0xd7, 0x36, 0x0f, 0x40, 0xc0, 0xe7, + 0x24, 0x60, 0x97, 0xb2, 0xcd, 0x0e, 0x3e, 0x71, 0xee, 0xe0, 0x95, 0xfb, 0x27, 0x0d, 0xf2, 0xe0, + 0xa4, 0x41, 0x7e, 0x3f, 0x69, 0x90, 0x2f, 0x4e, 0x1b, 0x85, 0x07, 0xa7, 0x8d, 0xc2, 0x6f, 0xa7, + 0x8d, 0xc2, 0x1b, 0xd7, 0x1d, 0x37, 0x7c, 0x7b, 0xda, 0x37, 0x07, 0xde, 0x18, 0x93, 0x76, 0x46, + 0x76, 0x5f, 0xc4, 0x05, 0xde, 0x63, 0x22, 0x8c, 0xe6, 0x91, 0x83, 0xc9, 0xc3, 0x23, 0x9f, 0x8b, + 0x7e, 0x49, 0xfe, 0x85, 0xbe, 0xfe, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe6, 0x87, 0xbf, 0x55, + 0xad, 0x0c, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -694,6 +808,8 @@ type QueryClient interface { RegisteredReactions(ctx context.Context, in *QueryRegisteredReactionsRequest, opts ...grpc.CallOption) (*QueryRegisteredReactionsResponse, error) // Params queries the posts module params Params(ctx context.Context, in *QueryParamsRequest, opts ...grpc.CallOption) (*QueryParamsResponse, error) + // PostReactions queries all the reactions of the post having the given id + PostReactions(ctx context.Context, in *QueryPostReactionsRequest, opts ...grpc.CallOption) (*QueryPostReactionsResponse, error) } type queryClient struct { @@ -758,6 +874,15 @@ func (c *queryClient) Params(ctx context.Context, in *QueryParamsRequest, opts . return out, nil } +func (c *queryClient) PostReactions(ctx context.Context, in *QueryPostReactionsRequest, opts ...grpc.CallOption) (*QueryPostReactionsResponse, error) { + out := new(QueryPostReactionsResponse) + err := c.cc.Invoke(ctx, "/desmos.posts.v1beta1.Query/PostReactions", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // QueryServer is the server API for Query service. type QueryServer interface { // Posts queries all the stored posts @@ -772,6 +897,8 @@ type QueryServer interface { RegisteredReactions(context.Context, *QueryRegisteredReactionsRequest) (*QueryRegisteredReactionsResponse, error) // Params queries the posts module params Params(context.Context, *QueryParamsRequest) (*QueryParamsResponse, error) + // PostReactions queries all the reactions of the post having the given id + PostReactions(context.Context, *QueryPostReactionsRequest) (*QueryPostReactionsResponse, error) } // UnimplementedQueryServer can be embedded to have forward compatible implementations. @@ -796,6 +923,9 @@ func (*UnimplementedQueryServer) RegisteredReactions(ctx context.Context, req *Q func (*UnimplementedQueryServer) Params(ctx context.Context, req *QueryParamsRequest) (*QueryParamsResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Params not implemented") } +func (*UnimplementedQueryServer) PostReactions(ctx context.Context, req *QueryPostReactionsRequest) (*QueryPostReactionsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method PostReactions not implemented") +} func RegisterQueryServer(s grpc1.Server, srv QueryServer) { s.RegisterService(&_Query_serviceDesc, srv) @@ -909,6 +1039,24 @@ func _Query_Params_Handler(srv interface{}, ctx context.Context, dec func(interf return interceptor(ctx, in, info, handler) } +func _Query_PostReactions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QueryPostReactionsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(QueryServer).PostReactions(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/desmos.posts.v1beta1.Query/PostReactions", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(QueryServer).PostReactions(ctx, req.(*QueryPostReactionsRequest)) + } + return interceptor(ctx, in, info, handler) +} + var _Query_serviceDesc = grpc.ServiceDesc{ ServiceName: "desmos.posts.v1beta1.Query", HandlerType: (*QueryServer)(nil), @@ -937,6 +1085,10 @@ var _Query_serviceDesc = grpc.ServiceDesc{ MethodName: "Params", Handler: _Query_Params_Handler, }, + { + MethodName: "PostReactions", + Handler: _Query_PostReactions_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "desmos/posts/v1beta1/query.proto", @@ -1268,10 +1420,10 @@ func (m *QueryRegisteredReactionsResponse) MarshalToSizedBuffer(dAtA []byte) (in i-- dAtA[i] = 0x12 } - if len(m.RegisteredReactions) > 0 { - for iNdEx := len(m.RegisteredReactions) - 1; iNdEx >= 0; iNdEx-- { + if len(m.Reactions) > 0 { + for iNdEx := len(m.Reactions) - 1; iNdEx >= 0; iNdEx-- { { - size, err := m.RegisteredReactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + size, err := m.Reactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -1408,6 +1560,97 @@ func (m *QueryReportsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *QueryPostReactionsRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryPostReactionsRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryPostReactionsRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pagination != nil { + { + size, err := m.Pagination.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if len(m.PostId) > 0 { + i -= len(m.PostId) + copy(dAtA[i:], m.PostId) + i = encodeVarintQuery(dAtA, i, uint64(len(m.PostId))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *QueryPostReactionsResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryPostReactionsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryPostReactionsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Pagination != nil { + { + size, err := m.Pagination.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if len(m.Reactions) > 0 { + for iNdEx := len(m.Reactions) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Reactions[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintQuery(dAtA []byte, offset int, v uint64) int { offset -= sovQuery(v) base := offset @@ -1542,8 +1785,8 @@ func (m *QueryRegisteredReactionsResponse) Size() (n int) { } var l int _ = l - if len(m.RegisteredReactions) > 0 { - for _, e := range m.RegisteredReactions { + if len(m.Reactions) > 0 { + for _, e := range m.Reactions { l = e.Size() n += 1 + l + sovQuery(uint64(l)) } @@ -1603,6 +1846,42 @@ func (m *QueryReportsResponse) Size() (n int) { return n } +func (m *QueryPostReactionsRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.PostId) + if l > 0 { + n += 1 + l + sovQuery(uint64(l)) + } + if m.Pagination != nil { + l = m.Pagination.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + +func (m *QueryPostReactionsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Reactions) > 0 { + for _, e := range m.Reactions { + l = e.Size() + n += 1 + l + sovQuery(uint64(l)) + } + } + if m.Pagination != nil { + l = m.Pagination.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + func sovQuery(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -2431,7 +2710,7 @@ func (m *QueryRegisteredReactionsResponse) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field RegisteredReactions", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Reactions", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -2458,8 +2737,8 @@ func (m *QueryRegisteredReactionsResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RegisteredReactions = append(m.RegisteredReactions, RegisteredReaction{}) - if err := m.RegisteredReactions[len(m.RegisteredReactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + m.Reactions = append(m.Reactions, RegisteredReaction{}) + if err := m.Reactions[len(m.Reactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -2819,6 +3098,244 @@ func (m *QueryReportsResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *QueryPostReactionsRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryPostReactionsRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryPostReactionsRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PostId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PostId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pagination", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Pagination == nil { + m.Pagination = &query.PageRequest{} + } + if err := m.Pagination.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *QueryPostReactionsResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryPostReactionsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryPostReactionsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Reactions", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Reactions = append(m.Reactions, PostReaction{}) + if err := m.Reactions[len(m.Reactions)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pagination", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Pagination == nil { + m.Pagination = &query.PageResponse{} + } + if err := m.Pagination.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipQuery(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/staging/posts/types/query.pb.gw.go b/x/staging/posts/types/query.pb.gw.go index 93fc8e3be9..f9d7770c57 100644 --- a/x/staging/posts/types/query.pb.gw.go +++ b/x/staging/posts/types/query.pb.gw.go @@ -301,6 +301,78 @@ func local_request_Query_Params_0(ctx context.Context, marshaler runtime.Marshal } +var ( + filter_Query_PostReactions_0 = &utilities.DoubleArray{Encoding: map[string]int{"post_id": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Query_PostReactions_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryPostReactionsRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["post_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "post_id") + } + + protoReq.PostId, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "post_id", err) + } + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_PostReactions_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.PostReactions(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Query_PostReactions_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryPostReactionsRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["post_id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "post_id") + } + + protoReq.PostId, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "post_id", err) + } + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Query_PostReactions_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.PostReactions(ctx, &protoReq) + return msg, metadata, err + +} + // RegisterQueryHandlerServer registers the http handlers for service Query to "mux". // UnaryRPC :call QueryServer directly. // StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. @@ -427,6 +499,26 @@ func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, serv }) + mux.Handle("GET", pattern_Query_PostReactions_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Query_PostReactions_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_PostReactions_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -588,6 +680,26 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie }) + mux.Handle("GET", pattern_Query_PostReactions_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Query_PostReactions_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_PostReactions_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -603,6 +715,8 @@ var ( pattern_Query_RegisteredReactions_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 1, 2, 3}, []string{"desmos", "posts", "v1beta1", "registered-reactions"}, "", runtime.AssumeColonVerbOpt(true))) pattern_Query_Params_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 1, 2, 3}, []string{"desmos", "posts", "v1beta1", "params"}, "", runtime.AssumeColonVerbOpt(true))) + + pattern_Query_PostReactions_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 1, 1, 0, 4, 1, 5, 3, 2, 4}, []string{"desmos", "posts", "v1beta1", "post_id", "reactions"}, "", runtime.AssumeColonVerbOpt(true))) ) var ( @@ -617,4 +731,6 @@ var ( forward_Query_RegisteredReactions_0 = runtime.ForwardResponseMessage forward_Query_Params_0 = runtime.ForwardResponseMessage + + forward_Query_PostReactions_0 = runtime.ForwardResponseMessage ) From 23cc1f543729fe90766393138ba9746d136d5eaa Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 19:40:28 +0800 Subject: [PATCH 06/26] Add post reactions query command --- x/staging/posts/client/cli/cli_test.go | 55 ++++++++++++++++++++++ x/staging/posts/client/cli/query.go | 39 ++++++++++++++- x/staging/posts/keeper/keeper_reactions.go | 3 ++ x/staging/posts/types/keys.go | 1 + 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/x/staging/posts/client/cli/cli_test.go b/x/staging/posts/client/cli/cli_test.go index 01573f7a92..a8a626aeb8 100644 --- a/x/staging/posts/client/cli/cli_test.go +++ b/x/staging/posts/client/cli/cli_test.go @@ -559,6 +559,61 @@ func (s *IntegrationTestSuite) TestCmdQueryParams() { } } +func (s *IntegrationTestSuite) TestCmdQueryPostReactions() { + val := s.network.Validators[0] + + testCases := []struct { + name string + args []string + expectErr bool + expectedOutput types.QueryPostReactionsResponse + }{ + { + name: "valid request is returned properly", + args: []string{ + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + fmt.Sprintf("--%s=json", tmcli.OutputFlag), + }, + expectErr: false, + expectedOutput: types.QueryPostReactionsResponse{ + Reactions: []types.PostReaction{ + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":broken_heart:", + "💔", + "cosmos12t08qkk4dm2pqgyy8hmq5hx92y2m29zedmdw7f", + ), + }, + Pagination: &query.PageResponse{ + NextKey: nil, + Total: 0, + }, + }, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.GetCmdQueryPostReactions() + clientCtx := val.ClientCtx + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + + var response types.QueryPostReactionsResponse + s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), &response), out.String()) + s.Require().Equal(tc.expectedOutput.Reactions, response.Reactions) + s.Require().Equal(tc.expectedOutput.Pagination, response.Pagination) + } + }) + } +} + // ___________________________________________________________________________________________________________________ func (s *IntegrationTestSuite) TestCmdCreatePost() { diff --git a/x/staging/posts/client/cli/query.go b/x/staging/posts/client/cli/query.go index 46d3ddcfc5..49e50e1241 100644 --- a/x/staging/posts/client/cli/query.go +++ b/x/staging/posts/client/cli/query.go @@ -34,6 +34,7 @@ func GetQueryCmd() *cobra.Command { GetCmdQueryUserAnswers(), GetCmdQueryRegisteredReactions(), GetCmdQueryParams(), + GetCmdQueryPostReactions(), ) return postQueryCmd } @@ -159,7 +160,7 @@ func GetCmdQueryUserAnswers() *cobra.Command { func GetCmdQueryRegisteredReactions() *cobra.Command { cmd := &cobra.Command{ Use: "registered-reactions [[subspace-id]]", - Short: "Retrieve tha registered reactions with optional subspace", + Short: "Retrieve the registered reactions with optional subspace", Args: cobra.RangeArgs(0, 1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) @@ -252,3 +253,39 @@ func GetCmdQueryParams() *cobra.Command { return cmd } + +// GetCmdQueryPostReactions returns the command allowing to query the reactions of a post +func GetCmdQueryPostReactions() *cobra.Command { + cmd := &cobra.Command{ + Use: "post-reactions [post-id]", + Short: "Retrieve the reactions of the post having the given id", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + + pageReq, err := client.ReadPageRequest(cmd.Flags()) + if err != nil { + return err + } + + res, err := queryClient.PostReactions( + context.Background(), + &types.QueryPostReactionsRequest{PostId: args[0], Pagination: pageReq}, + ) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + flags.AddPaginationFlagsToCmd(cmd, types.QueryPostReactions) + + return cmd +} diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index 7431df273d..8133ed7c07 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -54,6 +54,7 @@ func (k Keeper) GetPostReactions(ctx sdk.Context, postID string) []types.PostRea return reactions } +// GetAllRegisteredReactions returns all the post reactions func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { var reactions []types.PostReaction k.IteratePostReactions(ctx, func(_ int64, reaction types.PostReaction) bool { @@ -63,6 +64,8 @@ func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { return reactions } +// GetPostReaction returns the post reaction for the given postID, owner and short code. +// If such reaction does not exist, returns false instead. func (k Keeper) GetPostReaction(ctx sdk.Context, postID, owner, shortCode string) (types.PostReaction, bool) { store := ctx.KVStore(k.storeKey) diff --git a/x/staging/posts/types/keys.go b/x/staging/posts/types/keys.go index 600b69ee94..b189a9667b 100644 --- a/x/staging/posts/types/keys.go +++ b/x/staging/posts/types/keys.go @@ -28,6 +28,7 @@ const ( QueryReports = "reports" QueryUserAnswers = "user-answers" QueryRegisteredReactions = "registered-reactions" + QueryPostReactions = "post-reactions" QueryParams = "params" ) From 8b6b1723c216451d8b35251a10e9d9b22a417961 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 24 Jun 2021 19:45:57 +0800 Subject: [PATCH 07/26] Add comment to commen functions --- x/staging/posts/keeper/common_functions.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 131d7a6b47..c52cb739ff 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -136,6 +136,7 @@ func (k Keeper) IterateUserAnswersByPost(ctx sdk.Context, postID string, fn func } } +// IterateRegisteredReactions iterates through the registered reactions and performs the provided function func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, reaction types.RegisteredReaction) (stop bool)) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.RegisteredReactionsStorePrefix) @@ -154,6 +155,7 @@ func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, } } +// IteratePostReactions iterates through the post reactions and performs the provided function func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, reaction types.PostReaction) (stop bool)) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsStorePrefix) @@ -173,6 +175,7 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react } } +// IteratePostReactionsByPost iterates through the post reactions with the given post id and performs the provided function func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn func(index int64, reaction types.PostReaction) (stop bool)) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsPrefix(postID)) From 8c5642829410156b00400e919aedd9d94f09afe9 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 17:02:50 +0800 Subject: [PATCH 08/26] Fix fails after merge --- x/staging/posts/client/cli/cli_test.go | 2 +- x/staging/posts/simulation/decoder.go | 18 +++++++++--------- x/staging/posts/simulation/decoder_test.go | 13 +++++++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/x/staging/posts/client/cli/cli_test.go b/x/staging/posts/client/cli/cli_test.go index 873778b518..fb6eac6770 100644 --- a/x/staging/posts/client/cli/cli_test.go +++ b/x/staging/posts/client/cli/cli_test.go @@ -28,7 +28,7 @@ type IntegrationTestSuite struct { func TestIntegrationTestSuite(t *testing.T) { //TODO restore this when out of staging - // suite.Run(t, new(IntegrationTestSuite)) + //suite.Run(t, new(IntegrationTestSuite)) } func (s *IntegrationTestSuite) SetupSuite() { diff --git a/x/staging/posts/simulation/decoder.go b/x/staging/posts/simulation/decoder.go index 5d07e74944..42b8f1dde3 100644 --- a/x/staging/posts/simulation/decoder.go +++ b/x/staging/posts/simulation/decoder.go @@ -22,22 +22,22 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { return fmt.Sprintf("PostA: %s\nPostB: %s\n", postA.String(), postB.String()) case bytes.HasPrefix(kvA.Key, types.PostReactionsStorePrefix): - var postReactionsA, postReactionsB types.PostReaction - cdc.MustUnmarshalBinaryBare(kvA.Value, &postReactionsA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &postReactionsB) - return fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReactionsA, postReactionsB) + var reactionA, reactionB types.PostReaction + cdc.MustUnmarshalBinaryBare(kvA.Value, &reactionA) + cdc.MustUnmarshalBinaryBare(kvB.Value, &reactionB) + return fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", reactionA, reactionB) case bytes.HasPrefix(kvA.Key, types.CommentsStorePrefix): - var commentIDA, commentIDB string - commentIDA = string(kvA.Value) - commentIDB = string(kvA.Value) - return fmt.Sprintf("CommentA: %s\nCommentB: %s\n", commentIDA, commentIDB) + var commentA, commentB string + commentA = string(kvA.Value) + commentB = string(kvA.Value) + return fmt.Sprintf("CommentA: %s\nCommentB: %s\n", commentA, commentB) case bytes.HasPrefix(kvA.Key, types.RegisteredReactionsStorePrefix): var reactionA, reactionB types.RegisteredReaction cdc.MustUnmarshalBinaryBare(kvA.Value, &reactionA) cdc.MustUnmarshalBinaryBare(kvB.Value, &reactionB) - return fmt.Sprintf("ReactionA: %s\nReactionB: %s\n", reactionA, reactionB) + return fmt.Sprintf("RegisteredReactionA: %s\nRegisteredReactionB: %s\n", reactionA, reactionB) case bytes.HasPrefix(kvA.Key, types.ReportsStorePrefix): var reportsA, reportsB types.Reports diff --git a/x/staging/posts/simulation/decoder_test.go b/x/staging/posts/simulation/decoder_test.go index 2dc64a63a8..0371eb6278 100644 --- a/x/staging/posts/simulation/decoder_test.go +++ b/x/staging/posts/simulation/decoder_test.go @@ -58,7 +58,8 @@ func TestDecodeStore(t *testing.T) { "💙", address, ) - commentID := "g1ba4807a15d8579f79cfd90a07fc015e6125565c9271eb94aded0b2ebf86163" + + comment := "g1ba4807a15d8579f79cfd90a07fc015e6125565c9271eb94aded0b2ebf86163" registeredReaction := types.NewRegisteredReaction( address, @@ -93,8 +94,8 @@ func TestDecodeStore(t *testing.T) { Value: cdc.MustMarshalBinaryBare(&postReaction), }, { - Key: types.CommentsStoreKey(post.PostID, commentID), - Value: []byte(commentID), + Key: types.CommentsStoreKey(post.PostID, comment), + Value: []byte(comment), }, { Key: types.RegisteredReactionsStoreKey(registeredReaction.Subspace, registeredReaction.ShortCode), @@ -111,9 +112,9 @@ func TestDecodeStore(t *testing.T) { expectedLog string }{ {"Post", fmt.Sprintf("PostA: %s\nPostB: %s\n", post.String(), post.String())}, - {"Comment", fmt.Sprintf("CommentA: %s\nCommentB: %s\n", commentID, commentID)}, - {"PostReaction", fmt.Sprintf("PostReactionsA: %s\nPostReactionsB: %s\n", postReaction, postReaction)}, - {"Reaction", fmt.Sprintf("ReactionA: %s\nReactionB: %s\n", registeredReaction, registeredReaction)}, + {"PostReaction", fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReaction, postReaction)}, + {"Comment", fmt.Sprintf("CommentA: %s\nCommentB: %s\n", comment, comment)}, + {"RegisteredReaction", fmt.Sprintf("RegisteredReactionA: %s\nRegisteredReactionB: %s\n", registeredReaction, registeredReaction)}, {"Report", fmt.Sprintf("ReportsA: %s\nReportsB: %s\n", reports, reports)}, {"other", ""}, } From 40a2efa4c89a86e6a41c121b711742161690956f Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 17:09:43 +0800 Subject: [PATCH 09/26] Upadate docs --- docs/developers/queries/staging/reactions.md | 18 +++++++++++++++--- .../developers/queries/staging/user-answers.md | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/developers/queries/staging/reactions.md b/docs/developers/queries/staging/reactions.md index ab59344f57..074d7eb08a 100644 --- a/docs/developers/queries/staging/reactions.md +++ b/docs/developers/queries/staging/reactions.md @@ -1,10 +1,22 @@ # Query registered reactions -This query allows you to retrieve the list of registered reactions. +This query allows you to retrieve the list of registered reactions inside a optional subspace. **CLI** ```bash -desmos query posts registered-reactions +desmos query posts registered-reactions [[subspace-id]] # Example -# desmos query posts registered-reactions +# desmos query posts registered-reactions 4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e +``` + +# Query post reactions +This query allows you to retrieve the list of post reactions of a post. + + +**CLI** +```bash +desmos query posts post-reactions [post-id] + +# Example +# desmos query posts post-reactions 301921ac3c8e623d8f35aef1886fea20849e49f08ec8ddfdd9b96feaf0c4fd15 ``` \ No newline at end of file diff --git a/docs/developers/queries/staging/user-answers.md b/docs/developers/queries/staging/user-answers.md index 23493a0d3c..e5cfa50398 100644 --- a/docs/developers/queries/staging/user-answers.md +++ b/docs/developers/queries/staging/user-answers.md @@ -3,7 +3,7 @@ This query allows you to retrieve the details of answers made to a post's poll'. **CLI** ```bash -desmos query posts user-answers [id] [[user]] +desmos query posts user-answers [post-id] [[user]] # Example # desmos query posts user-answers a4469741bb0c0622627810082a5f2e4e54fbbb888f25a4771a5eebc697d30cfc From dac7e4001e0735078a8a4af5490ffd1dd0f079c4 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 17:10:40 +0800 Subject: [PATCH 10/26] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fdf9aeac7..6ddfa7e5db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Added the possibility to verify a profile with an external application ([#472](https://github.com/desmos-labs/desmos/issues/472)) - Improved the posts query ([#499](https://github.com/desmos-labs/desmos/issues/499)) - Added the post comments query ([#510](https://github.com/desmos-labs/desmos/pull/510)) +- Added the post reactions query ([#515](https://github.com/desmos-labs/desmos/pull/515)) # Version 0.16.3 ## Changes From a40d56c872cbe0c2fda4a8b1a406d5232b5b1c13 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 17:15:33 +0800 Subject: [PATCH 11/26] Run proto lint --- proto/desmos/posts/v1beta1/genesis.proto | 3 +- proto/desmos/posts/v1beta1/query.proto | 37 ++++++++++++++---------- x/staging/posts/types/query.pb.go | 6 ++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/proto/desmos/posts/v1beta1/genesis.proto b/proto/desmos/posts/v1beta1/genesis.proto index 37f61642b8..ceaa01fa0b 100644 --- a/proto/desmos/posts/v1beta1/genesis.proto +++ b/proto/desmos/posts/v1beta1/genesis.proto @@ -14,7 +14,8 @@ option go_package = "github.com/desmos-labs/desmos/x/staging/posts/types"; message GenesisState { repeated desmos.posts.v1beta1.Post posts = 1 [ (gogoproto.nullable) = false ]; - repeated desmos.posts.v1beta1.UserAnswer users_poll_answers = 2 [ (gogoproto.nullable) = false ]; + repeated desmos.posts.v1beta1.UserAnswer users_poll_answers = 2 + [ (gogoproto.nullable) = false ]; repeated desmos.posts.v1beta1.PostReaction posts_reactions = 3 [ (gogoproto.nullable) = false ]; repeated desmos.posts.v1beta1.RegisteredReaction registered_reactions = 4 diff --git a/proto/desmos/posts/v1beta1/query.proto b/proto/desmos/posts/v1beta1/query.proto index 40b7201bc0..3847a89a4c 100644 --- a/proto/desmos/posts/v1beta1/query.proto +++ b/proto/desmos/posts/v1beta1/query.proto @@ -48,17 +48,18 @@ service Query { option (google.api.http).get = "/desmos/posts/v1beta1/posts/params"; } - // PostComments queries the comments of the post having the given id - rpc PostComments(QueryPostCommentsRequest) returns (QueryPostCommentsResponse) { + rpc PostComments(QueryPostCommentsRequest) + returns (QueryPostCommentsResponse) { option (google.api.http).get = - "/desmos/posts/v1beta1/posts/{post_id}/comments"; + "/desmos/posts/v1beta1/posts/{post_id}/comments"; } // PostReactions queries all the reactions of the post having the given id rpc PostReactions(QueryPostReactionsRequest) - returns (QueryPostReactionsResponse) { - option (google.api.http).get = "/desmos/posts/v1beta1/posts/{post_id}/reactions"; + returns (QueryPostReactionsResponse) { + option (google.api.http).get = + "/desmos/posts/v1beta1/posts/{post_id}/reactions"; } } @@ -104,8 +105,10 @@ message QueryUserAnswersRequest { option (gogoproto.equal) = false; option (gogoproto.goproto_getters) = false; - string post_id = 1 - [ (gogoproto.jsontag) = "post_id", (gogoproto.moretags) = "yaml:\"post_id\"" ]; + string post_id = 1 [ + (gogoproto.jsontag) = "post_id", + (gogoproto.moretags) = "yaml:\"post_id\"" + ]; string user = 2; @@ -174,26 +177,29 @@ message QueryReportsResponse { // ___________________________________________________________________________________________________________________ - -// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC method. +// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC +// method. message QueryPostReactionsRequest { - string post_id = 1 - [ (gogoproto.jsontag) = "post_id", (gogoproto.moretags) = "yaml:\"post_id\"" ]; + string post_id = 1 [ + (gogoproto.jsontag) = "post_id", + (gogoproto.moretags) = "yaml:\"post_id\"" + ]; // pagination defines an optional pagination for the request. cosmos.base.query.v1beta1.PageRequest pagination = 2; } -// QueryPostReactionsResponse is the response type for the Query/PostReactions RPC method +// QueryPostReactionsResponse is the response type for the Query/PostReactions +// RPC method message QueryPostReactionsResponse { repeated desmos.posts.v1beta1.PostReaction reactions = 1 [ (gogoproto.nullable) = false ]; - + cosmos.base.query.v1beta1.PageResponse pagination = 2; } // ___________________________________________________________________________________________________________________ - + // QueryPostCommentsRequest is the request type for the Query/PostComments RPC // method. message QueryPostCommentsRequest { @@ -206,7 +212,8 @@ message QueryPostCommentsRequest { // QueryPostCommentsResponse is the response type for the Query/PostComments RPC // method. message QueryPostCommentsResponse { - repeated desmos.posts.v1beta1.Post comments = 1 [ (gogoproto.nullable) = false ]; + repeated desmos.posts.v1beta1.Post comments = 1 + [ (gogoproto.nullable) = false ]; cosmos.base.query.v1beta1.PageResponse pagination = 2; } \ No newline at end of file diff --git a/x/staging/posts/types/query.pb.go b/x/staging/posts/types/query.pb.go index c506d6a2b6..f44a788981 100644 --- a/x/staging/posts/types/query.pb.go +++ b/x/staging/posts/types/query.pb.go @@ -596,7 +596,8 @@ func (m *QueryReportsResponse) GetReports() []Report { return nil } -// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC method. +// QueryPostReactionsRequest is the request type for the Query/PostReactions RPC +// method. type QueryPostReactionsRequest struct { PostId string `protobuf:"bytes,1,opt,name=post_id,json=postId,proto3" json:"post_id" yaml:"post_id"` // pagination defines an optional pagination for the request. @@ -650,7 +651,8 @@ func (m *QueryPostReactionsRequest) GetPagination() *query.PageRequest { return nil } -// QueryPostReactionsResponse is the response type for the Query/PostReactions RPC method +// QueryPostReactionsResponse is the response type for the Query/PostReactions +// RPC method type QueryPostReactionsResponse struct { Reactions []PostReaction `protobuf:"bytes,1,rep,name=reactions,proto3" json:"reactions"` Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` From 4c1fac94c8fba4267670084d9fbf99a2fb179556 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 17:58:05 +0800 Subject: [PATCH 12/26] Fix benchmark test --- x/staging/posts/keeper/keeper_bench_test.go | 38 ++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/x/staging/posts/keeper/keeper_bench_test.go b/x/staging/posts/keeper/keeper_bench_test.go index 8580eff51d..d4948ffb85 100644 --- a/x/staging/posts/keeper/keeper_bench_test.go +++ b/x/staging/posts/keeper/keeper_bench_test.go @@ -62,7 +62,7 @@ func (suite *KeeperTestSuite) BenchmarkKeeper_GetPosts(b *testing.B) { } func (suite *KeeperTestSuite) BenchmarkKeeper_SavePostReaction(b *testing.B) { - fmt.Println("Benchmark Save a post registeredReactions") + fmt.Println("Benchmark Save Registered Reactions") r := rand.New(rand.NewSource(100)) for i := 0; i < b.N; i++ { @@ -78,25 +78,25 @@ func (suite *KeeperTestSuite) BenchmarkKeeper_SavePostReaction(b *testing.B) { } } -// func (suite *KeeperTestSuite) BenchmarkKeeper_GetPostReactions(b *testing.B) { -// fmt.Println("Benchmark Get a post registeredReactions") -// r := rand.New(rand.NewSource(100)) +func (suite *KeeperTestSuite) BenchmarkKeeper_GetPostReactions(b *testing.B) { + fmt.Println("Benchmark Get Post Reactions") + r := rand.New(rand.NewSource(100)) -// for i := 0; i < b.N; i++ { -// suite.k.SavePost(suite.ctx, RandomPost()) -// } + for i := 0; i < b.N; i++ { + suite.k.SavePost(suite.ctx, RandomPost()) + } -// posts := suite.k.GetPosts(suite.ctx) -// post := posts[r.Intn(len(posts))] -// reaction := postssim.RandomEmojiPostReaction(r) + posts := suite.k.GetPosts(suite.ctx) + post := posts[r.Intn(len(posts))] + reaction := postssim.RandomEmojiPostReaction(r) -// for i := 0; i < b.N; i++ { -// err := suite.k.SavePostReaction(suite.ctx, post.PostID, reaction) -// suite.Require().NoError(err) -// } + for i := 0; i < b.N; i++ { + err := suite.k.SavePostReaction(suite.ctx, reaction) + suite.Require().NoError(err) + } -// b.ResetTimer() -// for i := 0; i < b.N; i++ { -// suite.k.GetPostReactions(suite.ctx, post.PostID) -// } -// } + b.ResetTimer() + for i := 0; i < b.N; i++ { + suite.k.GetPostReactions(suite.ctx, post.PostID) + } +} From c362065add66f2cde0bf535036b0c73275eb6ad1 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 18:01:28 +0800 Subject: [PATCH 13/26] Improve commom functions --- x/staging/posts/keeper/common_functions.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index c52cb739ff..78b036083f 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -20,12 +20,10 @@ func (k Keeper) IteratePosts(ctx sdk.Context, fn func(index int64, post types.Po for ; iterator.Valid(); iterator.Next() { var post types.Post k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &post) - stop := fn(i, post) if stop { break } - i++ } } @@ -107,12 +105,10 @@ func (k Keeper) IterateUserAnswers(ctx sdk.Context, fn func(index int64, answer i := int64(0) for ; iterator.Valid(); iterator.Next() { answer := types.MustUnmarshalUserAnswer(k.cdc, iterator.Value()) - stop := fn(i, answer) if stop { break } - i++ } } @@ -126,12 +122,10 @@ func (k Keeper) IterateUserAnswersByPost(ctx sdk.Context, postID string, fn func i := int64(0) for ; iterator.Valid(); iterator.Next() { answer := types.MustUnmarshalUserAnswer(k.cdc, iterator.Value()) - stop := fn(i, answer) if stop { break } - i++ } } @@ -145,12 +139,10 @@ func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, i := int64(0) for ; iterator.Valid(); iterator.Next() { reaction := types.MustUnmarshalRegisteredReaction(k.cdc, iterator.Value()) - stop := fn(i, reaction) if stop { break } - i++ } } @@ -169,7 +161,6 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react if stop { break } - i++ } @@ -184,12 +175,10 @@ func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn fu i := int64(0) for ; iterator.Valid(); iterator.Next() { reaction := types.MustUnmarshalPostReaction(k.cdc, iterator.Value()) - stop := fn(i, reaction) if stop { break } - i++ } } From ffe32ce87e7dd5ae7748727e7330940f6ee2ed80 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 18:02:32 +0800 Subject: [PATCH 14/26] Remove space --- x/staging/posts/keeper/common_functions.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 78b036083f..24703c96e7 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -162,7 +162,6 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react break } i++ - } } From 00b6792e7c50d87448d5b164e43fc3d56159bc4a Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 18:18:17 +0800 Subject: [PATCH 15/26] Adjust decoder test order --- x/staging/posts/keeper/keeper_reactions.go | 2 +- x/staging/posts/simulation/decoder_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index 8133ed7c07..93449166f5 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -23,7 +23,7 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) e } // Save the new reaction - store.Set(key, k.cdc.MustMarshalBinaryBare(&reaction)) + store.Set(key, types.MustMarshalPostReaction(k.cdc, reaction)) return nil } diff --git a/x/staging/posts/simulation/decoder_test.go b/x/staging/posts/simulation/decoder_test.go index 0371eb6278..0483a9a003 100644 --- a/x/staging/posts/simulation/decoder_test.go +++ b/x/staging/posts/simulation/decoder_test.go @@ -89,14 +89,14 @@ func TestDecodeStore(t *testing.T) { Key: types.PostStoreKey(post.PostID), Value: cdc.MustMarshalBinaryBare(&post), }, - { - Key: types.PostReactionsStoreKey(postReaction.PostID, postReaction.Owner, postReaction.ShortCode), - Value: cdc.MustMarshalBinaryBare(&postReaction), - }, { Key: types.CommentsStoreKey(post.PostID, comment), Value: []byte(comment), }, + { + Key: types.PostReactionsStoreKey(postReaction.PostID, postReaction.Owner, postReaction.ShortCode), + Value: cdc.MustMarshalBinaryBare(&postReaction), + }, { Key: types.RegisteredReactionsStoreKey(registeredReaction.Subspace, registeredReaction.ShortCode), Value: cdc.MustMarshalBinaryBare(®isteredReaction), @@ -112,8 +112,8 @@ func TestDecodeStore(t *testing.T) { expectedLog string }{ {"Post", fmt.Sprintf("PostA: %s\nPostB: %s\n", post.String(), post.String())}, - {"PostReaction", fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReaction, postReaction)}, {"Comment", fmt.Sprintf("CommentA: %s\nCommentB: %s\n", comment, comment)}, + {"PostReaction", fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", postReaction, postReaction)}, {"RegisteredReaction", fmt.Sprintf("RegisteredReactionA: %s\nRegisteredReactionB: %s\n", registeredReaction, registeredReaction)}, {"Report", fmt.Sprintf("ReportsA: %s\nReportsB: %s\n", reports, reports)}, {"other", ""}, From 1a689b08298fcef8206982e1a8da1037fe290c5c Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 18:33:58 +0800 Subject: [PATCH 16/26] Fix typo and improve comments --- x/staging/posts/client/cli/cli_test.go | 6 +++--- x/staging/posts/client/cli/query.go | 2 +- x/staging/posts/keeper/keeper_reactions.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/x/staging/posts/client/cli/cli_test.go b/x/staging/posts/client/cli/cli_test.go index fb6eac6770..6b6eedc91c 100644 --- a/x/staging/posts/client/cli/cli_test.go +++ b/x/staging/posts/client/cli/cli_test.go @@ -650,7 +650,7 @@ func (s *IntegrationTestSuite) TestCmdQueryPostReactions() { testCases := []struct { name string args []string - expectErr bool + shouldErr bool expectedOutput types.QueryPostReactionsResponse }{ { @@ -659,7 +659,7 @@ func (s *IntegrationTestSuite) TestCmdQueryPostReactions() { "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", fmt.Sprintf("--%s=json", tmcli.OutputFlag), }, - expectErr: false, + shouldErr: false, expectedOutput: types.QueryPostReactionsResponse{ Reactions: []types.PostReaction{ types.NewPostReaction( @@ -685,7 +685,7 @@ func (s *IntegrationTestSuite) TestCmdQueryPostReactions() { clientCtx := val.ClientCtx out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) - if tc.expectErr { + if tc.shouldErr { s.Require().Error(err) } else { s.Require().NoError(err) diff --git a/x/staging/posts/client/cli/query.go b/x/staging/posts/client/cli/query.go index 0e84d487dd..5877dc57dc 100644 --- a/x/staging/posts/client/cli/query.go +++ b/x/staging/posts/client/cli/query.go @@ -294,7 +294,7 @@ func GetCmdQueryPostReactions() *cobra.Command { func GetCmdQueryPostComments() *cobra.Command { cmd := &cobra.Command{ Use: "post-comments [post-id]", - Short: "Retrieve tha comments of the post with the given id", + Short: "Retrieve the comments of the post with the given id", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index 93449166f5..0d0f0d924e 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -27,8 +27,8 @@ func (k Keeper) SavePostReaction(ctx sdk.Context, reaction types.PostReaction) e return nil } -// DeletePostReaction removes the reaction from the given user from the post having the -// given postID. If no reaction with the same value was previously added from the given user, an expError +// DeletePostReaction removes the reaction from the given data. +// If no reaction with the same value was previously added from the given user, an expError // is returned. func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) error { store := ctx.KVStore(k.storeKey) From 83710f2d71cc8f87bdd6dbae5c2d2e7da062f085 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 18:50:40 +0800 Subject: [PATCH 17/26] Add test to genesis in types --- x/staging/posts/types/genesis_test.go | 63 ++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/x/staging/posts/types/genesis_test.go b/x/staging/posts/types/genesis_test.go index 7c90095304..8f1b5dc424 100644 --- a/x/staging/posts/types/genesis_test.go +++ b/x/staging/posts/types/genesis_test.go @@ -2,6 +2,7 @@ package types_test import ( "testing" + "time" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" @@ -33,12 +34,39 @@ func TestValidateGenesis(t *testing.T) { shouldError: true, }, { - name: "Genesis with invalid post reaction errors", + name: "Genesis with post reaction to non existent post errors", genesis: types.NewGenesisState( []types.Post{}, nil, []types.PostReaction{ - {PostID: "1", Owner: ""}, + {PostID: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", Owner: ""}, + }, + nil, + nil, + types.DefaultParams(), + ), + shouldError: true, + }, + { + name: "Genesis with invalid post reaction errors", + genesis: types.NewGenesisState( + []types.Post{ + types.NewPost( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "", + "Message", + types.CommentsStateBlocked, + "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", + nil, + nil, + nil, + time.Time{}, + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", + ), + }, + nil, + []types.PostReaction{ + {PostID: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", Owner: ""}, }, nil, nil, @@ -60,6 +88,37 @@ func TestValidateGenesis(t *testing.T) { ), shouldError: true, }, + { + name: "Genesis with user answer to non existent post errors", + genesis: types.NewGenesisState( + []types.Post{ + types.NewPost( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "", + "Message", + types.CommentsStateBlocked, + "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", + nil, + nil, + nil, + time.Time{}, + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns", + ), + }, + []types.UserAnswer{ + types.NewUserAnswer( + "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", + "cosmos1s3nh6tafl4amaxkke9kdejhp09lk93g9ev39r4", + []string{}, + ), + }, + nil, + nil, + nil, + types.DefaultParams(), + ), + shouldError: true, + }, { name: "Genesis with invalid registered reaction errors", genesis: types.NewGenesisState( From c68d011efe643b2bd02398ad1e173a59150ab985 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 25 Jun 2021 19:25:19 +0800 Subject: [PATCH 18/26] Add unit test to keeper of reactions --- .../posts/keeper/keeper_reactions_test.go | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/x/staging/posts/keeper/keeper_reactions_test.go b/x/staging/posts/keeper/keeper_reactions_test.go index 57497a6975..47366930d9 100644 --- a/x/staging/posts/keeper/keeper_reactions_test.go +++ b/x/staging/posts/keeper/keeper_reactions_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/desmos-labs/desmos/x/staging/posts/types" ) @@ -291,6 +292,72 @@ func (suite *KeeperTestSuite) TestKeeper_GetPostReactions() { } } +func (suite KeeperTestSuite) Test_GetPostReaction() { + reactions := []types.PostReaction{ + types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "😊", + "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + ), + } + + tests := []struct { + name string + store func(ctx sdk.Context) + postID string + owner string + shortCode string + shouldFound bool + expResponse types.PostReaction + }{ + { + name: "Empty list are returned properly", + postID: "", + owner: "", + shortCode: "", + shouldFound: false, + }, + { + name: "Valid list of reactions is returned properly", + store: func(ctx sdk.Context) { + for _, r := range reactions { + suite.k.SavePost(suite.ctx, suite.testData.post) + err := suite.k.SavePostReaction(suite.ctx, r) + suite.Require().NoError(err) + } + }, + postID: "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + owner: "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + shortCode: ":smile:", + shouldFound: true, + expResponse: types.NewPostReaction( + "19de02e105c68a60e45c289bff19fde745bca9c63c38f2095b59e8e8090ae1af", + ":smile:", + "😊", + "cosmos15lt0mflt6j9a9auj7yl3p20xec4xvljge0zhae", + ), + }, + } + + for _, test := range tests { + test := test + suite.Run(test.name, func() { + ctx, _ := suite.ctx.CacheContext() + if test.store != nil { + test.store(ctx) + } + reaction, found := suite.k.GetPostReaction(ctx, test.postID, test.owner, test.shortCode) + if !test.shouldFound { + suite.Require().False(found) + } else { + suite.Require().True(found) + suite.Require().Equal(test.expResponse, reaction) + } + }) + } +} + // ___________________________________________________________________________________________________________________ func (suite *KeeperTestSuite) TestKeeper_SaveRegisteredReaction() { From 16a76babe4cd140a8e5d8e9d29dd945cbc325ea7 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 28 Jun 2021 14:14:10 +0800 Subject: [PATCH 19/26] Update x/staging/posts/keeper/common_functions.go Co-authored-by: Riccardo Montagnin --- x/staging/posts/keeper/common_functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staging/posts/keeper/common_functions.go b/x/staging/posts/keeper/common_functions.go index 24703c96e7..e24908ce8d 100644 --- a/x/staging/posts/keeper/common_functions.go +++ b/x/staging/posts/keeper/common_functions.go @@ -165,7 +165,7 @@ func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, react } } -// IteratePostReactionsByPost iterates through the post reactions with the given post id and performs the provided function +// IteratePostReactionsByPost iterates through the post reactions added to the post with the given id and performs the provided function func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn func(index int64, reaction types.PostReaction) (stop bool)) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsPrefix(postID)) From 25df3377b2c6bc2bcef06c2e3cc8390e787dceba Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 28 Jun 2021 14:48:12 +0800 Subject: [PATCH 20/26] Renaming cli post reactions into reactions --- x/staging/posts/client/cli/query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staging/posts/client/cli/query.go b/x/staging/posts/client/cli/query.go index 5877dc57dc..cdd92ade8c 100644 --- a/x/staging/posts/client/cli/query.go +++ b/x/staging/posts/client/cli/query.go @@ -257,7 +257,7 @@ func GetCmdQueryParams() *cobra.Command { // GetCmdQueryPostReactions returns the command allowing to query the reactions of a post func GetCmdQueryPostReactions() *cobra.Command { cmd := &cobra.Command{ - Use: "post-reactions [post-id]", + Use: "reactions [post-id]", Short: "Retrieve the reactions of the post having the given id", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { From 0ed0bab794c35684532603dc044389ae3bf22d0a Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 28 Jun 2021 14:53:20 +0800 Subject: [PATCH 21/26] Remove GetPostReactions function --- x/profiles/types/query_dtag_requests.pb.go | 2 +- x/staging/posts/keeper/keeper_bench_test.go | 6 +++++- x/staging/posts/keeper/keeper_reactions.go | 10 ---------- x/staging/posts/keeper/keeper_reactions_test.go | 6 +++++- x/staging/posts/simulation/operations_reactions.go | 6 +++++- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/x/profiles/types/query_dtag_requests.pb.go b/x/profiles/types/query_dtag_requests.pb.go index 02e3bfa2ea..3cf0a737b9 100644 --- a/x/profiles/types/query_dtag_requests.pb.go +++ b/x/profiles/types/query_dtag_requests.pb.go @@ -27,7 +27,7 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// QueryIncomingDTagTransferRequests is the request type for the +// QueryIncomingDTagTransferRequestsRequest is the request type for the // Query/IncomingDTagTransferRequests RPC endpoint type QueryIncomingDTagTransferRequestsRequest struct { // Receiver represents the address of the user to which query the incoming requests for diff --git a/x/staging/posts/keeper/keeper_bench_test.go b/x/staging/posts/keeper/keeper_bench_test.go index d4948ffb85..d15239ff46 100644 --- a/x/staging/posts/keeper/keeper_bench_test.go +++ b/x/staging/posts/keeper/keeper_bench_test.go @@ -97,6 +97,10 @@ func (suite *KeeperTestSuite) BenchmarkKeeper_GetPostReactions(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - suite.k.GetPostReactions(suite.ctx, post.PostID) + var reactions []types.PostReaction + suite.k.IteratePostReactionsByPost(suite.ctx, post.PostID, func(_ int64, reaction types.PostReaction) bool { + reactions = append(reactions, reaction) + return false + }) } } diff --git a/x/staging/posts/keeper/keeper_reactions.go b/x/staging/posts/keeper/keeper_reactions.go index 0d0f0d924e..9b79b764d6 100644 --- a/x/staging/posts/keeper/keeper_reactions.go +++ b/x/staging/posts/keeper/keeper_reactions.go @@ -44,16 +44,6 @@ func (k Keeper) DeletePostReaction(ctx sdk.Context, reaction types.PostReaction) return nil } -// GetPostReactions returns the list of reactions that has been associated to the post having the given id -func (k Keeper) GetPostReactions(ctx sdk.Context, postID string) []types.PostReaction { - var reactions []types.PostReaction - k.IteratePostReactionsByPost(ctx, postID, func(_ int64, reaction types.PostReaction) bool { - reactions = append(reactions, reaction) - return false - }) - return reactions -} - // GetAllRegisteredReactions returns all the post reactions func (k Keeper) GetAllPostReactions(ctx sdk.Context) []types.PostReaction { var reactions []types.PostReaction diff --git a/x/staging/posts/keeper/keeper_reactions_test.go b/x/staging/posts/keeper/keeper_reactions_test.go index 47366930d9..9d959cc97d 100644 --- a/x/staging/posts/keeper/keeper_reactions_test.go +++ b/x/staging/posts/keeper/keeper_reactions_test.go @@ -282,7 +282,11 @@ func (suite *KeeperTestSuite) TestKeeper_GetPostReactions() { suite.Require().NoError(err) } - stored := suite.k.GetPostReactions(suite.ctx, test.postID) + var stored []types.PostReaction + suite.k.IteratePostReactionsByPost(suite.ctx, test.postID, func(_ int64, reaction types.PostReaction) bool { + stored = append(stored, reaction) + return false + }) suite.Len(stored, len(test.reactions)) for _, l := range test.reactions { diff --git a/x/staging/posts/simulation/operations_reactions.go b/x/staging/posts/simulation/operations_reactions.go index 44b3b11476..33d2107fb4 100644 --- a/x/staging/posts/simulation/operations_reactions.go +++ b/x/staging/posts/simulation/operations_reactions.go @@ -194,7 +194,11 @@ func randomRemovePostReactionFields( } post, _ := RandomPost(r, posts) - reactions := k.GetPostReactions(ctx, post.PostID) + var reactions []types.PostReaction + k.IteratePostReactionsByPost(ctx, post.PostID, func(_ int64, reaction types.PostReaction) bool { + reactions = append(reactions, reaction) + return false + }) // Skip if the post has no reactions if len(reactions) == 0 { From db84db7a971711f55e1cce4a533b19428482b5e2 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 28 Jun 2021 14:53:58 +0800 Subject: [PATCH 22/26] Update docs --- docs/developers/queries/staging/reactions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/developers/queries/staging/reactions.md b/docs/developers/queries/staging/reactions.md index 074d7eb08a..7949899f7b 100644 --- a/docs/developers/queries/staging/reactions.md +++ b/docs/developers/queries/staging/reactions.md @@ -15,8 +15,8 @@ This query allows you to retrieve the list of post reactions of a post. **CLI** ```bash -desmos query posts post-reactions [post-id] +desmos query posts reactions [post-id] # Example -# desmos query posts post-reactions 301921ac3c8e623d8f35aef1886fea20849e49f08ec8ddfdd9b96feaf0c4fd15 +# desmos query posts reactions 301921ac3c8e623d8f35aef1886fea20849e49f08ec8ddfdd9b96feaf0c4fd15 ``` \ No newline at end of file From 729f49186922da87ea10e915ba6dd6f6f41f4ad6 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 28 Jun 2021 14:56:38 +0800 Subject: [PATCH 23/26] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 862ff9cb83..5f4689d722 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - Improved the posts query ([#499](https://github.com/desmos-labs/desmos/issues/499)) - Added the post comments query ([#510](https://github.com/desmos-labs/desmos/pull/510)) - Added the ability to paginate incoming DTag transfer requests ([#519](https://github.com/desmos-labs/desmos/pull/519)) -- Added the post reactions query and improved the registered reactions structure ([#515](https://github.com/desmos-labs/desmos/pull/515)) +- Added the post reactions query and improved the methods of registered reactions ([#515](https://github.com/desmos-labs/desmos/pull/515)) # Version 0.16.3 ## Changes From 9a0ec992735003eee666d0f6e6e8ec73647cda17 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 30 Jun 2021 15:23:18 +0200 Subject: [PATCH 24/26] Update docs/developers/queries/staging/reactions.md --- docs/developers/queries/staging/reactions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/developers/queries/staging/reactions.md b/docs/developers/queries/staging/reactions.md index 7949899f7b..837aa10715 100644 --- a/docs/developers/queries/staging/reactions.md +++ b/docs/developers/queries/staging/reactions.md @@ -1,5 +1,5 @@ # Query registered reactions -This query allows you to retrieve the list of registered reactions inside a optional subspace. +This query allows you to retrieve the list of registered reactions inside an optional subspace. **CLI** ```bash @@ -19,4 +19,4 @@ desmos query posts reactions [post-id] # Example # desmos query posts reactions 301921ac3c8e623d8f35aef1886fea20849e49f08ec8ddfdd9b96feaf0c4fd15 -``` \ No newline at end of file +``` From 8c6846f1e6207625f1e647b4f89dcf03a7c0f97d Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 30 Jun 2021 15:23:22 +0200 Subject: [PATCH 25/26] Update x/staging/posts/client/cli/query.go --- x/staging/posts/client/cli/query.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staging/posts/client/cli/query.go b/x/staging/posts/client/cli/query.go index cdd92ade8c..f33e0387f8 100644 --- a/x/staging/posts/client/cli/query.go +++ b/x/staging/posts/client/cli/query.go @@ -258,7 +258,7 @@ func GetCmdQueryParams() *cobra.Command { func GetCmdQueryPostReactions() *cobra.Command { cmd := &cobra.Command{ Use: "reactions [post-id]", - Short: "Retrieve the reactions of the post having the given id", + Short: "Retrieve the reactions that have been added to the post having the given id", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) From a07edc349af386d4eee9b27a9295d35a0cf62abf Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 30 Jun 2021 15:23:27 +0200 Subject: [PATCH 26/26] Update docs/developers/queries/staging/reactions.md --- docs/developers/queries/staging/reactions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developers/queries/staging/reactions.md b/docs/developers/queries/staging/reactions.md index 837aa10715..38ce37e2a9 100644 --- a/docs/developers/queries/staging/reactions.md +++ b/docs/developers/queries/staging/reactions.md @@ -10,7 +10,7 @@ desmos query posts registered-reactions [[subspace-id]] ``` # Query post reactions -This query allows you to retrieve the list of post reactions of a post. +This query allows you to retrieve the list of reactions that have been added to a post. **CLI**