Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Solve the challenge #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Concretely, we would like to see the following components added:
- The addition of a new `AllComments` gRPC query method. This method's input should take a `post_slug` (a string), and returns all the comments on a post.
- The addition of two CLI subcommands, `tx create-comment` and `query list-comments`, which call the `CreateComment` and `AllComments` service methods under the hood.

See solution steps in [SOLUTION.md](SOLUTION.md)

## Requirements:

- Go v1.17+: https://golang.org/
Expand Down
70 changes: 70 additions & 0 deletions SOLUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Implementing task

## Run node

For first step command is from [here](https://docs.cosmos.network/v0.46/run-node/run-node.html)
For next steps I followed this [instruction](https://docs.cosmos.network/v0.46/run-node/run-node.html)

1. Build node

make build

2. Init config of node with chain-id `my-test-chain`. `monadfix` is arbitrary string.

build/regen init monadfix --chain-id my-test-chain

- `regen` used insted of `simd`
- `--home` flag to specify dirictory of config. But then you should pass it to every command.

3. Add account to network

build/regen keys add monadfix --keyring-backend test

- where `monadfix` is custom name.

4. Fetch address. We got `cosmos1728x3mclm0f980828tf74nq3vqvaxkkkdfwuc8`

build/regen keys show monadfix -a --keyring-backend test

5. Add money to your address of your account. See address in step `4`

build/regen add-genesis-account cosmos1728x3mclm0f980828tf74nq3vqvaxkkkdfwuc8 100000000000stake

6. Create validator

build/regen gentx monadfix 100000000stake --chain-id my-test-chain --keyring-backend test

build/regen collect-gentxs

7. Update gas config in `app.toml`

Modify `minimum-gas-prices` to `"0stake"`

8. Run local blockchain network

build/regen start

## Interact with blog

0. Set chain-id

build/regen config chain-id my-test-chain

1. Create post

build/regen tx blog create-post cosmos1728x3mclm0f980828tf74nq3vqvaxkkkdfwuc8 "first-post" "title" "body body body" --keyring-backend test -y

2. List all posts

build/regen query blog list-post

3. Comment has key is comprised by `comment` + `slug` + `_` + `uuidV7`.
Prefix `comment` is added by KVStore.

4. Create comment

build/regen tx blog create-comment cosmos1728x3mclm0f980828tf74nq3vqvaxkkkdfwuc8 "first-slug" "comment comment" --keyring-backend test -y

5. List comments of the particular slug

build/regen query blog list-comments "first-slug"
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.23.0
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
Expand Down Expand Up @@ -89,7 +90,6 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/rs/zerolog v1.23.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -101,6 +101,7 @@ require (
github.com/tendermint/btcd v0.1.1 // indirect
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/uuid6/uuid6go-proto v0.2.1 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,6 @@ github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqn
github.com/regen-network/cosmos-proto v0.3.1 h1:rV7iM4SSFAagvy8RiyhiACbWEGotmqzywPxOvwMdxcg=
github.com/regen-network/cosmos-proto v0.3.1/go.mod h1:jO0sVX6a1B36nmE8C9xBFXpNwWejXC7QqCOnH3O0+YM=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4 h1:c9jEnU+xm6vqyrQe3M94UFWqiXxRIKKnqBOh2EACmBE=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4 h1:c9jEnU+xm6vqyrQe3M94UFWqiXxRIKKnqBOh2EACmBE=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4/go.mod h1:/J8/bR1T/NXyIdQDLUaq15LjNE83nRzkyrLAMcPewig=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4/go.mod h1:/J8/bR1T/NXyIdQDLUaq15LjNE83nRzkyrLAMcPewig=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down Expand Up @@ -834,6 +832,8 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/uuid6/uuid6go-proto v0.2.1 h1:wuHy43Hy7CnWnWOkQGthek1YO43SQD+A/X+YaDH5B3Q=
github.com/uuid6/uuid6go-proto v0.2.1/go.mod h1:SFdogh0w3t8piuOBD4PXotblZWBqP2wospaF7txygWQ=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vmihailenco/msgpack/v5 v5.1.4/go.mod h1:C5gboKD0TJPqWDTVTtrQNfRbiBwHZGo8UTqP/9/XvLI=
Expand Down
12 changes: 12 additions & 0 deletions proto/blog/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "blog/v1/types.proto";
// Query defines the gRPC querier service.
service Query {
rpc AllPosts(QueryAllPostsRequest) returns (QueryAllPostsResponse);
rpc PostComments(QueryPostCommentsRequest) returns (QueryPostCommentsResponse);
}

message QueryAllPostsRequest {
Expand All @@ -20,3 +21,14 @@ message QueryAllPostsResponse {

cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryPostCommentsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string slug = 2;
}

message QueryPostCommentsResponse {
repeated Comment comments = 1;

cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
11 changes: 11 additions & 0 deletions proto/blog/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/regen-network/bec/x/blog";
// Msg is the blog.v1 Msg service
service Msg {
rpc CreatePost(MsgCreatePost) returns (MsgCreatePostResponse);
rpc CreateComment(MsgCreateComment) returns (MsgCreateCommentResponse);
}

// MsgCreatePost is the Msg/CreatePost request type.
Expand All @@ -18,3 +19,13 @@ message MsgCreatePost {

// MsgCreatePostResponse is the Msg/CreatePost response type.
message MsgCreatePostResponse {}

// MsgCreateComment is the Msg/CreateComment request type.
message MsgCreateComment {
string slug = 1;
string author = 2;
string body = 3;
}

// MsgCreateCommentResponse is the Msg/CreateComment response type.
message MsgCreateCommentResponse {}
9 changes: 9 additions & 0 deletions proto/blog/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ message Post {
string title = 3;
string body = 4;
}

message Comment {
// slug is a short human-readable string for the post, used as unique
// identifier for the post.
string slug = 1;
string author = 2;
string body = 3;
}

46 changes: 45 additions & 1 deletion x/blog/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func GetQueryCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdAllPosts())
cmd.AddCommand(
CmdAllPosts(),
CmdPostComments(),
)

return cmd
}
Expand Down Expand Up @@ -63,3 +66,44 @@ func CmdAllPosts() *cobra.Command {

return cmd
}

func CmdPostComments() *cobra.Command {
cmd := &cobra.Command{
Use: "list-comments [slug]",
Short: "list all comments of the post",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

argsSlug := string(args[0])

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := blog.NewQueryClient(clientCtx)

params := &blog.QueryPostCommentsRequest{
Pagination: pageReq,
Slug: argsSlug,
}

res, err := queryClient.PostComments(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "blog")

return cmd
}
39 changes: 38 additions & 1 deletion x/blog/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdCreatePost())
cmd.AddCommand(
CmdCreatePost(),
CmdCreateComment(),
)

return cmd
}
Expand Down Expand Up @@ -61,3 +64,37 @@ func CmdCreatePost() *cobra.Command {

return cmd
}

func CmdCreateComment() *cobra.Command {
cmd := &cobra.Command{
Use: "create-comment [author] [slug] [body]",
Short: "Creates a new comment",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}

argsSlug := string(args[1])
argsBody := string(args[2])

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := &blog.MsgCreateComment{
Author: clientCtx.GetFromAddress().String(),
Slug: argsSlug,
Body: argsBody,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
3 changes: 2 additions & 1 deletion x/blog/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const (
ModuleName = "blog"
StoreKey = ModuleName

PostKey = "post"
PostKey = "post"
CommentKey = "comment"
)

func KeyPrefix(p string) []byte {
Expand Down
Loading