Getting started with Ethereum DApp development! Built with Node.js and truffle framework - blockchain voting system!

Overview of the first course

This course is aimed at beginners, covering the basic concepts related to Ethereum development, and will teach you how to build a complete decentralized application based on Ethereum - blockchain voting system.

ethereum logo

Through the study of this course, you will master:

  • The Basics of Ethereum Blockchain

  • The software environment required to develop and deploy Ethereum contracts

  • solidityWrite an Ethereum contract using a high-level language ( )

  • Compile, deploy and interact with contracts using NodeJS

  • Develop distributed applications using Truffleframeworks

  • Interact with the contract using the console or web page

Pre-order knowledge requirements

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

  • An object-oriented development language such as: Python, Ruby, Java...

  • Front-end development language: HTML/CSS/JavaScript

  • Use of Linxu command line

  • Basic Concepts of Databases

All code for the course has been tested on Ubuntu (Trusty, Xenial) and macOS.

Introduction to the second course

In this course, we will build a decentralized ( Decentralized) voting application. Using this voting application, users can trustlessvote for specific candidates in an untrusted ( ) distributed environment, and each vote will be recorded on the blockchain:

voting dapp

The so-called decentralized application ( DApp: Dcentralized Application) is an application without a central server. Copies of the application can be running on hundreds or thousands of computers in the network, making it nearly impossible to crash.

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

Section 3 Development Iteration

This course will cover the entire process of application development, and we will gradually introduce relevant concepts, languages ​​and tools involved in blockchain application development through three iterations:

voting dapp iteration

  • Vanilla: In the first iteration cycle, we did not use any development framework, but only used NodeJS for application development, which helped us better understand the core concepts of blockchain applications.

  • Truffle: In the second iteration, we will use the most popular decentralized application development framework Trufflefor development. Using a development framework helps us improve development efficiency.

  • Token: In the third iteration, we will introduce a token ( Token) for the voting application - now everyone calls it a token - it's all to ICOblame. Tokens are an indispensable incentive mechanism on the public chain, and it is also another distinguishing feature that distinguishes blockchain applications from traditional centralized applications.

Why Choose the Voting App as a Course Project?

Voting was chosen as our first blockchain application because collective decision-making—especially the voting mechanism—is a core value proposition of Ethereum.

Another reason is that voting is the basic component of many complex decentralized applications, so we chose the voting application as the first project to learn blockchain application development.

Section 4 Getting to Know the Blockchain

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

relation database

Essentially, a blockchain is first and foremost a distributed ( Distributed) database that maintains an ever-growing list of records. Now, let's batchstore the data in batches ( ), say 100 rows per batch, and connect the storage batches together, is it like a chain?

chain

In the blockchain, a batch of multiple data records is called a block ( block), and each row of data records in a block is called a transaction ( transaction):

basic blockchain

The very first block, usually called the genesis block ( genesis block), does not point to any other block.

Immutable

A distinctive feature of blockchain is that once data is written into the chain, it cannot be tampered with and rewritten.

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

So, how does the blockchain realize the immutability of data?

This is first thanks to the hash ( Hash) function - if you haven't touched the hash function, you might as well think of it as a digital fingerprint calculation function: input content of any length, and output a fixed-length code stream (fingerprint). An important property of hash functions is that any small change in the input will result in a change in the output. Therefore, the hash value can be used as the fingerprint of the content. You can learn more about hash functions by clicking here .

Since each block in the blockchain stores the hash value of the content of the previous block, if the content of any block is tampered with, the hash values ​​of all blocks after the tampered block will also change, so we It is easy to detect whether each block of the blockchain has been tampered with.

The challenge of decentralization

Once fully decentralized, there will be a large number of blockchain copies (ie: full nodes) on the network, and many things will become much more complicated than the previous centralized application environment, such as:

  • How can I guarantee that all replicas are synced to the latest state?

  • How to ensure that all transactions are broadcast to all node computers that run and maintain copies of the blockchain?

  • How to prevent malicious actors from tampering with the blockchain

  • ......

In the following courses, by comparing with the classic C/S architecture, we will gradually understand the core ideas of decentralized applications, and master how to build decentralized applications on Ethereum.

Section 5 C/S Architecture is Server-centric

The best way to understand the architecture of a decentralized application is to compare it to a familiar Client/Serverarchitecture. If you are a webdeveloper, you should be familiar with the following diagram, which is a typical Client/Serverarchitecture:

webapp architecture

The server side of a typical web application is usually implemented in languages ​​such as Java, Ruby, Python, etc. Front-end code is implemented by HTML/CSS/JavaScript. Then host the entire application in the cloud, such as AWS, Google Cloud Platform, Heroku.... or on a VPS host you rent.

ClientThe user interacts with the web application ( ) through the client ( ) Server. Typical clients include browsers, command line tools ( curl, wgetetc.), or APIaccess code. Note that in this architecture, there is always one (or group) of centralized web servers that all clients need to interact with. 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 the centralized architecture we are familiar with. In the next section, we will see some notable differences between blockchain-based decentralized architectures.

Section 6 Decentralized Architecture - Nodes Equal to Each Other

The following figure shows the decentralized application architecture based on Ethereum:

ethereum architecture

You should have noticed that each client (browser) interacts with its own instance of the node application, rather than requesting services from a centralized server.

In an ideal decentralized environment, everyone who wants to interact with a DApp would need to run a full blockchain node on their computer or mobile phone — in short, everyone runs a full node . This means that users have to download the entire blockchain before they can actually use a decentralized application.

But we don't live in a utopia, and expecting every user to run a full node before using your application is unrealistic. But the core idea behind decentralization is not to rely on centralized servers. Therefore, some solutions have emerged in the blockchain community, such as providing public blockchain nodes Infura, and browser plug-ins Metamask. With these solutions, you don't need to spend a lot of hard disk, memory and time to download and run a full blockchain node, and you can also take advantage of decentralization. We will evaluate each of these solutions separately in future courses.

Section 7 Ethereum - The World Computer

Ethereum is an implementation of a blockchain. In the Ethereum network, numerous nodes are connected to each other to form the Ethereum network:

ethereum

The Ethereum node software provides two core functions: data storage and contract code execution.

In each Ethereum full node, complete blockchain data is stored. Not only does Ethereum store transaction data on the chain, but the compiled contract code is also stored on the chain.

In the Ethereum full node, a virtual machine is also provided to execute the contract code.

transaction data

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

In order to ensure that all nodes in the network have the same copy of data and no invalid data is written to the database, Ethereum currently uses the proof-of-work  ( POW:Proof Of Work) algorithm to ensure network security, that is, miners mine ( Mining) to reach consensus ( Consensus) - Synchronize data to all nodes.

Proof of Work is not the only algorithm for reaching consensus, nor is mining the only option for blockchains. Now, we only need to understand that consensus means that the data of each node is consistent, which POWis just one of many algorithms used to establish consensus. This algorithm needs to be mined by miners to achieve credibility in an untrusted environment. trade. Consensus is the end, POW is the means.

contract code

Ethereum doesn’t just store transaction data on-chain, it can also store contract code on-chain.

At the database level, the role of the blockchain is to store transaction data. So where is the logic for voting for candidates, or retrieving vote results? In the Ethereum world, you can use the Soliditylanguage to write business logic/application code (aka contract: Contract), then compile the contract code to Ethereum bytecode, and deploy the bytecode to the blockchain:

contract development and deploy

Writing contract code can also be done in other languages, but  Solidityis by far the most popular option.

Ethereum Virtual Machine

The Ethereum blockchain not only stores data and code, but also contains a virtual machine (EVM: Ethereum Virtual Machine) in each node to execute contract code - it sounds like a computer operating system.

In fact, this is the core point that distinguishes Ethereum from Bitcoin ( Bitcoin): the existence of virtual machines has brought the blockchain into the 2.0 era, and it has also made the blockchain a friendly platform for application developers for the first time.

JS development library

In order to facilitate the construction of web-based DApps, Ethereum also provides a very convenient JavaScript library web3.js, which encapsulates the API protocol of Ethereum nodes, so that developers can easily connect to blockchain nodes without having to write cumbersome RPCprotocol packages . Therefore, we can directly introduce this library into common JS frameworks (such as reactjs, angularjs, etc.) to build decentralized applications:

web3.js

Course address: http://xc.hubwiz.com/course/5a952991adb3847553d205d1?affid=oschinaw

Guess you like

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