Blockchain Note – EOS DApp Development

In this chapter, we are going to learn about EOS DApp development and I am highly recommend you to understand the following concepts and read through the recommended blogs and tutorials mentioned below.

Step 1: Understand the following concepts

1. What is EOS?

EOS.IO is a blockchain protocol powered by the native cryptocurrency EOS. The protocol emulates most of the attributes of a real computer including hardware (CPU(s) & GPU(s) for processing, local/RAM memory, hard-disk storage) with the computing resources distributed equally among EOS cryptocurrency holders. EOSIO operates as a smart contract platform and decentralized operating system intended for the deployment of industrial-scale decentralized applications through a decentralized autonomous corporation model.

2. What is the difference between EOS vs ETH?

ETH
EOS
Consensus
POW+POS+Casper
BFT+DPOS
Transaction Fee
Gas fee
None
TPS
20
3000

3. What is Graphene Technology?

Graphene technology is the consensus mechanism of DPOS.
The graphene technology specifically refers to the consensus mechanism that EOS adjusts the workload proof (POW) supporting BTC and ETH, and adopts the consensus mechanism of DPOS (Delegated Proof Of Stake).

In the POW mode, all nodes have the opportunity to win the billing rights. The nodes compete with each other. Whoever cracks the answer first can get the billing right. The probability of obtaining a billing right is proportional to the power of the node.

The DPOS mode is changed to a holder of all the tokens of the chain, and a certain number of nodes are elected, and then these nodes cooperate and take turns to record.

In EOS’s DPOS mode, holders of all EOS tokens can obtain votes based on the number of EOS they hold, and vote for 21 block producers (witnesses). Then, the producers of the 21 blocks cooperate with each other and perform bookkeeping in turn in a certain order. The block interval is 3 seconds for a large block. In addition, these 21 block producers not only need to keep accounts, but also need to provide the computing and network resources (including CPU, memory, storage capacity, etc.) required by the EOS chain.

If one of the blocks is paused for a reason such as a network disconnection, a block will occur and the next accounting node will keep up with the accounting. This mechanism guarantees that even if only one node is working at the end, the entire system can still work, and then continue to select new nodes to ensure stable operation.

If a fork occurs in the process, DPOS still adopts the longest chain principle, and it is agreed that each node cannot be out of the two chains at the same time (otherwise the node will be judged to be a violation and disqualified), which makes After the fork is generated, after at most half of the total number of witness nodes (11 blocks in EOS), only one chain is retained.

In addition, there is an irreversible principle under DPOS. Once a block follows a block with more than 2/3 of the total number of witnesses, the block enters an irreversible state, which is equivalent to confirming that the block is a block in the main chain. The status can no longer be rolled back to the block before the block to divide, otherwise it is considered a violation. Because each node participates in at most one chain at the same time, only one block of the same height may only have one irreversible block, which further ensures the security of the block and prevents malicious attacks and malicious forks. In general, in the 21-block generation model, 15 blocks (15/21>2/3) enter the irreversible state (ie, the transaction is 100% safe) for 45 seconds.

Therefore, the advantages of DPOS are:
1) Solved the problem of consuming a lot of power in POW mode.
2) Nodes change from competition to collaboration, and there are only 21 nodes, making it easier to quickly reach consensus and improve the performance of the main chain (TPS).
3) Hard forks are not generated because nodes follow the longest chain principle and each node cannot participate in both chains at the same time. Therefore, after the fork is generated, after at most half of the total number of witness nodes (11 blocks in EOS), only one chain is retained. Every major system upgrade will not lead to hard forks, just need to upgrade all witness nodes at the same time.
4) It is more secure, and it is necessary to control more than 2/3 of the nodes to make the wrong block irreversible.
5) Confirmation is very fast

But the problem with DPOS is that, in essence, DPOS is not decentralized, but weakly centralized. Moreover, as the time of existence of these witness nodes occurs, the degree of centralization becomes higher and higher due to the rewards of participating in the bookkeeping. This can be a potential risk to the entire community. According to previous DPOS-based projects, such as BitShares and Steemit, the final degree of centralization is indeed very high. For a time, most of the witnesses were actually BM themselves or BM-controlled nodes.

To make another derivative, EOS has also improved the basic graphene technology, not only the DPOS consensus, but the consensus mechanism for upgrading to DPOS+BFT. The concept of a cell block is proposed and the Byzantine Fault Tolerance Mechanism (BFT) and irreversible state are added.

EOS makes a big chunk every 3 seconds, and the EOS team has proposed a scheme to continue dividing the 3 second block into several small blocks with an interval of 0.5 seconds. The billing rights are not switched between these small blocks. As soon as each small piece comes out, it will be broadcast immediately. This design allows the blocks to be confirmed more quickly while minimizing the effects of forks due to network delays.

A mechanism for BFT is introduced: whenever a cell block is broadcast within a blockchain network, the remaining nodes will immediately acknowledge the block. Blocks that have been confirmed by 2/3 nodes will enter an irreversible state. Under this consensus mechanism, the security confirmation time is 0.5s (block interval) + 0.3s (block propagation delay) <1s, thus achieving the second-level confirmation performance. In addition, if the block interval of the small block is continued to be divided, it is theoretically confirmed that the required time is approximately equal to the network delay of the block propagation, and may even be less than 0.5 second.

In addition, malicious attacks, unless controlled by more than 2/3 of the nodes, can not enter the irreversible state of the malicious block. Here, it is specifically mentioned that if a malicious node controls more than 1/2 but less than 2/3 of the node, since the malicious block does not enter the irreversible state, this can leave enough time for other observer nodes and witness nodes to be malicious. The node votes out. Therefore, this mode also greatly enhances security.

4. What is assembly?

The EOS DApp developers create applications via WebAssembly (WASM) technology.
WebAssembly is a new high-performance underlying binary language that was launched in 2015 and supported by Google, Microsoft, Apple and other major browser vendors. Other high-level programming languages ​​such as C, C++ and so on can be compiled into WASM format byte code. The most mature toolchain for building applications and WASM compilation is CLang/LLVM and its C/C++ compiler.
WebAssembly is not a direct machine language, but a virtual machine language abstracted out. In the Assembly browser, there will be a piece of Javascript that will re-translate the Web Assembly into Javascript. This technique is called polyfill, a technique commonly used when HTML5 comes out. For browsers that support Web Assembly, translate directly into Native code. From WebAssembly to machine language also needs a “translation” process, but it belongs to machine language to machine language translation, so the speed is very close to machine language and very fast.
If the developer uses C++ development, first translate the C++ code into LLVM intermediate code (IR), and then compile it into a WASM grid.
The byte code is then run on the WASM virtual machine on the EOS platform using the WASM. Along with The languages ​​supported by WebAssembly increasing, the languages ​​supported by EOS can be continuously increased without any modification at all levels.

5. Why using C/C++ ?

C++ is not a panacea, C++ is suitable for the following areas:
1) C++ is suitable for constructing parts with more stable requirements in the program, and the part with large changes in requirements can use scripting language;
2) The program must try to maximize the performance of the hardware, and the performance bottleneck lies in the CPU and memory;
3) Programs must communicate frequently with the operating system or hardware;
4) Programs must use C++ frameworks/libraries, such as most game engines (such as Unreal/Source) and middleware (such as Havok/FMOD). Although some C++ libraries provide bindings for other languages, native APIs usually perform best. up to date;
5) A target platform in the project is only supported by the C++ compiler.

C++ is suitable for developing server software, desktop applications, games, real-time systems, high-performance computing, embedded systems, and more.

Why blockchain projects choose C++?

1). C++’s rich class library has inherent advantages for projects that require high development schedules.
2). C++ has high performance and will not be described here. As far as the database is concerned, most of the projects are written in C or C++.
3). The Bitcoin project is mainly written by C++. The subsequent cottage project will basically fork the bitcoin repo, which will cause the situation of the underlying project C++ to bloom everywhere.
4). In the future, you can see more projects developed in languages ​​such as Go and Java, and other tools such as smart contracts will choose new languages ​​such as Solidity.

6. How does WebAssembly and C/C++ works in EOS?

1).The example code in C
#include <math.h>
float getSqrt (float num) {
    return sqrt(num);
}
2).Compile the C into wasm(WebAssembly’s format)
22922FC8-B83B-482E-950A-FFD8643DE372
Download the program.wasm to the local file.
3).Using JavaScript to upload the .wasm in the browser.
<!doctype html>
<title>WASM Test</title>

  fetch('./program.wasm')
  .then(res => {
    if (res.ok)
      return res.arrayBuffer();
    throw new Error(`Unable to fetch WASM.`);
  })
  .then(bytes => {
    return WebAssembly.compile(bytes);
  })
  .then(module => {
    return WebAssembly.instantiate(module);
  })
  .then(instance => {
    window.wasmSqrt = instance.exports.getSqrt;
  });
4).Execue the function in the web server
python -m SimpleHTTPServer
Open Chrome and goto http://localhost:8000/test.html
6AEB0B58-2E80-4AA9-8DF2-45B773213ED6

7. What is resources of CPU/RAM/NET in EOS ?

The EOS account is an operating system. To use this operating system, it is necessary to configure system resources.
After creating an EOS account, be sure to configure the system resources first.
EOS has three major resources: CPU, NET (network bandwidth) and RAM (memory).
CPU and NET need temporary mortgage EOS acquisition, similar to the deposit, no need to redeem EOS.
RAM can only be purchased directly with EOS and will be consumed during use, but the consumption of this part is negligible compared to the miners’ fees.
The general operation of the user, including the execution of the contract code, lasts for a short period of time on the CPU and network bandwidth, so the EOS uses a take-back mechanism in exchange for the collateral as EOS Token. For example, a mortgage of 1% Token can get up to 1% of network bandwidth. Just like you pay a deposit with a shared bicycle, you can redeem it without a deposit.
The allocation of RAM is slightly different. It is stored in the state and is occupied for a long time. Simply put, the state generated according to the data on the chain is stored in RAM. For example, the information of an account, including the current state of the balance, is stored in RAM, so it is necessary to purchase RAM to store data for a long time.
Note: When the account has no resources, it is not allowed to buy RAM or mortgage for NET and CPU. Because this is a transaction itself, it needs to consume resources. The first operation needs to be done through other accounts.
Any wallet can be directly mortgaged to the CPU and NET and purchased RAM.
Basic configuration recommended: CPU: 0.5EOS, RAM: 0.1EOS, NET: 0.1EOS, this configuration can meet normal trading needs.
What if I forgot to configure the resources and the CPU or RAM is completely consumed?
Use EOS resource free temporary mortgage tools.
Trading on RAM, the trading mechanism uses Bancor algorithm, which adjusts the price of RAM through market supply and demand. If the RAM supply exceeds demand, it does not need too much EOS when buying RAM, and it will not sell when RAM is sold. Get too much EOS, and if RAM is in short supply, just the opposite.
The following operations are required to consume RAM: create a new EOS account, create a contract, transfer money to other accounts, buy and sell RAM, mortgage to get CPU or NET, redeem mortgages, and more. When the user operates this operation, a certain amount of RAM is required in the account, otherwise the operation cannot be completed.
In EOS, CPU and NET acquisition differs from RAM in that it allocates the corresponding CPU and NET bandwidth according to the proportion of mortgage EOS through collateral.
If you want to release the NET and CPU you already own, you can redeem the mortgaged EOS by redemption, but you need to wait a certain amount of time to get the account.
NET and CPU are used to ensure that users use the basic functions of the EOS network, including transfer, execution of smart contracts, etc. The more operations are performed in a unit of time, the more CPU and NET are consumed, but unlike RAM, Over time, the CPU and NET are automatically released, which means that the CPU and NET are not consumable resources.

8. EOS architecture

3D9961DA-C1EA-46AF-9B04-418B973C29FE

By comparing traditional web service modules, it is easier to understand the purpose of each module.
EOS: eosd
Web: Database

Data storage is provided, except that eosd is a state-based blockchain data storage method, and the state of the smart contract is modified through the transaction.
EOS: Query Services + GraphQL
Web: REST & Graph QL & Microservices

EOS should package user account management, transfer, etc. into micro-services; GraphQL is relatively easy to use in the React framework. GraphQL is an application-level query language proposed by Facebook. With GraphQL, you can define your backend based on the graph pattern. Then the client can request the required data set.
EOS:Client (React.js)
Web: front end

Because of the default integration of GraphQL in the EOS architecture, the front end using React.js will be the best framework choice, so developers who intend to develop applications for EOS can pay more attention to React.
EOS: IPFS File Storage
Web: File Storage

Analogy to the current Internet application architecture model is more convenient for developers to get started, but also provides an experience payment model similar to the current Internet application is currently not clear, but compared with Ethereum’s computationally paid model compared to EOS, the model currently described is very complex and Dynamic, because EOS not only needs to limit computing, but also limits bandwidth, storage, etc. Finally, it is mentioned that application developers can provide out-of-chain data interfaces through a similar architecture to achieve high scalability of applications.

The holders of EOS voted for each super-node candidate through the voting system, and selected 21 nodes as super nodes. Then the 21 super nodes negotiate a block right ownership order with their own network resource status. When each super node has a block right, after the super node A generates the first new block, A signs the block and Broadcast to other super nodes. Other super nodes will verify the block and then sign it back to the A node. When the A node receives the block from 14 different node signatures, the block becomes an irreversible block. Concatenate into the previous blockchain. The constitution of the EOS community is mainly based on the basic principles established to regulate the differences among community members. Since many blockchain projects currently have internal differences in the development process, the blockchain system cannot be effectively evolved, so it must be at the beginning of the project. The evolutionary principles of setting up a project make EOS a chain that can continue to evolve to meet the needs of market and technology development. When EOS development requires a fork and upgrade contract, the EOS code can be modified and upgraded according to constitutional rules.

At the contract level EOS integrates the virtual machine with the EOS through the Open RPC (Remote Procedure Call) interface, and the implementation of the scripting language and virtual machine will be independent of the EOS operating system technology, as long as any development language or virtual machine is appropriate. Sandboxes with sufficient performance can be integrated with EOS via RPC. And EOS can now support Wren, WASM, EVM three virtual machines, so the application on Ethereum can be directly transplanted to the EOS system through simple modification. Due to the separation of virtual machines and EOS, developers can choose their own skilled programming language for smart contract development, which makes application development on EOS more flexible, which greatly reduces the barriers to the use of blockchain technology.

Step 2: Learning EOS DApp tutorial

Learning https://battles.eos.io/ Best free EOS DApp Card Game tutorial (Code)

Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s