How to Audit Smart Contract: You Need to look at the Codes!

By Dr. Smartcontract on The Capital

Dr. Smartcontract
The Dark Side
5 min readJan 12, 2020

--

Happy New Year!

My previous post was about the DAO attach which shows how smart contract failure would happen and affect the blockchain security.

Briefly, such failure can happen as the “hole” in smart contract allows a breach of the smart contract’s precondition.

The consequence will be not only financial losses but also a crack in the faith of blockchain network resulting in “doubts”.

Yess, D O U B T.

So, how would you be able to keep the smart contract secure?

This is where you need to bring the concept of “audit” to your smart contract.

Audit is a concept mostly seen in accounting. Yes, Money.

It is simply a review of financial records checking if the money went in and out properly.

Financial audit aims to find frauds in use of corporate budget.

And, same goes for the smart contract.

Smart contract audit is a process of reviewing smart contract codes.

As the financial audit searches for the errors hidden in the monetary records, so does the smart contract audit looks deep into the written codes to detect errors.

A Good Bug Hunting.

In computer programming, such audit process is called “Code Analysis”.

Right, the smart contract audit is based on a variety of code analyses.

In case you don’t know what source code is, it is the complex-looking array of alphabets that computer guys always put on their monitor especially in a black background.

There are multiple methods of code analysis backed up by the attempts to find errors more efficiently effectively.

But the very basic first-you-need-to-know “source code analysis”.

Source code analysis is an automated way of testing source codes of a program in order to detect and correct any errors exist within the codes.

In other words, it is just like a healthcare for computer program to make sure they have no illness.

Source code analysis has two ways of doing. One is Top Down method, the other is Bottom Up method.

First, Top Down method is analysing codes by following the programming process from the very beginning. It allows you to easily understand the overall structure of a program. Since it gives you a overview, it can detect errors not only in basic functions or variables but also in programming logic and algorithm.

However, such way of analysis needs much time to be completed as it goes through all the codes including one with no errors.

It is a time-consuming sorrow investigation.

Top Down scurtinises the whole program codes.

Bottom up, on the other hand, picks a certain routine amongst a program and tracks it back to find the routine’s origin — i.e. variables and functions. It can start analysis from the previously known, suspicious codes. It is rather target-oriented, specific methods of analysis requiring less time than Top Down approach.

Yet, unlike Top Down, it cannot fully understand the overall structure of a program and therefore often misses the program’s mainstream logic.

In Bottom Up approach, you may pick one spot which is suspicious and climb up to figure out what went wrong.

Well, it can be said as a “deductive vs. inductive” approach.

There is another type of code analysis you need to know in order to understand how smart contract audit works.

It is called “bytecode analysis” often used in smart contract audit with source code analysis.

Bytecode consists of binary numbers (i.e. 0 and 1) and it is rather a computer-centered language than other machine languages.

Unlike the source code designed to be read by human, bytecode is more specific and summarised that it is opt to be read by computers.

Oh, yes. This reminds me of Matrix world.

Nah, this image makes my eyes miserable.

>

>

:(

>

>

Now much better.

Back to the topic, the reason smart contract audit uses bytecode analysis is because it is rather effective way of reviewing codes.

Source code analysis is very efficient as it provides fast feedback and allows developers mitigate risks on the go.

However, code analysis needs to be precise.

It must be able to snipe the bugs! (Image source: BTCmanager)

Because if it takes weeks and months to anlayse codes delaying the release of your program and still there are errors remain undetected, the efforts you put in the audit process might turn out to be meaningless.

Bytecode, whilst source code runs directly over computer’s operating system, runs over an application program. In other words, it runs in a virtual condition and less requires hardware resources.

It sounds cool when you know that bytecode looks free from hardware specs. However, bytecode analysis has a precondition that all the codes must be scanned and vulnerabilities must be prioritised first before mitigation.

Despite all the efforts that bytecode analysis needs, it generally shows a better result of analysis than source code analysis adding precision to the entire audit process.

By combining source code and bytecode analysis you can have a deeper review of your smart contract’s code.

Indeed, bytecode analysis and source code analysis has a complementary relationship. Bytecode analysis isn’t capable of tracking back found defects to the source code. Yet, it can find errors originated from compiling process, which source code analysis is unable to do.

Today, I’ve talked about the very basic concepts of code analysis as an introduction to smart contract audits.

In the next post, I’ll write more deeply about source code analysis which is better known as “static analysis”. So, please keep your eyes on my blog!

About me…

Powered by Scope, a high-tech blockchain smart contract audit solution, Dr. Smart contract will present all about blockchain smart contract from very basics to current security issues.

So please keep your eyes on my humble blog!

About Scope

--

--

Dr. Smartcontract
The Dark Side

Healthcare Service for Your Blockchain Smartcontract Powered by Scope and Nexus Revolution