# The Expired Game Elements
Make sure you have all you need before proceeding:
- You understand the concepts of ABCI.
- Have Go installed.
- The checkers blockchain codebase with the elements necessary for forfeit. You can get there by following the previous steps or checking out the relevant version (opens new window).
In the previous section you prepared expiration of games:
- A First-In-First-Out (FIFO) that always has old games at its head and freshly updated games at its tail.
- A deadline field to guide the expiration.
- A winner field to further assist with forfeiting.
# New information
An expired game will expire in two different cases:
- It was never really played on so it is removed quietly.
- It was played on, making it a proper game, and forfeit is the outcome because a player failed to play in time.
In the latter case, you want to emit a new event, which differentiates forfeiting a game from a win involving a move. Therefore you define new error constants:
# Putting callbacks in place
When you use Starport to scaffold your module, it creates the x/checkers/module.go
(opens new window) file with a lot of functions to accommodate your application. In particular, the function that may be called on your module on EndBlock
is named EndBlock
:
Starport left it empty. It is here that you add what you need to see done, right before the block gets sealed. Create a brand new file named x/checkers/keeper/end_block_server_game.go
to encapsulate the knowledge about game expiry. Leave your function empty for now:
In x/checkers/module.go
you can update EndBlock
with:
With this you ensure that if your module's EndBlock
function is called, the expired games will be handled. For the whole application call your module you have to instruct it to do so. This takes place in app/app.go
, where the application is initialized with the proper order to call the EndBlock
functions in different modules. Add yours at the end:
Your ForfeitExpiredGames
function will now be called at the end of each block.
# Expire games handler
With the callbacks in place it is time to code the expiration properly. In ForfeitExpiredGames
, it is simply a matter of looping through the FIFO, starting from the head, and handling games that are expired. You can stop at the first active game as all those that come after are also active, thanks to the careful updating of the FIFO.
Prepare useful information:
Initialize the parameters before entering the loop:
Enter the loop:
Start with the loop breaking condition:
Fetch the expired game candidate and its deadline:
Test for expiration:
If the game has expired, remove it from the FIFO:
Then check whether the game is worth keeping. If it is, set the winner as the opponent of the player whose turn it is and save:
Emit the relevant event:
Move along the FIFO for the next run of the loop:
After the loop has ended, do not forget to save the latest FIFO state:
For an explanation as to why this setup is resistant to an attack from an unbounded number of expired games see the section on the game's FIFO.
# Next up
The next section introduces token wagers.