Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a storage next game id with starport.
starport scaffold single nextGame idValue:uint --module checkers --no-message Then changed the genesis value.
- Loading branch information
1 parent
995fda0
commit 463968f
Showing
21 changed files
with
1,555 additions
and
744 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
syntax = "proto3"; | ||
package xavierlepretre.checkers.checkers; | ||
|
||
option go_package = "github.com/xavierlepretre/checkers/x/checkers/types"; | ||
|
||
import "gogoproto/gogo.proto"; | ||
|
||
message NextGame { | ||
string creator = 1; | ||
uint64 idValue = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/spf13/cobra" | ||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
) | ||
|
||
func CmdShowNextGame() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "show-next-game", | ||
Short: "shows nextGame", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx := client.GetClientContextFromCmd(cmd) | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
params := &types.QueryGetNextGameRequest{} | ||
|
||
res, err := queryClient.NextGame(context.Background(), params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cli_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" | ||
"github.com/stretchr/testify/require" | ||
tmcli "github.com/tendermint/tendermint/libs/cli" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/xavierlepretre/checkers/testutil/network" | ||
"github.com/xavierlepretre/checkers/x/checkers/client/cli" | ||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
) | ||
|
||
func networkWithNextGameObjects(t *testing.T) (*network.Network, *types.NextGame) { | ||
t.Helper() | ||
cfg := network.DefaultConfig() | ||
state := types.GenesisState{} | ||
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) | ||
|
||
state.NextGame = &types.NextGame{Creator: "ANY"} | ||
buf, err := cfg.Codec.MarshalJSON(&state) | ||
require.NoError(t, err) | ||
cfg.GenesisState[types.ModuleName] = buf | ||
return network.New(t, cfg), state.NextGame | ||
} | ||
|
||
func TestShowNextGame(t *testing.T) { | ||
net, obj := networkWithNextGameObjects(t) | ||
|
||
ctx := net.Validators[0].ClientCtx | ||
common := []string{ | ||
fmt.Sprintf("--%s=json", tmcli.OutputFlag), | ||
} | ||
for _, tc := range []struct { | ||
desc string | ||
args []string | ||
err error | ||
obj *types.NextGame | ||
}{ | ||
{ | ||
desc: "get", | ||
args: common, | ||
obj: obj, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.desc, func(t *testing.T) { | ||
var args []string | ||
args = append(args, tc.args...) | ||
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowNextGame(), args) | ||
if tc.err != nil { | ||
stat, ok := status.FromError(tc.err) | ||
require.True(t, ok) | ||
require.ErrorIs(t, stat.Err(), tc.err) | ||
} else { | ||
require.NoError(t, err) | ||
var resp types.QueryGetNextGameResponse | ||
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) | ||
require.NotNil(t, resp.NextGame) | ||
require.Equal(t, tc.obj, resp.NextGame) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (k Keeper) NextGame(c context.Context, req *types.QueryGetNextGameRequest) (*types.QueryGetNextGameResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid request") | ||
} | ||
ctx := sdk.UnwrapSDKContext(c) | ||
|
||
val, found := k.GetNextGame(ctx) | ||
if !found { | ||
return nil, status.Error(codes.InvalidArgument, "not found") | ||
} | ||
|
||
return &types.QueryGetNextGameResponse{NextGame: &val}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package keeper | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
) | ||
|
||
func TestNextGameQuery(t *testing.T) { | ||
keeper, ctx := setupKeeper(t) | ||
wctx := sdk.WrapSDKContext(ctx) | ||
item := createTestNextGame(keeper, ctx) | ||
for _, tc := range []struct { | ||
desc string | ||
request *types.QueryGetNextGameRequest | ||
response *types.QueryGetNextGameResponse | ||
err error | ||
}{ | ||
{ | ||
desc: "First", | ||
request: &types.QueryGetNextGameRequest{}, | ||
response: &types.QueryGetNextGameResponse{NextGame: &item}, | ||
}, | ||
{ | ||
desc: "InvalidRequest", | ||
err: status.Error(codes.InvalidArgument, "invalid request"), | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.desc, func(t *testing.T) { | ||
response, err := keeper.NextGame(wctx, tc.request) | ||
if tc.err != nil { | ||
require.ErrorIs(t, err, tc.err) | ||
} else { | ||
require.Equal(t, tc.response, response) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
) | ||
|
||
// SetNextGame set nextGame in the store | ||
func (k Keeper) SetNextGame(ctx sdk.Context, nextGame types.NextGame) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextGameKey)) | ||
b := k.cdc.MustMarshalBinaryBare(&nextGame) | ||
store.Set([]byte{0}, b) | ||
} | ||
|
||
// GetNextGame returns nextGame | ||
func (k Keeper) GetNextGame(ctx sdk.Context) (val types.NextGame, found bool) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextGameKey)) | ||
|
||
b := store.Get([]byte{0}) | ||
if b == nil { | ||
return val, false | ||
} | ||
|
||
k.cdc.MustUnmarshalBinaryBare(b, &val) | ||
return val, true | ||
} | ||
|
||
// RemoveNextGame removes nextGame from the store | ||
func (k Keeper) RemoveNextGame(ctx sdk.Context) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NextGameKey)) | ||
store.Delete([]byte{0}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package keeper | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/xavierlepretre/checkers/x/checkers/types" | ||
) | ||
|
||
func createTestNextGame(keeper *Keeper, ctx sdk.Context) types.NextGame { | ||
item := types.NextGame{ | ||
Creator: "any", | ||
} | ||
keeper.SetNextGame(ctx, item) | ||
return item | ||
} | ||
|
||
func TestNextGameGet(t *testing.T) { | ||
keeper, ctx := setupKeeper(t) | ||
item := createTestNextGame(keeper, ctx) | ||
rst, found := keeper.GetNextGame(ctx) | ||
assert.True(t, found) | ||
assert.Equal(t, item, rst) | ||
} | ||
func TestNextGameRemove(t *testing.T) { | ||
keeper, ctx := setupKeeper(t) | ||
createTestNextGame(keeper, ctx) | ||
keeper.RemoveNextGame(ctx) | ||
_, found := keeper.GetNextGame(ctx) | ||
assert.False(t, found) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.