Debugging Chainlink With Visual Studio Code
This guide is geared towards those who may be new developers, or maybe just new to Golang. Debugging is an essential part of programming, and can greatly enhance your understanding of the system you are working on. In this guide, you will be shown how to set up Visual Studio Code for use with the Golang language. VS Code is a lightweight graphical code editor that allows for extensions (plugins) to be installed in order to add features, similar to Atom and Sublime Text.
Requirements
- A complete development environment for Chainlink
- Delve
- Visual Studio Code
Set up Visual Studio Code
Install the Go Extension
Launch VS Code Quick Open (Ctrl+P) and run: ext install Go. This will bring you to the extensions marketplace where you can install the Go extension. After installing, click the Reload button and the interface will refresh briefly.
To open the Chainlink project, type Ctrl+K Ctrl+O to bring up the Open Folder dialog. Navigate to your $GOPATH/src/github.com/smartcontractkit/chainlink directory and open it.
Select the main.go file in the root directory and notice a dialog appears at the bottom-right of the screen. Click the Install All button. This will install the Go Tools in VS Code to make reading the code and development easier.
Set Launch Parameters
Type Ctrl+Shift+D to bring up the Debug controls and click the Gear icon near the top-left to configure a launch.json. To run the Chainlink node against DevNet, you can use the following options by copying and pasting the text below over the entire file (you’ll want to replace the LINK_CONTRACT_ADDRESS with the address of your deployed LinkToken contract.
"version": "0.2.0", "configurations": [ { "name": "DevNet", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}", "env": { "LOG_LEVEL": "debug", "ROOT": "./internal/devnet", "ETH_URL": "ws://localhost:18546", "ETH_CHAIN_ID": 17, "TX_MIN_CONFIRMATIONS": "0", "TASK_MIN_CONFIRMATIONS": "0", "USERNAME": "chainlink", "PASSWORD": "twochains", "LINK_CONTRACT_ADDRESS": "0x83e41643c2598c4d6a78d68cf506bf209a22ba5d" }, "args": ["node", "-p", "T.tLHkcmwePT/p,]sYuntjwHKAsrhm#4eRs4LuKHwvHejWYAC2JP4M8HimwgmbaZ"], "showLog": true } ] }
After saving, you’ll notice a new folder, .vscode was created at the root of the project. You’ll want to make sure to add that to your global gitignore so that it is not included when you create pull requests.
Run in Debug Mode
First, you’ll want to run the DevNet image in its own window.
$ cd $GOPATH/src/github.com/smartcontractkit/chainlink $ ./internal/bin/devnet
Then, back in Visual Studio Code, you’ll start debugging by hitting F5 or clicking the “play” icon in the debug window at the top-left. If successful, you should see the same output on the “Debug Console” tab at the bottom as you would if you were to run the Chainlink node in a terminal window.
You’ll also see a debug file created at the root of the project. You’ll want to add that to your global .gitignore as well.
Breakpoints
In order to step through the code, you’ll want to set a breakpoint where you want to begin stepping. To do this, you’ll first stop debugging the running program by clicking the “stop” button at the top of the screen. Then, find a line that you want the program to pause execution. A good starting point to understand the flow of a run is in the services/job_runner.go file, with the BuildRun function. You can click anywhere on that line and hit the F9 button on your keyboard, or click the red dot next to the line number to add a breakpoint. Repeat those same steps to remove a breakpoint.
Now, start the node again, and add a job if there isn’t one already present. You can use this JobSpec to start with:
$ chainlink c '{"initiators":[{"type":"web"}],"tasks":[{"type":"HttpGet","url":"https://min-api.cryptocompare.com/data/price?fsym=LINK&tsyms=USD"},{"type":"JsonParse","path":["USD"]},{"type":"noop"}]}'
Then take note of the value for the “ID” field returned on the top table. You can start a run (replacing $JOBID with your actual job ID) in the TERMINAL tab by running:
$ chainlink r $JOBID
VS Code should pause at the BuildRun function. You can step through the code by hitting the F10 button on your keyboard, and F11 will allow you to “step into” functions. Notice the panels on the Debug tab, Variables and Call Stack, have been populated with information about the running program. Simply type the variable name at the > prompt in the Debug Console and hit Enter to get more information about the variable, but you’ll want to click the carrot to expand the properties. You can also get more information from variables by querying them in the Debug Console tab.
For example:
> job.Initiators[0] # hit enter <github.com/smartcontractkit/chainlink/store/models.Initiator> ID:0 JobID:"1f0daf1b11664da89b9675dc05a4b993" Type:"web" Schedule:"" Time:<github.com/smartcontractkit/chainlink/store/models.Time> Ran:false Address:<github.com/smartcontractkit/chainlink/vendor/github.com/ethereum/go-ethereum/common.Address>
When you want the program to resume like normal, hit the F5 button.
Hopefully this guide has been useful to you and will help you with debugging and adding features to Chainlink! If you have any questions, feel free to bring them up on our Slack (email [email protected] for access) or Gitter.