The novice course of intelligent contract programming in Ether Square

Source: Internet
Author: User

The original text starts with the Consensys developer blog, the original author is Eva and Consensys's development team. If you want to get more timely information, you can visit Consensys home Click on the lower left corner newsletter subscribe to the message. The translation of this article has been authorized by Mr. Lubin, founder of Consensys.

Some people say that the etheric square is too difficult to deal with, so we wrote this article to help you learn how to use the Consensys to write intelligent contracts and applications. The tools, wallets, applications, and ecosystems that are used here are still in development and will be better used in the future. The first part outlines, discusses key concepts, several large Ethernet square clients and the programming language used to write smart contracts. The second part discusses the overall workflow and some of the DAPP frameworks and tools that are currently prevalent. The third part is mainly about programming, and we will learn how to use truffle to write tests and build Dapp for smart contracts. The first part. Overview

If you are completely unfamiliar with the concept of cryptography, such as Bitcoin and how it works, we suggest that you take a look at the first chapters of the Bitcoin book Andreas Antonopoulos and read the ether white paper. (see Http://ethfans.org/posts/ethereum-whitepaper for the Chinese version of the Ether white paper)

If you think the chapters in the White paper are too obscure, you can also get to know the etheric square directly. Doing development on the ether does not require you to understand all those "cryptography Economics Computer Science" (Crypto economic computer), and much of the white paper is about the etheric square that wants to improve on the bit-currency architecture. Novice Tutorials

Ethereum.org offers an official beginner's introductory course, as well as a course in a coin contract and a public-chip contract. The contract language solidity also has official documents. Another good piece of information for learning about smart contracts (and my introductory information) is dappsforbeginners, but it may be a little outdated now.

The purpose of this article is to supplement this information, while introducing some basic developer tools that make it easier to get started in the ether, smart contracts, and build dapps (decentralized apps, distributed applications). I will try to explain what every step of the workflow is doing in my own (still novice) understanding, and I get a lot of help from Consensys cool developers. Basic Concepts

Getting to know these nouns is a good start:

Public key encryption system. Alice has a public key and a private key. She can create a digital signature with her private key, and Bob can use her public key to verify that the signature was actually created with Alice's private key, that is, Alice's signature indeed. When you create an Ethernet square or Bitcoin wallet, the long 0xdf ... A 5f address is essentially a public key, and the corresponding private key is saved somewhere. An online wallet like Coinbase can help you keep your private key, and you can keep it for yourself. If you lose the private key to a wallet that has money, you'll lose that money forever, so you'd better make a backup of the private key. "It is very painful to learn this by stepping on the pit," said one.

Point to Point Network. Just like BitTorrent, ether. All nodes in a distributed network have equal status without a central server. (There will be a half-centric hybrid service available to users and developers in the future, which we'll talk about later.) )

Block chain. A block chain is like a global, unique account book, or database, that records all the transaction history of the network.

Ethernet Square Virtual Machine (EVM). It allows you to write more powerful programs on the etheric square (scripts can also be written on the bitcoin). It is sometimes used to refer to the etheric square block chain, which is responsible for the implementation of intelligent contracts and everything.

Node. You can run the node through it to read and write the Ethernet block chain, and even for the Tai Fong virtual machine. Full nodes need to download the entire block chain. The light node is still under development.

Miners. Mining, which is the node that deals with blocks on the block chain. This page can be seen in the current active part of the etheric square Miner: stats.ethdev.com.

Proof of workload. The miners are always competing to solve some mathematical problems. The first solution (figure out the next block) will be rewarded with an Ethernet currency. All nodes then update their own block chains. All miners who want to work out the next block have the power to keep up with the other nodes and maintain the same block chain, so the entire network can always reach a consensus. (Note: The ether square is planning to turn to a certificate system (POS) without a miner, but that is not covered in this article.) )

Ether coins. Abbreviation ETH. A real digital currency that you can buy and use. Here is the trend chart of one of the exchanges that can trade the etheric currency. At the time of writing this article, 1 Ethernet coins worth 65 cents.

Gas. (gasoline) in the ether to execute the program and save the data must consume a certain amount of the ether, gas is the ether converted from the currency. This mechanism is used to ensure efficiency.

Dapp. The etheric square community calls the application of an intelligent contract (decentralized app) a centralized application. Dapp's goal is (or should be) to make your smart contract have a friendly interface, plus something extra, such as IPFS (a centralized network that can store and read data, not from the Etheric team but with a similar spirit). The Dapp can run on a central server that can interact with the etheric square node, or on any of the equal nodes of an Ethernet square. (Take a minute to think: Unlike a typical web site, Dapp cannot run on a regular server.) They need to submit transactions to the block chain and read important data from the block chain rather than the central database. Relative to the typical user login system, the user may be represented as a wallet address while other user data is stored locally. Many things will have a different architecture from the current Web application. )

If you want to see how these concepts are understood from another novice perspective, read just enough Bitcoin for Ethereum. Ethernet Square Client, intelligent contract language

Writing and deploying a smart contract does not require you to run an Ethernet square node. The following is a list of browser-based Ides and APIs. But if it is to learn, you should still run an Ethernet square node to understand the basic components, not to mention the running node is not difficult. running the Ethernet node available clients

The etheric square has many different language client implementations (that is, many ways to interact with the Ethernet network), including C + +, go, Python, Java, Haskell, and so on. Why do you need so many implementations? Different implementations can meet different needs (for example, the goal of Haskell is to be mathematically validated), to make the etheric square more secure and to enrich the entire ecosystem.

When I was writing this article, I used the client Geth (go-ethereum) of the go implementation, and at other times I used a tool called TESTRPC, which used the Python client pyethereum. The following examples will use these tools.

Note: I have used C + + clients and are still working with the Ethminer components and Geth to mine, so these different components can work together.
About digging: Digging is fun, a bit like taking care of your indoor potted plants, and it's a way to understand the whole system. Although the current price of the ether may not be able to fill the electricity bill, but later who knows. People are creating a lot of cool dapp that may make the etheric square more and more popular.

Interactive console. When the client is running, you can sync the block chain, build the wallet, and send and receive the Ethernet currency. One way to use Geth is through the JavaScript console (JavaScript console, like the one you press F12 out of the Chrome browser, just running in the terminal). You can also use a curl-like command to interact with the client via JSON RPC. The goal of this article is to take everyone over the side of the Dapp development process, so this piece is not much to say. But we should remember that these command-line tools are debugging, configuring nodes, and using the wallet's sharp weapon.

Run the node in the test network. If you are running the Geth client on the official network, it will take a considerable amount of time to download the entire block chain to sync with the network. (You can determine whether you have synchronized by comparing the last block number printed in the node log with the latest block listed in Stats.ethdev.com.) Another problem is that smart contracts run on formal networks need real etheric coins. This is not the problem when you run a node on a test network. At this time also do not need to sync the entire block chain, create a own private chain on the hook, for the development of more time saving.

Testrpc. You can create a test network with Geth, and another way to create a test network faster is to use TESTRPC. TESTRPC can help you create a bunch of test accounts that have money at startup. It runs faster and therefore better suited for development and testing. You can start from Testrpc, and then as the contract takes shape, transfer to the test network created by Geth-the startup method is simple, just specify a networkid:geth--networkid "12345". This is the TESTRPC code warehouse, and we'll talk about it later.

Let's talk about the available programming languages, and then we can start real programming. programming language for writing smart contracts

It's good to use solidity. There are several languages to write about smart contracts: a bit like JavaScript solidity, and the file name extension is. Sol. With Python close to the serpent, the filename ends with. SE. There are also Lisp-like LLL. Serpent has been popular for some time, but now the most popular and most stable to be regarded as solidity, so the use of solidity is good. I hear you like python? With solidity.

Solc compiler. After you write smart contracts with solidity, you need to compile them with SOLC. It is a component from the C + + client implementation (again, different implementations are complementary) and here is the installation method. If you do not want to install SOLC, you can also use a browser-based compiler, such as solidity real-time compiler or Cosmo. The latter part of the programming section will assume that you have installed the SOLC.

Note: The etheric square is in active development and sometimes there are different steps between the new versions. Make sure you are using the latest dev version, or a stable version. If you have a problem, go to the gitter chat room of the etheric Square project or forums.ethereum.org and ask the other person what version they are using.

Web3.js API. Once the solidity contract has been compiled and sent to the network, you can use the Web3.js JavaScript API in the etheric square to invoke it to build Web applications that interact with it.

These are the basic tools needed to write intelligent contracts and build dapp that interact with the etheric square. Part Two. Dapp framework, tools and workflow DAPP Development Framework

Although the tools mentioned above can be developed, using the framework created by the Community of great gods makes development easier.

Truffle and embark. It was truffle who led me into the door. The summer before truffle, I saw how a bunch of gifted students had a sleepless night in a hackathon (programming marathon), and though it turned out pretty good, I was scared. Then truffle comes in and helps you get rid of a lot of insignificant little things so you can quickly get into the write code-compile-deploy-test-package Dapp the process. Another similar DAPP build and test framework is embark. I've only used truffle, but the two camps have a lot of dapp.

Meteor. Another development stack used by many Dapp developers is made up of web3.js and Meteor, Meteor is a common webapp development framework (Ethereum-meteor-wallet project provides a great example of getting started, and Silentciero is building a lot of Meteor and W Eb3.js and Dapp integrated templates). I've downloaded and run some nice dapp that was constructed in this way. There will be some interesting discussions at the Ðξvcon1 developers ' conference from November 9 to 13th about using these tools to build dapp and related best practices (meetings are broadcast on YouTube).

APIs. Blockapps.net is going to provide a set of restful APIs for Dapp to eliminate the hassle of developers running the local node, a centralized service based on the Ethernet square Haskell. This runs counter to the DAPP model, but it can be useful in situations where you can't run an Ethernet node locally, such as when you want a browser or a user with a mobile device to use your dapp. Blockapps provides a command-line tool bloc that can be used after registering a developer account.

Many people are worried about the need to run the etheric square node to use Dapp words will scare users away, in fact, including Blockapps, many tools can solve this problem. Metamask allows you to use the function of the ether square in the browser without the node, the official Alethzero of the ether or Alethone is the client who is developing an Easy-to-use interface, Consensys is creating a light wallet lightwallet, These tools will make dapp easier to use. Light client and horizontal fragmentation (sharding) are also planned and developed. This is a peer-to-peer ecosystem that can evolve into a hybrid architecture. Smart Contract integrated Development Environment (IDE)

Ide. I haven't used it yet, but I'll try it as soon as possible.

A browser-based IDE. Solidity real-time compiler and Cosmo can help you quickly start writing smart contracts in your browser. You can even get these tools to use your local node, just let the local node open a port (be aware of security). These tools sites must be trustworthy, and don't put all your assets in such a local node. The Cosmo UI has instructions on how to use Geth to do this. After your smart contract has been debugged, you can use the development framework to add a user interface and package it as a dapp, which is exactly the work of truffle, which is explained in detail in the following programming chapters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.