Skip to content

Commit

Permalink
ERC721 posts (#338)
Browse files Browse the repository at this point in the history
* Add body field to posts

* Add MaxPostBodyLength param

* Added ERC721 fields to post

* regenerated proto files

* fix merge issues

* Fix linting error

Co-authored-by: Jake Hartnell <jake.hartnell@gmail.com>
  • Loading branch information
shanev and JakeHartnell committed Mar 13, 2021
1 parent f1bef1d commit 3f7bed1
Show file tree
Hide file tree
Showing 14 changed files with 822 additions and 138 deletions.
18 changes: 18 additions & 0 deletions proto/stargaze/curating/v1beta1/curating.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ message Post {
(gogoproto.moretags) = "yaml:\"total_amount\"",
(gogoproto.jsontag) = "total_amount"
];
string chain_id = 11 [
(gogoproto.customname) = "ChainID",
(gogoproto.moretags) = "yaml:\"chain_id\"",
(gogoproto.jsontag) = "chain_id",
(gogoproto.nullable) = true
];
string owner = 12 [ (gogoproto.moretags) = "yaml:\"owner\"" ];
string contract_address = 13
[ (gogoproto.moretags) = "yaml:\"contract_address\"" ];
string metadata = 14 [ (gogoproto.moretags) = "yaml:\"metadata\"" ];
bool locked = 15 [ (gogoproto.moretags) = "yaml:\"locked\"" ];
string parent_id = 16 [
(gogoproto.customname) = "ParentID",
(gogoproto.customtype) = "PostID",
(gogoproto.moretags) = "yaml:\"parent_id\"",
(gogoproto.jsontag) = "parent_id",
(gogoproto.nullable) = true
];
}

message Upvote {
Expand Down
16 changes: 16 additions & 0 deletions proto/stargaze/curating/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ message MsgPost {
string reward_account = 4
[ (gogoproto.moretags) = "yaml:\"reward_account\"" ];
string body = 5 [ (gogoproto.moretags) = "yaml:\"body\"" ];
string chain_id = 6 [
(gogoproto.customname) = "ChainID",
(gogoproto.moretags) = "yaml:\"chain_id\"",
(gogoproto.jsontag) = "chain_id",
(gogoproto.nullable) = true
];
string contract_address = 7
[ (gogoproto.moretags) = "yaml:\"contract_address\"" ];
string metadata = 8 [ (gogoproto.moretags) = "yaml:\"metadata\"" ];
string parent_id = 9 [
(gogoproto.customname) = "ParentID",
(gogoproto.customtype) = "PostID",
(gogoproto.moretags) = "yaml:\"parent_id\"",
(gogoproto.jsontag) = "parent_id",
(gogoproto.nullable) = true
];
}

message MsgUpvote {
Expand Down
22 changes: 17 additions & 5 deletions x/curating/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ func setup(t *testing.T) (*simapp.SimApp, sdk.Context) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(
ctx,
vendorID,
&postID,
bodyHash,
body,
addrs[0],
addrs[0],
"chain id",
nil,
"metadata",
nil,
)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand Down Expand Up @@ -159,12 +171,12 @@ func TestEndBlocker_RemoveFromExpiredQueue(t *testing.T) {

postID, err := types.PostIDFromString("777")
require.NoError(t, err)
_, err = app.CuratingKeeper.CreatePost(ctx, uint32(1), &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, uint32(1), &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

postID, err = types.PostIDFromString("888")
require.NoError(t, err)
_, err = app.CuratingKeeper.CreatePost(ctx, uint32(1), &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, uint32(1), &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

// force 2 different keys in the iterator underlying store
Expand All @@ -173,7 +185,7 @@ func TestEndBlocker_RemoveFromExpiredQueue(t *testing.T) {

postID, err = types.PostIDFromString("999")
require.NoError(t, err)
_, err = app.CuratingKeeper.CreatePost(ctx.WithBlockHeader(b), uint32(1), &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx.WithBlockHeader(b), uint32(1), &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

// fast-forward blocktime to simulate end of curation window
Expand Down Expand Up @@ -217,7 +229,7 @@ func TestEndblocker_BurnCoinsFromVotingPool(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1], "", nil, "", nil)
require.NoError(t, err)

// amt = 1
Expand Down
21 changes: 15 additions & 6 deletions x/curating/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ func TestPost(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(
ctx,
vendorID,
&postID,
bodyHash,
body,
addrs[0],
addrs[0],
"chain_id", nil, "metadata", nil,
)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand All @@ -38,7 +47,7 @@ func TestPost(t *testing.T) {
vps := app.CuratingKeeper.GetCurationQueueTimeSlice(ctx, ctx.BlockTime())
require.NotNil(t, vps)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.Equal(t, types.ErrDuplicatePost, err)
}

Expand All @@ -56,7 +65,7 @@ func TestPost_EmptyCreator(t *testing.T) {
require.NoError(t, err)

addrs := simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(1000000))
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, nil, addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, nil, addrs[1], "", nil, "", nil)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand Down Expand Up @@ -87,7 +96,7 @@ func TestPost_EmptyRewardAccount(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], nil)
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], nil, "", nil, "", nil)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand Down Expand Up @@ -115,7 +124,7 @@ func TestPost_WithRewardAccount(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[1], "", nil, "", nil)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand Down Expand Up @@ -146,7 +155,7 @@ func TestDeletePost(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[1], "", nil, "", nil)
require.NoError(t, err)

_, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand Down
13 changes: 12 additions & 1 deletion x/curating/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ func (k msgServer) Post(goCtx context.Context, msg *types.MsgPost) (*types.MsgPo
}

post, err := k.CreatePost(
ctx, msg.VendorID, &postID, bodyHash, msg.Body, creator, rewardAccount)
ctx,
msg.VendorID,
&postID,
bodyHash,
msg.Body,
creator,
rewardAccount,
msg.ChainID,
creator, // owner = creator initially
msg.Metadata,
msg.ParentID,
)
if err != nil {
return nil, err
}
Expand Down
55 changes: 48 additions & 7 deletions x/curating/keeper/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ func (k Keeper) GetPost(

// CreatePost registers a post on-chain and starts the curation period.
// It can be called from CreateUpvote() when a post doesn't exist yet.
func (k Keeper) CreatePost(ctx sdk.Context, vendorID uint32, postID *types.PostID,
bodyHash types.BodyHash, body string, creator, rewardAccount sdk.AccAddress) (post types.Post, err error) {
func (k Keeper) CreatePost(
ctx sdk.Context,
vendorID uint32,
postID *types.PostID,
bodyHash types.BodyHash,
body string,
creator, rewardAccount sdk.AccAddress,
chainID string,
contractAddress sdk.AccAddress,
metadata string,
parentID *types.PostID,
) (post types.Post, err error) {

err = k.validateVendorID(ctx, vendorID)
if err != nil {
Expand All @@ -46,13 +56,14 @@ func (k Keeper) CreatePost(ctx sdk.Context, vendorID uint32, postID *types.PostI
if err != nil {
return post, err
}
err = k.validatePostBodyLength(ctx, body)
if err != nil {
return post, err
}
if rewardAccount.Empty() {
rewardAccount = creator
}

curationWindow := k.GetParams(ctx).CurationWindow
curationEndTime := ctx.BlockTime().Add(curationWindow)

if postID != nil {
_, found, err := k.GetPost(ctx, vendorID, *postID)
if err != nil {
Expand All @@ -72,8 +83,23 @@ func (k Keeper) CreatePost(ctx sdk.Context, vendorID uint32, postID *types.PostI
return post, types.ErrInvalidPostID
}

post = types.NewPost(vendorID, *postID, bodyHash, body, creator, rewardAccount, curationEndTime)

curationWindow := k.GetParams(ctx).CurationWindow
curationEndTime := ctx.BlockTime().Add(curationWindow)
post = types.NewPost(
vendorID,
*postID,
bodyHash,
body,
creator,
rewardAccount,
curationEndTime,
chainID,
creator,
contractAddress,
metadata,
false,
parentID,
)
k.SetPost(ctx, post)
k.InsertCurationQueue(ctx, vendorID, *postID, curationEndTime)

Expand All @@ -88,9 +114,24 @@ func (k Keeper) CreatePost(ctx sdk.Context, vendorID uint32, postID *types.PostI
sdk.NewAttribute(types.AttributeKeyBody, body),
sdk.NewAttribute(types.AttributeCurationEndTime, curationEndTime.Format(time.RFC3339)),
sdk.NewAttribute(types.AttributeKeyVoteDenom, types.DefaultVoteDenom),
sdk.NewAttribute(types.AttributeKeyChainID, chainID),
sdk.NewAttribute(types.AttributeKeyContractAddress, contractAddress.String()),
sdk.NewAttribute(types.AttributeKeyMetadata, metadata),
sdk.NewAttribute(types.AttributeKeyLocked, "false"),
),
})

if parentID != nil {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypePost,
sdk.NewAttribute(types.AttributeKeyVendorID, fmt.Sprintf("%d", vendorID)),
sdk.NewAttribute(types.AttributeKeyPostID, postID.String()),
sdk.NewAttribute(types.AttributeKeyParentID, parentID.String()),
),
})
}

return post, nil
}

Expand Down
8 changes: 4 additions & 4 deletions x/curating/keeper/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCreatePost(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

post, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand All @@ -47,7 +47,7 @@ func TestCreatePost(t *testing.T) {

// add another post
postID, err = types.PostIDFromString("501")
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

// fast forward block time
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestCreateVendor0Post(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, nil, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, nil, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

post, found, err := app.CuratingKeeper.GetPost(ctx, vendorID, postID)
Expand All @@ -95,7 +95,7 @@ func TestCreateVendor0Post(t *testing.T) {

// add another post
postID, err = types.PostIDFromString("2")
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, nil, bodyHash, body, addrs[0], addrs[0])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, nil, bodyHash, body, addrs[0], addrs[0], "", nil, "", nil)
require.NoError(t, err)

// fast forward block time
Expand Down
14 changes: 13 additions & 1 deletion x/curating/keeper/upvote.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ func (k Keeper) CreateUpvote(
if !found {
// no deposit is locked
// this curator gets both creator + curator rewards (sent to reward_account)
_, err = k.CreatePost(ctx, vendorID, &postID, types.BodyHash{}, "", nil, rewardAccount)
_, err = k.CreatePost(
ctx,
vendorID,
&postID,
types.BodyHash{},
"",
nil,
rewardAccount,
"",
nil,
"",
nil,
)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions x/curating/keeper/upvote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestCreateUpvote_ExistingPost(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1], "", nil, "", nil)
require.NoError(t, err)

err = app.CuratingKeeper.CreateUpvote(ctx, vendorID, postID, addrs[0], addrs[0], 5)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestCreateUpvote_ExpiredPost(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1], "", nil, "", nil)
require.NoError(t, err)

ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour*24*3 + 1))
Expand All @@ -123,7 +123,7 @@ func TestMultipleUpvotes(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1], "", nil, "", nil)
require.NoError(t, err)

// amt = 1
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestCreateUpvote_ExistingUpvote(t *testing.T) {
bodyHash, err := types.BodyHashFromString(body)
require.NoError(t, err)

_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1])
_, err = app.CuratingKeeper.CreatePost(ctx, vendorID, &postID, bodyHash, body, addrs[1], addrs[1], "", nil, "", nil)
require.NoError(t, err)

err = app.CuratingKeeper.CreateUpvote(ctx, vendorID, postID, addrs[0], addrs[0], 5)
Expand Down
Loading

0 comments on commit 3f7bed1

Please sign in to comment.