Debugging is a crucial exercise when authoring Solidity smart contracts. Debugging denotes finding issues, bugs, and removing them by changing code. It’s very difficult to debug a sensible contract if there’s adequate support from tools and utilities. Generally, debugging involves executing each line of code step by step, finding the present state of temporary, local, and global variables, and walking through each instruction while executing contracts. There are the subsequent ways to debug Solidity contracts:
- Using the Remix editor
- Block explorer
- The Remix editor
We used the Remix editor to write down Solidity contracts. Though, we’ve not used the debugging utility presented in Remix. The Remix debugger provides us with help to detect the runtime behavior of contract execution and identify issues. The debugger works in Solidity and therefore the resultant contract bytecode. With the debugger, the execution is often paused to look at contract code, state variables, local variables, and stack variables, and consider the EVM instructions generated from contract code.
The contract features a single state variable and performance. The function loops over the provided input till it reaches the worth of the counter and returns a cumulative sum to the caller.
The following instructions show the bytecode for function execution:
Solidity Locals: This instruction illustrates the incoming parameter, data type, and its value. Solidity State: This instruction shows the state variables, their data type, and current value.
Step detail: This is frequently important for debugging gas usage, consumption, and remaining gas.
Call Stack: This instruction displays the interim variables needed by function code. Memory: This instruction demonstrates the local variables used within the function.
Call Data: This function shows the particular payload the client sends to the contract. The primary four bytes ask the function identifier and therefore the rest contain 32 bytes for every incoming parameter.
An important aspect of debugging is to prevent the execution at each line of code of interest. Breakpoints help do that. Clicking on any line beside the road number helps in fixing a breakpoint. Clicking again removes the breakpoint. During the execution of a function, when it hits this line; the execution is halted, and therefore the values and execution are often verified from the Debugger tab. Using Remix, it’s also possible to perform Step over back, Step back, Step into, Step over forward, Jump to the previous breakpoint, Jump out, and Jump to the subsequent breakpoint. It also provides the power to look at information employing a block number or transaction hash a few particular blocks or transactions.
Events are often trapped and may help provide relevant information about the present execution. Contracts should declare events. Functions should invoke these events at appropriate locations with information that gives enough context to whoever is reading these events.
Using a Block Explorer
A Block Explorer is an Ethereum browser. It provides reports and knowledge about current blocks and transactions in its network. It is an excellent spot to find out more about existing and past data.