Hence, verifying smart contract behaviors and solving security issues are extremelycrucial and challenging when blockchain technologies evolve with much diversity... 8 1.7.1 The pilot st
Trang 1ĐẠI HỌC QUỐC GIA TP HCM
TRƯỜNG ĐẠI HỌC BÁCH KHOA
Trang 2CÔNG TRÌNH ĐƯỢC HOÀN THÀNH TẠI TRƯỜNG ĐẠI HỌC BÁCH KHOA – ĐHQG – HCM
Cán bộ hướng dẫn khoa học: PGS TS Quản Thành Thơ
Cán bộ chấm nhận xét 1: PGS TS Huỳnh Tường Nguyên
2 Thư ký: TS Nguyễn Lê Duy Lai
3 Phản biện 1: PGS TS Huỳnh Tường Nguyên
4 Phản biện 2: TS Trần Thanh Tùng
5 Ủy viên: TS Nguyễn Văn Sinh
Xác nhận của Chủ tịch Hội đồng đánh giá LV và Trưởng Khoa quản lý chuyên ngành sau khi luận văn đã được sửa chữa (nếu có)
Trang 3ĐẠI HỌC QUỐC GIA TP.HCM
TRƯỜNG ĐẠI HỌC BÁCH KHOA
CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM
Độc lập - Tự do - Hạnh phúc NHIỆM VỤ LUẬN VĂN THẠC SĨ
I TÊN ĐỀ TÀI: Xác thực hợp đồng thông minh bằng kỹ thuật phân tích tĩnh
J NHIỆM VỤ VÀ NỘI DUNG: Nghiên cứu các phương pháp phân tích tĩnh và cấu
trúc của hợp đồng thông minh để đưa ra các giải pháp xác thực đúng đắn Xây
dựng một ứng dụng web đi kèm để trực quan hóa đề tài, đồng thời kiểm tra tính
chính xác của giải pháp đề xuất đối với các ứng dụng thực tế
III NGÀY GIAO NHIỆM VỤ: 19/08/2019
IV NGÀY HOÀN THÀNH NHIỆM VỤ: 07/06/2020
V CÁN BỘ HƯỚNG DẪN: PGS.TS Quản Thành Thơ
Trang 4I would like to extend thanks to the many people, who so generously contributed tothe work presented in this thesis report
Special mention goes to my supervisor, Prof Quan Thanh Tho My master course is
an amazing experience, he always help me when I am in troubles Moreover, I also
thanks to Mr Nguyen Huu Hoang and other faculties wholeheartedly I am very
appreciated for their support, not only about the tremendous academic guide but alsofor giving me so many wonderful opportunities and important advises
Profound gratitude goes to all members in the group of Professor Tho for their hearted support
whole-Ho Chi Minh City, 03 August 2020
Trang 5I, Bang Ngoc Bao Tam, declare that my thesis, "Verification of Ethereum Smart tracts: A Model Checking Approach" and the work presented in it are my own Iconfirm that:
Con-• This work was done wholly or mainly while in candidature for a master byresearch degree at this University
• Where any part of this thesis has previously been submitted for a degree orany other qualification at this University or any other institution, this has beenclearly stated
• Where I have consulted the published work of others, this is always clearlyattributed
• Where I have quoted from the work of others, the source is always given Withthe exception of such quotations, this thesis is entirely my own work
• I have acknowledged all of the main sources of help
Ho Chi Minh City, 03 August 2020
Trang 6Abstract (English)
This decade has already witnessed an extraordinary evolution in the technology andcomputing ecosystem Technology innovation and its impact are already runningvery high From Internet of Things to Artificial Intelligence to Blockchain Each ofthem has a disruptive force within multiple industries and Blockchain is termed asone of the most disruptive technologies of today [30] So much so, Blockchain has thepotential to change almost every industry today and its working [21].The applicability
of Blockchain because of its advantages and pervasiveness has already picked upstream and seems like it will continue to long time to come Blockchain is not a newtechnology however it has gained super momentum in the last couple of years "It
is a big leap forward in terms of things about decentralized and distributed tions It is about thinking of current architectural landscape and strategizing to movetowards immutable distributed databases" [21] The advantages and many helpingorganizations reach out to their stakeholders without requiring any central authorityand intermediaries
applica-While the first generation of blockchain was designed only to solve cryptocurrenciesproblems, Ethereum, one of the most popular current systems, focuses on imple-menting decentralized computing approaches [5] One new prominent of thesereliable platforms is to enable smart contract, which can automatically execute onthe blockchain and enforce by the consensus protocol These properties help smartcontracts allow the performance of credible transactions without third parties Thus,smart contracts are likely to apply in a wide range of fields including ownership ofcopyrights, financial instruments, document existence and asset tracking for the IoT.However, only the correctness of executions is not sufficient to keep smart contractssecure In fact, adversaries may take advantage of undocumented methods and ex-ploit potential bugs as well as vulnerabilities in the contracts, which can cause harm
to users More recently, $31M worth of Ether was stolen due to a critical security bug
in a digital wallet contract
Hence, verifying smart contract behaviors and solving security issues are extremelycrucial and challenging when blockchain technologies evolve with much diversity
Trang 7Keywords:Ethereum smart contracts, Semantic structure analysis, Symbolic tion, Control-flow graph.
Trang 8execu-Abstract (Vietnamese)
Trong thập kỷ vừa qua đã đã đánh dấu một cột mốc phát triển vượt bật của cácngành, nghề, lĩnh vực về công nghệ, và đặc biệt là các hệ thống máy tính Từ nềntảng Kết nối vạn vật (IoT), Trí tuệ nhân tạo cho đến Blockchain, đã góp phần đổimới nền công nghệ hiện tại, tạo tiền đề để thúc đẩy công cuộc cách mạng côngnghiệp 4.0 Tất cả các nền tảng công nghệ kể trên đều đạt được những đột phánhất định trong nhiều lĩnh vực, tuy nhiên, Blockchain vẫn được đánh là mộttrong những công nghệ đột phá nhất hiện nay [1] Blockchain có đủ tiềm năng
để thay đổi sự vận hành, cách thức giao dịch, lưu trữ dữ liệu, của hầu hết mọingành công nghiệp ngày nay [2] Bởi vì những lợi thế cũng như sự phổ biến của
nó mà khả năng ứng dụng của Blockchain dường như sẽ còn tiếp tục phát triểntrong nhiều lĩnh vực hơn nữa Blockchain tuy không phải là một công nghệ mới,nhưng nó đã gặt hái được nhiều kết quả, thêm nhiều động lực để nâng tầm vịthế trong vài năm qua “Đó là một bước tiến lớn trong mọi mặt về các ứng dụngphân tán và phi tập trung Là sự thay đổi trong suy nghĩ về tổng thể kiến trúchiện tại và chiến lược để hướng tới cơ sở dữ liệu phân tán bất biến” [2] Một ví dụ
cụ thể để minh họa cho lợi ích của các ứng dụng phân tán là các tổ chức hoặc
cá nhân có thể dễ dàng tiếp cận với các bên liên quan của họ mà không cầnbất kỷ yếu tố trung gian nào và vẫn đáp ứng được các yêu cầu, quyền lợi đôi bên
Trong khi các thế hệ blockchain đầu tiên được thiết kế chỉ để giải quyết các vấn
đề về tiền điện tử thì Ethereum, một trong những nền tảng phổ biến nhất trongthời gian trở lại đây, được tạo ra để tiếp cận các phương pháp giải quyết chocác hệ thống, ứng dụng phi tập trung [3] Một điểm quan trọng đáng lưu ý củacác nền tảng xác thực này là nó cho phép thực hiện các giao dịch thông quahợp đồng thông minh Toàn bộ hoạt động của hợp đồng thông minh được thựchiện một cách tự động và không có sự can thiệp từ bên ngoài, hay thông quamột bên thứ ba trung gian mà chỉ dựa vào các ràng buộc được định nghĩa sẵnbên trong hợp đồng Do đó, hợp đồng thông minh có khả năng áp dụng trongrất nhiều lĩnh vực bao gồm xác thực bản quyền, công việc về tài chính, truyvết dữ liệu, Tuy nhiên, chỉ dựa vào tính chính xác trong quá trình thực thi
là không đủ để giữ an toàn cho các hợp đồng thông minh Trong thực tế, các
Trang 9kẻ xấu có thể lợi dụng các phương pháp chưa được biết đến và khai thác cáclỗi tiềm ẩn cũng như các lỗ hổng trong hợp đồng Điều đó có thể gây ra hậuquả to lớn cho người dùng Cũng trong khoảng thời gian gần đây, một số lượnglớn Ether (trị giá khoảng 31 triệu đô la) đã bị đánh cắp do một lỗi nghiêm trọngliên quan đến bảo mật vấn đề bảo mật của ví điện tử
Qua đó có thể thấy rằng, việc xác thực các hành vi hợp đồng thông minh vàgiải quyết các vấn đề bảo mật là vô cùng cần thiết, những cũng đặt ra rất nhiềukhó khăn, thách thức khi các công nghệ blockchain ngày càng phát triển vớimột số lượng lớn các hệ sinh thái đi kèm Tuy nhiên, các giao thức xác thựchiện tại dường như đưa ra quá nhiều chi tiết kỹ thuật Điều đó có thể khiển chomột phần lớn người dùng chưa có nhiều kiến thức về mảng này sẽ không thểhiểu hết được Để khắc phục nhược điểm này, chúng tôi thiết kế một mô hìnhvới ý tưởng chính là thực hiện phân tích tĩnh trên đồ thị dòng điều khiển Bàiluận này đề xuất một hướng tiếp cận hoàn toàn mới trong việc xác thực hợpđồng thông minh Áp dụng mô hình này sẽ đem lại nhiều lợi ích nhiều hơn chongười dùng, kể cả những người không có kiến thức về công nghệ thông tin
Trang 101.1 Overview 1
1.2 Smart Contracts 2
1.3 Verification of Smart Contracts 2
1.4 Motivating example 7
1.5 Problem Statement 7
1.6 Scope of this research 7
1.7 Research Outcomes 8
1.7.1 The pilot study I: Verification of Ethereum Smart Contracts: An Model Checking Approach 8
1.7.2 The pilot study II: An Intelligent Chatbot for Automatic Verifica-tion of Ethereum Smart Contracts 8
1.8 Report Outline 8
2 LITERATURE SURVEY 9 3 RESEARCH BACKGROUND 10 3.1 Blockchain 10
3.1.1 Characteristics 11
3.1.2 How does a Blockchain work? 13
3.2 Is Blockchain Secure ? 15
Trang 113.3 Consensus Protocol 15
3.3.1 Proof of Work 15
3.3.2 Proof of Stake 16
3.4 What is Blockchain good for? 17
3.5 Ethereum Smart Contracts 18
3.5.1 Benefits 18
3.5.2 Ethereum Smart Contracts structures: 19
3.5.3 Ethereum Smart Contracts example: 20
3.6 Abstract Syntax Tree 22
3.7 Control-flow Graph 23
3.8 Symbolic Execution 24
4 Verification of Ethereum Smart Contracts: A Model Checking Aprroach 26 4.1 Main Contributions 26
4.2 Approach 26
4.3 Technical detail 27
4.3.1 Building an Abstract Syntax Tree 28
4.3.2 Generating CFG of Smart Contracts 29
4.3.3 Symbolic Execution 36
4.3.4 Comparing states 38
5 CONCLUSION 39 5.1 Summary of the work 39
5.2 Limitations and Future Directions 40
A Appendix A: An Intelligent Chatbot for Automatic Verification of Ethereum SmartContracts 45 A.1 Main Contributions 45
A.2 Background 45
A.2.1 Chatbot 45
A.2.2 How does Chatbot work? 46
A.3 Context 47
A.4 Our Propose Chatbot 48
A.5 Building An Intelligent Chatbot for Automatic Verification of Ethereum Smart Contracts based on Rasa Framework 49
A.5.1 Rasa NLU 51
A.5.2 Rase Core 54
A.5.3 Blockchain’s API 56
A.5.4 Result 57
Trang 12Contents
Trang 13List of Figures
1.1 Compiling smart contract to Bytecodes 2
1.2 A contract account contains funds, a compiled contract, and other data 3 1.3 VMs perform the tracsaction 3
1.4 Process of verification smart contracts [24] 5
1.5 A report example after perform smart contracts verification [24] 6
3.1 Blockchain’s list of blocks 11
3.2 Different between centralized and decentralized system [27] 11
3.3 Execution flow of blockchain 14
3.4 Flowchart of PoW 16
3.5 Flowchart of PoS 17
3.6 Potential fields will arise based on blockchain technology [cite] 18
3.7 Abstract syntax tree of if statement 23
3.8 Examples of CFG of while and for statements [13] 24
3.9 Symbolic execution of Code_example_1 25
4.1 Flowchart of our method for verification of Ethereum smart contracts 27 4.2 Context-free grammar of two structures: sendeth & sendtoken 28
4.3 An example of execution process for generating states 29
4.4 CFG of sendeth function 35
4.5 CFG of sendtoken function 36
4.6 Symbolic execution of sendeth function based on CFG 37
4.7 Assign amountE = 30 and amountT = 1 38
4.8 The states after changing and assigning amountE = 30 and amountT = 1 38 A.1 An example of actions between chatbot and user according to the above example 48
A.2 Chatbot flow 49
A.3 Rasa NLU and Rasa Core in Rasa Stack [25] 50
A.4 Thread processing of Rasa [25] 50
A.5 Rasa component lifecycle 53
Trang 14List of Figures
A.7 Generating address through chatbot 58
A.8 Checking account balance through chatbot (S1) 58
A.9 Checking account balance through chatbot (S2) 58
A.10 Checking account balance through chatbot (S3) 58
A.11 Checking transaction information through chatbot (S1) 59
A.12 Checking transaction information through chatbot (S2) 59
A.13 Checking transaction information through chatbot (S3) 59
A.14 Transfering transaction through chatbot 59
A.15 Finding book by name through chatbot 60
A.16 Buying book with ISBN through chatbot 61
A.17 Result of verification of smart contracts when execute the transaction successfully 61
Trang 15List of Tables
3.1 Hash table using SHA256 to compute input value 13
Trang 16ap-While the first generation of blockchain was designed only to solve cryptocurrenciesproblems, Ethereum, one of the most popular current systems, focuses on imple-menting decentralized computing approaches [11] One new prominent of these reli-able platforms is to enable smart contracts, which can automatically execute on theblockchain and enforced by the consensus protocol [31] Accordingly, smart contractsare likely to apply in a wide range of fields including ownership of copyrights, financialinstruments, document existence and asset tracking for the Internet-of-Things [9].The increased adoption of smart contracts demands strong security guarantees Un-fortunately, it is challenging to create smart contracts that are free of security bugs As
a consequence, critical vulnerabilities in smart contracts are discovered and exploited
every few months [Hacking, 7] Moreover, we have to require not only the security
but also the correctness of executions, to keep smart contracts more secure In fact,
Trang 171.2 Smart Contracts
adversaries may take advantage of undocumented methods and exploit potential
bugs as well as vulnerabilities in the contracts, which can cause harm to users
1.2 Smart Contracts
Moreover, one of the best things about Blockchain is the way it records information
to make the values difficult or impossible to change, hack, or cheat the system And,
because it is a decentralized system that exists between all permitted parties [6] So,
there’s no need to pay intermediaries (Middlemen) and it saves your time, conflict
Blockchains have their problems, but they are rated, undeniably, faster, cheaper,
and more secure than traditional systems, which is why banks and governments are
turning to them [26]
Based on the Blockchain property, smart contracts are created to excute code on
Blockchain and allow the performance of credible transactions without third parties
The best way to describe smart contracts is to compare the technology to a vending
machine Ordinarily, you would go to a lawyer or a notary, pay them, and wait while
you get the document With smart contracts, you simply drop a bitcoin into the
vending machine (i.e ledger), and your escrow, driver’s license, or whatever drops into
your account [26] More so, smart contracts not only define the rules and penalties
around an agreement in the same way that a traditional contract does, but also
automatically enforce those obligations
1.3 Verification of Smart Contracts [20, 15]
In this section, we will describe – in general terms – the process of verifying a smart
contract
A smart contract is written in a programming language (commonly Solidity) and then
translated into bytecodes Once a smart contract is reduced to bytecodes, it can be
deployed on the blockchain as a contract account at some address [6]
Figure 1.1 – Compiling smart contract to Bytecodes
In the following example, our contract has been deployed at address 0xEc6e05AdDF3f321b604bECE30bE2cfB161D8
Trang 181.3 Verification of Smart Contracts
Figure 1.2 – A contract account contains funds, a compiled contract, and other data
The picture also shows an externally owned account (at 0xDc7a) Such an accountcontains only funds (300 ether in this case)
A contract begins working after a transaction addressed to it is submitted to thenetwork Here’s a transaction that transfers ether from the submitter’s account toanother account:
• transfer(2000 ether, address(0xDc7a))
That transaction will be performed by potentially thousands of computers (miners)all executing a copy of the same virtual machine (VM) The VM interprets the transferfunction’s bytecodes, which will instruct it (in this case) to subtract 2000 ether from0xEc6e and add it to 0xDc7a
Figure 1.3 – VMs perform the tracsaction
Our goal is to have great confidence that the contract will behave correctly no matter
Trang 191.3 Verification of Smart Contracts
what value is to be transferred, no matter what the balances of the two accounts, and
no matter which account is to receive the value
There are various ways we could fail to get what we want
1 The programmer could misunderstand the intent For example, suppose no onesaid what to do if there wasn’t enough money to transfer Should the transferfunction fail? Or should the contract make a "best effort" and transfer only whatwas available? The programmer might assume one – the wrong one – is the
"obvious" choice and implement it
2 The programmer might know what was needed but make a mistake when menting the intent For example, the Hackergold exploit was due to mistyping+= as =+ An experienced tester would check a list of conditions like these: trans-fer zero ether, transfer all the ether, transfer slightly more than all the ether,transfer the largest possible amount of ether (hoping to discover overflow bugs),transfer an account’s value to itself,
imple-3 Verification and testing find bugs, so they increase confidence A smart contractverification involves developers scrutinizing the code that is used to underwritethe terms of the smart contract This verification also allows developers thechance to identify any potential bugs or vulnerabilities before the smart contract
is deployed
To be more precise, we first start with testing to verify the correctness of smart tracts Running a test means predicting the final balances, making the transfer, andchecking if what the contract actually did is the same as what it should have done.How well this process works depends on how easily the contract can get the right resultfor the wrong reason As a simple example not involving smart contracts, suppose youwere testing addition and tried two cases: 0, 0 and 2, 2 The code correctly produces 0and 4, so you conclude it’s correct But what if addition was mistakenly implemented
con-as multiplication? The tests still pcon-ass, but we all know, the smart contract will fail tomeet correctness
On the other hand, smart contract verification is the review of code by developers toresolve if the code is secure acting as whether there are any existing vulnerabilities,capabilities for future bugs or any errors in coding that could expose users Withthe complexity of the latest programming platform, it is possible for even a well-experienced developer also to make mistakes when writing code It becomes critical
Trang 201.3 Verification of Smart Contracts
The smart contract security verification, which is executed using standards and tocols such as Solidified Verification Standard, is done before the code is hosted onEthereum platform thus it can increase the security of the entire ecosystem and indi-vidual projects and block hacks Even-though it won’t guarantee that the code is 100%secure, the experts reconsidering the code could find the problems and discuss possi-ble improvements to the code to enhance the security of cryptocurrencies The expert
pro-of developers can highlight critical issues that require to be checked and improved.Particularly, smart contract verification are conducted by third parties to assure thatthe code is reviewed as carefully as possible Depending on the complexity of thesmart contract, companies may select to engage the services of a specialist smartcontract verification team to conduct the verification
Once check the entire verification will be completed on a deployed smart contractbecause your verification should be performed on a release candidate (RC), or the finalSmart Contract stage before public release, as this is what is closest to the end-userproduct
In addiction, we will explain the important verification steps for the smart contract
Figure 1.4 – Process of verification smart contracts [24]
First you have provided A Legal Disclaimer and also Explain your background likeWho You Are and your verification Process in the contract
After that, the verification process will:
• Conduct attack vulnerability tests
• Detail Vulnerabilities Found and Concerns
Trang 211.3 Verification of Smart Contracts
• Inspect Contract Complexity
• Inspect Failure Preparation
• Figure out the Code Currency Search of Re-used Versus Duplicated Code
• Inspect External Calls
• Check Initial Balance
• Check security of on-chain data
• Analyze N-party Contracts
•
Finally, after inspection through these functions, the process must compile all the formation and export a report So, this is the basic steps for smart contract verificationprocess
in-Figure 1.5 – A report example after perform smart contracts verification [24]
Trang 221.4 Motivating example
1.4 Motivating example
Let’s imagine that John wants to buy Mike’s house This agreement is formed onthe Ethereum blockchain using a smart contract This smart contract contains anagreement between John and Mike In the simplest terms, the agreement will look
like this: “WHEN John pays Mike 300 Ether, THEN John will receive ownership of the
house through a token”
Once this smart contract agreement has been put into place, it cannot be changed —meaning John can feel safe to pay Mike 300 Ether for the house Smart contracts areautomatically executed once the conditions of the agreement are met This meansthere is no need for a third party, like a bank, a broker or a government Due to thisreason, John feel not safe because he do not know anything about the contracts, andcan be cheated He want to figure out that if he transfer an amount of Ether, Does hereceive a token? (in this situation, this is an ownership of Mike’s house)
So, he decide to hire a third party that could check the contracts and solve his lems However, the knowledge and process of verification smart contracts are toocomplicated and difficult for normal people like John to follow Although, after verifi-cation successfully, they give him a detail report such as Fig.1.5, he still confuses anddoes not get exactly what he want to know
ap-1.6 Scope of this research
The research just covers some types of Ethereum smart contracts (limit area), which perform basic operations Specifically, direct exchange ETH and token from user and contract owner through smart contract Moreover, these contracts also have the specific forms, structures that based on our knowledge In addiction, we just focus
on the result before and after finish a transaction and do not deal with the contract
Trang 231.7 Research Outcomes
security vulnerabilities.
1.7 Research Outcomes
1.7.1 The pilot study I: Verification of Ethereum Smart Contracts:
An Model Checking Approach
• "Verification of Ethereum Smart Contracts: An Model Checking Approach",
accepted by The 3rd Asia Conference on Machine Learning and Computing
(ACMLC2019), Hong Kong, China And It was published on International Journal
of Machine Learning and Computing, Vol 10, No 4, July 2020
1.7.2 The pilot study II: An Intelligent Chatbot for Automatic
Verifi-cation of Ethereum Smart Contracts
• "An Intelligent Chatbot for Automatic Verification of Ethereum Smart
Con-tracts", submitted to The 7th IEEE International Conference on Data Science and
Advanced Analytics (DSAA2020), Sydney, Australia.
1.8 Report Outline
Further details in this report, we first provide a literature survey in Chapter 2 thatintroduces the state-of-the-art verification methods Then, we discuss the researchbackground and problem statement in Chapter 3 In the next chapter, we presentthe pilot study that we have finished to support for this thesis "An Model CheckingApproach for verification of Smart Contracts, especially Ethereum Smart Contracts".Specifically, we design a process that not only satisfy the requirements of correctness
of smart contracts, but also provide enough information that user can understand andfollow easily Not but at least, in Chapter 5, we introduce a new process for verificationEthereum smart contracts through intelligent Chatbot Finally, we summarize andconclude the report in Chapter 6
Trang 242 LITERATURE SURVEY
Ethereum smart contracts are how things get done in the Ethereum ecosystem Whensomeone wants to get a particular task done in Ethereum, they initiate a smart contractwith one or more people Smart contract security verification are fundamentally thesame as the regular code verification, which is meticulously investigating code toexplore security flaws and vulnerabilities before the code is publicly deployed.Many decentralized applications, which are centered around Smart contracts, haveimplemented a variety of software tools to aid in the verification practice These tools,such as automated code-checking for vulnerabilities, may be used as a supplement,but should not replace the formal verification process One option, as mentionedpreviously, is Mythril [23], which can be used for detecting uint overflows and under-flows Another tool is Manticore [22], used here to analyzing binaries and Ethereumsmart contracts Or, Securify [34] can invest security violations of contracts on a byte-code level through semantic inference , whereas SmartCheck [32] parses the contractlanguage for lexical and syntax analysis
However, a simple smart contract with no business logic costs around $4000 Morecomplicated and advanced smart contracts can go from 50,000$ all the way up to100,000$ Plus, if that’s not enough, there will usually last 4 weeks and then it takes
8 weeks for the verification process to be completed [4] Moreover, the important
thing is that if the users are non-technical individuals, they may not understand the results of smart contract verification or figure what the need even if they read the report carefully.
Trang 253 RESEARCH BACKGROUND
3.1 Blockchain
A blockchain is a distributed system that consists of a network of computers connectedover the Internet which collectively performs the bookkeeping[19] In blockchain,one ledger is distributed and maintained across the network Every computer in thenetwork validates the transactions, but no one has control over the network, unlikemultiple users working in a centralized server [19]
A blockchain contains a series of blocks, which are data units used to store tional information of the network [30] For a block to be added to a blockchain, thefollowing steps should happen:
transac-• A transaction must occur and should be verified by a network of computers
• The transaction must be stored in a block; implying upon approval of the action by the network, the dollar value, your digital signature, and the receiver’sdigital signature will be stored in a block
trans-• The block must be given a hash to differentiate it from other blocks This blockalso contains information of the hash of the most recent block added into ablockchain Once hashed, this block can be added to the blockchain
• After the block is added to the blockchain, the details of this transaction will bepublic and anyone within the network can look into this transaction information,such as who was the sender? Who is the receiver? When did the transactionhappen? Who added this transaction to the block?, etc The data is verifiableand auditable but not editable
Trang 263.1 Blockchain
A block is nothing but digital information A “chain” is defined as a public database
So, when we say blockchain – we mean to talk about the digital information which isstored in a public database Let’s take an example:
Figure 3.1 – Blockchain’s list of blocks
3.1.1 Characteristics
Blockchain can also have some or all of the following properties:
Figure 3.2 – Different between centralized and decentralized system [27]
• Decentralization: in blockchain network, every single nodes have an
indepen-dent ledger which cryptographically store transaction data in blocks If a nodefail to response or is being attacked by DDOS, it will not effect the whole network
Trang 273.1 Blockchain
In contrast to conventional network (centralization), if a server is attacked, usercould not access to the server[3]
• Public or private: public blockchain is the network that any nodes can
par-ticipate without permission or any authorization and everyone can read data.Meanwhile private blockchain is permissioned blockchain in which a node has
to get the permission to be able to join into the network
• Transparency: The decentralization of the distributed ledger means that those
transaction records are identically recorded in multiple locations [30] Havingthe same records spread across a large network for all to see is the core ofblockchain transparency It is also why the blockchain is considered hacking-resistant
facilitate, verify, or enforce the negotiation or performance of a contract Smartcontracts allow the performance of credible transactions without third parties
• Byzantine Fault Tolerant (BFT): when it comes to decentralization network, one
of the most popular problems that need to tackle is how to resist some of thenodes fail or act maliciously Each blockchain network has its own typical way
of solving this problem
• Incentivisation mechanisms: the blockchain networks include a set of
indepen-dent nodes which has various behaviors to the data on blockchain To makethese nodes act or perform correctly, there always have some rewards or ben-efits that influences on system participants For example, in Bitcoin network,
if a node solve a math problem, it will receive a correspond bitcoin (BTC) as areward
• Cryptographically secure: to ensure the integrity of the ledger, every transactions
in a block are hashed and being hashed by the following block which ensurethat there is no way to change the past transactions without changing the hash
of the following block
• Immutability: in the context of the blockchain, means that once something has
been entered into the blockchain, it cannot be tampered with The reason whythe blockchain gets this property is that of the cryptographic hash function [27].For instance, we are going to put in certain inputs and use SHA-256 (SecureHashing Algorithm 256) to see how the hashing process works
Trang 283.1 Blockchain
As you can see, no matter how big or small your input is, the output will always have afixed 256-bits length This becomes critical when you are dealing with a huge amount
of data and transactions
Table 3.1 – Hash table using SHA256 to compute input value
518007d1764826381969Welcome to HCMUT 4139dcb2d6c866b271b6b22b28f11fc1c5c7bfaac39a
465ee9024e17c29f343c
3.1.2 How does a Blockchain work?
When a block stores new data it is added to the blockchain Blockchain, as its namesuggests, consists of multiple blocks strung together In order for a block to be added
to the blockchain, however, four things must happen:
1 A transaction must occur Let’s continue with the example of your impulsiveAmazon purchase After hastily clicking through multiple checkout prompt, you
go against your better judgment and make a purchase As we discussed above,
in many cases a block will group together potentially thousands of transactions,
so your Amazon purchase will be packaged in the block along with other users’transaction information as well
2 That transaction must be verified After making that purchase, your transactionmust be verified With other public records of information, like the SecuritiesExchange Commission, Wikipedia, or your local library, there’s someone incharge of vetting new data entries With blockchain, however, that job is left
up to a network of computers When you make your purchase from Amazon,that network of computers rushes to check that your transaction happened
in the way you said it did That is, they confirm the details of the purchase,including the transaction’s time, dollar amount, and participants (More on howthis happens in a second.)
3 That transaction must be stored in a block After your transaction has beenverified as accurate, it gets the green light The transaction’s dollar amount, your
Trang 29When that new block is added to the blockchain, it becomes publicly available foranyone to view.
Figure 3.3 – Execution flow of blockchain
In conclusion, The reasons why Blockchain has gained so much admiration are:
• It is not owned by a single entity, hence it is decentralized
• The data is cryptographically stored inside
• The blockchain is immutable, so no one can tamper with the data that is insidethe blockchain
Trang 303.2 Is Blockchain Secure ?
3.2 Is Blockchain Secure ?
Assume that a hacker attempts to edit your transaction so that you actually have to payfor your purchase twice As soon as they edit the dollar amount of your transaction,the block’s hash will change The next block in the chain will still contain the old hash,and the hacker would need to update that block in order to cover their tracks [30].However, doing so would change that block’s hash And the next, and so on
In order to change a single block, then, a hacker would need to change every singleblock after it on the blockchain [3] Recalculating all those hashes would take anenormous and improbable amount of computing power In other words, once a block
is added to the blockchain it becomes very difficult to edit and impossible to delete
To address the issue of trust, blockchain networks have implemented tests for ers that want to join and add blocks to the chain The tests, called “consensus models,”require users to “prove” themselves before they can participate in a blockchain net-work [27]
comput-3.3 Consensus Protocol
A consensus protocol is a mechanism that allows users or machines to coordinate in adistributed setting [28] It needs to ensure that all agents in the system can agree on asingle source of truth, even if some agents fail In other words, the system must befault-tolerant
The peer-to-peer network structure in cryptocurrency is structured according to theconsensus mechanism that they are utilizing [28] For cryptocurrency like Bitcoin andEthereum which uses a normal proof-of-work consensus mechanism (Ethereum willeventually move on to Proof of Stake), all the nodes have the same privilege To bemore specific:
Trang 313.3 Consensus Protocol
1 A node collects transactions sent by users, and put all of transactions into block
2 Header of the most recent block will be put into a block and then be hashed
3 After finishing all of preparation, a node will try its best to find the nonce inwhich will make the hash of a block is less than the target result, if the nonce isnot satisfied the result, it will increase the nonce by 1 and keep the incrementagain and again till it reach the requirement
4 When a specific node in a network first find a node, it will broadcast that blockinto the network and receive reward
Figure 3.4 – Flowchart of PoW
3.3.2 Proof of Stake
Proof of Stake (PoS) is a type of consensus algorithm by which a cryptocurrencyblockchain network aims to achieve distributed consensus In PoS-based cryptocur-rencies the creator of the next block is chosen via various combinations of randomselection and wealth or age (i.e., the stake)
Trang 323.4 What is Blockchain good for?
1 The validators will use some amount of cryptocurrencies to stake some nodes
2 A set of nodes which are staked by most users will be chosen to produce blocks
3 The chosen node will create a block by collecting transactions and then ing that block If it is valid, the node will add it to blockchain and broadcast intothe network
validat-4 When the block reaches finality, all validators who staked that node will receivesreward
Figure 3.5 – Flowchart of PoS
3.4 What is Blockchain good for?
The blockchain potentially cuts out the middleman for these types of transactions andgive internet users the ability to create value and authenticates digital information.Thus, there are some new business applications will result from these criteria asmentioned in Fig.3.6
Trang 333.5 Ethereum Smart Contracts
Figure 3.6 – Potential fields will arise based on blockchain technology [cite]
3.5 Ethereum Smart Contracts
The main purpose of smart contracts are self-executing with specific instructionswritten on their code which gets executed when a certion condition is made SmartContract works by following the simple conditional statements "if/when then " thatare developed into code on a blockchain A network of systems/computers will thenexecute the actions that are coded into the smart contract such as releasing funds to aappropriate party, issuing a ticket, when predetermined conditions are met Afterthe execution of that smart contract, the state of the blockchain is updated
In our framework, we use Ethereum smart contracts written in Solidity language due
to Ethereum is the most popular blockchain platform for creating smart contracts
[19]
3.5.1 Benefits
• Speed and accuracy: Smart contracts are digital and automated, so you will
not have to spend time processing paperwork or reconciling and correctingthe errors that are often written into documents that have been filled manually
Trang 343.5 Ethereum Smart Contracts
are written in
• Trust: Smart contracts automatically execute transactions following
predeter-mined rules, and the encrypted records of those transactions are shared acrossparticipants Thus, nobody has to question whether information has beenaltered for personal benefit
• Security: Blockchain transaction records are encrypted, and that makes them
very hard to hack Because each individual record is connected to previous andsubsequent records on a distributed ledger, the whole chain would need to bealtered to change a single record
• Savings: Smart contracts remove the need for intermediaries because
partic-ipants can trust the visible data and the technology to properly execute thetransaction There is no need for an extra person to validate and verify the terms
of an agreement because it is built into the code
3.5.2 Ethereum Smart Contracts structures:
Contracts in Solidity are similar to classes in object-oriented languages Each contractcan contain declarations of state variables, functions, function transforms, events,structure types, and enum types Furthermore, the contract can be inherited fromother contracts
• State Variables: variables are permanently stored in the contract.
Trang 353.5 Ethereum Smart Contracts
• Function Modifiers: can be used to modify the semantics of a function
accord-ing to the declaration
• Event: can be used to modify the semantics of a function according to the
Trang 363.5 Ethereum Smart Contracts
Trang 373.6 Abstract Syntax Tree
Ethereum smart contract in Solidity
3.6 Abstract Syntax Tree
In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree sentation of the abstract syntactic structure of source code written in a programminglanguage Each node of the tree denotes a construct occurring in the source code [10].The syntax is "abstract" in the sense that it does not represent every detail appearing
repre-in the real syntax, but rather just the structural or content-related details For repre-instance,grouping parentheses are implicit in the tree structure, so these do not have to berepresented as separate nodes [16] Likewise, a syntactic construct like an if-condition-
Trang 38Characteristics of Control Flow Graph:
• Control flow graph is process oriented
• Control flow graph shows all the paths that can be traversed during a programexecution
• Control flow graph is a directed graph
• Edges in CFG portray control flow paths and the nodes in CFG portray basicblocks
For instance:
Trang 393.8 Symbolic Execution
(a) CFG of while-statement (b) CFG of for-statement
Figure 3.8 – Examples of CFG of while and for statements [13]
3.8 Symbolic Execution
Generally, symbolic execution (SE for sort) is a means of analyzing a program todetermine what inputs cause each part of a program to execute [18] The key idea of
SE is:
• Evaluate the program on symbolic input values
• Use an automated theorem to check whether there are corresponding concreteinput values that make the program fail
Consider the program below, which reads in three values and fails when a = 0, b
< 5 During a normal execution ("concrete" execution), the program would read
Trang 403.8 Symbolic Execution
proceed with the assignment and the conditional branch, which would evaluate assertfunction according to the condition in it During symbolic execution, the programreads a =α, b =β and c = γ