Skip to content

Commit

Permalink
Merge branch 'master' of github.com:desmos-labs/desmos into riccardo/…
Browse files Browse the repository at this point in the history
…subspaces-sections-implementation

� Conflicts:
�	x/posts/keeper/genesis_test.go
  • Loading branch information
RiccardoM committed Jun 1, 2022
2 parents 986bad3 + 8379c67 commit 32ef4ff
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 171 deletions.

This file was deleted.

20 changes: 18 additions & 2 deletions proto/desmos/posts/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";
package desmos.posts.v1;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

import "desmos/posts/v1/models.proto";

option go_package = "github.com/desmos-labs/desmos/v3/x/posts/types";
Expand All @@ -18,9 +20,11 @@ message GenesisState {

repeated Attachment attachments = 3 [ (gogoproto.nullable) = false ];

repeated UserAnswer user_answers = 4 [ (gogoproto.nullable) = false ];
repeated ActivePollData active_polls = 4 [ (gogoproto.nullable) = false ];

repeated UserAnswer user_answers = 5 [ (gogoproto.nullable) = false ];

Params params = 5 [ (gogoproto.nullable) = false ];
Params params = 6 [ (gogoproto.nullable) = false ];
}

// SubspaceDataEntry contains the data for a given subspace
Expand All @@ -41,3 +45,15 @@ message GenesisPost {
uint32 initial_attachment_id = 2
[ (gogoproto.customname) = "InitialAttachmentID" ];
}

// ActivePollData contains the data of an active poll
message ActivePollData {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = true;

uint64 subspace_id = 1 [ (gogoproto.customname) = "SubspaceID" ];
uint64 post_id = 2 [ (gogoproto.customname) = "PostID" ];
uint32 poll_id = 3 [ (gogoproto.customname) = "PollID" ];
google.protobuf.Timestamp end_date = 4
[ (gogoproto.nullable) = false, (gogoproto.stdtime) = true ];
}
1 change: 1 addition & 0 deletions x/posts/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
nil,
)),
},
nil,
[]types.UserAnswer{
types.NewUserAnswer(1, 1, 1, []uint32{1}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"),
types.NewUserAnswer(1, 1, 1, []uint32{0, 1}, "cosmos1u65w3xnhga8ngyg44eudh07zdxmkzny6uaudfc"),
Expand Down
26 changes: 21 additions & 5 deletions x/posts/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
k.getSubspaceDataEntries(ctx),
k.getPostData(ctx),
k.getAllAttachments(ctx),
k.getAllActivePollsData(ctx),
k.getAllUserAnswers(ctx),
k.GetParams(ctx),
)
Expand Down Expand Up @@ -59,6 +60,21 @@ func (k Keeper) getAllAttachments(ctx sdk.Context) []types.Attachment {
return attachments
}

// getAllActivePollsData returns the active polls data
func (k Keeper) getAllActivePollsData(ctx sdk.Context) []types.ActivePollData {
var data []types.ActivePollData
k.IterateActivePolls(ctx, func(index int64, poll types.Attachment) (stop bool) {
data = append(data, types.NewActivePollData(
poll.SubspaceID,
poll.PostID,
poll.ID,
poll.Content.GetCachedValue().(*types.Poll).EndDate,
))
return false
})
return data
}

// getAllUserAnswers returns all the user answers stored inside the given context
func (k Keeper) getAllUserAnswers(ctx sdk.Context) []types.UserAnswer {
var answers []types.UserAnswer
Expand Down Expand Up @@ -87,11 +103,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
// Initialize the attachments
for _, attachment := range data.Attachments {
k.SaveAttachment(ctx, attachment)
if poll, ok := attachment.Content.GetCachedValue().(*types.Poll); ok {
if poll.EndDate.After(ctx.BlockTime()) {
k.InsertActivePollQueue(ctx, attachment)
}
}
}

// Initialize the active polls
for _, pollData := range data.ActivePolls {
k.setPollAsActive(ctx, pollData.SubspaceID, pollData.PostID, pollData.PollID, pollData.EndDate)
}

// Initialize the user answers
Expand Down
134 changes: 66 additions & 68 deletions x/posts/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (suite *KeeperTestsuite) TestKeeper_ExportGenesis() {
expGenesis: types.NewGenesisState([]types.SubspaceDataEntry{
types.NewSubspaceDataEntry(1, 1),
types.NewSubspaceDataEntry(2, 2),
}, nil, nil, nil, types.Params{}),
}, nil, nil, nil, nil, types.Params{}),
},
{
name: "posts are exported properly",
Expand Down Expand Up @@ -113,7 +113,7 @@ func (suite *KeeperTestsuite) TestKeeper_ExportGenesis() {
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
)),
}, nil, nil, types.Params{}),
}, nil, nil, nil, types.Params{}),
},
{
name: "attachments are exported properly",
Expand All @@ -137,7 +137,56 @@ func (suite *KeeperTestsuite) TestKeeper_ExportGenesis() {
"ftp://user:[email protected]/image.png",
"image/png",
)),
}, nil, types.Params{}),
}, nil, nil, types.Params{}),
},
{
name: "active polls are exported properly",
store: func(ctx sdk.Context) {
suite.k.SetParams(ctx, types.Params{})
suite.k.SaveAttachment(ctx, types.NewAttachment(1, 1, 2, types.NewPoll(
"What animal is best?",
[]types.Poll_ProvidedAnswer{
types.NewProvidedAnswer("Cat", nil),
types.NewProvidedAnswer("Dog", nil),
},
time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC),
false,
false,
nil,
)))
suite.k.InsertActivePollQueue(ctx, types.NewAttachment(1, 1, 2, types.NewPoll(
"What animal is best?",
[]types.Poll_ProvidedAnswer{
types.NewProvidedAnswer("Cat", nil),
types.NewProvidedAnswer("Dog", nil),
},
time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC),
false,
false,
nil,
)))
},
expGenesis: types.NewGenesisState(
nil,
nil,
[]types.Attachment{
types.NewAttachment(1, 1, 2, types.NewPoll(
"What animal is best?",
[]types.Poll_ProvidedAnswer{
types.NewProvidedAnswer("Cat", nil),
types.NewProvidedAnswer("Dog", nil),
},
time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC),
false,
false,
nil,
)),
},
[]types.ActivePollData{
types.NewActivePollData(1, 1, 2, time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC)),
},
nil,
types.Params{}),
},
{
name: "user answers are exported properly",
Expand All @@ -146,7 +195,7 @@ func (suite *KeeperTestsuite) TestKeeper_ExportGenesis() {
suite.k.SaveUserAnswer(ctx, types.NewUserAnswer(1, 1, 1, []uint32{1}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"))
suite.k.SaveUserAnswer(ctx, types.NewUserAnswer(1, 1, 2, []uint32{1, 2, 3}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"))
},
expGenesis: types.NewGenesisState(nil, nil, nil, []types.UserAnswer{
expGenesis: types.NewGenesisState(nil, nil, nil, nil, []types.UserAnswer{
types.NewUserAnswer(1, 1, 1, []uint32{1}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"),
types.NewUserAnswer(1, 1, 2, []uint32{1, 2, 3}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"),
}, types.Params{}),
Expand All @@ -156,7 +205,7 @@ func (suite *KeeperTestsuite) TestKeeper_ExportGenesis() {
store: func(ctx sdk.Context) {
suite.k.SetParams(ctx, types.NewParams(20))
},
expGenesis: types.NewGenesisState(nil, nil, nil, nil, types.NewParams(20)),
expGenesis: types.NewGenesisState(nil, nil, nil, nil, nil, types.NewParams(20)),
},
}

Expand Down Expand Up @@ -304,69 +353,6 @@ func (suite *KeeperTestsuite) TestKeeper_ImportGenesis() {
)), stored)
},
},
{
name: "poll attachment is added to active poll queue properly",
store: func(ctx sdk.Context) {
suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
))

suite.k.SavePost(ctx, types.NewPost(
1,
0,
1,
"External ID",
"This is a text",
"cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd",
1,
nil,
nil,
types.REPLY_SETTING_EVERYONE,
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
nil,
))
},
data: types.GenesisState{
Attachments: []types.Attachment{
types.NewAttachment(1, 1, 1, types.NewPoll(
"What animal is best?",
[]types.Poll_ProvidedAnswer{
types.NewProvidedAnswer("Cat", nil),
types.NewProvidedAnswer("Dog", nil),
},
time.Date(3000, 1, 1, 12, 00, 00, 000, time.UTC),
false,
false,
nil,
)),
},
},
check: func(ctx sdk.Context) {
stored, found := suite.k.GetAttachment(ctx, 1, 1, 1)
suite.Require().True(found)
suite.Require().Equal(types.NewAttachment(1, 1, 1, types.NewPoll(
"What animal is best?",
[]types.Poll_ProvidedAnswer{
types.NewProvidedAnswer("Cat", nil),
types.NewProvidedAnswer("Dog", nil),
},
time.Date(3000, 1, 1, 12, 00, 00, 000, time.UTC),
false,
false,
nil,
)), stored)

store := ctx.KVStore(suite.storeKey)
endDate := time.Date(3000, 1, 1, 12, 00, 00, 000, time.UTC)
suite.Require().True(store.Has(types.ActivePollQueueKey(1, 1, 1, endDate)))
},
},
{
name: "user answer is imported properly",
store: func(ctx sdk.Context) {
Expand Down Expand Up @@ -418,6 +404,18 @@ func (suite *KeeperTestsuite) TestKeeper_ImportGenesis() {
suite.Require().Equal(types.NewUserAnswer(1, 1, 1, []uint32{1}, "cosmos1vs8dps0ktst5ekynmszxuxphfq08rhmepsn8st"), stored)
},
},
{
name: "active polls are imported properly",
data: types.GenesisState{
ActivePolls: []types.ActivePollData{
types.NewActivePollData(1, 1, 2, time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC)),
},
},
check: func(ctx sdk.Context) {
store := ctx.KVStore(suite.storeKey)
suite.Require().True(store.Has(types.ActivePollQueueKey(1, 1, 2, time.Date(2100, 1, 1, 12, 00, 00, 000, time.UTC))))
},
},
{
name: "params are imported properly",
data: types.GenesisState{
Expand Down
13 changes: 10 additions & 3 deletions x/posts/keeper/polls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/desmos-labs/desmos/v3/x/posts/types"
Expand Down Expand Up @@ -62,12 +64,17 @@ func (k Keeper) Tally(ctx sdk.Context, subspaceID uint64, postID uint64, pollID

// --------------------------------------------------------------------------------------------------------------------

// setPollAsActive sets the poll with the given details as active
func (k Keeper) setPollAsActive(ctx sdk.Context, subspaceID uint64, postID uint64, pollID uint32, endTime time.Time) {
store := ctx.KVStore(k.storeKey)
bz := types.GetPollIDBytes(subspaceID, postID, pollID)
store.Set(types.ActivePollQueueKey(subspaceID, postID, pollID, endTime), bz)
}

// InsertActivePollQueue inserts a poll into the active poll queue
func (k Keeper) InsertActivePollQueue(ctx sdk.Context, poll types.Attachment) {
store := ctx.KVStore(k.storeKey)
bz := types.GetPollIDBytes(poll.SubspaceID, poll.PostID, poll.ID)
content := poll.Content.GetCachedValue().(*types.Poll)
store.Set(types.ActivePollQueueKey(poll.SubspaceID, poll.PostID, poll.ID, content.EndDate), bz)
k.setPollAsActive(ctx, poll.SubspaceID, poll.PostID, poll.ID, content.EndDate)
}

// RemoveFromActivePollQueue removes a poll from the active poll queue
Expand Down
20 changes: 19 additions & 1 deletion x/posts/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package simulation

import (
"math/rand"
"time"

"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand All @@ -26,11 +27,12 @@ func RandomizeGenState(simState *module.SimulationState) {
posts := randomPosts(simState.Rand, subspacesGenesis.Subspaces, simState.Accounts, params)
subspacesDataEntries := getSubspacesData(posts)
attachments := randomAttachments(simState.Rand, posts)
activePolls := getActivePollsData(attachments)
genesisPosts := getGenesisPosts(posts, attachments)
userAnswers := randomUserAnswers(simState.Rand, attachments, simState.Accounts)

// Save the genesis
postsGenesis := types.NewGenesisState(subspacesDataEntries, genesisPosts, attachments, userAnswers, params)
postsGenesis := types.NewGenesisState(subspacesDataEntries, genesisPosts, attachments, activePolls, userAnswers, params)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(postsGenesis)
}

Expand Down Expand Up @@ -84,6 +86,22 @@ func randomAttachments(r *rand.Rand, posts []types.Post) (attachments []types.At
return attachments
}

// getActivePollsData gets the active polls data from the given attachments slice
func getActivePollsData(attachments []types.Attachment) []types.ActivePollData {
var data []types.ActivePollData
for _, attachment := range attachments {
if poll, ok := attachment.Content.GetCachedValue().(*types.Poll); ok && poll.EndDate.After(time.Now()) {
data = append(data, types.NewActivePollData(
attachment.SubspaceID,
attachment.PostID,
attachment.ID,
poll.EndDate,
))
}
}
return data
}

// randomUserAnswers returns randomly generated user answers
func randomUserAnswers(r *rand.Rand, attachments []types.Attachment, accs []simtypes.Account) (answers []types.UserAnswer) {
if len(attachments) == 0 {
Expand Down
Loading

0 comments on commit 32ef4ff

Please sign in to comment.