Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build your first blockchain application

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to build your first blockchain application, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

When it comes to blockchain, people naturally think of bitcoin. Bitcoin may or may not be a bubble, but the blockchain technology behind Bitcoin is by no means a bubble and highly subversive. Today, all the tech giants are accumulating blockchain technology and patents. The building is full of rain and rain, and the outbreak of the application scene of blockchain technology is imminent, if not at present. The salary of blockchain developers has also reached the top of the pyramid, which is the real reason why we want to learn blockchain technology.

This article will first introduce the key technical points behind Bitcoin and blockchain, then explain the improvements of blockchain technology made by the Ethernet Square intelligent contract, and finally explain how to use the super ledger project to create a simple blockchain bank transfer system.

The blockchain bank transfer system has been simultaneously released in the application store of [Xingyun interest Code Development Cloud platform]. Before starting the next reading, you can log in to the platform to deploy and experience the system.

Starting with General Byzantium.

General Byzantium (Byzantine Generals Problem) problem is a fictional model proposed by Leslie Lamport in 1982 to explain the consistency problem. Byzantium is the capital of the ancient Eastern Roman Empire. Because of its vast territory, many generals guarding the border (multiple nodes in the system) need to send messages through messengers and reach some unanimous decisions. However, because there may be traitors among the generals (node errors in the system), these traitors will try to send different messages to different generals in an attempt to interfere with consistency.

The question of Byzantium is how to get loyal generals to agree on their actions under such circumstances. This is one of the core problems of distributed systems. The reason why the Byzantine problem is difficult to solve is that there may be multiple proposals in the system at any time (because the cost of the proposal is very low), and it is very difficult and easy to interfere to complete the final consistency confirmation process. But once confirmed, it is final confirmation.

Mining machines in Bitcoin Network

The only function of the Bitcoin network is "money transfer", in which the maximum number of bitcoins is set to 21 million. How did these bitcoins be dug up? The answer is bookkeeping reward. The mining machine (computer) in the Bitcoin network is the bookkeeper, who gets a certain Bitcoin reward for each bookkeeping, commonly known as mining income.

The rate of Bitcoin production halves every four years, so mining will get less and less bitcoin up to zero. After that, it is necessary to charge the transfer fee from the transferor to maintain the operation of the mining machine, just like the ordinary commercial bank charges the user transfer fee.

The role of the mining machine in the Bitcoin network is equivalent to that of the general in the Byzantine problem. When a user initiates a bitcoin transfer, the miner tries to reach a consensus on the deal, which, like the Byzantine generals, requires a miner to initiate a proposal and a miner to vote (endorse). In the Bitcoin network, only the proposed miner can earn mining revenue. Who will get the chance to launch a proposal?

In the design of Bitcoin blockchain network, an innovative PoW (Proof of Work) algorithm is proposed. One is to limit the number of proposals in the entire network over a period of time (to increase the cost of proposals), and the other is to relax the need for final conformance confirmation and agree that everyone should confirm it and broaden it along the longest known chain. The final confirmation of the system is the existence in the sense of probability. In this way, even if someone tries to sabotage it, it will pay a high economic price (more than half of the computing power of the system).

The mining machines are competing for the opportunity of the proposal, they solve the same math problem at the same time, and the mining machine that is the first to hand in the paper will win the opportunity of this proposal. The math problems (schematic version) are as follows:

Hash (hash value of the previous block + random number)

< 00000000000FFFFFFFFFFFFFFFFFFFFF 比赛开始后,矿机把上一个区块的hash值加上不断尝试的随机数,重新计算hash,直到hash的结果小于某个值时,求解成功。比特币网络会自动调整求解难度,即调整不等式右边hash值的大小,把提案频率控制在10分钟左右。 以太坊的智能合约 智能合约(Smart Contract)是以太坊中最为重要的一个概念,即以计算机程序的方式来缔结和运行各种合约。最早在上世纪 90 年代,Nick Szabo 等人就提出过类似的概念,但一直依赖因为缺乏可靠执行智能合约的环境,而被作为一种理论设计。区块链技术的出现,恰好补充了这一缺陷。 智能合约作为运行在以太坊虚拟机(Ethereum Virtual Machine,EVM)中的应用,可以接受来自外部的交易请求和事件,通过触发运行提前编写好的代码逻辑,进一步生成新的交易和事件,可以进一步调用其它智能合约。智能合约的执行结果可能对以太坊网络上的账本状态进行更新。这些修改由于经过了以太坊网络中的共识,一旦确认后无法被伪造和篡改。 比特币网络中的转账和以太坊的智能合约,类似功能机和智能手机,前者只能运行预置的软件(比特币网络的转账),后者可以安装各种APP (以太坊的智能合约)。智能手机带来的颠覆无需赘言,同样,只需看一下ICO市场的躁动就能窥见智能合约带来的改变。 私有链和超级账本项目 由IBM和Linux基金会主导的超级账本(Hyperledger)项目,是开发私有链和联盟链的不二之选。该项目下有很多子项目,比如最重要的Fabric项目,提供区块链运行的基础支撑;Composer项目简化了智能合约(Hyperledger中称为chaincode)的开发和部署。

At the just-concluded ThoughtWorks technology radar summit, ThoughtWorks also listed Hyperledger as the recommended framework.

Using super ledger to build block chain bank transfer system

The blockchain network part of the system includes a CA node (certificate issuance and management system), an Orderer node (Proposer in distributed decision-making), three Peer nodes (used as credit endorsement) and three corresponding databases (storage World State). In addition, there are two business nodes, which are responsible for defining and implementing the business, and deploy the service in the Fabric network, providing RESTful interface and UI at the same time. The system is created for the purpose of technical demonstration, so it does not have many functions in complete banking applications, such as accounts, passwords and so on. As shown in the following figure:

The blockchain system has been released synchronously in the application factory of [Xingyun interest Code Development Cloud platform]. Before starting the next reading, you can create a project and select "blockchain Banking system" in the template. It only takes a few minutes to deploy everything to the cloud and quickly feel the applications based on blockchain technology.

Http://factory.cloudtogo.cn/

The following are the specific steps for the development of the system.

First, install Fabric and Composer

Prepare a Ubuntu16.04 virtual machine and execute the following script as a non-root user to install the runtime environment that Fabric and Composer depend on.

#! / bin/bash

#. / prereqs-ubuntu.sh

# Exit on any failure

Set-e

# Array of supported versions

Declare-a versions= ('trusty'' xenial' 'yakkety')

# check the version and extract codename of ubuntu if release codename not provided by user

If [- z "$1" >

Source / etc/lsb-release | |\

(echo "Error: Release information not found"; exit 1)

CODENAME=$ {DISTRIB_CODENAME}

Else

CODENAME=$ {1}

Fi

# check version is supported

If echo ${versions [@]} | grep-Q-w ${CODENAME}; then

Echo "Installing Hyperledger Composer prereqs for Ubuntu ${CODENAME}"

Else

Echo "Error: Ubuntu ${CODENAME} is not supported"

Exit 1

Fi

# Update package lists

Echo "# Updating package lists"

Sudo apt-add-repository-y ppa:git-core/ppa

Sudo apt-get update

# Install Git

Echo "# Installing Git"

Sudo apt-get install-y git

# Install nvm dependencies

Echo "# Installing nvm dependencies"

Sudo apt-get-y install build-essential libssl-dev

# Execute nvm installation script

Echo "# Executing nvm installation script"

Curl-o-https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

# Set up nvm environment without restarting the shell

Export NVM_DIR= "${HOME} / .nvm"

[- s "${NVM_DIR} / nvm.sh"] & &. "${NVM_DIR} / nvm.sh"

[- s "${NVM_DIR} / bash_completion"] & &. "${NVM_DIR} / bash_completion"

# Install node

Echo "# Installing nodeJS"

Nvm install-lts

# Configure nvm to use version 6.9.5

Nvm use-lts

Nvm alias default 'lts/*'

# Install the latest version of npm

Echo "# Installing npm"

Npm install npm@latest-g

# Ensure that CA certificates are installed

Sudo apt-get-y install apt-transport-https ca-certificates

# Add Docker repository key to APT keychain

Curl-fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add-

# Update where APT will search for Docker Packages

Echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu ${CODENAME} stable" |\

Sudo tee / etc/apt/sources.list.d/docker.list

# Update package lists

Sudo apt-get update

# Verifies APT is pulling from the correct Repository

Sudo apt-cache policy docker-ce

# Install kernel packages which allows us to use aufs storage driver if V14 (trusty/utopic)

If ["${CODENAME}" = = "trusty"]; then

Echo "# Installing required kernel packages"

Sudo apt-get-y install linux-image-extra-$ (uname-r) linux-image-extra-virtual

Fi

# Install Docker

Echo "# Installing Docker"

Sudo apt-get-y install docker-ce

# Add user account to the docker group

Sudo usermod-aG docker $(whoami)

# Install docker compose

Echo "# Installing Docker-Compose"

Sudo curl-L\

"https://github.com/docker/compose/releases/download/1.13.0/docker-compose-$(uname-s)-$(uname-m)"\

-o / usr/local/bin/docker-compose

Sudo chmod + x / usr/local/bin/docker-compose

# Install python v2 if required

Set + e

COUNT= "$(python-V 2 > & 1 | grep-c 2.)"

If [${COUNT}-ne 1]

Then

Sudo apt-get install-y python-minimal

Fi

# Install unzip, required to install hyperledger fabric.

Sudo apt-get-y install unzip

# Print installation details for user

Echo''

Echo 'Installation completed, versions installed are:'

Echo''

Echo-n 'Node:'

Node-version

Echo-n 'npm:'

Npm-version

Echo-n 'Docker:'

Docker-version

Echo-n 'Docker Compose:'

Docker-compose-version

Echo-n 'Python:'

Python-V

# Print reminder of need to logout in order for these changes to take effect!

Echo''

Echo "Please logout then login before continuing."

After the above script is executed, log out and log back in. Execute the following script to install Composer (Composer will install and start the Fabric network).

#! / bin/bash

#. / install-composer.sh

Npm install-g composer-cli

Npm install-g composer-rest-server

Npm install-g generator-hyperledger-composer

Npm install-g yo

Npm install-g composer-playground

Mkdir ~ / fabric-dev-servers & & cd ~ / fabric-dev-servers

Curl-O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.tar.gz

Tar-xvf fabric-dev-servers.tar.gz

Cd ~ / fabric-dev-servers

. / downloadFabric.sh

. / startFabric.sh

. / createPeerAdminCard.sh

At this point, execute docker ps to see the container in which the Fabric network is running.

Second, create the business network, write the service code (chaincode) and package

1. Planning business network

$yo hyperledger-composer

> Y

> Business Network

> test-bank

> Basic Bank App

> jack

> jack@cloudtogo.cn

> [enter]

> test

> No: generate a populated sample network

$cd ~ / test-bank

$rm test/logic.js

two。 Write business code, change the content of models/test.cto to the content of

Namespace test

Asset Account identified by accountId {

O String accountId

-> Customer owner

O Double balance

}

Participant Customer identified by customerId {

O String customerId

O String firstName

O String lastName

}

Transaction AccountTransfer {

-> Account from

-> Account to

O Double amount

}

Modify the content of lib/logic.js to:

Function accountTransfer (accountTransfer) {

If (accountTransfer.from.balance

< accountTransfer.amount) { throw new Error ("Insufficient funds"); } accountTransfer.from.balance -= accountTransfer.amount; accountTransfer.to.balance += accountTransfer.amount; return getAssetRegistry('test.Account') .then (function (assetRegistry) { return assetRegistry.update(accountTransfer.from); }) .then (function () { return getAssetRegistry('test.Account'); }) .then(function (assetRegistry) { return assetRegistry.update(accountTransfer.to); }); } 3. 更新ACL并打包成可部署的格式 $ vim permissions.acl >

:% s/SampleParticipant/Customer/g

>:% s/SampleTransaction/AccountTransfer/g

>:% s/SampleAsset/Account/g

>: qw

$composer archive create-t dir-n.

Third, deploy to Fabric network

$composer network install-card PeerAdmin@hlfv1-archiveFile test-bank@0.0.1.bna

$composer network start-networkName test-bank-networkVersion 0.0.1\

-card PeerAdmin@hlfv1-- networkAdmin admin-- networkAdminEnrollSecret adminpw-- file networkadmin.card

$composer card import-file networkadmin.card

$composer network ping-card admin@test-bank

4. Generate RESTful API

$composer-rest-server

> admin@test-bank

> always use namespaces

> N

> N

> Y

> N

At this point, the entire blockchain bank transfer system has been deployed, and you can visit http://vmip:3000/explorer to create users and accounts and initiate transfer operations.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report