How Do Blockchain, Smart Contracts, and Apps Work?
The participants of Tezos DeFi Hackathon 2022 asked how smart contracts work, who calls them, what the incoming parameters are, and where Storage comes from. Baking Bad developer Mikhail Zaikin explained this in great detail.
We decided that this knowledge should be shared with the community. We took Mikhail’s post as the basis, expanded it with examples and added some illustrations.
What Is Blockchain
Blockchain is a database that consists of blocks. Each block contains information about transactions and operations in a small period of time.
The blocks are stacked into a chain; hence the name blockchain. Each successive block contains the hash sum of the previous block, which is the result of converting all the data in the block. It serves as a means to verify that the information in the old blocks has not changed since their creation. If you replace one operation in a block, its hash sum will change and nodes will notice the switch. Therefore, a blockchain cannot be edited and the old operations cannot be rolled back.
Blockchain is stored by nodes, i.e. computers linked together in a P2P network. P2P means that all nodes are equal, perform the same function, and directly exchange information about transactions and blocks with each other.
Thus, each node stores the history of all completed transactions, as well as State, the final result of operations: address balances, list and the code of smart contracts and their data known as Storage.
How Blockchain Executes Operations
An operation is a transformation of the state of a blockchain. A node applies input parameters and Entrypoints to the old state, and returns the new state. Operations can have different types: smart contract deployment (publishing), tez transaction, smart contract invocation, and others.
For example, when calling a smart contract, the user specifies its address and the arguments to transfer to the contract. Nodes propagate this request throughout the network, add it to the current block, and then apply it each to its own state.
To process the request, the node at the address of the smart contract finds its code and data (Storage) in the blockchain. It then runs the contract interpreter, loads the code, the input arguments, and current contract data into it. On output, it receives new data and overwrites it into the state.
Blockchain applications often use multiple smart contracts that reference each other. In this case, a node will load its code and data into the contract interpreter as the operation progresses.
During block creation, most nodes on the network perform the same operations. As a result, all copies of the blockchain in the network contain the same result, and the network considers the call completed.
How Do Smart Contracts Work
A smart contract consists of two parts: the code to be executed and the repository in which all the data is stored. The developer specifies functions and incoming arguments in the code, as well as the type of data in the storage.
When a contract is deployed in the blockchain, the developer specifies its initial state, i.e. the data that nodes will transform when performing operations. For example, when creating a smart contract for a token, the developer specifies the number of tokens and the addresses of users who are authorized to dispose of them.
As the nodes write the contract code in a block, the developer cannot change it later. Teams test the code on test networks and conduct security audits so that they don’t publish a non-working or vulnerable product to the mainnet as they won’t be able to fix it.
How Apps Work with Smart Contracts
Decentralized exchanges, NFT marketplaces, and other applications obtain user balance data from the blockchain.
When a user clicks a button, for example, to exchange tokens at a DEX, the application generates a transaction request with user-specified arguments and passes it to nodes.
Subscribe and never miss updates from the world of Tezos: