如何从0构建区块链(一)

区块链研究实验室 阅读 2797 2021-3-11 22:34
分享至
微信扫一扫,打开网页后点击屏幕右上角分享按钮

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

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

如何从0构建区块链(一)

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

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

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

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

先决条件:

网络

密码学

数据结构与算法

分布式系统

Javascript / Go / Python

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

什么是区块链?

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

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

如何从0构建区块链(一)

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

做一些CRUD(创建,检索,更新和删除)操作,

将相同的信息存储两次,

删除整个数据库,

我们不能与他人共享敏感信息,

数据库可以集中化(单点故障,安全问题),

无法信任存储在其中的所有内容。

一些数据库可以允许表之间的关系(例如RDBMS),而另一些数据库则不能容忍这种关系(例如NoSQL数据库),

恶意的人可能会炸毁数据库

需要一名数据库管理员(他/她可以更改或泄露信息)

用户无法控制自己的数据

等等…

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

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

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

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

如何从0构建区块链(一)

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

如何从0构建区块链(一)

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

在区块链生态系统中要记住的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

资料夹结构:

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

main.go

// use the main packagepackage main//import the fmt packageimport ("fmt")func main()

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

block.go

package mainimport ("fmt" // this will help us to print on the screen)func CreateBlock(Header, Body string)

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

如何从0构建区块链(一)

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

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

structures.go

package main //Import the main package// Create the Block data structure// A block contains this info:type Block struct { Timestamp int64 // the time when the block was created PreviousBlockHash []byte // the hash of the previous block MyBlockHash []byte // the hash of the current block AllData []byte // the data or transactions (body info)}// Prepare the Blockchain data structure :type Blockchain struct { Blocks []*Block // remember a blockchain is a series of blocks}

block.go

package mainimport ( // We will need these libraries: "bytes" // need to convert data into byte in order to be sent on the network, computer understands better the byte(8bits)language "crypto/sha256" //crypto library to hash the data "strconv" // for conversion "time" // the time for our timestamp)// Now let's create a method for generating a hash of the block// We will just concatenate all the data and hash it to obtain the block hashfunc (block *Block) SetHash() { timestamp := []byte(strconv.FormatInt(block.Timestamp, 10)) // get the time and convert it into a unique series of digits headers := bytes.Join([][]byte, []byte{}) // concatenate all the block data hash := sha256.Sum256(headers) // hash the whole thing block.MyBlockHash = hash[:] // now set the hash of the block}// Create a function for new block generation and return that blockfunc NewBlock(data string, prevBlockHash []byte) *Block { block := &Block, []byte(data)} // the block is received block.SetHash() // the block is hashed return block // the block is returned with all the information in it}/* let's now create the genesis block function that will return the first block. The genesis block is the first block on the chain */func NewGenesisBlock() *Block { return NewBlock("Genesis Block", []byte{}) // the genesis block is made with some data in it}

blockchain.go

package main// create the method that adds a new block to a blockchainfunc (blockchain *Blockchain) AddBlock(data string) { PreviousBlock := blockchain.Blocks[len(blockchain.Blocks)-1] // the previous block is needed, so let's get it newBlock := NewBlock(data, PreviousBlock.MyBlockHash) // create a new block containing the data and the hash of the previous block blockchain.Blocks = append(blockchain.Blocks, newBlock) // add that block to the chain to create a chain of blocks}/* 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 */func NewBlockchain() *Blockchain { // the function is created return &Blockchain{[]*Block} // the genesis block is added first to the chain}

main.go

//Time to put everything together and testpackage mainimport ( "fmt" // just for printing something on the screen)func main() { newblockchain := NewBlockchain() // Initialize the blockchain // create 2 blocks and add 2 transactions newblockchain.AddBlock("first transaction") // first block containing one tx newblockchain.AddBlock("Second transaction") // second block containing one tx // Now print all the blocks and their contents for _, block := range newblockchain.Blocks { // iterate on each block fmt.Printf("Hash of the block %x\n", block.MyBlockHash) // print the hash of the block fmt.Printf("Hash of the previous Block: %x\n", block.PreviousBlockHash) // print the hash of the previous block fmt.Printf("All the transactions: %s\n", block.AllData) // print the transactions } // our blockchain will be printed}

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

如何从0构建区块链(一)

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

fmt.Printf("Block ID : %d \n", i) fmt.Printf("Timestamp : %d \n", block.Timestamp+int64(i))
//Time to put everything together and testpackage mainimport ( "fmt" // just for printing something on the screen)func main() { newblockchain := NewBlockchain() // Initialize the blockchain // create 2 blocks and add 2 transactions newblockchain.AddBlock("first transaction") // first block containing one tx newblockchain.AddBlock("Second transaction") // second block containing one tx // Now print all the blocks and their contents for i, block := range newblockchain.Blocks { // iterate on each block fmt.Printf("Block ID : %d \n", i) // print the block ID 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 fmt.Printf("Hash of the block : %x\n", block.MyBlockHash) // print the hash of the block fmt.Printf("Hash of the previous Block : %x\n", block.PreviousBlockHash) // print the hash of the previous block fmt.Printf("All the transactions : %s\n", block.AllData) // print the transactions } // our blockchain will be printed}

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

如何从0构建区块链(一)

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

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

btcfans公众号

微信扫描关注公众号,及时掌握新动向

来源链接
免责声明:
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
标签: 区块链 Python
上一篇:如何使用Hyperledger在区块链上创建应用程序 下一篇:一位投资者以969美元的价格买入Beeple的NFT后,获得约30倍收益

相关资讯