In-depth understanding of Ethereum and smart contracts

foreword

Eth (Ethereum) I don't know if you have heard of it, but you must have heard of ICO. But did you know that ICOs are actually implemented with the help of Eth’s smart contracts? What is a smart contract and why is Eth so popular? What are the characteristics of smart contracts and how do I write smart contracts? There are also Dapps based on smart contracts, that is, distributed apps. What is Dapp?

This series of articles will comprehensively describe the principles, characteristics and mechanisms of Ethereum, as well as the various concepts involved.

How the blockchain works

First of all, you must understand the blockchain. Here is just a brief description. The blockchain is a network-wide billing chain. The users of the chain encrypt the transaction content with their own private key, and then send it to the network. The miners encrypt the content and submit it to the blockchain through the competition of computing power. On the blockchain, at the same time, the transaction fee paid by the user is obtained.

Since everyone is competing for encrypted content, the computing power of the entire network is the witness of the content submitted by users, so blockchain transactions are fast and irreversible.

Accounts and Messaging

A transaction in the blockchain is the transfer of basic units from one address to another, and Ethereum abstracts this behavior into message passing here. Each message transmission has a sender and a receiver. The content of the message can be a transaction or a piece of information. Transfer is actually a message transfer.

There are two types of accounts in Ethereum, one is a normal account operated by humans, and the other is a smart account. The normal account only has the current amount of Eth money, and the smart account stores two things: status and code. When the corresponding message is received, the code is executed, changing its state. These accounts are also the carriers of so-called smart contracts.

These smart accounts are the basis of smart contracts. To understand how smart contracts are executed, you must first understand EVM.

EVM

As mentioned earlier, the real calculations are carried out in the computers of individual miners. The improvement of Ethereum is that when miners collect enough messages to encrypt and generate a block, they must start a runtime environment, that is, EVM, to run the corresponding code when the smart account receives the message.

This environment contains some built-in variables, such as the current Block's Number, the address of the message source, etc., and also provides some APIs, a stack for the execution of smart contracts.

After running the code through the EVM, the status of the smart account changes, and then the miners encrypt these statuses with the changes in the funds in the normal account to generate a new block, which is linked to the entire network bill. Therefore, a transaction will only appear in one block, and it can only be mounted after the confirmation of the majority of computing power, so the uniqueness and correctness of the execution of these codes can be guaranteed.

Example of a smart contract

The concept is sometimes obscure, let's take an example of a smart contract for fundraising to help understand:

Suppose we want to raise donations from users of the entire network, then we can define a smart account, which has three states: the current total amount of donations, the donation target and the address of the recipient, and then define two functions for it:

Receive donation function

Each time the fundraising function receives a transfer request, first check whether the sender has enough money (EVM will provide the address of the sender, and the program can obtain the person's current blockchain financial status through the address. ) and each time the fundraising function is called, the current total amount of fundraising will be compared with the donation target. If it exceeds the target, all the currently received donations will be sent to the specified recipient address, otherwise, only the current donation will be updated. The state value of the total amount of donations.

donation function

Send all donations to the saved recipient address, and reset the current total donation amount to zero.

Everyone who wants to raise funds initiates a transfer to the smart account with their own eth address, and specifies the function to call to accept their donation.

So we have a fundraising smart contract. People can donate money in it. After reaching the limit, the money will be automatically sent to the designated account. Miners all over the world are calculating and guaranteeing this contract. There is no misappropriation, which is the charm of smart contracts.

The core smart contract mechanism of Ethereum is probably like this. In fact, it is not complicated. Next, we will talk about how ICO is realized with the help of smart contracts. In fact, the principle is very simple. You will be surprised when you understand it.

Original text: http://baijiahao.baidu.com/s?id=1576362675066362360&wfr=spider&for=pc

--------------------------------------------------------------------

If you want to learn Ethereum DApp development efficiently , you can visit the most popular online interactive tutorials provided by Huizhi.com:

1.  An introductory tutorial for Ethereum DApps for blockchain newbies
2.  Blockchain +IPFS+Node.js+ MongoDB+Express decentralized Ethereum e-commerce application development practice

3. Other more content can also visit this Ethereum blog .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326035843&siteId=291194637