Skip to content

Commit

Permalink
add wage handler for checkers blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
walter-cavinaw committed Nov 1, 2023
1 parent 75c9a48 commit d2fc7d4
Show file tree
Hide file tree
Showing 27 changed files with 579 additions and 83 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mock-expected-keepers:
mockgen -source=x/checkers/types/expected_keepers.go -package testutil -destination=x/checkers/testutil/expected_keepers_mocks.go
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
checkersmoduletypes.ModuleName: nil,
// this line is used by starport scaffolding # stargate/app/maccPerms
}
)
Expand Down Expand Up @@ -394,6 +395,7 @@ func New(
keys[checkersmoduletypes.StoreKey],
keys[checkersmoduletypes.MemStoreKey],
app.GetSubspace(checkersmoduletypes.ModuleName),
app.BankKeeper,
)
checkersModule := checkersmodule.NewAppModule(appCodec, app.CheckersKeeper, app.AccountKeeper, app.BankKeeper)

Expand Down
15 changes: 15 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ paths:
afterIndex:
type: string
description: Pertains to the FIFO. Toward tail.
wager:
type: string
format: uint64
pagination:
type: object
properties:
Expand Down Expand Up @@ -218,6 +221,9 @@ paths:
afterIndex:
type: string
description: Pertains to the FIFO. Toward tail.
wager:
type: string
format: uint64
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -29788,6 +29794,9 @@ definitions:
afterIndex:
type: string
description: Pertains to the FIFO. Toward tail.
wager:
type: string
format: uint64
pagination:
type: object
properties:
Expand Down Expand Up @@ -29842,6 +29851,9 @@ definitions:
afterIndex:
type: string
description: Pertains to the FIFO. Toward tail.
wager:
type: string
format: uint64
alice.checkers.checkers.QueryGetSystemInfoResponse:
type: object
properties:
Expand Down Expand Up @@ -29890,6 +29902,9 @@ definitions:
afterIndex:
type: string
description: Pertains to the FIFO. Toward tail.
wager:
type: string
format: uint64
alice.checkers.checkers.SystemInfo:
type: object
properties:
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ require (
github.com/cosmos/cosmos-sdk v0.45.4
github.com/cosmos/ibc-go/v3 v3.0.0
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/ignite-hq/cli v0.22.0
github.com/kr/pretty v0.3.1 // indirect
github.com/spf13/cast v1.4.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
github.com/tendermint/spn v0.2.1-0.20220609194312-7833ecf4454a
github.com/tendermint/tendermint v0.34.19
github.com/tendermint/tm-db v0.6.7
golang.org/x/net v0.10.0 // indirect
google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0
golang.org/x/net v0.12.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/grpc v1.57.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
231 changes: 215 additions & 16 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions proto/checkers/stored_game.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ message StoredGame {
uint64 moveCount = 8;
string beforeIndex = 9; // Pertains to the FIFO. Toward head.
string afterIndex = 10; // Pertains to the FIFO. Toward tail.
uint64 wager = 11;
}

1 change: 1 addition & 0 deletions proto/checkers/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ message MsgCreateGame {
string creator = 1;
string black = 2;
string red = 3;
uint64 wager = 4;
}

message MsgCreateGameResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export interface CheckersStoredGame {

/** Pertains to the FIFO. Toward tail. */
afterIndex?: string;

/** @format uint64 */
wager?: string;
}

export interface CheckersSystemInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface StoredGame {
beforeIndex: string;
/** Pertains to the FIFO. Toward tail. */
afterIndex: string;
wager: number;
}

const baseStoredGame: object = {
Expand All @@ -30,6 +31,7 @@ const baseStoredGame: object = {
moveCount: 0,
beforeIndex: "",
afterIndex: "",
wager: 0,
};

export const StoredGame = {
Expand Down Expand Up @@ -64,6 +66,9 @@ export const StoredGame = {
if (message.afterIndex !== "") {
writer.uint32(82).string(message.afterIndex);
}
if (message.wager !== 0) {
writer.uint32(88).uint64(message.wager);
}
return writer;
},

Expand Down Expand Up @@ -104,6 +109,9 @@ export const StoredGame = {
case 10:
message.afterIndex = reader.string();
break;
case 11:
message.wager = longToNumber(reader.uint64() as Long);
break;
default:
reader.skipType(tag & 7);
break;
Expand Down Expand Up @@ -164,6 +172,11 @@ export const StoredGame = {
} else {
message.afterIndex = "";
}
if (object.wager !== undefined && object.wager !== null) {
message.wager = Number(object.wager);
} else {
message.wager = 0;
}
return message;
},

Expand All @@ -180,6 +193,7 @@ export const StoredGame = {
message.beforeIndex !== undefined &&
(obj.beforeIndex = message.beforeIndex);
message.afterIndex !== undefined && (obj.afterIndex = message.afterIndex);
message.wager !== undefined && (obj.wager = message.wager);
return obj;
},

Expand Down Expand Up @@ -235,6 +249,11 @@ export const StoredGame = {
} else {
message.afterIndex = "";
}
if (object.wager !== undefined && object.wager !== null) {
message.wager = object.wager;
} else {
message.wager = 0;
}
return message;
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface MsgCreateGame {
creator: string;
black: string;
red: string;
wager: number;
}

export interface MsgCreateGameResponse {
Expand All @@ -29,7 +30,7 @@ export interface MsgPlayMoveResponse {
winner: string;
}

const baseMsgCreateGame: object = { creator: "", black: "", red: "" };
const baseMsgCreateGame: object = { creator: "", black: "", red: "", wager: 0 };

export const MsgCreateGame = {
encode(message: MsgCreateGame, writer: Writer = Writer.create()): Writer {
Expand All @@ -42,6 +43,9 @@ export const MsgCreateGame = {
if (message.red !== "") {
writer.uint32(26).string(message.red);
}
if (message.wager !== 0) {
writer.uint32(32).uint64(message.wager);
}
return writer;
},

Expand All @@ -61,6 +65,9 @@ export const MsgCreateGame = {
case 3:
message.red = reader.string();
break;
case 4:
message.wager = longToNumber(reader.uint64() as Long);
break;
default:
reader.skipType(tag & 7);
break;
Expand All @@ -86,6 +93,11 @@ export const MsgCreateGame = {
} else {
message.red = "";
}
if (object.wager !== undefined && object.wager !== null) {
message.wager = Number(object.wager);
} else {
message.wager = 0;
}
return message;
},

Expand All @@ -94,6 +106,7 @@ export const MsgCreateGame = {
message.creator !== undefined && (obj.creator = message.creator);
message.black !== undefined && (obj.black = message.black);
message.red !== undefined && (obj.red = message.red);
message.wager !== undefined && (obj.wager = message.wager);
return obj;
},

Expand All @@ -114,6 +127,11 @@ export const MsgCreateGame = {
} else {
message.red = "";
}
if (object.wager !== undefined && object.wager !== null) {
message.wager = object.wager;
} else {
message.wager = 0;
}
return message;
},
};
Expand Down
9 changes: 7 additions & 2 deletions x/checkers/client/cli/tx_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ var _ = strconv.Itoa(0)

func CmdCreateGame() *cobra.Command {
cmd := &cobra.Command{
Use: "create-game [black] [red]",
Use: "create-game [black] [red] [wager]",
Short: "Broadcast message createGame",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argBlack := args[0]
argRed := args[1]
argWager, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -30,6 +34,7 @@ func CmdCreateGame() *cobra.Command {
clientCtx.GetFromAddress().String(),
argBlack,
argRed,
argWager,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions x/checkers/keeper/end_block_server_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func (k Keeper) ForfeitExpiredGames(goCtx context.Context) {
if storedGame.MoveCount <= 1 {
// No point in keeping a game that was never really played
k.RemoveStoredGame(ctx, gameIndex)
if storedGame.MoveCount == 1 {
k.MustRefundWager(ctx, &storedGame)
}
} else {
storedGame.Winner, found = opponents[storedGame.Turn]
if !found {
panic(fmt.Sprintf(types.ErrCannotFindWinnerByColor.Error(), storedGame.Turn))
}
k.MustPayWinnings(ctx, &storedGame)
storedGame.Board = ""
k.SetStoredGame(ctx, storedGame)
}
Expand Down
5 changes: 3 additions & 2 deletions x/checkers/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type (
storeKey sdk.StoreKey
memKey sdk.StoreKey
paramstore paramtypes.Subspace
bank types.BankEscrowKeeper
}
)

Expand All @@ -25,19 +26,19 @@ func NewKeeper(
storeKey,
memKey sdk.StoreKey,
ps paramtypes.Subspace,

bank types.BankEscrowKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{

cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
bank: bank,
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/checkers/keeper/msg_server_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (
MoveCount: 0,
BeforeIndex: types.NoFifoIndex,
AfterIndex: types.NoFifoIndex,
Wager: msg.Wager,
}

err := storedGame.Validate()
Expand All @@ -47,6 +48,7 @@ func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (
sdk.NewAttribute(types.GameCreatedEventGameIndex, newIndex),
sdk.NewAttribute(types.GameCreatedEventBlack, msg.Black),
sdk.NewAttribute(types.GameCreatedEventRed, msg.Red),
sdk.NewAttribute(types.GameCreatedEventWager, strconv.FormatUint(msg.Wager, 10)),
),
)

Expand Down
5 changes: 5 additions & 0 deletions x/checkers/keeper/msg_server_create_game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestCreateGame(t *testing.T) {
Creator: alice,
Black: bob,
Red: carol,
Wager: 45,
})
require.Nil(t, err)
require.EqualValues(t, types.MsgCreateGameResponse{
Expand All @@ -38,6 +39,7 @@ func TestCreate1GameHasSaved(t *testing.T) {
Creator: alice,
Black: bob,
Red: carol,
Wager: 45,
})
systemInfo, found := keeper.GetSystemInfo(sdk.UnwrapSDKContext(context))
require.True(t, found)
Expand All @@ -56,6 +58,7 @@ func TestCreate1GameHasSaved(t *testing.T) {
Deadline: types.FormatDeadline(ctx.BlockTime().Add(types.MaxTurnDuration)),
BeforeIndex: "-1",
AfterIndex: "-1",
Wager: 45,
}, game1)
}

Expand All @@ -65,6 +68,7 @@ func TestCreate1GameEmitted(t *testing.T) {
Creator: alice,
Black: bob,
Red: carol,
Wager: 45,
})
ctx := sdk.UnwrapSDKContext(context)
require.NotNil(t, ctx)
Expand All @@ -78,6 +82,7 @@ func TestCreate1GameEmitted(t *testing.T) {
{Key: "game-index", Value: "1"},
{Key: "black", Value: bob},
{Key: "red", Value: carol},
{Key: "wager", Value: "45"},
},
}, event)
}
Loading

0 comments on commit d2fc7d4

Please sign in to comment.