# IBC Hello World
The Hello World example is a time-honored tradition in computer programming.
The Interblockchain Communication Protocol is an important part of the Cosmos SDK ecosystem. Understanding how to create and send packets across blockchain will help you navigate between blockchains with the Cosmos SDK.
You will learn how to
- Use Interblockchain Communication Protocol (IBC) to create and send packets between blockchains.
- Navigate between blockchains using the Cosmos SDK and the Starport Relayer.
- Create a basic blog post and save the post on another blockchain.
Follow this tutorial on YouTube
# What is Cosmos SDK and IBC?
The Cosmos SDK is a framework to create a blockchain app. The Cosmos SDK allows developers to easily create custom blockchains that can natively interoperate with other blockchains. The IBC module in the Cosmos SDK is the standard for the interaction between two blockchains. The IBC module defines how packets and messages are constructed to be interpreted by the sending and the receiving blockchain. The Cosmos IBC relayer package lets you can connect between sets of IBC-enabled chains. This tutorial teaches you how to create two blockchains and then start and use the relayer with Starport to connect two blockchains. This tutorial covers essentials like modules, IBC packets, relayer, and the lifecycle of packets routed through IBC.
This tutorial uses Starport (opens new window) v0.15.0. The Starport tool is the easiest way to build a blockchain.
/usr/local/bin, run the following command:
# Create a Blockchain App
Create a blockchain app with a blog module to write posts on other blockchains that contain the Hello World message (or rather Hello Mars, Hello Cosmos and Hello Earth) for an IBC module.
For this simple example, create an app that contains a blog module that has a post transaction with title and text.
After you define the logic, run two blockchains that have this module installed.
- The chains can send posts between each other using IBC.
- On the sending chain, save the
After the transaction is acknowledged by the receiving chain, you know that the post is saved on both blockchains.
- The sending chain has the additional data
- Sent posts that are acknowledged and timed out contain the title and the target chain of the post. These identifiers are visible on the parameter
chain. The following chart shows the lifecycle of a packet that travels through IBC.
# Initialize the Blockchain
Scaffold a new blockchain named
A new directory named planet is created. This directory contains a working blockchain app.
Scaffold a module inside your blockchain named
blog with IBC capabilities.
The blog module contains the logic for creating blog posts and routing them through IBC to the second blockchain.
A new directory with the code for an IBC module is created in
--ibc flag includes all the logic for the scaffolded IBC module.
- Create the transaction types for the module.
starport typecommand to scaffold the boilerplate code for creating, reading, updating and deleting (CRUD) objects on your blockchain. The module has the logic for:
- Creating Blog posts
- Processing acknowledgments for sent posts
- Managing post time outs
starport type commands create CRUD code for managing post, sent posts and timed-out posts types. The scaffolded code includes proto files for defining data structures, messages, messages handlers, keepers for modifying the state and CLI commands.
To manage multiple modules within your Starport app, use the
--module flag to define which module the new transaction type is added to.
The first argument of the starport type command specifies the name of the type being created, the following arguments define fields associated with the type.
--module is an optional flag that allows to specify in which module the type will be scaffolded (without this flag the type is scaffolded in the module that matches the name of the repo).
Scaffold a sendable and interpretable IBC packet that contains the title and the content of the blog post.
The title and content will be stored on the target chain, while the
postID will be acknowledged on the sending chain.
starport type command creates a new transaction type, the
starport packet command creates the logic for an IBC packet that can be sent to another blockchain.
Notice the fields in the
ibcPost packet match the ones in the
post type created earlier.
--ackflag defines which identifier is returned to the sending blockchain.
--moduleflag specifies that the packet should be created in a particular IBC module.
starport packet command also scaffolds a CLI command that is capable of sending an IBC packet:
# Modify the Source Code
After creating the types and transactions, you must manually insert the logic to manage updates in the data tables. Modify the source code to save the data as specified earlier in this tutorial.
# Add Creator to the Blog Post Packet
Start with the proto file that defines the structure of the IBC packet.
To identify the creator of the post in the receiving blockchain, add the creator field inside the packet.
We did not specify this field directly in the command because it would automatically become a parameter in
SendIbcPost CLI command.
To make sure the receiving chain has content on the creator of a blog post, add this value to the IBC
The content of the
sender of the message is automatically included in
SendIbcPost message. The sender is verified as the signer of the message, so you can add the
msg.Sender as the creator to the new packet before it is sent over IBC.
# Receive the Post
The methods for primary transaction logic are in the
planet/x/blog/keeper/ibcPost.go file. Use these methods to manage IBC packets:
TransmitIbcPostPacketis called manually to send the packet over IBC. This method also defines the logic before the packet is sent over IBC to another blockchain app.
OnRecvIbcPostPackethook is automatically called when a packet is received on the chain. This method defines the packet reception logic.
OnAcknowledgementIbcPostPackethook is called when a sent packet is acknowledged on the source chain. This method defines the logic when the packet has been received.
OnTimeoutIbcPostPackethook is called when a sent packet times out. This method defines the logic when the packet is not received on the target chain
You must modify the source code to add the logic inside those functions so that the data tables are modified accordingly.
On reception of the message, we create a new post with the title and the content on the receiving chain.
To identify the blockchain app that a message is originating from and who created the message, use an identifier in the following format:
Finally, the AppendPost function that is generated by Starport returns the ID of the new appended post. You can return this value to the source chain through acknowledgment.
Append the type instance as
PostID on receiving the packet:
- the context
ctxis an immutable data structure (opens new window) that has header data from the transaction. See how the context is initiated (opens new window)
- the identifier format that you defined earlier
titleis the Title of the blog post
contentis the Content of the blog post
ibcPost.go file, make sure to import
"errors", then modify
OnRecvIbcPostPacket with the following code:
# Receive the Post Acknowledgement
On the sending blockchain, store a
sentPost so you know that the post has been received on the target chain.
Store the title and the target to identify the post.
When a packet is scaffolded, the default type for the received acknowledgment data is a type that identifies if the packet treatment has failed. The
Acknowledgement_Error type is set if
OnRecvIbcPostPacket returns an error from the packet.
# Store Information about the Timed-out Packet
Store posts that have not been received by target chains in
timedoutPost posts. This logic follows the same format as
This is the basic
blog module setup.
The blockchain is now ready!
Spin up the blockchain and send a blog post from one blockchain app to the other.
Multiple terminal windows are required to complete these next steps.
# Test the IBC modules
To test the IBC module, start two blockchain networks on the same machine. Both blockchains use the same source code. Each blockchain has a unique chain ID.
One blockchain is named
earth and the other blockchain is named
The following files are required in the project directory:
Open a terminal window and run the following command to start the "earth" blockchain:
Open another terminal window and run the following command to start the "mars" blockchain:
# Start the Relayer
First, configure the relayer. Use the Starport
configure command with the
The output looks like:
Then, start the relayer process in a separate terminal window:
# Send packets
You can now send packets and verify the received posts:
To verify that the post has been received on Mars:
The packet has been received:
To check if the packet has been acknowledged on Earth:
To test timeout, set the timeout time of a packet to 1 nanosecond, verify that the packet is timed out, and check the timed-out posts:
Check the timed-out posts:
You can also send a post from Mars:
List post on Earth:
🎉 Congratulations. 🎉
By completing this tutorial, you’ve learned to build an IBC module for the Cosmos SDK, build your own blockchain app, and use the Interblockchain Communication Protocol (IBC). Here’s what you accomplished in this tutorial:
- Built a Hello World blockchain app as an IBC module
- Used the relayer to connect two blockchains with each other
- Transferred IBC packets from one blockchain to another