Ethereum Dapp Development Introduction Combat! Build with the node. JS and truffle framework-blockchain voting system!

Source: Internet
Author: User

first section Overview

For beginners, the content covers the basic concepts related to ethereum development and will teach you how to build a complete de-centralized application based on Ethereum-blockchain voting system.

By learning, you will master:

    • Basic knowledge of Ethereum blockchain

    • Software environment required to develop and deploy ethereum contracts

    • Authoring Ethereum contracts using high-level languages ( solidity )

    • Use Nodejs to compile, deploy, and interact with contracts

    • Using Truffle frameworks to develop distributed applications

    • Interacting with a contract using the console or Web page

Pre-order knowledge requirements

In order to successfully complete, it is best to have some basic understanding of the following technologies:

    • An object-oriented development language, for example: Python,ruby,java ...

    • Front-end development language: Html/css/javascript

    • Linxu use of the command line

    • Basic concepts of the database

All the code has been tested on Ubuntu (trusty, xenial) and MacOS.

Section II Introduction

We will build a de-centralized ( Decentralized ) voting application. With this voting app, users can vote on a particular candidate in an unreliable ( trustless ) distributed environment, and each poll is recorded on the blockchain:

The so-called de-centric application ( DApp : dcentralized application) is an application that does not exist in the hub server. A copy of the app can be run on hundreds or thousands of computers in the network, making it almost impossible to crash.

Blockchain-based voting is completely centralized, so there is no need for any centralized organization.

Section III Development iterations

Will cover the entire process of application development, and we will progressively introduce the relevant concepts, languages, and tools involved in blockchain application development through three iterations:

    • Vanilla: In the first iteration cycle, we use only nodejs for application development without any development framework, which helps us to better understand the core concepts of blockchain applications.

    • Truffle: In the second iteration cycle, we will use the most popular de-centralized application development framework Truffle for development. Using the development framework helps us improve our development efficiency.

    • Token: In the third iteration, we're going to introduce tokens for the voting app- Token -and now everyone is calling it a pass. ICO Tokens are an indispensable incentive mechanism in the public chain, and another notable feature of blockchain applications that are differentiated from traditional central applications.

Why choose the voting app as a course item?

The choice of voting as our first Blockchain application is because collective decision-making, especially the voting mechanism, is a core value proposition of Ethereum.

Another reason is that voting is a fundamental component of many complex de-centralized applications, so we chose the voting app as the first project to learn about blockchain application development.

Fourth section of the first knowledge of the block chain

If you are familiar with relational databases, you should know that a table can contain many rows of data records. For example, the following data table contains 6 transactions:

Essentially, a blockchain is first a distributed ( Distributed ) database that maintains a growing list of records. Now, let's bulk ( batch ) store the data, like 100 rows per batch, and connect each storage batch together, is it like a chain?

In a blockchain, batches of multiple data records are called blocks ( block ), and each row of data in a block is called a transaction ( transaction ):

The first block, often called the Genesis Block ( genesis block ), does not point to any other block.

Non-tamper resistance

A notable feature of blockchain is that once the data is written to the chain, it cannot be tampered with.

In a traditional relational database, you can easily update a data record. However, in a blockchain, once the data is written it can no longer be updated-so the blockchain is growing.

So, how does blockchain implement the data's non-tamper characteristics?

This is primarily due to the hash ( Hash ) function--if you have not yet touched the hash function, think of it as a computational function of a digital fingerprint: Enter any length of content and output a fixed length stream (fingerprint). An important feature of the hash function is that any slight change in the input will result in a change in the output. Therefore, the hash value can be used as the thumbprint of the content. You can click here to learn more about hashing functions.

Since each block in the blockchain stores the hash value of the previous block content, if any block's contents are tampered with, the hash of all blocks after the tampered block is changed so that we can easily detect if the blocks of the blockchain have been tampered with.

The challenge of de-centering

Once fully centralized, there will be a large number of blockchain replicas (i.e. full nodes) on the network, and many things will become more complex than the previously centralized application environment, for example:

    • How do I ensure that all replicas are synced to the latest state?

    • How can I ensure that all transactions are broadcast to all node computers that run and maintain a copy of the blockchain?

    • How to prevent malicious participants from tampering with blockchain

    • ......

In the course of the following, we will gradually understand the core idea of the central application and how to construct the de-centric application in Ethereum by comparing with the classic C/s architecture.

Fifth section C/S architecture server-centric

The best way to understand a centralized application architecture is to compare it to a familiar Client/Server architecture. If you are a web developer, you should know very well that this is a typical Client/Server architecture:

The service side of a typical Web application is usually implemented by languages such as Java,ruby,python and so on. The front-end code is implemented by Html/css/javascript. Then host the entire app in the cloud, such as AWS, Google cloud Platform, Heroku ..., or on one of your rented VPS hosts.

Clientthe user interacts with the Web App () through the client () Server . Typical clients include browsers, command-line tools ( curl , and wget so on), or API access code. Note In this architecture, there is always one (or set) of centralized Web servers that all clients need to interact with this (group) server. When a client makes a request to the server, the server processes the request, interacts with the database/cache, reads/writes/updates the database, and returns a response to the client.

This is a centralized architecture that we are familiar with. In the next section, we'll see some notable differences in the blockchain-based de-centric architecture.

Sixth: The central architecture--the nodes that are equal to each other

A de-centric application architecture based on Ethereum is given:

You should have noticed that each client (browser) interacts with the respective node application instance rather than requesting a service from a centralized server.

In an ideal de-centric environment, every person who wants to interact with DAPP needs to run a full blockchain node on their computer or phone-in short, everyone runs a full node. This means that users have to download the entire blockchain before they can actually use a de-centralized application.

But we don't live in a utopia, and it's unrealistic to expect every user to run a full node before using your app. But the core idea behind the centrality is that servers that do not rely on a centralized server. As a result, there have been some solutions in the blockchain community, such as the provision of public blockchain nodes Infura , and browser plugins Metamask . With these options, you don't need to spend a lot of hard disk, memory, and time downloading and running full blockchain nodes, but you can also take advantage of the benefits of being centralized. We will evaluate each of these solutions in a future course.

Seventh Ethereum-World computer

Ethereum is the realization of a block chain. In the Ethereum network, numerous nodes are connected to each other, forming the Ethereum network:

Ethereum node software provides two core functions: Data storage, contract code execution.

In each Ethereum full node, a complete blockchain data is stored. Ethereum not only stores the transaction data on the chain, but the compiled contract code is also stored on the chain.

The Ethereum full node also provides a virtual machine to execute the contract code.

Transaction data

Each transaction in the Ethereum is stored on the blockchain. When you deploy a contract, one deployment is a deal. When you vote for a candidate, a vote is another trade. All of these transactions are public and can be seen and verified by everyone. This data can never be tampered with.

To ensure that all nodes in the network have the same copy of the data, and that no invalid data is written to the database, Ethereum currently uses a proof of effort ( POW:Proof Of Work ) algorithm to secure the network, i.e. by mining miners ( Mining ) to reach consensus ( Consensus )-Synchronizing data to all nodes.

Proof of workload is not the only algorithm to reach consensus, and mining is not the only option for blockchain. Now, we just need to understand that consensus means that the data of each node is consistent, POW just one of the many algorithms used to build consensus, and this algorithm needs to be mined by miners to achieve credible transactions in non-trusted environments. Consensus is the purpose, and POW is the means.

Contract Code

Ethereum not only stores transaction data on the chain, it also stores the contract code on the chain.

At the database level, the role of blockchain is to store transaction data. So where do you put the logic to vote for the candidate, or to retrieve the results of the poll? In the Ethereum world, you can use Solidity language to write business logic/application code (i.e. contract: Contract ), then compile the contract code into Ethereum bytecode and deploy the bytecode to the blockchain:

Writing contract code can also be used in other languages, though? Solidity Is by far the most popular choice.

Ethereum Virtual Machine

The Ethereum blockchain not only stores data and code, but each node also contains a virtual machine (Evm:ethereum virtual machines) to execute the contract code-sounding like a computer operating system.

In fact, this is one of the core differences between Ethereum and Bitcoin ( Bitcoin ): The presence of virtual machines has enabled the blockchain to enter the 2.0 era, and has made blockchain the first application developer-friendly platform.

JS Development Library

To facilitate the building of web-based Dapp, Ethereum also provides a handy JavaScript library web3.js that encapsulates the Ethereum node's API protocol, making it easy for developers to connect to blockchain nodes without having to write cumbersome RPC protocol packages. Therefore, we can directly introduce the library in the common JS framework (such as Reactjs, ANGULARJS, etc.) to build the centralized application:

Ethereum Dapp Development Introduction Combat! Build with the node. JS and truffle framework-blockchain voting system!

Related Article

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.