# Help Find a Correct Move
Make sure you have everything you need before proceeding:
- You understand the concepts of queries and Protobuf.
- You have Go installed.
- You have the checkers blockchain codebase up to gas metering. If not, follow the previous steps or check out the relevant version (opens new window).
In this section, you will:
- Improve usability with queries.
- Create a battery of unit and integration tests.
A player sends a MsgPlayMove
when making a move. This message can succeed or fail for several reasons. One error situation is when the message represents an invalid move. A GUI is the first place where a bad move can be caught, but it is still possible that a GUI wrongly enforces the rules.
Since sending transactions includes costs, how do you assist participants in making sure they at least do not make a wrong move?
Players would appreciate being able to confirm that a move is valid before burning gas. To add this functionality, you need to create a way for the player to call the Move
(opens new window) function without changing the game's state. Use a query because they are evaluated in memory and do not commit anything permanently to storage.
# Some initial thoughts
When it comes to finding a correct move, ask:
- What structure will facilitate this check?
- Who do you let make such checks?
- What acceptable limitations do you have for this?
- Are there new errors to report back?
- What event should you emit?
# Code needs
- What Ignite CLI commands, if any, will assist you?
- How do you adjust what Ignite CLI created for you?
- Where do you make your changes?
- How would you unit-test these new elements?
- How would you use Ignite CLI to locally run a one-node blockchain and interact with it via the CLI to see what you get?
# New information
To run a query to check the validity of a move you need to pass:
- The game ID: call the field
gameIndex
. - The
player
color, as queries do not have a signer. - The origin board position:
fromX
andfromY
. - The target board position:
toX
andtoY
.
The information to be returned is:
- A boolean for whether the move is valid, called
possible
. - A text which explains why the move is not valid, called
reason
.
As with other data structures, you can create the query message object with Ignite CLI:
Among other files, you should now have this:
Ignite CLI has created the following boilerplate for you:
- The Protobuf gRPC interface function (opens new window) to submit your new
QueryCanPlayMoveRequest
and its default implementation. - The routing of this new query (opens new window) in the query facilities.
- An empty function (opens new window) ready to implement the action.
# Query handling
Now you need to implement the answer to the player's query in grpc_query_can_play_move.go
. Differentiate between two types of errors:
- Errors relating to the move, returning a reason.
- Errors indicating that testing the move is impossible, returning an error.
The game needs to be fetched. If it does not exist at all, you can return an error message because you did not test the move:
Has the game already been won?
Is the
player
given actually one of the game players?Is it the player's turn?
Attempt the move and report back:
If all went well:
Quite straightforward.
# Unit tests
A query is evaluated in memory, while using the current state in a read-only mode. Thanks to this, you can take some liberties with the current state before running a test, as long as reading the state works as intended. For example, you can pretend that the game has been progressed through a number of moves even though you have only just planted the board in that state in the keeper. For this reason, you can easily test the new method with unit tests, even though you painstakingly prepared integration tests.
Take inspiration from the other tests on queries (opens new window), which create an array of cases to test in a loop. Running a battery of test cases makes it easier to insert new cases and surface any unintended impact. Create a new grpc_query_can_play_move_test.go
file where you:
Declare a
struct
that describes a test case:Create the common OK response, so as to reuse it:
Prepare your array of cases:
In the array add your first test case, one that returns an OK response:
Add other test cases (opens new window). Examples include a missing request:
Or a player playing out of turn:
With the test cases defined, add a single test function that runs all the cases:
All test cases are run within a single unit test. To avoid having one case bleed into the next, the keeper is created afresh inside the loop.
# Integration tests
You can also add integration tests on top of your unit tests. Put them alongside your other integration tests. Create grpc_query_can_play_move_test.go
.
Test if it is possible to play on the first game that is created in the system:
With these, your query handling function should be covered.
# Interact via the CLI
Set the game expiry to 5 minutes and start ignite chain serve
. Remember that the CLI can always inform you about available commands:
Which prints:
What can checkersd
tell you about the command:
Which prints:
You can test this query at any point in a game's life.
When there is no such game:
Trying this on a game that does not exist returns:
Confirm this was an error from the point of view of the executable:
This prints:
There is room to improve the error message, but it is important that you got an error, as expected.
When you ask for a bad player color:
If the player tries to play the wrong color on a game that exists, it returns:
This is a proper message response, and a reason elaborating on the message.
When you ask for a player out of turn:
If the opponent tries to play out of turn, it returns:
When you ask for a piece that is not that of the player:
If black tries to play a red piece, it returns:
When it is correct:
If black tests a correct move, it returns:
When the player must capture:
If black fails to capture a mandatory red piece, it returns:
The reason given is understandable, but it does not clarify why the move is invalid. There is room to improve this message.
After the game has been forfeited:
If black tries to capture a red piece on a running game, it returns:
Wait five minutes for the forfeit:
Now it returns:
These query results satisfy our expectations.
To summarize, this section has explored:
- How application usability can be improved with queries, such as by avoiding the cost of sending technically valid transactions which will nevertheless inevitably be rejected due to the application's current state.
- How queries allow the user to evaluate the application state in read-only mode, without committing anything permanently to storage, with the result that a planned transaction can be judged as acceptable or not before burning gas.
- How effective query construction will allow the application to signal not just that a planned transaction will fail but also the reason it will fail, improving the user's knowledge base for future actions.
- How to create a query object with Ignite CLI; implement appropriate answers to a player's query; perform integration tests which extrapolate on the application's actual current state; and interact via the CLI to test the effectiveness of the query object.