Mastering Ethereum
上QQ阅读APP看书,第一时间看更新

Workings of smart contracts

Smart contracts are blockchain applications that can execute code and do a diverse variety of tasks. These are programs that the miners execute when mining blocks. They are secure and unstoppable agreements that are automatically executed and enforced. You are probably familiar with them, since this book is aimed at Ethereum developers; however, if you don't know how to use them or if you want to learn more about them, you can rest assured that you'll understand everything because of detailed explanations that you'll be given at the appropriate times.

Unlike traditional paper contracts, smart contracts don't have real-world legal consequences on Ethereum, and they can't be enforced by a legal entity when they break. Instead, they rely on the principle that code is law, which means that the code is the one governing the behaviors of the contract. You can't execute functions that are not defined in the smart contract and you must respect each function's requirements.

What's interesting about smart contracts is that they allow the creation of unstoppable applications that will keep the data and functionalities running on top of the blockchain regardless of whether the underlying web interface is not available or is censured for whatever reasons. Smart contracts open the world to a new type of application that you can deploy and forget, knowing that it will work under any circumstances.

There's an ongoing debate about whether the code is acceptable as a real contract in a court of law. One side thinks that smart contracts have to go further and be enforced legally for a more general view of applications, while the other side thinks that the code as law is enough to guarantee the security and enforcement of the agreements.

Smart contracts are programs that run on top of the blockchain in the software layer. They allow developers to create decentralized trustless programs that have certain functions for managing transactions between individuals. Instead of relying on a centralized server, they are the database and the server in decentralized applications.

Inherently, smart contracts must be limited in their capacity given that we are executing code that will stay as a transaction on the blockchain permanently, meaning that every function you execute gets registered in the blockchain without having the option to undo what's been done. This imposes some natural limitations, since you can't just modify the public database that every node of the Ethereum will have to download without some restrictions. Every change has to be downloaded by all the peers of the network.

The purpose of them is to solve the trust problem that many companies are facing when dealing with voting, banking, and situations where people are expected to blindly trust companies without disclosing what they do with your data and money. They also provide accessibility, since anybody with an internet connection will be able to access them and retrieve the data stored inside.

However, smart contracts are limited in what they can do. They are not a great solution for many cases, such as the following:

  • Applications that require a performing backend that is able to process information at a rapid pace, such as creating users and interacting with an application. They are slow by nature, since they depend on the block times of Ethereum and they cannot work in real-time situations.
  • Applications that store large amounts of data on a database. Smart contracts work like a database, since they allow anybody to store certain information on top of the blockchain, but they are limited and it's costly to do so.

Gas is another concept that is important to understand properly. It was created because smart contracts are Turing complete, which means that they can execute loops such as while or for endlessly until a task is done. It could happen that developers create endless loops that are stuck in the same state forever. To avoid a situation where thousands of nodes are stuck processing one transaction with no limit, each transaction has a gas cost that depends on the processing power required to execute it.

So, they created a system where you have to pay for every action you take using the blockchain. You can store information on the blockchain, but you'll have to pay an important cost, because every single node using it will have to download your changes. The way it calculates the gas cost is simple:

  1. You pay a certain maximum gas that you're willing to spend for your transaction.
  2. Your local Ethereum node calculates how much it will cost to execute your transaction by checking the opcodes used to generate a precise approximation of the computing power needed. You see, every small operation using the blockchain can be measured because we use a sort of assembly language that tells us when this super computer is being used and how.
  3. The right amount of gas is used, and the rest is refunded to you.

You also have to determine a gas price ranging from 1 to usually 100 or more to tell miners which transactions to process first, since they get paid more the more expensive each gas is.