0% found this document useful (0 votes)
54 views7 pages

Tutorial A7: Debugging A Smart Contract

This document provides instructions for debugging a smart contract using the VS Code debugger. It describes how to start a debug session, set breakpoints to pause execution, step through the code, and add watches to monitor variable values. However, the tutorial notes that smart contract debugging is not currently supported in this version of the IBM Blockchain extension. It directs the user to skip to the next tutorial.

Uploaded by

Vicente DB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

Tutorial A7: Debugging A Smart Contract

This document provides instructions for debugging a smart contract using the VS Code debugger. It describes how to start a debug session, set breakpoints to pause execution, step through the code, and add watches to monitor variable values. However, the tutorial notes that smart contract debugging is not currently supported in this version of the IBM Blockchain extension. It directs the user to skip to the next tutorial.

Uploaded by

Vicente DB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IBM Blockchain Platform

Tutorial A7: Debugging a smart contract

Estimated time: 15 minutes

Smart Contract Debugging is not supported in this version of the extension yet - Skip to next
tutorial.

In the previous tutorial, we upgraded our smart contract by adding a new transaction method to our smart
contract called 'queryAllAssets'. In this tutorial we will:

Understand the tools to debug smart contracts


Use the VS Code debugger to step through our newly added transaction to see how it works
Use 'watches' to monitor variables in smart contracts

This tutorial is not intended to demonstrate all the features of the VS Code debugger. For more information,
see the VS Code debugger documentation.

In order to successfully complete this tutorial, you must have first completed tutorial A6: Upgrading a smart
contract in the active workspace.

Smart Contract Debugging is not supported in this Beta - Skip to next tutorial.

A7.1: Expand the first section below to get started.

Start a debug session

The VS Code debugger contains four views in its own side bar: Variables, Watch, Call Stack and Breakpoints.

A7.2: Click the Debugger icon in the VS Code activity bar to show the Debugger side bar.
At the top of the side bar is a drop-down list that shows the available debug configurations. The IBM
Blockchain Platform has added a configuration to allow us to debug our demo-contract smart contract
package.

A7.3: Click the drop-down list at the top of the Debugger side bar and ensure that 'Debug Smart
Contract (demo-contract)' is selected.

We will now start a session to debug our demo-contract smart contract.

A7.4: Click the green Start arrow to the left of the drop-down list.

We now need to tell VS Code the location of the Fabric Environment in which we are going to debug our
smart contract.

A7.5: In the command palette that appears, click '1 Org Local Fabric'.
After a brief pause, the bar at the bottom of VS Code will change color to indicate that a debug session has
started for the 1 Org Local Fabric environment.

If necessary, VS Code will prompt us to upgrade the smart contract at this time. If you are prompted to do this,
just accept any defaults and let the upgrade complete.

A7.6: Expand the next section of the tutorial to continue.

Step through a smart contract transaction method

We are now going to run our new queryAllAssets transaction method in the debugger to see how it works. We
will set a breakpoint in the smart contract to allow us to pause and step through the code.

To do this, we first need to ensure that our smart contract has focus in the editor.

A7.7: Click on the my-asset-contract.ts tab in the editor.

If the file is not loaded in the editor, you will need to use the Explorer side bar to load the demo-contract -> src
-> my-asset-contract.ts file, then switch back to the Debugger side bar.

We will now set a breakpoint.

A7.8: Scroll to the first statement of the queryAllAssets method and click the mouse just to the left of
the line number; a red dot will appear.
As with all debuggers, this causes execution to pause whenever this statement is reached. You can see all
breakpoints listed in the Breakpoints view.

We will now evaluate a transaction to debug this method.

A7.9: Click the blue IBM Blockchain Platform icon in the Debug bar at the top of the screen.

A7.10: Click 'Evaluate Transaction'.

A7.11: Click 'MyAssetContract - queryAllAssets'.

As you will recall, there are no arguments or transient data to supply to this transaction.

A7.12: Press Enter to specify no arguments on the transaction.

A7.13: Press Enter to specify no transient data.


The smart contract will now run and pause at the breakpoint we previously set.

A7.14: Click 'Step Over' in the Debug bar multiple times to progress through the transaction's
implementation.

As you step through the smart contract, note that the Variables and Call Stack views change depending on
the current scope.

You should also see that the while loop is called twice before finishing; this is because, as you may recall,
there are two assets in the world state ('002' and '003').

Transaction timeouts?
You might see a transaction timeout error while the debugger is paused. This is OK, and you can use
the debugger to explore how errors are handled. Click 'Continue' in the Debug bar and invoke the
transaction again if you wish.

A7.15: When you have stepped through to the end of the transaction, click 'Continue' to unpause
execution.

A7.16: Expand the next section of the tutorial to continue.

Add a variable watch

It is also possible to put a watch on a smart contract variable or expression to check if and when certain
conditions hold. This can be helpful if your smart contract is producing unexpected results.

A7.17: Select the first occurrence of allResults in the queryAllAssets method, where this variable is
initialized.

A7.18: Right-click over the selected text and select 'Debug: Add to Watch'.
You will now see 'allResults' appear in the Watch view.

A7.19: Click the blue IBM Blockchain Platform Debug icon in the debug bar a second time, and
repeat the earlier steps to evaluate the queryAllAssets transaction again.

As you step through the debugger, look particularly at the Watch view to see how the allResults array is built
up as the transaction progresses. Note that watch variable is shown as a tree, which you can expand for
more details.

Try setting additional breakpoints or watches and run them through the debugger.

A7.20: When you are finished, click the Stop button in the debug bar to stop the debug session.

Hot fixes
It is not possible to make changes to smart contracts while debugging. You must stop the debugger
before your smart contract can be upgraded.

Summary
In this tutorial we have used the VS Code debugger to step through a smart contract that is deployed to our
Fabric environment.

In the next tutorial we will see how we can generate functional tests for our smart contract.

→ Next: A8: Testing a smart contract

You might also like