Best Practices: Debug your Blockchain

Best Practices: Debug your Blockchain

Streamlining the Debugging Process for Blockchain Development with Ignite CLI.
Debugging is an essential part of any development process, especially when working with complex blockchain applications. The ignite chain debug command offers a robust and interactive way to debug your blockchain application. It utilizes Delve, a full-featured debugger for the Go programming language, enabling you to control execution, evaluate variables, and gain insights into various states of your application.

Simplified Debugging with Ignite CLI

  • Building a Debug Binary: The debug binary, crucial for a detailed debugging session, is automatically built by ignite chain serve. Alternatively, you can manually build it using the --debug flag with ignite chain init or ignite chain build.

  • Starting a Debugging Session: Easily initiate a debugging session using the command:

ignite chain debug

This starts your blockchain app in debug mode, opening up a terminal debugger shell.

  • Setting Breakpoints: Before resuming execution, set breakpoints where you want the debugger to pause. Use the syntax (dlv) break <filename>:<line>, for instance:
(dlv) break x/hello/client/cli/query_say_hello.go:14
  • Resuming Execution: After setting breakpoints, resume the execution with:
(dlv) continue
  • Exiting the Debugger: To exit, use the quit or exit commands.
    Advanced Debugging: Setting up a Debug Server

  • Start a Debug Server: For scenarios where a terminal client is not preferred, start a debug server using:

ignite chain debug --server

Optionally, specify a custom address with --server-address.

Utilizing Different Debugging Clients

  • Gdlv: Delve UI: This graphical frontend for Delve simplifies the debugging process and requires no additional configuration. Connect to the debug server by running:
gdlv connect 127.0.0.1:30500
  • Visual Studio Code:

For those using VS Code, set up a debugging environment by installing the Go extension and configuring the launch.json file in the .vscode folder. An example configuration is:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to Debug Server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "${workspaceFolder}",
            "port": 30500,
            "host": "127.0.0.1"
        }
    ]
}

Example: Debugging a Blockchain Query

  • Create a New Blockchain:
ignite scaffold chain hello
  • Scaffold a Query:
ignite scaffold query say-hello name --response name
  • Initialize with Debug Binary:
ignite chain init --debug
  • Launch Debugger and Set Breakpoint:
ignite chain debug
(dlv) break x/hello/keeper/query_say_hello.go:12
(dlv) continue
  • Trigger the Breakpoint:

From another terminal, call the query:

hellod query hello say-hello bob
  • Control Execution in Debugger:

Use Delve commands (next, print) to navigate and inspect variables, such as:

(dlv) print req.Name

By following these steps, you can efficiently debug your blockchain application, gaining deeper insight into its workings and swiftly resolving any issues that arise. This streamlined approach makes it easier to develop robust and error-free blockchain applications.