如何从0构建区块链之一

区块链
什么是区块链?建造一个难吗?从哪里开始?使用哪种编程语言?相信我,在遇到对区块链技术感兴趣的人时,我经常会遇到这些问题,您也可能遇到其中的一些问题。

[[387336]]

本文转载自微信公众号「区块链研究实验室」,作者链三丰。转载本文请联系区块链研究实验室公众号。

什么是区块链?建造一个难吗?从哪里开始?使用哪种编程语言?

相信我,在遇到对区块链技术感兴趣的人时,我经常会遇到这些问题,您也可能遇到其中的一些问题。

 

在线上有大量的区块链资源,但是将它们理解为这项蓬勃发展的技术的新手,这让人感到不知所措。

在这篇文章中,我将step by step给你展示,你就会明白blockchain以及如何的基本概念,采用方案一的Python,Javascript,或Golang 。

我选择这些语言是因为大多数人都熟悉它们,尤其是Javascript和Python。但是出于速度,耐用性和安全性的考虑,大多数区块链核心引擎都是内置的c / c ++(比特币,EOS),Go(超级账本结构,以太坊),Java(以太坊),Rust,Haskell和/或Ruby(以太坊)然后提供与其他易于使用的编程语言。

此外,一些区块链引擎结合了多种编程语言,以实现健壮性并易于开发人员使用,以太坊是最佳用例。

先决条件:

  • 网络
  • 密码学
  • 数据结构与算法
  • 分布式系统
  • Javascript / Go / Python

您只需要基本概念即可对拳头区块链原型进行编程。

什么是区块链?

让我们先了解一下,区块链不是比特币,区块链不是数字货币, 区块链是一套已经存在的不同技术。

让我们通过举一个例子来简化事情,因为工程师对数字的理解会更好。让我们来看一个存储一些信息的MySQL数据库。

 

使用上述数据库,我们可以:

  • 做一些CRUD(创建,检索,更新和删除)操作,
  • 将相同的信息存储两次,
  • 删除整个数据库,
  • 我们不能与他人共享敏感信息,
  • 数据库可以集中化(单点故障,安全问题),
  • 无法信任存储在其中的所有内容。
  • 一些数据库可以允许表之间的关系(例如RDBMS),而另一些数据库则不能容忍这种关系(例如NoSQL数据库),
  • 恶意的人可能会炸毁数据库
  • 需要一名数据库管理员(他/她可以更改或泄露信息)
  • 用户无法控制自己的数据
  • 等等…

那么,为什么我们需要不同的东西,可靠的东西,独立于人的东西,自动的东西,不可变的东西呢?那就是区块链开始的地方。

区块链是一个安全,可信的去中心化数据库和网络。

“Truth can only be found in one place: the code. ”

区块链是一连串的区块,区块类似于数据库中的表,但是它们不能被删除或更新,区块包含称为交易的信息和其他附加数据。这些区块经过密码验证并链接形成一个不变的区块链,称为区块链或分类账。

 

然后,同一条链通过P2P网络分布到整个网络中的所有节点。

 

因此,代替集中式数据库,跨节点共享的所有事务(数据)都包含在块中,这些块链接在一起以创建分类帐。该分类账代表区块链中的所有数据。分类账中的所有数据均通过加密哈希和数字签名进行保护,并通过共识算法进行验证。网络上的节点参与以确保跨网络分布的所有数据副本都是相同的。

在区块链生态系统中要记住的5个关键概念:

  • 加密哈希和数字签名
  • 不变的分类帐
  • P2P网络
  • 共识算法(PoW,PoS,PBFT,ETc…)
  • 块验证(采矿,锻造等)

我们将继续详细解释这些概念。

使用区块链的好处:

  • 删除中介组织
  • 不变的分类帐
  • 透明度
  • 安全

何时使用区块链?

区块链不是灵丹妙药,因此在以下情况下使用它:

  • 存储的数据无需修改(存在证明)
  • 数据不能被其所有者拒绝(不可否认)
  • 你想权力下放
  • 你想要一个真理的源头
  • 您想要高安全性
  • 您不必担心速度(例如,比特币平均需要10分钟才能验证一笔交易)。

但是某些区块链速度更快,因为它们使用除PoW之外的不同共识算法

我们稍后再讨论。

区块链用例

区块链应用领域

  • 房地产:土地所有权
  • 医疗保健:安全记录患者的数据
  • 财务:减少税收和中介机构,反洗钱,跨境支付
  • 供应链:跟踪从供应商到客户的商品(真实性,原创内容的创建)
  • 网络安全:DDOS攻击
  • 将权力交还给用户:拥有数据并与所需的人安全地共享(DID)
  • 加密货币兑换
  • 投票机制

区块链平台和应用

  • 比特币
  • 以太坊
  • 织物
  • EOS
  • 链环
  • 卡尔达诺
  • 等等…

区块链类型

  • 私有:仅在内部使用,并且在我们更了解用户的情况下使用(例如Hyperledger Fabric)
  • 公开:每个人都可以看到正在发生的事情(比特币,以太坊)
  • 混合:合并前两个。

“Talk is cheap. Show me the code.”

有两种方法可以构建区块链:

  • 一种简单的方法是使用现有的预先构建的区块链开源资源,例如以太坊,Fabric,EOS等…
  • 如果它们都不符合您的要求,那么请从头开始构建

在本系列中,我们将从头开始构建一个,以便您可以彻底了解区块链的状态机。

如何建立区块链?

好的,现在让我们用三种不同的编程语言Go,Python和Javascript创建第一个小区块链demo。这些原型可以帮助您理解我们前面介绍的概念。

首先,我们将创建一个块,第二,我们将添加数据(标题和正文)给它,第三,我们将散列块,和最后但并非最不重要,我们将链中的所有块了。

一个块包含前面提到的信息,但是为了简化起见,我们将删除一些信息。让我们深入研究细节!

希望您像我之前提到的那样熟悉Go编程,如果不尝试学习基础知识:函数,方法,数据类型,结构,流控制,迭代等…

创建一个文件夹并向其中添加2个文件, main.go and block.go

资料夹结构:

  1. go // the folder 
  2.   main.go // file 1 
  3.   block.go // file 2 

main.go

  1. // use the main package 
  2. package main 
  3. //import the fmt package 
  4. import ( 
  5. "fmt" 
  6. func main(){ 
  7. fmt.Println("I am building my first blockchain") // print this 
  8. CreateBlock("The hearder will be shown here""the body will be shown here") // call the function that will create the block and pass some parameters in it. 

如果您运行此程序,则由于该CreateBlock函数尚未定义,因此将显示错误消息,因此请继续在其中创建它block.go:

block.go

  1. package main 
  2. import ( 
  3. "fmt" // this will help us to print on the screen 
  4. func CreateBlock(Header, Body string){ 
  5. fmt.Println(Header ,"\n", Body) // Show me the block content 

Go的优点在于您不必导入或导出函数,只需使用大写字母声明它们,Go就会为您找到它们。现在打开终端并移至您创建的文件夹,run go build然后.\go在Windows,./goLinux和Macbook上运行。

 

我们刚刚创建了一个简单的Go程序,该程序调用一个函数并传递一些字符串数据。让我们继续做更多美好的事情。让我们添加2个文件,blockchain.go而structures.go现在我们有4个文件:main.go, block.go, structures.go, and blockchain.go

我将在代码的每一行中添加一些注释,以使您了解我在做什么。

structures.go

  1. package main //Import the main package 
  2. // Create the Block data structure 
  3. // A block contains this info: 
  4. type Block struct { 
  5.   Timestamp         int64  // the time when the block was created 
  6.   PreviousBlockHash []byte // the hash of the previous block 
  7.   MyBlockHash       []byte // the hash of the current block 
  8.   AllData           []byte // the data or transactions (body info) 
  9.  
  10. // Prepare the Blockchain data structure : 
  11. type Blockchain struct { 
  12.   Blocks []*Block // remember a blockchain is a series of blocks 

block.go

  1. package main 
  2.  
  3. import ( 
  4.   // We will need these libraries: 
  5.   "bytes"         // need to convert data into byte in order to be sent on the network, computer understands better the byte(8bits)language 
  6.   "crypto/sha256" //crypto library to hash the data 
  7.   "strconv"       // for conversion 
  8.   "time"          // the time for our timestamp 
  9.  
  10. // Now let's create a method for generating a hash of the block 
  11. // We will just concatenate all the data and hash it to obtain the block hash 
  12. func (block *Block) SetHash() { 
  13.   timestamp := []byte(strconv.FormatInt(block.Timestamp, 10))                                  // get the time and convert it into a unique series of digits 
  14.   headers := bytes.Join([][]byte{timestamp, block.PreviousBlockHash, block.AllData}, []byte{}) // concatenate all the block data 
  15.   hash := sha256.Sum256(headers)                                                               // hash the whole thing 
  16.   block.MyBlockHash = hash[:]                                                                  // now set the hash of the block 
  17.  
  18. // Create a function for new block generation and return that block 
  19. func NewBlock(data string, prevBlockHash []byte) *Block { 
  20.   block := &Block{time.Now().Unix(), prevBlockHash, []byte{}, []byte(data)} // the block is received 
  21.   block.SetHash()                                                           // the block is hashed 
  22.   return block                                                              // the block is returned with all the information in it 
  23.  
  24. /* let's now create the genesis block function that will return the first block. The genesis block is the first block on the chain */ 
  25. func NewGenesisBlock() *Block { 
  26.   return NewBlock("Genesis Block", []byte{}) // the genesis block is made with some data in it 

blockchain.go

  1. package main 
  2.  
  3. // create the method that adds a new block to a blockchain 
  4. func (blockchain *Blockchain) AddBlock(data string) { 
  5.   PreviousBlock := blockchain.Blocks[len(blockchain.Blocks)-1] // the previous block is needed, so let's get it 
  6.   newBlock := NewBlock(data, PreviousBlock.MyBlockHash)        // create a new block containing the data and the hash of the previous block 
  7.   blockchain.Blocks = append(blockchain.Blocks, newBlock)      // add that block to the chain to create a chain of blocks 
  8.  
  9. /* Create the function that returns the whole blockchain and add the genesis to it first. the genesis block is the first ever mined block, so let's create a function that will return it since it does not exist yet */ 
  10. func NewBlockchain() *Blockchain { // the function is created 
  11.   return &Blockchain{[]*Block{NewGenesisBlock()}} // the genesis block is added first to the chain 

main.go

  1. //Time to put everything together and test 
  2. package main 
  3. import ( 
  4.   "fmt" // just for printing something on the screen 
  5. func main() { 
  6.   newblockchain := NewBlockchain() // Initialize the blockchain 
  7.   // create 2 blocks and add 2 transactions 
  8.   newblockchain.AddBlock("first transaction")  // first block containing one tx 
  9.   newblockchain.AddBlock("Second transaction") // second block containing one tx 
  10.   // Now print all the blocks and their contents 
  11.   for _, block := range newblockchain.Blocks { // iterate on each block 
  12.     fmt.Printf("Hash of the block %x\n", block.MyBlockHash)                 // print the hash of the block 
  13.     fmt.Printf("Hash of the previous Block: %x\n", block.PreviousBlockHash) // print the hash of the previous block 
  14.     fmt.Printf("All the transactions: %s\n", block.AllData)                 // print the transactions 
  15.   } // our blockchain will be printed 

让我们现在运行它,go build然后.\go

 

我们在区块链中的区块没有任何ID和时间戳,因此让我们通过修改main.go文件来添加该信息,并在中添加这两行for loop:

  1. fmt.Printf("Block ID : %d \n", i)                                         
  2. fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i)) 
  1. //Time to put everything together and test 
  2. package main 
  3.  
  4. import ( 
  5.   "fmt" // just for printing something on the screen 
  6.  
  7. func main() { 
  8.   newblockchain := NewBlockchain() // Initialize the blockchain 
  9.   // create 2 blocks and add 2 transactions 
  10.   newblockchain.AddBlock("first transaction")  // first block containing one tx 
  11.   newblockchain.AddBlock("Second transaction") // second block containing one tx 
  12.   // Now print all the blocks and their contents 
  13.   for i, block := range newblockchain.Blocks { // iterate on each block 
  14.     fmt.Printf("Block ID : %d \n", i)                                        // print the block ID 
  15.     fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i))                // print the timestamp of the block, to make them different, we just add a value i 
  16.     fmt.Printf("Hash of the block : %x\n", block.MyBlockHash)                // print the hash of the block 
  17.     fmt.Printf("Hash of the previous Block : %x\n", block.PreviousBlockHash) // print the hash of the previous block 
  18.     fmt.Printf("All the transactions : %s\n", block.AllData)                 // print the transactions 
  19.   } // our blockchain will be printed 

让我们保存代码,go build然后再次运行它,然后./go

 

如您所见,区块链结构良好。除创世块外,每个块均包含其哈希值和上一个块的哈希值,这使其不可变,如果更改了该块中的数据,则哈希值将自动更改,并且该块将被丢弃。创世块没有任何先前的哈希,因为它是第一个哈希,没有先前的块。

 

全部完成!恭喜,您刚刚在Go created中创建了自己的区块链DEMO!

 

责任编辑:武晓燕 来源: 区块链研究实验室
相关推荐

2021-03-16 21:39:47

区块链DEMOGo

2021-03-17 20:29:36

区块链DEMOPython

2018-05-23 15:20:08

区块链数字货币比特币

2018-03-19 19:30:19

2019-11-07 23:06:55

区块链数据存储去中心化

2021-04-16 20:43:18

Go区块链编程

2021-12-22 23:28:04

区块链人工智能技术

2019-10-29 15:46:07

区块链区块链技术

2021-11-12 15:52:16

区块链开发技术

2018-03-19 17:40:10

Python区块链

2018-08-22 17:32:45

2020-02-05 15:05:11

区块链时间戳分布式

2020-12-08 10:24:22

区块链比特币时间戳

2021-09-23 22:40:10

区块链比特币技术

2018-03-27 09:52:30

区块链数字货币比特币

2018-07-30 14:28:22

2018-07-19 10:45:00

甲骨文区块链云服务

2021-05-10 15:09:47

区块链互联网金融

2018-01-23 11:09:04

区块链技术重用

2022-10-18 08:00:00

点赞
收藏

51CTO技术栈公众号