Introduction to Ethereum. Smart contracts
Ethereum has become a proof of concept for the future of web communication, primarily due to its main feature, Smart Contract. Explaining how it works.
What is Ethereum?
Primarily, Ethereum is a platform for creating decentralized online services, operating on the basis of blockchain technology. Only secondarily is it a cryptocurrency. The main distinction between Ethereum and, say, Bitcoin is the smart contracts. They enable programming within the blockchain. This allows the integration of more complex mechanisms into Ethereum, such as NFTs.
Thanks to Smart Contracts, we can explore new ways of conducting business, transparent financial institutions, transparent patenting, transparent voting, and the creation of transparent contracts. In fact, the application sphere of Smart Contracts is exceedingly broad, and new ideas continue to emerge daily.
Smart Contracts
In essence, a contract is a text file with code written in the programming language Solidity. It provides a certain API, in the form of functions and triggers, which can be used within the smart contract.
The Logic of Executing Smart Contracts in Ethereum
The execution of a contract in Ethereum occurs in several stages:
-
Creating a Transaction
: It all starts with the creation and sending of a transaction by the user. This transaction contains instructions for calling the smart contract's functions, and may also include data and ETH for transfer to the contract.
-
Dissemination of the Transaction
The sent transaction is disseminated across the Ethereum network of nodes. It waits in the transaction pool until a miner (or validator in PoS) selects it for inclusion in a new block.
-
Transaction Processing by Miners
The miner selects the transaction from the pool and begins the processing. This includes verifying the transaction's authenticity (signature) and the sufficiency of funds to pay for gas.
-
Contract Execution
After verification, the miner (or validator) executes the smart contract code. This execution occurs in the Ethereum Virtual Machine (EVM) - an isolated environment where each operation costs a certain amount of gas.
-
State Change
As a result of the contract execution, a change in the blockchain state may occur. For example, this could be the transfer of tokens, changing data in the contract, etc.
-
Block Confirmation
After the miner completes the formation of the block with transactions, he disseminates this block across the network. Other nodes verify the block and, upon successful verification, add it to their copy of the blockchain.
-
Gas Payment
The user who sent the transaction pays a gas fee in ETH. This fee goes to the miner (or validator) as a reward for processing the transaction and supporting the network.
-
Finalization
After the block is confirmed, the transaction and all its results (including contract execution) are considered final and irreversible.
Who Pays for Smart Contract Computations
The computations associated with executing smart contracts in the Ethereum network are paid for by the users who send transactions. These users pay a gas fee, which compensates for the resources needed to process transactions and execute smart contracts.
What Affects the Gas Fee?
The gas fee depends on several factors:
-
Computational Complexity
More complex operations require more computational resources, hence, are more expensive.
-
Network Congestion
During periods of high network congestion, the gas price can increase as users compete for their transactions to be included in a block quickly.
-
Gas Limit
The user sets the maximum amount of gas they are willing to spend on executing the transaction. If the limit is too low, the transaction may not be executed.
Miners or validators (depending on whether the network uses proof of work or proof of stake) select transactions with higher gas fees, as this increases their income. These fees serve as an incentive for maintaining the operation of the Ethereum network.
What Range of Functions is Available in a Contract
Smart contracts in Ethereum can perform a wide range of computations, but there are certain limitations and features to consider:
-
Turing Completeness
Smart contract programming languages, such as Solidity and Vyper, are Turing complete, theoretically allowing them to perform any computation similar to conventional programming languages.
-
Limited Resources
Each operation in a contract costs a certain amount of gas, and the amount of gas that can be used in a single transaction is limited. This means that very complex or resource-intensive computations may be impractical due to high gas costs or block limits.
-
Determinism
Computations in smart contracts must be deterministic, meaning that given the same input, they must always produce the same output. This is necessary for maintaining the consistency of the blockchain state.
-
No External Network Requests
Smart contracts cannot directly make requests to external servers or APIs. Oracles are used to obtain external data.
-
Data Storage
Contracts can store data in their state, but due to gas costs, large amounts of data can be expensive to store.
-
Mathematical and Logical Operations
Contracts can perform various mathematical and logical operations, manage execution flow (loops, conditional structures), create and interact with other contracts.
-
Cryptographic Function Handling
Ethereum supports certain cryptographic functions, such as hashing and digital signature verification.
-
Execution Time Limitations
Due to the gas limit for each transaction, smart contracts are not suitable for computations requiring long execution times.
These limitations and capabilities define what types of applications and computations can be effectively implemented using smart contracts in Ethereum.
Accessing External Resources from the Blockchain, such as a Website's API
Directly in the code of Ethereum smart contracts, it is not possible to make a request to an external website or call an external API. However, for interacting with external data and APIs in Ethereum smart contracts, oracles are used. Oracles are third-party services that provide data from the external world to the blockchain. They act as intermediaries, collecting data needed by smart contracts and providing it to the blockchain in a reliable and verified form.
Examples of using oracles include obtaining information on asset prices, weather data, sports event outcomes, and much more. However, using oracles requires trust in the data provider and proper management of risks associated with the centralization of this aspect of decentralized applications.
This is related to several key features of the blockchain and smart contracts:
-
Determinism
The blockchain requires that each transaction and contract execution be deterministic. This means that every node in the network must come to the same result when executing the contract code. External requests could lead to different results at different times, violating this principle.
-
Security and Isolation
Incorporating external data or requests into the blockchain could create attack vectors and vulnerabilities in network security.
-
Synchronization and Speed
The Ethereum blockchain operates in a synchronous mode, and adding dependence on external asynchronous requests could significantly slow down or disrupt the mining and transaction verification process.
Data Storage in Smart Contracts
Smart contracts in Ethereum have the capability to store the results of computations. These data are stored in the blockchain state and can be accessible both within the contract itself and to other contracts or external inquiries. Here are the key aspects of data storage in smart contracts:
-
State Variables: Smart contracts can define state variables that are stored on the blockchain and can be modified through the contract's functions. These variables can store values such as numbers, strings, arrays, and more complex data structures.
-
State Modification: When a smart contract is executed as a result of a transaction and changes its state variables, these changes are recorded in the blockchain after the successful execution of the transaction.
-
Long-term Storage: Since data is recorded in the blockchain, it is long-term and
immutable, meaning it will be stored as long as the Ethereum network exists.
-
Data Reading: Smart contracts can include functions for reading stored data. Such functions can be called by external users or other contracts without sending a transaction and without spending gas.
-
Gas Limitations and Cost: Storing data on the Ethereum blockchain requires gas, and thus can be costly, especially when storing large amounts of data. This requires optimization and economical use of storage.
Smart contracts can use storage for a variety of purposes, including tracking cryptocurrency balances, voting results, unique identifiers for NFTs (non-fungible tokens), and much more.
Thus, the mechanisms of data storage and management are key components in the design and functionality of smart contracts on Ethereum.
Modifying a Smart Contract After Deployment to the Network
Once a smart contract is deployed (deployed) in the Ethereum network, its code becomes immutable. This means that after deployment, it is not possible to change the logic or functionality of the contract. Here are the key aspects of this feature:
-
Immutability of Code: The code of a smart contract and all its operations are recorded in the Ethereum blockchain and become an integral part of its history. After deployment, the contract code cannot be changed or deleted.
-
Security and Trust: This immutability is an important feature for ensuring security and trust in the network. Users and parties interacting with the contract can be assured that the contract's logic will not be changed after its deployment.
-
Updates and Modifications: If it is necessary to update or change the logic of a smart contract, developers typically deploy a new contract and, if necessary, migrate data from the old contract. In some cases, developers include mechanisms in the contract for updating some of its aspects (e.g., addresses of other contracts), but the core logic remains unchanged.
-
Self-Destruction: There is a function in Solidity,
selfdestruct
, that allows "destroying" a contract, freeing up space in the blockchain and returning residual funds to a specified address. However, after calling this function, the contract becomes non-functional, and its code still remains in the blockchain as history. -
Proxy Contract Architecture: To create more flexible systems that can be updated, developers sometimes use a proxy contract architecture, where the logic is stored in a separate contract, and the proxy contract delegates function calls to it. This allows updating the logic by only updating the contract with the logic, but this approach has its nuances and risks.
The core logic and functionality of a smart contract remain unchanged after its deployment, and creating a new contract is usually required for making changes.
Summary
Ethereum is much more than just a cryptocurrency. Such platforms not only offer us the freedom of payment transactions but also provide a communication between different businesses without the need for bureaucracy. The absence of bureaucracy leads to faster progress for humanity.