# Add Your First Message
In the previous section, you added a game storage space in the checkers module storage. It is now time to actually put games in storage:
- Add a message type to create a game.
- Add all the necessary keeper and message server functions.
- Add CLI commands.
# Add a game creation message
With the storage ready to receive games, it is time to add a message to let players create games:
- A game starts with an initial board and turn, as per the rules. The game creator does not choose how the initial pieces are placed.
- A game needs an unused index to be created. For simplicity's sake, the index will be passed along with the message.
You are going to:
- Create the message type.
- Compile Protobuf.
- Create a message server next to the keeper.
- Handle the message.
- Register the necessary elements in the module.
# The game creation object type and Protobuf service
These are defined together in a new file proto/alice/checkers/v1/tx.proto
that you create:
MsgCreateGame
does not mentionBoard
orTurn
as this (as mentioned) should not be under the control of the message sender.- The response is empty as there is no extra information to return. For instance, here the game index is known in advance.
option (cosmos.msg.v1.signer)
identifiescreator
as the field that will serve as the signer. At compilation time, the SDK will automagically pick up the value of this annotation to haveMsgCreateGame
implementsdk.Msg.GetSigners
.
# Compile Protobuf
Since you have defined a new message type and an associated service
, you should recompile everything:
In the new tx.pg.go
(opens new window), you can find type MsgCreateGame struct
(opens new window) and type MsgServer interface
(opens new window). Now you can use them closer to the keeper.
# New message server
Create a new file keeper/msg_server.go
and take inspiration from minimal-module-example
. It needs to:
- Check that the
Index
is not too short or too long. - Check that the
Index
is not already taken. - Create a new board with the game rules.
- If valid, put the game into storage.
- The creator and its signature are not checked. This is not necessary, as the app has validated the transaction before sending the message.
- The creator is not saved in storage. This is a design decision; you may in fact decide to keep the creator.
# Register the types in the module
Now that you have message types and server, you should register the types in the currently empty codec.go
. Inspire yourself from what you find in minimal-module-example
:
In module/module.go
, register the new service. The lines were previously commented out:
# Add the CLI commands
At this stage, your module is able to handle messages passed to it by the app. However, you are not yet able to craft such messages in the first place. When working from the command line, this crafting is handled by the CLI client. The CLI client usually tells you what messages and transactions it can create when you run minid tx --help
. Go ahead and check:
You can see that checkers
is missing from the list of available commands. Fix that by entering your desired command in module/autocli.go
. Taking inspiration from minimal-module-example
:
Note that the create
in "create index black red"
is parsed out and used as the command in the minid tx checkers create
command-line.
# Test again
Back in the minimal chain, compile to see the command that was added:
Now you should see the create
command:
Just like you did in the previous section, re-initialize and start it. You need to re-initialize because your genesis has changed once again:
Now create a game from another shell. First list alice
and bob
's addresses as created by make init
:
This returns something like:
With this information, you can send your first create game message:
This returns you the transaction hash as expected. To find what was put in storage, wait a bit and then stop the chain with CTRL-C. Now call up:
This should return something with:
This means you can get a full exported genesis.
# Up next
If you run minid query --help
, you can see that there is no checkers
in the list of commands. So although you can create games, you cannot retrieve them yet without dumping the whole storage.
Fixing this is the object of the next section.