A Beginner's Guide to Ethereum Development

What is Ethereum - A Beginner's Guide to Ethereum Development

Many students have been eager to join the blockchain development team, but they feel that they are unable to start. This article will introduce the obscure concepts involved in the development of Ethereum in a popular way based on the Ethereum platform, and easily guide you to get started.

write in front

Before reading this article, you should have a general understanding of what blockchain is. If you don’t know it yet, please subscribe to the column: Blockchain technology
guides you to learn blockchain technology from scratch.

What is Ethereum

Ethereum (Ethereum) is a decentralized application platform built on blockchain technology. It allows anyone to build and use decentralized applications running on blockchain technology in the platform.

For students who do not understand this sentence, it can be understood that Ethereum is Android in the blockchain. It is a development platform that allows us to write applications based on blockchain technology just like Android Framework.

Before there was no Ethereum, writing a blockchain application was like this: copy a copy of Bitcoin code, and then change the underlying code such as encryption algorithm, consensus mechanism, network protocol, etc. new currency).
The Ethereum platform encapsulates the underlying blockchain technology, allowing blockchain application developers to develop directly based on the Ethereum platform. Developers only need to focus on the development of the application itself, which greatly reduces the difficulty.

At present, a relatively complete development ecosystem has been formed around Ethereum: with community support, there are many development frameworks and tools to choose from.

smart contract

What is a smart contract

Programs on Ethereum are called smart contracts , which are collections of code and data (state).

Smart contracts can be understood as contracts (special transactions) written in code that can be automatically executed (message-driven) on the blockchain.

Smart contract English is Smart Contract, which has nothing to do with the intelligence of artificial intelligence (AI:Artificial Intelligence). Nick Szabo first proposed the concept of smart contract in 1995. Its concept is very simple, that is, to write legal provisions into executable code. There was no blockchain at that time, but smart contracts were the best match for the blockchain. We know that contracts are required to be in duplicate, three or four copies and cannot be controlled by one party, which is decentralization.

In Bitcoin Script , we mentioned that Bitcoin transactions can be programmed, but Bitcoin Script has many limitations and limited programs that can be written,
while Ethereum is more complete (in computer science terms, it is called it In order to be "Turing complete"), let's write programs (smart contracts) that can do almost anything just like any high-level language.

Smart contracts are very suitable for application scenarios that require high trust, security and durability, such as: digital currency, digital assets, voting, insurance, financial applications, prediction markets, property ownership management, Internet of Things, peer-to-peer transactions, etc.
At present, apart from digital currency, there are not many applications that have actually landed (just like the mobile platform has just started to come out). It is believed that in 1 to 3 years, various killers will gradually appear.

Programming language: Solidity

The officially recommended programming language for smart contracts is Solidity, with the file extension ending in .sol.
The Solidity language is very similar to JavaScript, and it is used to develop contracts and compile them into Ethereum Virtual Machine byte code.

There are also Viper, Serpent, LLL and Bamboo. It is recommended that you use Solidity.
Update: Serpent is no longer officially recommended. Serpent users are advised to switch to Viper, which are all Python-like languages.

Browser-Solidity is a Solidity IDE for browsers. You can click in and have a look. In the future, we will introduce the Solidity language in more articles.

Operating environment: EVM

EVM (Ethereum Virtual Machine) Ethereum Virtual Machine is the running environment of smart contracts in Ethereum.

Solidity is to the EVM as it is to the JVM, so it is easy for everyone to understand.
The Ethereum Virtual Machine is an isolated environment with no external access to the code running inside the EVM.

The EVM runs on the Ethereum node. After we deploy the contract on the Ethereum network, the contract can run in the Ethereum network.

Compilation of the contract

The bytecode form of the contract runs on the Ethereum virtual machine. We need to compile the contract before deployment. You can choose the Browser-Solidity Web IDE or the solc compiler.

Deployment of the contract

When developing applications on Ethereum, an Ethereum client (wallet) is often used. Usually we are in the development, generally do not come into contact with the concept of client or wallet, what is it?

Ethereum client (wallet)

Ethereum client, in fact, we can understand it as a developer tool, which provides account management, mining, transfer, deployment and execution of smart contracts and other functions.

EVM is provided by the Ethereum client

Geth is a typical client used in the development of Ethereum and is developed based on the Go language. Geth provides an interactive command console through which various functions (APIs) of Ethereum are included. We will introduce the use of Geth in an article later, here we have a concept first.

The Geth console is similar to the console in the Chrome browser developer tools, but the Geth console runs in a terminal.
Compared with Geth, Mist is an Ethereum client with a graphical interface.

How to deploy

The deployment of a smart contract refers to publishing the contract bytecode on the blockchain and using a specific address to mark the contract, which is called a contract account.

There are two types of accounts in Ethereum:

  • External account
    This type of account is controlled by a private key (controlled by a person) and is not associated with any code.
  • Contract Accounts
    These types of accounts are controlled by their contract code and have code associated with it.

Unlike Bitcoin's UTXO design, Ethereum uses a simpler account concept.
Both types of accounts are the same for EVM.

The difference and relationship between external accounts and contract accounts are as follows: an external account can send messages to another external account or contract account by creating and signing transactions with its own private key.
Sending messages between two external accounts is the process of transferring value. But a message from an external account to a contract account activates the code of the contract account, allowing it to perform various actions (such as transferring tokens, writing to internal storage, mining a new token, performing some operations, creating a new contract, etc. Wait).
Only when the external account issues an instruction, the contract account will perform the corresponding operation.

Contract deployment is to deploy the compiled contract bytecode to the Ethereum blockchain in the form of sending transactions through an external account (the actual deployment is successful only after the actual miner produces a block).

run

After the contract is deployed, when you need to call the method of the smart contract, you only need to send a message (transaction) to the contract account. After the message is triggered, the code of the smart contract will be executed in the EVM.

Gas

Similar to cloud computing, occupying the resources of the blockchain (whether it is a simple transfer transaction, or the deployment and execution of a contract) also requires corresponding fees (there is no free lunch in the world, right!).
The Gas mechanism is used for billing on Ethereum. Gas can also be considered as a unit of work. The more complex the smart contract is (the number and type of calculation steps, the memory occupied, etc.), the more Gas is required to complete the operation.
The amount of Gas required to run the contract for any particular contract is fixed and determined by the complexity of the contract.
The gas price is specified by the person running the contract when submitting the request to run the contract to determine the fee he is willing to pay for this transaction: Gas price (in Ether) * Gas ​​amount.

The purpose of Gas is to limit the amount of work required to execute a transaction, while paying for execution. When the EVM executes the transaction, the gas will be gradually consumed according to specific rules, no matter where the execution is, once the gas is exhausted, an exception will be triggered. All state modifications made in the current call frame will be rolled back, and if there is still gas remaining at the end of the execution, the gas will be returned to the sending account.

Without this limit, someone would write a contract that cannot be stopped (eg: infinite loop) to block the network.

So in fact (stringing up the previous content), we need an external account with an ether balance to initiate a transaction (ordinary transaction or deploy, run a contract), and when running, the miner charges the corresponding workload fee.

Ethereum network

Some anxious students want to ask, how to develop smart contracts without ether? You can choose from the following methods:

Select the Ethereum official website test network Testnet

In the test network, we can easily get free ether, the disadvantage is that it takes a long time to initialize the node.

Use a private chain

Create our own Ethereum private test network, also commonly called a private chain, that we can use as a test environment to develop, debug, and test smart contracts.
Through the above-mentioned Geth, it is easy to create a test network of your own, and you can mine as much ether as you want, without synchronizing the entire blockchain data of the official network.

Use developer network (mode)

Compared with the private chain, under the developer network (mode), a developer account with a large balance will be automatically allocated for us to use.

Use a simulated environment

Another way to create a test network is to use testrpc, which is an Ethereum environment simulated locally using memory, which is more convenient and faster for development and debugging. And testrpc can help us create 10 test accounts with funds at startup.
When developing a contract, you can deploy it to the Geth node after passing the test in testrpc.

Update: testrpc has now been incorporated into the Truffle development framework, now called Ganache CLI.

Dapp: Decentralized Application

The Ethereum community refers to applications based on smart contracts as decentralized applications. If we understand blockchain as an immutable database and smart contracts as programs that deal with databases, then it is easy to understand Dapps. A Dapp not only has smart contracts, for example, it also needs to have a friendly user interface and other things.

Truffle

Truffle is a Dapp development framework. It can help us deal with a lot of insignificant little things, so that we can quickly start the process of writing code - compiling - deploying - testing - packaging DApp.

Summarize

Let's summarize now that Ethereum is a platform that allows us to easily use blockchain technology to develop decentralized applications. In this application, Solidity is used to write smart contracts that interact with the blockchain. After the contracts are written After that, we need to use the Ethereum client to deploy and run the contract with an account with a balance (using the Truffle framework can help us do these things better). For the convenience of development, we can use Geth or testrpc to build a test network.

Note: In this article, in order to facilitate everyone's understanding, some concepts are analogized, and some are strictly inaccurate. However, I also think that for beginners, it is not necessary to master each concept very carefully and accurately. Learning is a step-by-step process. In the process, many times we will find that after a period of time, we will have a different understanding of the same thing.

After this article, do you understand all these concepts? Now you can start developing, take a look at smart contract development environment setup and Hello World contract

Guess you like

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