Skip to content

Commit

Permalink
added helpers and test;
Browse files Browse the repository at this point in the history
  • Loading branch information
olimdzhon committed Jul 28, 2024
1 parent c8d3af3 commit ad21670
Show file tree
Hide file tree
Showing 18 changed files with 1,916 additions and 78 deletions.
1,151 changes: 1,123 additions & 28 deletions api/checkers/checkers/tx.pulsar.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions api/checkers/checkers/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/static/openapi.yml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: ibc
commit: 54361afc727f439da900e4de7ede78e7
digest: shake256:1787780063b7068e482a3246104afafe8e7f5e9ae29f2199a24bf56d590a6de681010a674b02c6814c8fcf6e1bf2de66b80adeaf0b57997003b3cc3e6ea6dbe8
commit: 7f7c07e11c014b1f848fa5728330e31d
digest: shake256:0d08fc2ad790a120d894f28ccd554d3060bfd602b61aa754614e3366f13da7cff7cb075ecfa7043e74609c054211472adddb37aa0d7f861334c35c0693b02f2e
- remote: buf.build
owner: cosmos
repository: ics23
Expand Down
34 changes: 22 additions & 12 deletions proto/checkers/checkers/tx.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
syntax = "proto3";

package checkers.checkers;

import "amino/amino.proto";
Expand All @@ -12,29 +13,38 @@ option go_package = "github.com/olimdzhon/checkers/x/checkers/types";
// Msg defines the Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

// UpdateParams defines a (governance) operation for updating the module
// parameters. The authority defaults to the x/gov module account.
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc UpdateParams (MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc CreateGame (MsgCreateGame ) returns (MsgCreateGameResponse );
}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "checkers/x/checkers/MsgUpdateParams";

option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "checkers/x/checkers/MsgUpdateParams";
// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the module parameters to update.
//

// NOTE: All parameters must be supplied.
Params params = 2 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
Params params = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}
message MsgUpdateParamsResponse {}

message MsgCreateGame {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
string black = 2;
string red = 3;
}

message MsgCreateGameResponse {
string gameIndex = 1;
}

17 changes: 17 additions & 0 deletions x/checkers/keeper/msg_server_create_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/olimdzhon/checkers/x/checkers/types"
)

func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (*types.MsgCreateGameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgCreateGameResponse{}, nil
}
6 changes: 6 additions & 0 deletions x/checkers/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
{
RpcMethod: "CreateGame",
Use: "create-game [black] [red]",
Short: "Send a createGame tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "black"}, {ProtoField: "red"}},
},
// this line is used by ignite scaffolding # autocli/tx
},
},
Expand Down
25 changes: 24 additions & 1 deletion x/checkers/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ var (
)

const (
// this line is used by starport scaffolding # simapp/module/const
opWeightMsgCreateGame = "op_weight_msg_create_game"
// TODO: Determine the simulation weight value
defaultWeightMsgCreateGame int = 100

// this line is used by starport scaffolding # simapp/module/const
)

// GenerateGenesisState creates a randomized GenState of the module.
Expand All @@ -46,6 +50,17 @@ func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgCreateGame int
simState.AppParams.GetOrGenerate(opWeightMsgCreateGame, &weightMsgCreateGame, nil,
func(_ *rand.Rand) {
weightMsgCreateGame = defaultWeightMsgCreateGame
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgCreateGame,
checkerssimulation.SimulateMsgCreateGame(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand All @@ -54,6 +69,14 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
// ProposalMsgs returns msgs used for governance proposals for simulations.
func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
opWeightMsgCreateGame,
defaultWeightMsgCreateGame,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
checkerssimulation.SimulateMsgCreateGame(am.accountKeeper, am.bankKeeper, am.keeper)
return nil
},
),
// this line is used by starport scaffolding # simapp/module/OpMsg
}
}
29 changes: 29 additions & 0 deletions x/checkers/simulation/create_game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/olimdzhon/checkers/x/checkers/keeper"
"github.com/olimdzhon/checkers/x/checkers/types"
)

func SimulateMsgCreateGame(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := &types.MsgCreateGame{
Creator: simAccount.Address.String(),
}

// TODO: Handling the CreateGame simulation

return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "CreateGame simulation not implemented"), nil, nil
}
}
7 changes: 7 additions & 0 deletions x/checkers/testutil/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testutil

const (
Alice = "cosmos1jmjfq0tplp9tmx4v9uemw72y4d2wa5nr3xn9d3"
Bob = "cosmos1xyxs3skf3f4jfqeuv89yyaqvjc6lffavxqhc8g"
Carol = "cosmos1e0w5t53nrq7p66fye6c8p0ynyhf6y24l4yuxd7"
)
3 changes: 3 additions & 0 deletions x/checkers/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCreateGame{},
)
// this line is used by starport scaffolding # 3

registry.RegisterImplementations((*sdk.Msg)(nil),
Expand Down
8 changes: 4 additions & 4 deletions x/checkers/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

// x/checkers module sentinel errors
var (
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrSample = sdkerrors.Register(ModuleName, 1101, "sample error")
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrSample = sdkerrors.Register(ModuleName, 1101, "sample error")
ErrInvalidBlack = sdkerrors.Register(ModuleName, 1100, "black address is invalid: %s")
ErrInvalidRed = sdkerrors.Register(ModuleName, 1101, "red address is invalid: %s")
ErrGameNotParseable = sdkerrors.Register(ModuleName, 1102, "game cannot be parsed")
ErrInvalidRed = sdkerrors.Register(ModuleName, 1101, "red address is invalid: %s")
ErrGameNotParseable = sdkerrors.Register(ModuleName, 1102, "game cannot be parsed")
)
35 changes: 23 additions & 12 deletions x/checkers/types/full_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func (storedGame StoredGame) GetBlackAddress() (black sdk.AccAddress, err error) {
black, errBlack := sdk.AccAddressFromBech32(storedGame.Black)
return black, errorsmod.Wrapf(errBlack, ErrInvalidBlack.Error(), storedGame.Black)
black, errBlack := sdk.AccAddressFromBech32(storedGame.Black)
return black, errorsmod.Wrapf(errBlack, ErrInvalidBlack.Error(), storedGame.Black)
}

func (storedGame StoredGame) GetRedAddress() (red sdk.AccAddress, err error) {
Expand All @@ -20,15 +20,26 @@ func (storedGame StoredGame) GetRedAddress() (red sdk.AccAddress, err error) {
}

func (storedGame StoredGame) ParseGame() (game *rules.Game, err error) {
board, errBoard := rules.Parse(storedGame.Board)
if errBoard != nil {
return nil, errorsmod.Wrapf(errBoard, ErrGameNotParseable.Error())
}
board.Turn = rules.StringPieces[storedGame.Turn].Player
if board.Turn.Color == "" {
return nil, errorsmod.Wrapf(fmt.Errorf("turn: %s", storedGame.Turn), ErrGameNotParseable.Error())
}
return board, nil
board, errBoard := rules.Parse(storedGame.Board)
if errBoard != nil {
return nil, errorsmod.Wrapf(errBoard, ErrGameNotParseable.Error())
}
board.Turn = rules.StringPieces[storedGame.Turn].Player
if board.Turn.Color == "" {
return nil, errorsmod.Wrapf(fmt.Errorf("turn: %s", storedGame.Turn), ErrGameNotParseable.Error())
}
return board, nil
}


func (storedGame StoredGame) Validate() (err error) {
_, err = storedGame.GetBlackAddress()
if err != nil {
return err
}
_, err = storedGame.GetRedAddress()
if err != nil {
return err
}
_, err = storedGame.ParseGame()
return err
}
53 changes: 53 additions & 0 deletions x/checkers/types/full_game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/olimdzhon/checkers/x/checkers/rules"
"github.com/olimdzhon/checkers/x/checkers/testutil"
"github.com/olimdzhon/checkers/x/checkers/types"
"github.com/stretchr/testify/require"
)

const (
alice = testutil.Alice
bob = testutil.Bob
)

func GetStoredGame1() types.StoredGame {
return types.StoredGame{
Black: alice,
Red: bob,
Index: "1",
Board: rules.New().String(),
Turn: "b",
}
}

func TestCanGetAddressBlack(t *testing.T) {
aliceAddress, err1 := sdk.AccAddressFromBech32(alice)
black, err2 := GetStoredGame1().GetBlackAddress()
require.Equal(t, aliceAddress, black)
require.Nil(t, err2)
require.Nil(t, err1)
}

func TestGetAddressWrongBlack(t *testing.T) {
storedGame := GetStoredGame1()
storedGame.Black = "cosmos1jmjfq0tplp9tmx4v9uemw72y4d2wa5nr3xn9d4" // Bad last digit
black, err := storedGame.GetBlackAddress()
require.Nil(t, black)
require.EqualError(t,
err,
"black address is invalid: cosmos1jmjfq0tplp9tmx4v9uemw72y4d2wa5nr3xn9d4: decoding bech32 failed: invalid checksum (expected 3xn9d3 got 3xn9d4)")
require.EqualError(t, storedGame.Validate(), err.Error())
}

func TestCanGetAddressRed(t *testing.T) {
bobAddress, err1 := sdk.AccAddressFromBech32(alice)
red, err2 := GetStoredGame1().GetBlackAddress()
require.Equal(t, bobAddress, red)
require.Nil(t, err2)
require.Nil(t, err1)
}
Loading

0 comments on commit ad21670

Please sign in to comment.