Formal Modeling and Verification of Smart Contracts with Spin
Abstract
:1. Introduction
- According to the specifications of blockchain transactions, we build general formal models for smart contracts and clients in the transaction. The method can describe the behavior the characteristics of smart contracts interacting with clients in the form of a state diagram under specific scenarios.
- A formal verification method of smart contracts based on Spin is proposed. The formal models of smart contracts and clients in the transaction can be presented with the Promela language. Spin can run the Promela model to simulate the process execution and verify whether the model caters to these LTL formulae and assertions.
2. Related Works
2.1. Modeling of Smart Contracts
2.2. Correctness Verification of Smart Contracts
2.3. Security Assurance of Smart Contracts
3. The Proposed Approach
3.1. Transition System of Ethereum Transactions
- S, the set of states.
- , the set of actions, where:
- –
- represents the internal action.
- –
- represents the change of time.
- –
- , the communication action, where c denotes the channel, denotes the message v sent by channel c, and denotes that the variable x receives the message from channel c.
- , the transition relation, where:
- –
- represents the transition condition, where , in which is the set of internal variables and t represents the set of discrete time variables.
- , the set of initial states.
3.2. General Model for Ethereum Transactions
- .
- .
- is the transition relation, as shown in Figure 2.
- .
- .
- .
- is the transition relation, as shown in Figure 3.
- .
- .
- .
- is the transition relation, as shown in Figure 4.
- .
4. The Ethereum Commodity Market
4.1. The Ethereum Transaction
4.2. Framework
5. Modeling the Ethereum Commodity Market
5.1. Factory Modeling
- Requesting a transaction: In this phase, sends a transaction request to . When receives the transaction request, provides the order quantity to . The states defined for include , , , and .
- Transferring the payment to : In this phase, checks whether the account balance is sufficient to pay for the goods. If satisfied, the payment is transferred to the smart contract account. The state defined for is .
- Getting the ID of : In this phase, needs to obtain the ID of , so that it can confirm the identity of when it receives the goods. The state defined for is .
- Receiving the goods: When receives the goods, it confirms the receipt to . The states defined for include , , and .
- Confirming receipt: When receives the goods, it confirms the receipt to . The state defined for is .
5.2. Supplier Modeling
- Responding to the request of : In this phase, responds to the transaction request sent by and reviews the order quantity. The states defined for are , , , , and .
- Renting : Because of delivering the goods to , sends a transaction request to . The states defined for include , , and .
- Transferring the fare to : In this phase, checks whether the account balance meets the cost of renting . If satisfied, it transfers the fare to the smart contract account; otherwise, it declares that the transaction has failed and notifies to refund the payment. The state defined for is .
- Delivering the goods: After renting successfully, delivers the goods to by . When confirms the receipt, receives the payment from and confirms shipment to . The states defined for include , , and .
- Sending the ID of to : In this phase, needs to send the identity information of to , so that can recognize correctly. The state defined for is .
- Confirming the success of shipment: When learns that has successfully received the goods, it needs to confirm the success of shipment to . The states defined for are and .
5.3. Truck Modeling
- Responding to the request of : In this phase, responds to the request of by judging its current state. The states defined for are , , and .
- Picking up the goods: If arrives during the working hours, it can pick up the goods from successfully. The states defined for include , , and .
- Carrying the goods: If the delivery time and identity information of are correct, it can deliver the goods to successfully. The states defined for include , , and .
5.4. Ethereum Modeling
- Waiting for to transfer the payment: The safety of the transaction between and is guaranteed by . At the beginning, waits for to transfer the payment to trigger the transaction. The state defined for is .
- Notifying of delivery: Once receives the payment, it notifies of the delivery. The states defined for are and .
- Transferring the payment to : If confirms the receipt of the goods, transfers the payment to ; otherwise, it returns the payment to eventually. The states defined for are and .
- Waiting for to transfer the fare: The safety of the transaction between and is guaranteed by . At the beginning, waits for to transfer the fare to trigger the transaction. The state defined for is .
- Notifying of work: Once receives the fare, it notifies of the work. The states defined for are and .
- Transferring the fare to : If confirms the shipment, transfers the fare to ; otherwise, it returns the fare to eventually. The states defined for are and .
6. Implementation
6.1. Model Checker Spin
6.2. Modeling the Ethereum Commodity Market with Spin
- Each state name is translated into an value, and each variable representing the current state in a state machine is also declared an variable. The following is the initial declaration of the state machines’ current state:mtype fa_currState = start;mtype su_currState = idle;mtype tr_currState = initiate;mtype sca_currState = begin;mtype scb_currState = begin;
- The interact signals between state machines are sent and received through channels. The type of all channels is abstract as , and the type of all messages is abstracted as . In addition, the size of each channel is set to 0 to ensure synchronization between processes. The declaration of the channels is as follows:chan fa_event = [0] of {mtype};chan su_event = [0] of {mtype};chan t_event = [0] of {mtype};chan sca_event = [0] of {mtype};chan scb_event = [0] of {mtype};
- The transition function and the step function are defined for each state. The transition function represents the procedure of the model migrating from one state to the next. The step function represents the change in the model state, which is called by the transition function. The functions of each state are translated into macros. The transition function of the model in state is declared as follows:inline T_check_carryT(){if:: (time>=9 && time<=17) -> su_event ! carryT_ok;S_t_inter1_entry();:: else -> su_event ! ter_t1;S_initiate_entry();fi}
- All state transition functions of each model are put into a region function. By calling the function in a loop, each process can find the current state and determine the operation that should be performed to move to the next state. The region functions of , , , , and are, respectively, function , function , function , function , and function , which are translated into macros. The region function of is declared as follows:inline R_sca(){if:: (sca_currState == begin) -> T_sca_begin();:: (sca_currState == sca_re_f) -> T_sca_re_f();:: (sca_currState == sca_fund_ok) -> T_sca_fund_ok();:: (sca_currState == pay_su) -> T_pay_su();:: (sca_currState == return_fa) -> T_return_fa();fi}
- There are five executable processes , , , , and , which describe the behavior of , , , , and in the transaction. Besides the above processes, another process is created. This process declares that the behaviors of processes , , , , and are active in the initial system. Keyword starts these processes, which run concurrently in the system from then on. The process is declared as follows:init {run Fa_stm();run Su_stm();run Tr_stm();run SCa_stm();run SCb_stm();}
6.3. Simulation
6.4. Verification
6.4.1. Properties
6.4.2. Analysis
7. Discussion and Limitations
8. Conclusions
Author Contributions
Funding
Institutional Review Board Statement
Informed Consent Statement
Data Availability Statement
Conflicts of Interest
References
- Yaga, D.; Mell, P.; Roby, N.; Scarfone, K. Blockchain technology overview. arXiv 2019, arXiv:1906.11078. [Google Scholar]
- Zheng, Z.; Xie, S.; Dai, H.N.; Chen, W.; Chen, X.; Weng, J.; Imran, M. An overview on smart contracts: Challenges, advances and platforms. Future Gener. Comput. Syst. 2020, 105, 475–491. [Google Scholar] [CrossRef]
- Zheng, Z.; Xie, S.; Dai, H.; Chen, X.; Wang, H. An overview of blockchain technology: Architecture, consensus, and future trends. In Proceedings of the 2017 IEEE International Congress on Big Data (BigData Congress), Boston, MA, USA, 11–14 December 2017; pp. 557–564. [Google Scholar]
- Belchior, R.; Vasconcelos, A.; Guerreiro, S.; Correia, M. A survey on blockchain interoperability: Past, present, and future trends. ACM Comput. Surv. (CSUR) 2021, 54, 1–41. [Google Scholar] [CrossRef]
- Zohar, A. Bitcoin: Under the hood. Commun. ACM 2015, 58, 104–113. [Google Scholar] [CrossRef]
- Haleem, A.; Javaid, M.; Singh, R.P.; Suman, R.; Rab, S. Blockchain technology applications in healthcare: An overview. Int. J. Intell. Netw. 2021, 2, 130–139. [Google Scholar] [CrossRef]
- Farouk, A.; Alahmadi, A.; Ghose, S.; Mashatan, A. Blockchain platform for industrial healthcare: Vision and future opportunities. Comput. Commun. 2020, 154, 223–235. [Google Scholar] [CrossRef]
- Raja Santhi, A.; Muthuswamy, P. Influence of blockchain technology in manufacturing supply chain and logistics. Logistics 2022, 6, 15. [Google Scholar] [CrossRef]
- He, M.; Wang, H.; Sun, Y.; Bie, R.; Lan, T.; Song, Q.; Zeng, X.; Pustisĕk, M.; Qiu, Z. T2L: A traceable and trustable consortium blockchain for logistics. Digit. Commun. Netw. 2022. [Google Scholar] [CrossRef]
- Wang, X.; Yang, W.; Noor, S.; Chen, C.; Guo, M.; van Dam, K.H. Blockchain-based smart contract for energy demand management. Energy Procedia 2019, 158, 2719–2724. [Google Scholar] [CrossRef]
- Szabo, N. The idea of smart contracts. Nick Szabo’s Pap. Concise Tutor. 1997, 6, 199. [Google Scholar]
- Vujičić, D.; Jagodić, D.; Ranđić, S. Blockchain technology, bitcoin, and Ethereum: A brief overview. In Proceedings of the 2018 17th International Symposium Infoteh-Jahorina (Infoteh), East Sarajevo, Bosnia and Herzegovina, 21–23 March 2018; pp. 1–6. [Google Scholar]
- Buterin, V. A next-generation smart contract and decentralized application platform. White Pap. 2014, 3, 2-1. [Google Scholar]
- Atzei, N.; Bartoletti, M.; Cimoli, T. A survey of attacks on ethereum smart contracts (sok). In Proceedings of the International Conference on Principles of Security and Trust, Uppsala, Sweden, 24–25 April 2017; Springer: Berlin/Heidelberg, Germany, 2017; pp. 164–186. [Google Scholar]
- Kushwaha, S.S.; Joshi, S.; Singh, D.; Kaur, M.; Lee, H.N. Systematic review of security vulnerabilities in ethereum blockchain smart contract. IEEE Access 2022, 10, 6605–6621. [Google Scholar]
- Liu, J.; Liu, Z. A survey on security verification of blockchain smart contracts. IEEE Access 2019, 7, 77894–77904. [Google Scholar] [CrossRef]
- Bhargavan, K.; Delignat-Lavaud, A.; Fournet, C.; Gollamudi, A.; Gonthier, G.; Kobeissi, N.; Kulatova, N.; Rastogi, A.; Sibut-Pinote, T.; Swamy, N.; et al. Formal verification of smart contracts: Short paper. In Proceedings of the 2016 ACM Workshop on Programming Languages and Analysis for Security, Vienna, Austria, 24 October 2016; pp. 91–96. [Google Scholar]
- Kalra, S.; Goel, S.; Dhawan, M.; Sharma, S. Zeus: Analyzing safety of smart contracts. In Proceedings of the 25th Annual Network and Distributed System Security Symposium (NDSS), San Diego, CA, USA, 18–21 February 2018; pp. 1–12. [Google Scholar]
- Baier, C.; Katoen, J.P. Principles of Model Checking; MIT Press: Cambridge, MA, USA, 2008. [Google Scholar]
- Cimatti, A.; Clarke, E.; Giunchiglia, E.; Giunchiglia, F.; Pistore, M.; Roveri, M.; Sebastiani, R.; Tacchella, A. Nusmv 2: An opensource tool for symbolic model checking. In Proceedings of the International Conference on Computer Aided Verification, Copenhagen, Denmark, 27–31 July 2002; Springer: Berlin/Heidelberg, Germany, 2002; pp. 359–364. [Google Scholar]
- Holzmann, G.J. The model checker SPIN. IEEE Trans. Softw. Eng. 1997, 23, 279–295. [Google Scholar] [CrossRef] [Green Version]
- Shoukry, Y.; Nuzzo, P.; Balkan, A.; Saha, I.; Sangiovanni-Vincentelli, A.L.; Seshia, S.A.; Pappas, G.J.; Tabuada, P. Linear temporal logic motion planning for teams of underactuated robots using satisfiability modulo convex programming. In Proceedings of the 2017 IEEE 56th Annual Conference on Decision and Control (CDC), Melbourne, Australia, 12–15 December 2017; pp. 1132–1137. [Google Scholar]
- Almakhour, M.; Sliman, L.; Samhat, A.E.; Mellouk, A. Verification of smart contracts: A survey. Pervasive Mob. Comput. 2020, 67, 101227. [Google Scholar] [CrossRef]
- Hamdaqa, M.; Met, L.A.P.; Qasse, I. iContractML 2.0: A domain-specific language for modeling and deploying smart contracts onto multiple blockchain platforms. Inf. Softw. Technol. 2022, 144, 106762. [Google Scholar] [CrossRef]
- Jurgelaitis, M.; Butkienė, R. Solidity Code Generation From UML State Machines in Model-Driven Smart Contract Development. IEEE Access 2022, 10, 33465–33481. [Google Scholar] [CrossRef]
- Mavridou, A.; Laszka, A. Designing secure ethereum smart contracts: A finite state machine based approach. In Proceedings of the International Conference on Financial Cryptography and Data Security, Nieuwpoort, Curacao, 26 February–2 March 2018; Springer: Berlin/Heidelberg, Germany, 2018; pp. 523–540. [Google Scholar]
- Ladleif, J.; Weske, M. A unifying model of legal smart contracts. In Proceedings of the International Conference on Conceptual Modeling, Vienna, Austria, 3–6 November 2019; Springer: Berlin/Heidelberg, Germany, 2019; pp. 323–337. [Google Scholar]
- Amani, S.; Bégel, M.; Bortin, M.; Staples, M. Towards verifying ethereum smart contract bytecode in Isabelle/HOL. In Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and Proofs, Los Angeles, CA, USA, 8–9 January 2018; pp. 66–77. [Google Scholar]
- Grishchenko, I.; Maffei, M.; Schneidewind, C. Ethertrust: Sound Static Analysis of Ethereum Bytecode; Technische Universität Wien: Vienna, Austria, 2018; pp. 1–41. [Google Scholar]
- Nehai, Z.; Piriou, P.Y.; Daumas, F. Model-checking of smart contracts. In Proceedings of the 2018 IEEE International Conference on Internet of Things (iThings) and IEEE Green Computing and Communications (GreenCom) and IEEE Cyber, Physical and Social Computing (CPSCom) and IEEE Smart Data (SmartData), Halifax, NS, Canada, 30 July–3 August 2018; pp. 980–987. [Google Scholar]
- Browne, M.C.; Clarke, E.M.; Grümberg, O. Characterizing finite Kripke structures in propositional temporal logic. Theor. Comput. Sci. 1988, 59, 115–131. [Google Scholar] [CrossRef]
- Osterland, T.; Rose, T. Model checking smart contracts for ethereum. Pervasive Mob. Comput. 2020, 63, 101129. [Google Scholar] [CrossRef]
- Luu, L.; Chu, D.H.; Olickel, H.; Saxena, P.; Hobor, A. Making smart contracts smarter. In Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, Vienna, Austria, 24–26 October 2016; pp. 254–269. [Google Scholar]
- Torres, C.F.; Schütte, J.; State, R. Osiris: Hunting for integer bugs in ethereum smart contracts. In Proceedings of the 34th Annual Computer Security Applications Conference, San Juan, PR, USA, 3–7 December 2018; pp. 664–676. [Google Scholar]
- Chen, T.; Li, X.; Luo, X.; Zhang, X. Under-optimized smart contracts devour your money. In Proceedings of the 2017 IEEE 24th International Conference on Software Analysis, Evolution and Reengineering (SANER), Klagenfurt, Austria, 20–24 February 2017; pp. 442–446. [Google Scholar]
- Neumann, R. Promela formalization. Arch. Form. Proofs 2014, 2014, 1–103. [Google Scholar]
Property | Name | Kind | States Stored | Transitions Usage | Result |
---|---|---|---|---|---|
Deadlock | DF | D.P. | 1,084,815 | 3,301,742 | P |
Invariant | TAB | LTL | 759,662 | 2,269,701 | P |
Safety | SAF | LTL | 759,662 | 2,269,701 | P |
TAS | LTL | 759,662 | 2,269,701 | P | |
Liveness | TBRS | LTL | 759,662 | 2,269,701 | P |
BRCV | LTL | 967,856 | 2,869,858 | P | |
TWT | A | 1,084,815 | 2,216,927 | P |
Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations. |
© 2022 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Yang, Z.; Dai, M.; Guo, J. Formal Modeling and Verification of Smart Contracts with Spin. Electronics 2022, 11, 3091. https://doi.org/10.3390/electronics11193091
Yang Z, Dai M, Guo J. Formal Modeling and Verification of Smart Contracts with Spin. Electronics. 2022; 11(19):3091. https://doi.org/10.3390/electronics11193091
Chicago/Turabian StyleYang, Zhe, Meiyi Dai, and Jian Guo. 2022. "Formal Modeling and Verification of Smart Contracts with Spin" Electronics 11, no. 19: 3091. https://doi.org/10.3390/electronics11193091
APA StyleYang, Z., Dai, M., & Guo, J. (2022). Formal Modeling and Verification of Smart Contracts with Spin. Electronics, 11(19), 3091. https://doi.org/10.3390/electronics11193091