近几年区块链概念越来越火,特别是区块链技术被纳入国家基础设施建设名单后,各大企业也开始招兵买马,对区块链技术进行研究,从各大招聘网站的区块链职位来看,薪资待遇都很不错,月薪30K到80K的都有,这对于我们程序员来说也是一种机遇,说明学习区块链技术刻不容缓。
In recent years, the concept of block chains has gained momentum, particularly with the incorporation of block chain technology into the national infrastructure development list, and major enterprises have also begun to recruit and to study block chain technology. In terms of the number of block chain posts in the major recruitment sites, the pay is very good, with a monthly salary of between 30K and 80K, which is also an opportunity for our programmers to demonstrate the urgency of learning block chain technology.
我个人从2016年就开始在做区块链系统开发的相关工作,最近身边很多朋友都在找我,想让我给他们讲讲区块链技术开发的相关知识,介于此,索性我就手撸了一套简单的java区块链系统,并写了一份详细的开发教程,发布到博客,希望能够对热爱区块链技术的小伙伴学习入门有所帮助。
I have personally been working on block chain development since 2016, and a lot of my friends have been looking for me recently, asking me to share with them knowledge about the development of block chain technology. In this way, I have developed a simple Java block chain system, and have written a detailed development curriculum, published on my blog, in the hope that it will be useful to learn an introduction to block chain technology for small partners who love it.
这套区块链系统代码非常简洁清晰,对于新手来说非常好理解,旨在告诉大家其实区块链技术并没有那么高深复杂。系统中除了springboot框架外,其他基本都是纯原生开发,就连P2P网络也是用的java socket来实现的。
The code for the block chain system is very simple and well understood for newcomers to tell you that the block chain technology is not as sophisticated as it is. The system, apart from the Springboot framework, is essentially pure development, and even the P2P network is made possible by java socket.
(1)区块链
(1) block chain
从技术层面来看,区块链是由包含交易信息的区块按照时间顺序从后向前有序链接起来的数据结构。
At the technical level, the block chain is the data structure of blocks containing transactional information that are linked in chronological order from the point forward.
从应用层面来说,区块链是一个分布式的共享账本和数据库,具有去中心化、不可篡改、全程留痕、集体维护、公开透明等特点。基于这些特点,区块链技术可以开发出自带信任体系特征的系统,实现多个主体之间的协作信任与一致行动。
At the application level, the block chain is a distributed and shared account book and database with decentralised, non-alterable, full-range marks, collective maintenance, and open transparency features. On the basis of these features, block chain technology can develop systems that are characterized by trust systems and achieve collaborative trust and concerted action among multiple subjects.
区块是区块链中的最小组成单位,主要有包含元数据的区块头和存放一条或者多条交易信息的区块体两部分组成,每个区块都记录着当前区块的哈希和上一个区块的哈希,通过两个哈希值的关联,让所有的区块以链式结构串起来,就形成了一个完整的区块链。
Blocks are the smallest constituent units of the block chain, consisting mainly of blocks containing metadata and blocks containing one or more transactional information, each of which records Hashi in the current block and Hashi in the previous block, and which, through the association of the two Hashi values, bind all blocks into a chain structure, forming a complete chain of blocks.
区块链中的第一个区块被称作为创世区块,无需关联上一个区块。以BTC网络为例,每个区块主要包含如下信息字段:
The first block in the block chain is called a creation block and does not need to be associated with the previous block. For example, each block contains the following information fields:
区块大小:用字节表示的区块数据大小
Block size: the size of block data in bytes
区块头:组成区块头的包括以下几个字段:
Blockhead: The component block shall consist of the following fields:
1. 区块头hash值
1. Hash value for block head
2. 父区块头hash值
2. Hash value for head of parent block
3. 时间戳:区块产生的近似时间
Time stamp: approximate time generated by blocks
4. Merkle根:该区块中交易的merkle树根的哈希值
4. Merklegen: Hashi value of Merkle root traded in the block
5. 难度目标:该区块工作量证明算法的难度目标
5. Difficulty objective: the difficulty target of the workload certification algorithm for the block
6. Nonce:用于工作量证明算法的计数器
Nonce: counter for workload certification algorithms
交易计数器:交易的数量
Transaction counter: number of transactions
交易:记录在区块里的交易信息
Transactions: transaction information recorded in blocks
区块链结构的简易模型,如下图所示:
A simple model of the structure of the block chain, as shown in the figure below:
区块中的交易集合记录的是一些特定的信息,在BTC网络中主要记录的是交易信息,在其他区块链网络中可以按照业务逻辑来保存相应的业务数据,如审计信息、版权信息、票据信息等,这也是区块链经常用来当做共享账本的原因。
The pool of transactions in blocks contains specific information, which is mainly recorded in the BTC network, and the corresponding business data, such as audit information, copyright information, paper information, etc., can be kept in other block chain networks in accordance with business logic, which is also the reason why block chains are often used as shared ledgers.
打个比方,可以把区块链当做一个用来记账的笔记本,一个区块就相当于一页纸,上面记录了某一时间段內所有的账务信息,从第一页到最后一页,按照页码顺序排列起来就是一个完整的账本。
For example, the block chain can be used as a notebook for accounting purposes, and a block is equivalent to a page of paper that records all the account information for a given period of time, from the first to the last page, which is a complete account book in the order of the page number.
(2)区块链网络
(2) block chain network
实际的区块链系统由多个区块链节点组成,每个节点都运行着相同一套区块链主干网络的副本,且各个节点间通过P2P网络进行交互,并最终形成一个完整的区块链网络系统。
The actual block chain system consists of multiple block chain nodes, each running a copy of a backbone network of the same block chain, interacting between the various nodes through the P2P network and eventually forming a complete block chain network system.
P2P网络具有可靠性、去中心化,以及开放性,各个节点之间交互运作、协同处理,每个节点在对外提供服务的同时也使用网络中其他节点所提供的服务。当某一个区块链节点产生新的区块时,会通过广播的方式告诉其他节点,其他节点通过网络接收到该区块信息时,会对这个区块信息进行验证,当有一定数量的节点都验证通过后,各个节点会把该区块更新到各自现有的区块链上,最终使得整个区块链网络中的各个节点信息保持一致,这也是区块链去中心化、可信任特性的体现。
The P2P network is reliable, decentralized, and open, the various nodes interact and co-operate with each other, each of which provides external services while using the services provided by other nodes in the network. When a new block is created, the other nodes are informed by broadcast, and when the information is received through the network, the information on the block is validated. When a certain number of nodes are validated, the nodes are updated to their existing node chains, ultimately aligning the information of the nodes in the entire network of blocks, which is also a reflection of the centralization and trustability of the block chain.
区块链网络简易模型,如下图所示:
A simple model of the block chain network, as shown in the figure below:
(1)公有链
(1) public chain
公有区块链(Public Block Chains)是指:世界上任何个体或者团体都可以发送交易,且交易能够获得该区块链的有效确认,任何人都可以参与使用和维护该区块链,信息公开透明。公有区块链是最早的区块链,例如BTC、以太坊等虚拟数字货币均基于公有区块链。不过目前公有链实际应用价值不大,并没有产生特别合适的应用场景。
The public block chain (Public Block Chains) means that any individual or group in the world can send a transaction and that the transaction can be validated effectively in the block chain, that anyone can participate in the use and maintenance of the block chain and that the information is open and transparent. The public block chain is the first block chain, such as BTC, Ether, and other virtual digital currencies based on the public block chain.
(2)联盟链
(2) Union chain
行业区块链(Consortium Block Chains):由某个群体内部指定多个预选的节点为记账人,每个块的生成由所有的预选节点共同决定(预选节点参与共识过程),其他接入节点可以参与交易,但有权限限制,信息受保护,如银联组织。目前联盟链是各个区块链技术团队主要研究的对象,由于联盟链拥有区块链技术的大部分特征,并且在权限管理、数据安全、监管方面更有优势,是企业优先考虑的区块链技术方案。
Industry block chains (Consortium Block Chains): Each block is generated jointly by all preselected nodes (preselected nodes are involved in the consensus process) and other access nodes can be involved in the transaction, but there are limitations on access and information is protected, such as the Silver Union Organization. The Union chain is currently the subject of major research by technical teams in each sector chain, given that the Union chain has most of the characteristics of the block chain technology and has greater advantages in terms of authority management, data security and regulation.
市面上也有一些比较主流的联盟链技术框架,让开发维护联盟链更加便捷。国内一些大的软件厂商也都有自己的企业区块链技术解决方案,例如蚂蚁金服区块链平台,腾讯的TrustSQL平台,东软的SaCa EchoTrust区块链应用平台以及京东区块链防伪追溯平台等等。
There are more mainstream union chain technical frameworks on the market, which make it easier to develop and maintain the union chain. Some large software manufacturers in the country also have their own business block chain technical solutions, such as the ants gold-save platform, the trustSQL platform, the SaCa EchoTrust block chain application platform in the East soft, and the Kyoto East block chain anti-false-retrospection platform.
(3)私有链
私有区块链(Private Block Chains):仅仅使用区块链的总账技术进行记账,可以是一个公司,也可以是个人,独享该区块链的写入权限,利用区块链的不易篡改特性,把区块链作为账本数据库来使用。
Private Block Chain (Private Block Chains): The use of the block chain master record-keeping technique alone can be a company or individual, with exclusive access to the block chain, using the block chain as an easy-to-manufacture feature and using the block chain as an account database.
(1)共识机制
(1) Consensus Mechanism
共识机制被称作为区块链系统的灵魂,是区块链系统信任体系的基础。区块链系统作为一个多节点的分布式账本系统,当有新的信息需要记录时,哪个节点来负责记账,记账奖励发放给哪个节点,哪些节点负责验证记账结果,如何让各个节点达成最终一致,将记账结果被网络中所有节点以同样的顺序复制并记录下来,就是共识机制要做的事情。
The Consensus mechanism is called the soul of the block chain system and the foundation of the system of trust in the block chain system. The block chain system, as a distributed account system for multiple nodes, is responsible for recording accounts when new information is available, which node is awarded, which node is responsible for validating the account results, how the node is finally agreed, and the log results are copied and recorded in the same order by all nodes in the network, which is what the consensus mechanism is supposed to do.
而按照百度百科上的说法:
And according to the encyclopedia:
目前,较为主流的共识算法有PoW、PoS、DPoS、PBFT等,在实际使用时,每种算法都有各自的优点和缺点。在应用于不同场景时,区块链项目将会采用不同的共识机制和算法。
At present, the more dominant consensus algorithms are PoW, PoS, DPOS, PBFT, etc. Each algorithm has its own strengths and weaknesses when used in practice. When applied to different scenarios, block chain projects will use different consensus mechanisms and algorithms.
(2)去中心化
去中心化,是互联网发展过程中形成的社会关系形态和内容产生形态,是相对于“中心化”而言的新型网络内容生产过程。在一个分布有众多节点的区块链系统中,每个节点都具有高度自治的特征。任何一个节点都可能成为阶段性的中心,但不具备强制性的中心控制功能。节点与节点之间的影响,会通过网络而形成关联关系。这种开放式、扁平化、平等性的系统现象或结构,我们称之为去中心化。
Decentralization is the form of social relations and content generation in the development of the Internet, and the production of new forms of network content relative to “centreization.” In a block chain system with numerous nodes, each node is characterized by a high degree of autonomy. Any node can become a centre of stages, but it does not have a mandatory central control function. The influence between nodes and nodes can be linked through the network. This open, flat, egalitarian systemic phenomenon or structure, which we call decentralization.
去中心化的系统具有容错力高、抗攻击力强的特征。中心化的系统一旦中心出现问题,整个系统都会崩溃,但是区块链系统中的任何一个节点出现问题,并不会对整个区块链网络产生太大的影响。
Decentralized systems have faulty, aggressive features. Centralized systems collapse when they have problems at the centre, but problems at any node in the block chain system do not have much impact on the entire block chain network.
另外,去中介化并不代表着不接受监管,“去中心化”去的是中央控制方和中介方,而不是监管方。监管节点可以方便地接入任何一个区块链网络。并且由于区块链的公开透明特性,监管机构反而可以更加方便地监控整个系统的交易数据。
Moreover, de-intermediation does not mean non-regulation, and “de-centralization” goes to central control and intermediaries, rather than to regulators. Regulatory nodes provide easy access to any network of block chains.
(3)智能合约
(3) Smart Contract
从技术层面讲,智能合约是一段部署在在区块链上的程序代码,当满足程序设定的条件时,它便会在区块链上运行,并得到相应的结果。这种情况有点类似于微信的小程序,区块链提供虚拟机和脚本语言,用户根据脚本语言的语法开发带有一定业务逻辑的程序,部署在区块链上,当满足执行的条件时,智能合约便会被区块链虚拟机解释并运行。
At the technical level, an intelligent contract is a code that is deployed on the block chain, and when the conditions set by the application are met, it operates on the block chain, with the corresponding results. This situation is somewhat similar to that of a small micro-program, which provides virtual machine and script language, where the user develops a program with a certain business logic based on the syntax of the script language and is deployed on the block chain, and when the conditions for implementation are met, the smart contract is explained and operated by the virtual machine of the block chain.
典型的应用便是以太坊平台的智能合约,在这个平台里可以支持用户通过简单的几行代码就能实现他们想要的合约,实现无需人为监督的、不可篡改、自动化运行的合约,买卖房子不需要再找中介、借钱不需要再找公证人……人们可以随时随地根据自身需求发起合约,它的执行不依赖某个人和组织,所有的信任完全基于以太坊区块链平台本身。
The typical application is the smart contract of the Taiwan platform, where users can be supported to achieve what they want through simple lines of code, to achieve contracts that do not need to be supervised, non-rogued or automated, to buy or sell houses without having to look for intermediaries, to borrow money without having to look for notaries... People can initiate contracts at any time and according to their own needs, and its implementation is not dependent on a person or organization, and all trust is based solely on the Etai district chain platform itself.
(4)不可篡改性
(4) Non-frozen
大部分人习惯称它为不可篡改性,但是从技术层面来说,我个人觉得叫做不可逆转性更贴切,既然是一个计算机系统,增删改查是基本的功能属性,只不过区块链系统删除和修改操作比较特殊一点。
Most people tend to refer to it as immutable, but at the technical level I personally think it is more appropriate to call it irreversible, and since it is a computer system, the additions and deletions are essential functional attributes, although the removal and modification of the block chain system is more unusual.
区块链是由每个区块的哈希值串连起来的链式结构,而区块的哈希值=SHA256(“当前区块内容+上一个区块的哈希值”),任何一个区块的内容发生修改,都会引起哈希值的变化,而哈希值的变化也会引起子区块哈希值发生变化,进而引起整个区块链的改变。
The chain of blocks is a chain structure in which the Hashi value of each block is linked, while the Hashi value of the block = SHA256 (“The content of the current block plus the content of the previous block”), changes in the content of any one block will cause a change in the Hashi value, and changes in the Hashi value of the sub-block, leading to a change in the entire chain of blocks.
因此任何人想要修改区块的数据几乎是不可能的,除非他把整个区块链中从创世区块到最新的区块的所有哈希值全部重新修改一遍,并且修改完之后,还得广播告诉网络中的其他所有节点,让其他所有节点接受修改。
It is therefore almost impossible for anyone to change the data on blocks unless he re-modifies all the Hashi values in the entire block chain, from the original block to the latest one, and when the changes are completed, they have to be broadcast to all the other nodes in the network to allow all the other nodes to accept the modifications.
不过按照目前计算机的算力,想要在短时间内从区块链头部到尾部全部修改一遍,是一件非常困难的事,并且即使修改完了,其他节点也不会接受修改,因为凭一己之力,没有能够让所有节点达成共识的条件。
However, according to the computing power of the current computer, it would be very difficult to have all the changes from the head to the end of the block chain in a short period of time, and even if the changes were completed, the other nodes would not accept the changes, because, by their own means, there were no conditions under which consensus could be reached on all nodes.
(1)公有链应用:BTC网络
区块链1.0产品,对于比特币,中本聪是这样定义的:是一种完全通过点对点技术实现的电子现金系统,它使得在线支付能够直接由一方发起并支付给另外一方,中间不需要通过任何的金融机构。
The block chain 1.0 product is defined in the case of Bitcoin as an electronic cash system that is fully achieved through point-to-point technology and enables online payments to be initiated and paid directly by one party to the other, without the need to pass through any financial institution.
与所有的货币不同,比特币不依靠特定货币机构发行,它依据特定算法,通过大量的计算产生,比特币经济使用整个P2P网络中众多节点构成的分布式数据库来确认并记录所有的交易行为,并使用密码学的设计来确保货币流通各个环节安全性。之后人们根据比特币网络技术整理出了区块链技术体系,去解决信任的问题,而比特币网络原理也成为了区块链技术初学者的经典教材。
Unlike all currencies, Bitcoin does not depend on specific monetary institutions for distribution, which is generated by a large number of calculations, using distributed databases consisting of numerous nodes in the entire P2P network to identify and record all transactions, and using cryptography to ensure the security of all links in currency circulation. After that, the bitcoin network technology has been developed to address trust issues, and the Bitcoin network doctrine has become a classic teaching material for technocrats of block chains.
(2)公有链应用:以太坊网络
区块链2.0产品的代表,以太坊是一个为去中心化应用(Dapp)而生的开源区块链平台,拥有着大部分区块链技术的特征,但与其它区块链不同的是,以太坊是可编程的,开发者可以用它来构建不同的应用程序,通过其专用加密货币以太币(简称“ETH”)提供去中心化的以太虚拟机(Ethereum Virtual Machine)来处理点对点合约(就是一些脚本程序代码)。如果把比特币网络看作是一套分布式的数据库,而以太坊则更进一步,它可以看作是一台分布式的计算机:区块链是计算机的ROM,合约是程序,而以太坊的矿工们则负责计算,担任CPU的角色。
The representative of the block chain 2.0 product, the Tai Lok, is an open-source chain platform for decentralizing applications (Dapp), with most of the characteristics of block chain technology, but unlike the other block chains, it can be programmed by developers, which can be used to construct different applications by providing decentralised Ether Virtual Machine (Etheum Virtual Machine) with a dedicated encrypted currency in Tetheum (“ETH”) to handle point-to-point contracts (i.e., some script program codes). If the Bitcoin network is seen as a distributed database, and it goes even further with Tai Hok, it can be seen as a distributed computer: the block chain is a computer ROM, the contract is a program, and the miners in Taiku are responsible for calculating and playing the role of CPU.
以太坊的概念首次在2013至2014年间由程序员Vitalik Buterin受比特币启发后提出,大意为“下一代加密货币与去中心化应用平台”。虽然以太坊作为平台可以在其上开发新的应用,但是由于以太坊的运行和BTC网络一样,采用的是Token机制,且平台性能不足,经常出现网络拥堵的情况,平台用来学习开发与测试区块链技术还可以,用于实际生产的话不太现实。
For the first time between 2013 and 2014, Vitalik Buterin, a programmer, was inspired by Bitcoin, to the effect that “the next generation of encrypted money and decentralized applications platform” could be developed on it. Although the portal could be used as a platform for the development of new applications, the TOKEN mechanism, like the BTC network, was used as a result of the TOKEN mechanism and the platform’s lack of performance, often resulting in network congestion, and the platform’s use to learn to develop and test block chain technologies could also be used for actual production purposes.
(3)联盟链开发框架:Hyperledger Fabric
(3) framework for the development of the Union chain: Hyperledger Fabric
Hyperledger Fabric 也叫超级账本,它是 IBM 贡献给 Linux 基金会的商用分布式账本,是面向企业应用的全球最大的分布式开源项目。像其他区块链技术一样,它也有一个账本,可以使用智能合约。Fabric的智能合约可以有多种架构,它可以用主流语言编程,例如Go、Java和Javascript,此外也可以使用Solidity。
Hyperledger Fabric, also known as Superbook, is the IBM commercial distribution book for the Linux Foundation and the largest global distribution open source for business applications. Like other block chain technology, it also has an account book that can use smart contracts. Fabric’s smart contracts can be structured in multiple ways and can be programmed in mainstream languages, such as Go, Java and Javascript, as well as using Solidity.
至今,Fabric已获得了阿里巴巴、AWS、Azure、百度、谷歌、华为、IBM、甲骨文、腾讯等互联网巨头的支持。许多企业的区块链平台都把Fabric作为底层框架来使用,例如甲骨文。不过由于IBM对区块链的定义强调了区块链的分布式和不可变两个元素,对共识机制进行了削弱,采用了Kafka和zookeeper的“排序服务”实现共识,因此部分业内人士也称超级账本是“伪区块链”,但是即便如此,也抵挡不了企业对超级账本的喜爱,目前Fabric 2.0版本已经正式发布。
To date, Fabric has gained the support of Internet giants such as Alibaba, AWS, Azure, Peduc, Google, Hua, IBM, Oracle, Tetsu, etc. Many businesses use Fabric as a bottom frame, for example in Oracle. However, because the IBM definition of block chains emphasizes the distribution and immutable elements of the block chain, the consensus mechanism has been weakened and Kafka and zookeeper's “sequencing service” has been adopted to achieve consensus, so some industry members have called the super-accounts a “false chain”, but even so, it has not been possible to resist the enterprise's love of super-books, which is now officially published in Fabric 2.0.
(4)小结
目前公有链在实际应用中并没有太多的业务场景落地,大部分都是以挖矿为主题或者线上宠物饲养的游戏为主,并且由于数字货币的匿名性,有些不法分子利用这一特点,将数字货币用于洗钱、暗网买卖等违法行为,是各个国家的打击对象,我国政策法规也严厉禁止,因此对于技术人员来说,公有链可以作为研究学习的对象,其他方面暂时没有太多实际意义。
At present, there is not much of the business landscape in the practical application of the public chain, mostly based on the theme of mining or on-line pet-breeding, and because of the anonymity of digital money, some outlaws take advantage of this feature by using it for offences such as money-laundering, black-net trading, etc., which are the target of various countries and whose policies and regulations are strictly prohibited. Thus, for technical personnel, the public chain can be the subject of research and learning, and other aspects are for the time being of little practical significance.
目前大部分区块链企业的研究方向主要是针对企业的联盟链和私有链,并且国家层面也在大力支持区块链技术的发展,特别是区块链底层核心技术的研发,倡导把区块链作为核心技术自主创新的重要突破口,明确主攻方向,加大投入力度,着力攻克一批关键核心技术,加快推动区块链技术和产业创新发展。不过现在市面上主流的区块链平台大部分还是以国外公司主导的为主,国内区块链底层核心技术的发展,还需要技术人员的加倍努力。
Most block-chain enterprises are currently looking primarily at the enterprise’s alliances and private chains, and there is strong support at the national level for the development of block-chain technology, especially at the bottom of the block chain, advocating for the development of block-chains as an important entry point for core technology-owned innovation, clarifying the direction of the attack, increasing investment, and focusing on key core technologies to accelerate the development of sector-chain technology and industrial innovation. However, the majority of the sector-chain platforms on the market are still dominated by foreign firms, and the development of core technologies at the bottom of the domestic block chain will require intensive efforts by skilled personnel.
目前主流的区块链技术架构主要分为五层,数据层是最底层的技术,主要实现了数据存储、账户信息、交易信息等模块,数据存储主要基于Merkle树,通过区块的方式和链式结构实现,而账户和交易基于数字签名、哈希函数和非对称加密技术等多种密码学算法和技术,来保证区块链中数据的安全性。
The current mainstream block chain technical architecture is mainly divided into five layers, with the data layer being the lowest technology, with modules such as data storage, account information, transaction information, etc., mainly based on Merkle trees, through block mode and chain structure, while accounts and transactions are based on multiple cryptographic algorithms and techniques such as digital signatures, Hashi functions and non-symmetric encryption techniques to ensure the security of data in the block chain.
网络层主要实现网络节点的连接和通讯,又称点对点技术,各个区块链节点通过网络进行通信。共识层是通过共识算法,让网络中的各个节点对全网所有的区块数据真实性正确性达成一致,防止出现拜占庭攻击、51攻击等区块链共识算法攻击。
The network level primarily links and communicates the network nodes, also known as point-to-point technology, and communicates through the network through each block chain nodes. The consensus level allows the network nodes to agree on the correctness of the data on all the blocks on the network, preventing the emergence of the Byzantine attacks, 51 attacks, etc.
激励层主要是实现区块链代币的发行和分配机制,是公有链的范畴,我们不做分析。应用层一般把区块链系统作为一个平台,在平台之上实现一些去中心化的应用程序或者智能合约,平台提供运行这些应用的虚拟机。
The incentive layer is primarily a mechanism for the issuance and distribution of block chain tokens, which we do not analyse as a public chain. The application layer usually uses block chain systems as a platform for decentralizing applications or smart contracts over the platform, which provides virtual machines to run these applications.
接下来我们基于Java语言来开发一套小型的区块链系统,来实现数据层、网络层、共识层的一些功能,用简单的代码来直观抽象的概念,以便加深对以上区块链技术基础理论的理解。
Next, we develop a small block chain system based on Java language to achieve some of the functions of the data layer, network layer, consensus layer, visualizing abstract concepts with simple codes in order to deepen understanding of the underlying technical theory of the above block chain.
(1)开发环境
(1) development environment
开发语言
Java
JDK版本
JDK1.8或者OpenJDK11
开发框架
SpringBoot2.2.1
工程管理
Maven3.6
测试工具
Postman
Development language
Java
JDK version
JDK1.8 or OpenJDK11
Development framework
SpringBoot2.2.1
Engineering management
Maven3.6
Test tool
Postman
(2)区块链基本模型构建
区块是区块链系统的最小单元,第一步我们先实现最简单的区块结构,新建Block.java类,主要包含以下几个字段:
Block.java
Blocks are the smallest units of the block chain system, and the first step is to achieve the simplest block structure by creating a new Block.java class with the following main fields:
区块链是由区块按照区块哈希前后顺序串联起来的数据结构,哈希值通过散列算法对区块进行二次哈希计算而得到的数字摘要信息(不了解散列函数的,可以先百度了解一下SHA算法),用于保证区块的信息安全以及整条区块链的有效性。因此第二步我们新增计算区块Hash值的方法,采用SHA256算法,通过java实现:
CryptoUtil.java
The block chain is the data structure of blocks that are linked in sequence to the block Hashi, and the Hashi value, which calculates the block through a hash algorithm, provides a summary of the number (if not disassembly the column function, you can first know the SHA algorithm) to ensure the information security of the block and the validity of the whole block chain. The second step is therefore to add the Hash value of the block, using the SHA256 algorithm, which is achieved through Java:
strang>CryptoUtil.java
/strang>
第三步,创建一个链式结构对象,按照先后顺序来保存区块对象,从来形成一个有序的区块链表,考虑到线程安全问题,采用CopyOnWriteArrayList来实现,为了方便测试,暂且把区块链结构保存在本地缓存中,实际的区块链网络最终会实现持久层的功能,把区块链数据保存至数据库中,例如BTC核心网络采用的是K-V数据库LevelDB:
BlockCache.java
Step three: creation of a chain structure object to save block objects in order of order, from which an orderly block chain table will be formed, taking into account the safety of the threads, to be implemented using the CopyOnWrite Arraylist, so that, for ease of testing, the structure of the block chain will be preserved in a local cache, and the actual block chain network will eventually achieve the function of a lasting layer by preserving block chain data into a database, for example, the BTC core network using the K-V database LevelDB:
streng>BlockCache.java
/strang>
第四步,有了区块链结构后,需要新增向区块链中添加区块的方法,同时每次添加区块的时候,我们需要验证新区块的有效性,例如Hash值是否正确,新区块中上一区块的Hash属性的值,与上一区块的Hash值是否相等。
In the fourth step, with the structure of the block chain, a new method of adding blocks to the block chain is required, and each time a block is added, we need to verify the validity of the new block, such as whether the Hash value is correct and the value of the Hash properties of the previous block in the new block is equal to the Hash value of the previous block.
另外,区块链中必须有个创世区块,我们直接通过硬编码实现:
BlockService.java
In addition, there must be a creative block in the chain of blocks, and we will do it directly through hard code:
; BlockService.java
/strang>.
以上关键代码实现之后,我们就构建了一个非常简单的区块链模型,包含一个基本的区块模型和一个区块链模型,并且能够生成新的区块并添加到区块链中,接下来我们进行测试。
When the key code is achieved, we build a very simple block chain model, which includes a basic block model and a block chain model, and can generate new blocks and add them to the block chain, which we then test.
第五步,我们编写一个Controller类进行调用:
BlockController.java
In step five, we prepare a Controller for call:
BlockController.java
第六步,系统测试
Step six, system test.
首先系统启动后,先查看区块链中的数据,可以看到当前系统中的区块链为空:
After the system is first activated, look at the data in the block chain and see that the block chain in the current system is empty:
然后我们调用创建创世区块的方法,查看返回结果:
Then we call on the way to create the creation block and look back at the results:
我们把生成的创世区块添加到本地区块链中后,转换成JSON字符串返回,可以看到当前区块链中存储的有一个区块对象,至此我们已经实现了一个简单的区块链。实际的区块链系统模型要复杂的多,需要根据不同的业务场景扩展相应的字段,但是基本特征都是一样的。
When we add the creation blocks to the local chain, we convert them back into a JSON string, and we can see that there is a block object stored in the current block chain, so we have achieved a simple block chain. The actual block chain system model is much more complex and needs to expand the fields according to different business scenarios, but the basic features are the same.
(3)共识机制实现
在上章节中,我们实现了一个简单的区块链结构,并且能够生成并添加新的区块,但是问题来了,实际的区块链系统是一个多节点、分布式、去中心化的网络,每个节点通过网络交互,实时同步保存着同样的整条区块链数据,那么我们生成的区块,如何才能被其他节点认可,并同步添加到其他所有节点上呢,这个时候我们就需要一套规则,让所有网络节点的参与者达成能够达成共识,接纳并保存新的区块,也就是所谓的“共识机制”。
In the previous chapter, we achieved a simple block chain structure and were able to generate and add new blocks, but the problem arose when the actual block chain system was a multi-point, distributed, decentralized network, each node interacting through the network, kept the same whole block chain data in real time, so how the blocks we generated could be recognized by other nodes and added simultaneously to all other nodes, at which time we needed a set of rules that would allow participants in all network nodes to reach a consensus that would receive and preserve new blocks, the so-called “consensus mechanism”.
理论基础部分已经提到了,共识机制有很多种,各有各的优势与缺点,接下来我们就用java代码来模拟实现我们最为熟知的一种机制:工作量证明(Proof of Work),顾名思义就是对工作量的证明,在基于POW机制构建的区块链网络中,节点通过计算随机哈希散列的数值争夺记账权,求得正确的数值并生成区块的能力是节点算力的具体表现,计算的过程一般被形象地称为“挖矿”。
As mentioned in the theoretical underpinnings, there are many kinds of consensus mechanisms, each with its own strengths and shortcomings, and we then use the java code to simulate one of our most well-known mechanisms: the Proof of Work, which, by definition, is proof of the workload, in a network of blocks constructed on the basis of the PoW mechanism, nodes compete for rights to account by calculating random figures in the Hashigsian, the ability to obtain the correct values and generate blocks is a concrete expression of node-counting power.
简单来说就是,区块链系统设定一套计算规则或者说是一套计算题,在新区块生成前,各个节点都投入到这道题的求解计算中,哪个节点先计算出结果,并得到其它节点的验证和认可,这个节点就会获得新区块的记账权,并获得系统相应的奖励,共识结束。
In short, the block chain system sets a set of calculation rules, or a set of questions, and before the new block is created, each node is put into the solvency of the question, which node calculates the result first and is validated and endorsed by other nodes, and the node is given the right to account for the new block, and the system is rewarded accordingly and the consensus ends.
典型的PoW共识机制应用就是BTC网络,在BTC网络中,共识计算的目标是找到满足某个特定要求的区块Hash(哈希值)。这个区块哈希值就是工作结果的一个证明,计算工作的目的就是为了寻找到这个证明值,上一章节中,测试时我们已经见过这个Hash值:
The typical PoW consensus application is the BTC network, where the goal of the consensus calculation is to find the Hash (Hash) block that meets a particular requirement. The Hash value of the block is a proof of the work results, and the purpose of the calculation is to find this certificate value, which we have seen in the previous chapter:
BTC网络PoW使用的Hashcash算法,大致逻辑如下:
The Hashcash algorithm used by the BTC network PoW is based on the following general logic:
- 获取某种公开可知的数据data(BTC网络中,指的是区块头);
- 添加一个计数器nonce,初始值设置为0;
- 计算data与nonce拼接字符串的哈希值;
- 检查上一步的哈希值是否满足某个条件,满足则停止计算,不满足则nonce加1,然后重复第3步和第4步,直到满足这个特定的条件为止。
接下来我们用Java代码实现这个算法,设定满足的特定条件为,Hash值前4位都是0,则计算成功(实际区块链网络中的特定条件要求更高,对计算的运算能力要求也高,并且系统随着计算难度动态调整满足的特定条件,来保证区块生成的速度)。
Next, we use the Java code to achieve this algorithm, with a specific condition to be met that the top four Hash values are zero and the calculation is successful (the specific conditions in the actual block chain network are higher, the computational capacity is higher, and the system ensures the speed at which blocks are generated by adjusting to the specific conditions that are met as the computational difficulties move).
第一步,我们新建一个共识机制服务类,添加一个“挖矿”方法,计算成功后,获取记账权,调用添加区块的方法,把区块添加到区块链中:
PowService.java
As a first step, we will create a new consensus mechanism service category, add a "mining" method, calculate success, obtain rights to account, call blocks by adding blocks to the block chain:
Powservice.java
第二步,编写测试共识机制服务的Controller类方法:
BlockController.java
Step two, development of Controller-type methodology for testing consensus mechanism services:
BlockController.java
第三步,启动系统,进行测试。
Step three, start the system, test it.
首先执行http://localhost:8080/create方法,生成创世区块。
The first was the implementation of the http://localhost:8080/create method, which generated the creation block.
其次调用http://localhost:8080/mine方法进行工作量计算证明,生成新的区块,并添加到本地区块链中:
Second, a workload calculation certificate using the http://localhost:8080/mine method was used to generate new blocks and add them to the chain of the area:
我们来看一下,系统后台计算的过程,此次计算共花费1048ms计算出满足条件的Hash值,共计算4850次:
Let's take a look at the system's back-office calculations, which cost a total of 1048 ms to calculate the desired Hash values for a total of 4850 calculations:
至此,我们实现了一个简单的工作量证明机制,并在当前区块链系统节点上运行,完成了正确结果的计算,生成了一个新的区块。
To this end, we have achieved a simple workload certification mechanism and have operated on the current block chain system nodes, completed the calculation of the correct results and generated a new block.
接下来我们将会开发一个P2P网络,实现多个节点的同时运行,当一个节点挖矿完成后,通过P2P网络广播给其他节点,其他节点验证通过后,会把新产生的区块添加到自己的区块链上,进而保证整个区块链网络所有节点的数据一致性。
Next, we will develop a P2P network to operate simultaneously with multiple nodes, and when one node is completed, broadcast to other nodes via the P2P network, and when the other nodes are validated, the newly created blocks will be added to their own block chains, thereby ensuring data consistency across the entire block chain network.
(4)P2P网络开发
(4) P2P network development
前面我们已经实现了一个基本的区块链系统,并且实现了PoW工作量证明共识机制,通过挖矿计算出正确的结果同时生成一个新的区块添加到区块链中,但是这些都是基于单节点的运行,实际的区块链是有多个节点同时运行的分布式网络系统,所有节点同时计算抢夺记账权,共同维护一条完整的区块链。
We have already achieved a basic block chain system, and the PoW Workload Certification Consensus mechanism, which calculates the correct results by mining, while generating a new block to be added to the block chain, but these are run on the basis of single nodes, the actual block chain is a distributed network system with multiple nodes running simultaneously, all nodes calculating the seizure rights at the same time and jointly maintaining a complete block chain.
接下来我们基于Java的WebSocket实现一个Peer-to-Peer网络,实现多个节点间的相互通信,通过本章节,我们将要实现以下功能:
Next, we achieve a Peer-to-Peer network based on Java WebSocket, with multiple nodes communicating, and through this chapter we will achieve the following functions:
开发测试本章节的功能,我们最好准备两台电脑或者虚拟机,再或者Docker集群环境也可以,便于多节点的运行测试。如果只有一台电脑也可以,各个节点运行的端口号设置为不相同即可。
To develop the functionality of this chapter, it is better to prepare two computers or virtual machines, or the Docker cluster environment, so that multiple nodes can be tested. If only one computer is available, the port numbers running at each node will be different.
第一步,开发思路整理
目前我们已经实现了单节点的区块生成,那么我们接下来只需要实现各个节点的消息同步即可。 Now that we have achieved the creation of a single node, we simply need to synchronize the message for each node. 首先,通过java代码实现p2p网络的server端和client端,每个节点既是服务端也是客户端。 First, the server and clitend end of the p2p network is achieved through the java code, and each node is both a service end and a client. 然后,一个节点启动时,会寻找区块链网络上的有效节点,并建立socket连接(BTC网络可以通过使用“DNS”种子方式获取BTC有效节点,DNS种子提供比特币节点的IP地址列表),我们直接把节点列表配置到application.yml文件中。 Then, when a node is activated, a valid node on the block chain network is sought, and a socket connection is established (the BTC network can access the BTC node by using the “DNS” seed, which provides a list of IP addresses for the Bitcoin node), and we directly configure the node list into the application.yml file. 接着,从连接上的节点获取最新的区块信息,如果当前节点首次运行,则获取整个区块链信息,并替换到本地。 The latest block information is then obtained from the node on the connection and, if the current node is running for the first time, the whole block chain information is obtained and replaced locally. 之后,各个节点同时挖矿计算,哪个节点先计算完成,就把生成的新区块全网广播给其他所有节点(实际的区块链网络一直在计算,我们为了便于测试,手动触发一个节点挖矿产生区块,然后全网广播给其他所有节点)。 After that, each node is counted simultaneously, and which node is calculated first, the new block is broadcast to all other nodes (the actual block chain network has been calculated and we manually trigger a node to produce blocks for ease of testing, and then the whole node is broadcast to all other nodes). 最后,当一个节点收到广播内容后,对接收到的新区块进行验证,验证通过后添加到本地区块链上,验证的首要条件是新区块的高度必须比本地的区块链高度要高。 Finally, when a node receives the broadcast content, the new block received is validated and added to the chain of the area after the validation, with the primary condition that the height of the new block be higher than that of the local block chain. 第二步,先实现P2P网络server端
新建一个P2PServer类,并添加一个初始化server端的方法:
P2PServer.java
Create a new P2PServer class and add a method for initialization of the server end:
P2PServer.java
第三步,实现P2P网络client端
P2PClient.java
Step 3 to achieve P2P network clitendend
P2PCliet.java
第四步,定义P2P网络同步的消息模型
Step 4, define the P2P network synchronization message model
同步的消息模型,我们定义为四类,分别是:
Synchronized message models, which we define as four categories, are:
BlockConstant.java
定义一个各个节点间传递的消息模型:
Message.java
Define a model for transmitting messages between nodes:
Message.java
第五步,实现向其他节点广播的方法
新建一个p2p网络服务类,向外发送消息,或者处理当前节点收到其他节点发送的请求。
P2PService.java
Create a new p2p web service class to send messages out, or process requests received from other nodes at the current node.
P2Pservice.java
第六步,开发消息处理路由
Step six, develop a message processing path
第五步中已经实现了向外发送消息,本步骤实现接收消息。
首先设计一个服务端和客户端共用的消息路由,来分发消息给对应的处理单元。
P2PService.java
Step 5 has already achieved out-posting, and this step has achieved receiving.
First design a common routing of messages between the service and the client to distribute messages to the corresponding processing unit.
P2Pservice.java
第七步,开发消息处理单元
Step seven, develop message processing module
有了消息路由之后,接着编写不同的处理单元,处理其他节点发送来的区块或者区块链信息,总体原则是:先校验其他节点发送来的区块或者区块链的有效性,然后判断它们的高度比当前节点的区块链高度要高,如果高则替换本地的区块链,或者把新区块添加到本地区块链上。
P2PService.java
After having a source of information, different processing units are prepared to process information on blocks or block chains sent by other nodes. The general principle is that
第一步,打包生成测试用的可执行jar包
First step, package-generation test for executable jar bags
准备两台机器(虚拟机或者Docker集群都行),同时运行两个节点,节点信息如下:
Prepare two machines (virtual or docker clusters) and run two nodes simultaneously, with the following information:
Node1
IP地址
192.168.0.104
192.168.0.112
http端口号
8080
8090
websocket服务端口号
7001
7002
websocket服务地址
ws://192.168.0.104:7001
ws://192.168.0.112:7002
IP address
192.168.104
192.168.0.112
http port number
8080
8090
websocket service port number
7001
7002
websocket service address
ws://192.168.0.1004:7001
ws://192.168.01.112:1202
通过mvn package -Dmaven.test.skip=true命令对工程进行打包,生成可直接运行的jar包,打包前对工程进行配置,配置信息如下图:
节点Node1打包:
Packing the project by mvn package -Dmaven.test.skip=true order, generating a directly running jar package, configuring the project before packing with the following configuration information:
strong > node Node1 packing:
节点Node2打包:
分别打包之后,生成两个节点的可执行jar包,如下:
After separate packing, two nodes of executable jar packages are generated, as follows:
把两个jar包分别放在对应IP的windows机器上,打开命令行模式,进入jar所在文件夹,分别执行以下命令运行两个节点:
java -jar dce-blockchain-node1.jar
java -jar dce-blockchain-node2.jar
Put the two jar bags on a windows machine corresponding to IP, open the command line mode, enter the jar folder and run two nodes for each of the following commands:
java-jar dce-blockchain-node1.jar
java-jar dce-blockchain-node2.jar
启动节点2的时候,可以看到后台日志,已经连接上节点1,如下图所示:
At the start of node 2, you can see the back-office log, which has been connected to the previous node 1, as shown in the figure below:
第二步,对两个节点进行测试
首先,两个节点启动后,用postman分别执行http://192.168.0.104:8080/scan和http://192.168.0.112:8090/scan请求,可以看到两个节点的区块链内容都为空。
First, following the launch of the two nodes, requests from postman at http://192.168.104:8080/scan and http://192.168.012:8090/scan, respectively, can be seen that the content of the blocks chain at both nodes is empty.
然后,在节点1上分别执行http://192.168.0.104:8080/create和http://192.168.0.104:8080/mine请求,来生成创世区块,以及通过挖矿产生第二个区块,执行后查看节点1的区块链信息如下:
执行http://192.168.0.104:8080/scan结果:
The following information on the chain of blocks for node 1 was then examined following the execution of requests from http://192.168.104:8080/create and http://192.168.104:8080/mine, respectively, for the generation of the creation block and the production of a second block through mining:
Implementation of the results from http://192.168.104:8080/scan:
最后,我们来验证节点2是否已经完成了节点1生成的区块链信息的网络同步,Postman执行http://192.168.0.112:8090/scan请求,查看返回结果:
Finally, we came to verify whether node 2 has completed the network sync of block chain information generated by node 1 and Postman executed the request from http://192.168.112:8090/scan to check the return results:
从结果可以看到,区块链网络节点2已经接收到节点1发送的区块链信息,系统日志如下:
From the results, it can be seen that node 2 of the block chain network has received information on the block chain sent by node 1. The system logs as follows:
反过来,我们在节点2上再执行一次挖矿操作,可以看到节点1上,已经接收到节点2挖矿新产生的区块信息,并添加到节点1的区块链上:
In turn, we carry out another mining operation on No. 2 and see no. 1 where information on the newly created blocks of node 2 is received and added to the chain of node 1:
至此,我们已经实现了一个完整的小型区块链网络,并实现各个节点间的通信,多个节点共同维护同一个区块链信息。
To this end, we have achieved a complete network of small block chains, as well as communications between nodes, where multiple nodes jointly maintain the same block chain information.
结语:
区块链系统非常庞大,涉及方方面面的技术,本人所演示的代码主要对区块链基础的一些概念进行了诠释,感兴趣的同学,还可以在此基础上继续开发,来实现例如持久层、消息的加密解密、系统账户模型、预言机、侧链技术以及智能合约等区块链系统功能。
The block chain system is large and involves a wide range of technologies, and the code that I have shown mainly interprets some of the concepts underlying the block chain, and interested students can continue to develop it on this basis to achieve the functions of block chain systems such as persistent layers, encryption of messages, system account models, predictors, side chain technology and smart contracts.
1、本文完整源代码地址
1. Full source address of the text
https://gitee.com/luckytuan/dce-blockchain
到此这篇关于基于java开发一套完整的区块链系统详细教程(附源码)的文章就介绍到这了,更多相关区块链系统教程分享内容请搜索区块网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持区块网!
This is reflected in the article on the detailed curriculum for the development of a complete block chain system (with a source code) based on Java. More relevant block chain system lessons are shared by searching for previous articles on the block network or continuing to view related articles below, and hopefully more support will be given to the block network in the future.
以上内容就是小编今天要带给您的资讯,不知道您还喜欢吗?如果您觉得对您有帮助的话,欢迎常来欧意交易所哦
1
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论