成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何從0構(gòu)建區(qū)塊鏈之一

區(qū)塊鏈
什么是區(qū)塊鏈?建造一個(gè)難嗎?從哪里開(kāi)始?使用哪種編程語(yǔ)言?相信我,在遇到對(duì)區(qū)塊鏈技術(shù)感興趣的人時(shí),我經(jīng)常會(huì)遇到這些問(wèn)題,您也可能遇到其中的一些問(wèn)題。

[[387336]]

本文轉(zhuǎn)載自微信公眾號(hào)「區(qū)塊鏈研究實(shí)驗(yàn)室」,作者鏈三豐。轉(zhuǎn)載本文請(qǐng)聯(lián)系區(qū)塊鏈研究實(shí)驗(yàn)室公眾號(hào)。

什么是區(qū)塊鏈?建造一個(gè)難嗎?從哪里開(kāi)始?使用哪種編程語(yǔ)言?

相信我,在遇到對(duì)區(qū)塊鏈技術(shù)感興趣的人時(shí),我經(jīng)常會(huì)遇到這些問(wèn)題,您也可能遇到其中的一些問(wèn)題。

 

在線(xiàn)上有大量的區(qū)塊鏈資源,但是將它們理解為這項(xiàng)蓬勃發(fā)展的技術(shù)的新手,這讓人感到不知所措。

在這篇文章中,我將step by step給你展示,你就會(huì)明白blockchain以及如何的基本概念,采用方案一的Python,Javascript,或Golang 。

我選擇這些語(yǔ)言是因?yàn)榇蠖鄶?shù)人都熟悉它們,尤其是Javascript和Python。但是出于速度,耐用性和安全性的考慮,大多數(shù)區(qū)塊鏈核心引擎都是內(nèi)置的c / c ++(比特幣,EOS),Go(超級(jí)賬本結(jié)構(gòu),以太坊),Java(以太坊),Rust,Haskell和/或Ruby(以太坊)然后提供與其他易于使用的編程語(yǔ)言。

此外,一些區(qū)塊鏈引擎結(jié)合了多種編程語(yǔ)言,以實(shí)現(xiàn)健壯性并易于開(kāi)發(fā)人員使用,以太坊是最佳用例。

先決條件:

  • 網(wǎng)絡(luò)
  • 密碼學(xué)
  • 數(shù)據(jù)結(jié)構(gòu)與算法
  • 分布式系統(tǒng)
  • Javascript / Go / Python

您只需要基本概念即可對(duì)拳頭區(qū)塊鏈原型進(jìn)行編程。

什么是區(qū)塊鏈?

讓我們先了解一下,區(qū)塊鏈不是比特幣,區(qū)塊鏈不是數(shù)字貨幣, 區(qū)塊鏈?zhǔn)且惶滓呀?jīng)存在的不同技術(shù)。

讓我們通過(guò)舉一個(gè)例子來(lái)簡(jiǎn)化事情,因?yàn)楣こ處煂?duì)數(shù)字的理解會(huì)更好。讓我們來(lái)看一個(gè)存儲(chǔ)一些信息的MySQL數(shù)據(jù)庫(kù)。

 

使用上述數(shù)據(jù)庫(kù),我們可以:

  • 做一些CRUD(創(chuàng)建,檢索,更新和刪除)操作,
  • 將相同的信息存儲(chǔ)兩次,
  • 刪除整個(gè)數(shù)據(jù)庫(kù),
  • 我們不能與他人共享敏感信息,
  • 數(shù)據(jù)庫(kù)可以集中化(單點(diǎn)故障,安全問(wèn)題),
  • 無(wú)法信任存儲(chǔ)在其中的所有內(nèi)容。
  • 一些數(shù)據(jù)庫(kù)可以允許表之間的關(guān)系(例如RDBMS),而另一些數(shù)據(jù)庫(kù)則不能容忍這種關(guān)系(例如NoSQL數(shù)據(jù)庫(kù)),
  • 惡意的人可能會(huì)炸毀數(shù)據(jù)庫(kù)
  • 需要一名數(shù)據(jù)庫(kù)管理員(他/她可以更改或泄露信息)
  • 用戶(hù)無(wú)法控制自己的數(shù)據(jù)
  • 等等…

那么,為什么我們需要不同的東西,可靠的東西,獨(dú)立于人的東西,自動(dòng)的東西,不可變的東西呢?那就是區(qū)塊鏈開(kāi)始的地方。

區(qū)塊鏈?zhǔn)且粋€(gè)安全,可信的去中心化數(shù)據(jù)庫(kù)和網(wǎng)絡(luò)。

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

區(qū)塊鏈?zhǔn)且贿B串的區(qū)塊,區(qū)塊類(lèi)似于數(shù)據(jù)庫(kù)中的表,但是它們不能被刪除或更新,區(qū)塊包含稱(chēng)為交易的信息和其他附加數(shù)據(jù)。這些區(qū)塊經(jīng)過(guò)密碼驗(yàn)證并鏈接形成一個(gè)不變的區(qū)塊鏈,稱(chēng)為區(qū)塊鏈或分類(lèi)賬。

 

然后,同一條鏈通過(guò)P2P網(wǎng)絡(luò)分布到整個(gè)網(wǎng)絡(luò)中的所有節(jié)點(diǎn)。

 

因此,代替集中式數(shù)據(jù)庫(kù),跨節(jié)點(diǎn)共享的所有事務(wù)(數(shù)據(jù))都包含在塊中,這些塊鏈接在一起以創(chuàng)建分類(lèi)帳。該分類(lèi)賬代表區(qū)塊鏈中的所有數(shù)據(jù)。分類(lèi)賬中的所有數(shù)據(jù)均通過(guò)加密哈希和數(shù)字簽名進(jìn)行保護(hù),并通過(guò)共識(shí)算法進(jìn)行驗(yàn)證。網(wǎng)絡(luò)上的節(jié)點(diǎn)參與以確保跨網(wǎng)絡(luò)分布的所有數(shù)據(jù)副本都是相同的。

在區(qū)塊鏈生態(tài)系統(tǒng)中要記住的5個(gè)關(guān)鍵概念:

  • 加密哈希和數(shù)字簽名
  • 不變的分類(lèi)帳
  • P2P網(wǎng)絡(luò)
  • 共識(shí)算法(PoW,PoS,PBFT,ETc…)
  • 塊驗(yàn)證(采礦,鍛造等)

我們將繼續(xù)詳細(xì)解釋這些概念。

使用區(qū)塊鏈的好處:

  • 刪除中介組織
  • 不變的分類(lèi)帳
  • 透明度
  • 安全

何時(shí)使用區(qū)塊鏈?

區(qū)塊鏈不是靈丹妙藥,因此在以下情況下使用它:

  • 存儲(chǔ)的數(shù)據(jù)無(wú)需修改(存在證明)
  • 數(shù)據(jù)不能被其所有者拒絕(不可否認(rèn))
  • 你想權(quán)力下放
  • 你想要一個(gè)真理的源頭
  • 您想要高安全性
  • 您不必?fù)?dān)心速度(例如,比特幣平均需要10分鐘才能驗(yàn)證一筆交易)。

但是某些區(qū)塊鏈速度更快,因?yàn)樗鼈兪褂贸齈oW之外的不同共識(shí)算法

我們稍后再討論。

區(qū)塊鏈用例

區(qū)塊鏈應(yīng)用領(lǐng)域

  • 房地產(chǎn):土地所有權(quán)
  • 醫(yī)療保健:安全記錄患者的數(shù)據(jù)
  • 財(cái)務(wù):減少稅收和中介機(jī)構(gòu),反洗錢(qián),跨境支付
  • 供應(yīng)鏈:跟蹤從供應(yīng)商到客戶(hù)的商品(真實(shí)性,原創(chuàng)內(nèi)容的創(chuàng)建)
  • 網(wǎng)絡(luò)安全:DDOS攻擊
  • 將權(quán)力交還給用戶(hù):擁有數(shù)據(jù)并與所需的人安全地共享(DID)
  • 加密貨幣兌換
  • 投票機(jī)制

區(qū)塊鏈平臺(tái)和應(yīng)用

  • 比特幣
  • 以太坊
  • 織物
  • EOS
  • 鏈環(huán)
  • 卡爾達(dá)諾
  • 等等…

區(qū)塊鏈類(lèi)型

  • 私有:僅在內(nèi)部使用,并且在我們更了解用戶(hù)的情況下使用(例如Hyperledger Fabric)
  • 公開(kāi):每個(gè)人都可以看到正在發(fā)生的事情(比特幣,以太坊)
  • 混合:合并前兩個(gè)。

“Talk is cheap. Show me the code.”

有兩種方法可以構(gòu)建區(qū)塊鏈:

  • 一種簡(jiǎn)單的方法是使用現(xiàn)有的預(yù)先構(gòu)建的區(qū)塊鏈開(kāi)源資源,例如以太坊,F(xiàn)abric,EOS等…
  • 如果它們都不符合您的要求,那么請(qǐng)從頭開(kāi)始構(gòu)建

在本系列中,我們將從頭開(kāi)始構(gòu)建一個(gè),以便您可以徹底了解區(qū)塊鏈的狀態(tài)機(jī)。

如何建立區(qū)塊鏈?

好的,現(xiàn)在讓我們用三種不同的編程語(yǔ)言Go,Python和Javascript創(chuàng)建第一個(gè)小區(qū)塊鏈demo。這些原型可以幫助您理解我們前面介紹的概念。

首先,我們將創(chuàng)建一個(gè)塊,第二,我們將添加數(shù)據(jù)(標(biāo)題和正文)給它,第三,我們將散列塊,和最后但并非最不重要,我們將鏈中的所有塊了。

一個(gè)塊包含前面提到的信息,但是為了簡(jiǎn)化起見(jiàn),我們將刪除一些信息。讓我們深入研究細(xì)節(jié)!

希望您像我之前提到的那樣熟悉Go編程,如果不嘗試學(xué)習(xí)基礎(chǔ)知識(shí):函數(shù),方法,數(shù)據(jù)類(lèi)型,結(jié)構(gòu),流控制,迭代等…

創(chuàng)建一個(gè)文件夾并向其中添加2個(gè)文件, main.go and block.go

資料夾結(jié)構(gòu):

  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. 

如果您運(yùn)行此程序,則由于該CreateBlock函數(shù)尚未定義,因此將顯示錯(cuò)誤消息,因此請(qǐng)繼續(xù)在其中創(chuàng)建它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的優(yōu)點(diǎn)在于您不必導(dǎo)入或?qū)С龊瘮?shù),只需使用大寫(xiě)字母聲明它們,Go就會(huì)為您找到它們。現(xiàn)在打開(kāi)終端并移至您創(chuàng)建的文件夾,run go build然后.\go在Windows,./goLinux和Macbook上運(yùn)行。

 

我們剛剛創(chuàng)建了一個(gè)簡(jiǎn)單的Go程序,該程序調(diào)用一個(gè)函數(shù)并傳遞一些字符串?dāng)?shù)據(jù)。讓我們繼續(xù)做更多美好的事情。讓我們添加2個(gè)文件,blockchain.go而structures.go現(xiàn)在我們有4個(gè)文件:main.go, block.go, structures.go, and blockchain.go

我將在代碼的每一行中添加一些注釋?zhuān)允鼓私馕以谧鍪裁础?/p>

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 

讓我們現(xiàn)在運(yùn)行它,go build然后.\go

 

我們?cè)趨^(qū)塊鏈中的區(qū)塊沒(méi)有任何ID和時(shí)間戳,因此讓我們通過(guò)修改main.go文件來(lái)添加該信息,并在中添加這兩行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然后再次運(yùn)行它,然后./go

 

如您所見(jiàn),區(qū)塊鏈結(jié)構(gòu)良好。除創(chuàng)世塊外,每個(gè)塊均包含其哈希值和上一個(gè)塊的哈希值,這使其不可變,如果更改了該塊中的數(shù)據(jù),則哈希值將自動(dòng)更改,并且該塊將被丟棄。創(chuàng)世塊沒(méi)有任何先前的哈希,因?yàn)樗堑谝粋€(gè)哈希,沒(méi)有先前的塊。

 

全部完成!恭喜,您剛剛在Go created中創(chuàng)建了自己的區(qū)塊鏈DEMO!

 

責(zé)任編輯:武曉燕 來(lái)源: 區(qū)塊鏈研究實(shí)驗(yàn)室
相關(guān)推薦

2021-03-16 21:39:47

區(qū)塊鏈DEMOGo

2021-03-17 20:29:36

區(qū)塊鏈DEMOPython

2018-05-23 15:20:08

區(qū)塊鏈數(shù)字貨幣比特幣

2018-03-19 19:30:19

2019-11-07 23:06:55

區(qū)塊鏈數(shù)據(jù)存儲(chǔ)去中心化

2021-04-16 20:43:18

Go區(qū)塊鏈編程

2021-12-22 23:28:04

區(qū)塊鏈人工智能技術(shù)

2019-10-29 15:46:07

區(qū)塊鏈區(qū)塊鏈技術(shù)

2021-11-12 15:52:16

區(qū)塊鏈開(kāi)發(fā)技術(shù)

2018-03-19 17:40:10

Python區(qū)塊鏈

2018-08-22 17:32:45

2021-09-23 22:40:10

區(qū)塊鏈比特幣技術(shù)

2020-12-08 10:24:22

區(qū)塊鏈比特幣時(shí)間戳

2020-02-05 15:05:11

區(qū)塊鏈時(shí)間戳分布式

2018-03-27 09:52:30

區(qū)塊鏈數(shù)字貨幣比特幣

2018-07-30 14:28:22

2018-01-23 11:09:04

區(qū)塊鏈技術(shù)重用

2022-10-18 08:00:00

2018-05-06 16:17:01

2020-08-18 10:58:05

區(qū)塊鏈比特幣區(qū)塊鏈戰(zhàn)略
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲xxxxx| 日韩视频精品在线 | 亚洲毛片在线 | 亚洲福利网 | 久久免费视频观看 | 一级免费视频 | 亚洲视频免费一区 | 成年人在线观看视频 | www.国产 | 亚洲精品在线视频 | 在线一区二区三区 | 色欧美综合 | 亚洲成人黄色 | 国产精品永久 | 国产福利精品一区 | 欧美中国少妇xxx性高请视频 | 91精品国产综合久久久久久首页 | 久久久www成人免费精品 | 综合国产 | 又黑又粗又长的欧美一区 | 黄免费在线 | 国产精品永久久久久 | 三a毛片 | 亚洲成人精 | 久久日韩精品一区二区三区 | 亚洲一区国产精品 | 天天综合网91 | 国产乱码精品一区二区三区中文 | 一级片免费在线观看 | 久久久久久黄 | 国产精品无| 国产综合精品一区二区三区 | 国产三级国产精品 | 天堂影院av | 九九综合| 亚洲vs天堂 | 亚洲精品1区2区3区 91免费看片 | 久久免费精品视频 | 日韩毛片在线视频 | 最新中文字幕在线播放 | 久久亚洲国产精品日日av夜夜 |