當前位置:
首頁 > 最新 > 想學習區塊鏈?那就用 Python 構建一個

想學習區塊鏈?那就用 Python 構建一個

編譯:linux中國 - qhwdw 英文:Daniel van Flymen

https://linux.cn/article-9882-1.html

了解區塊鏈是如何工作的最快的方法是構建一個。

你看到這篇文章是因為和我一樣,對加密貨幣的大熱而感到興奮。並且想知道區塊鏈是如何工作的 —— 它們背後的技術基礎是什麼。

但是理解區塊鏈並不容易 —— 至少對我來說是這樣。我徜徉在各種難懂的視頻中,並且因為示例太少而陷入深深的挫敗感中。

我喜歡在實踐中學習。這會使得我在代碼層面上處理主要問題,從而可以讓我堅持到底。如果你也是這麼做的,在本指南結束的時候,你將擁有一個功能正常的區塊鏈,並且實實在在地理解了它的工作原理。


開始之前 …

記住,區塊鏈是一個不可更改的、有序的記錄(被稱為區塊)的鏈。它們可以包括交易transaction、文件或者任何你希望的真實數據。最重要的是它們是通過使用哈希鏈接到一起的。

如果你不知道哈希是什麼,這裡有解釋。

本指南的目標讀者是誰?你應該能輕鬆地讀、寫一些基本的 Python 代碼,並能夠理解 HTTP 請求是如何工作的,因為我們討論的區塊鏈將基於 HTTP。

我需要做什麼?確保安裝了 Python 3.6+(以及 ),還需要去安裝 Flask 和非常好用的 Requests 庫:

pip install Flask==0.12.2 requests==2.18.4

當然,你也需要一個 HTTP 客戶端,像 Postman 或者 cURL。哪個都行。

最終的代碼在哪裡可以找到?源代碼在 這裡。


第 1 步:構建一個區塊鏈

打開你喜歡的文本編輯器或者 IDE,我個人喜歡 PyCharm。創建一個名為 的新文件。我將僅使用一個文件,如果你看暈了,可以去參考 源代碼。


我們將創建一個 類,它的構造函數將去初始化一個空列表(去存儲我們的區塊鏈),以及另一個列表去保存交易。下面是我們的類規劃:

classBlockchain(object):

def__init__(self):

self.chain=[]

self.current_transactions=[]

defnew_block(self):

# Creates a new Block and adds it to the chain

pass

defnew_transaction(self):

# Adds a new transaction to the list of transactions

pass

@staticmethod

defhash(block):

# Hashes a Block

pass

@property

deflast_block(self):

# Returns the last Block in the chain

pass

我們的 Blockchain 類的原型

我們的 類負責管理鏈。它將存儲交易並且有一些為鏈中增加新區塊的輔助性質的方法。現在我們開始去充實一些類的方法。

每個區塊有一個索引、一個時間戳(Unix 時間)、一個交易的列表、一個證明(後面會詳細解釋)、以及前一個區塊的哈希。

單個區塊的示例應該是下面的樣子:

block={

"index":1,

"timestamp":1506057125.900785,

"transactions":[

{

"sender":"8527147fe1f5426f9dd545de4b27ee00",

"recipient":"a77f5cdfa2934df3954a5c7c7da5df1f",

"amount":5,

}

],

}

我們的區塊鏈中的塊示例

此刻,鏈的概念應該非常明顯 —— 每個新區塊包含它自身的信息和前一個區域的哈希。這一點非常重要,因為這就是區塊鏈不可更改的原因:如果攻擊者修改了一個早期的區塊,那麼所有的後續區塊將包含錯誤的哈希。

這樣做有意義嗎?如果沒有,就讓時間來埋葬它吧 —— 這就是區塊鏈背後的核心思想。


我們將需要一種區塊中添加交易的方式。我們的 就是做這個的,它非常簡單明了:

classBlockchain(object):

...

defnew_transaction(self,sender,recipient,amount):

"""

Creates a new transaction to go into the next mined Block

:param sender: Address of the Sender

:param recipient: Address of the Recipient

:param amount: Amount

:return: The index of the Block that will hold this transaction

"""

self.current_transactions.append({

"sender":sender,

"recipient":recipient,

"amount":amount,

})

returnself.last_block["index"]+1

在 運行後將在列表中添加一個交易,它返回添加交易後的那個區塊的索引 —— 那個區塊接下來將被挖礦。提交交易的用戶後面會用到這些。


當我們的 被實例化後,我們需要一個創世區塊(一個沒有祖先的區塊)來播種它。我們也需要去添加一些 「證明」 到創世區塊,它是挖礦(工作量證明 PoW)的成果。我們在後面將討論更多挖礦的內容。

除了在我們的構造函數中創建創世區塊之外,我們還需要寫一些方法,如 、 以及 :

importhashlib

importjson

fromtimeimporttime

classBlockchain(object):

def__init__(self):

self.current_transactions=[]

self.chain=[]

# Create the genesis block

self.new_block(previous_hash=1,proof=100)

defnew_block(self,proof,previous_hash=None):

"""

Create a new Block in the Blockchain

:param proof: The proof given by the Proof of Work algorithm

:param previous_hash: (Optional) Hash of previous Block

:return: New Block

"""

block={

"index":len(self.chain)+1,

"timestamp":time(),

"transactions":self.current_transactions,

"proof":proof,

"previous_hash":previous_hashorself.hash(self.chain[-1]),

}

# Reset the current list of transactions

self.current_transactions=[]

self.chain.append(block)

returnblock

defnew_transaction(self,sender,recipient,amount):

"""

Creates a new transaction to go into the next mined Block

:param sender: Address of the Sender

:param recipient: Address of the Recipient

:param amount: Amount

:return: The index of the Block that will hold this transaction

"""

self.current_transactions.append({

"sender":sender,

"recipient":recipient,

"amount":amount,

})

returnself.last_block["index"]+1

@property

deflast_block(self):

returnself.chain[-1]

@staticmethod

defhash(block):

"""

Creates a SHA-256 hash of a Block

:param block: Block

:return:

"""

# We must make sure that the Dictionary is Ordered, or we"ll have inconsistent hashes

block_string=json.dumps(block,sort_keys=True).encode()

returnhashlib.sha256(block_string).hexdigest()

上面的內容簡單明了 —— 我添加了一些注釋和文檔字元串,以使代碼清晰可讀。到此為止,表示我們的區塊鏈基本上要完成了。但是,你肯定想知道新區塊是如何被創建、打造或者挖礦的。


工作量證明Proof of Work(PoW)演算法是在區塊鏈上創建或者挖出新區塊的方法。PoW 的目標是去撞出一個能夠解決問題的數字。這個數字必須滿足「找到它很困難但是驗證它很容易」的條件 —— 網路上的任何人都可以計算它。這就是 PoW 背後的核心思想。

我們來看一個非常簡單的示例來幫助你了解它。

我們來解決一個問題,一些整數 乘以另外一個整數 的結果的哈希值必須以 結束。因此,。為簡單起見,我們先把 固定下來。在 Python 中的實現如下:

fromhashlibimportsha256

x=5

y=# We don"t know what y should be yet...

whilesha256(f"".encode()).hexdigest()[-1]!="0":

y+=1

print(f"The solution is y = ")

在這裡的答案是。因為它產生的哈希值是以 0 結尾的:

hash(5 * 21) = 1253e9373e...5e3600155e860

在比特幣中,工作量證明演算法被稱之為 Hashcash。與我們上面的例子沒有太大的差別。這就是礦工們進行競賽以決定誰來創建新塊的演算法。一般來說,其難度取決於在一個字元串中所查找的字元數量。然後礦工會因其做出的求解而得到獎勵的幣——在一個交易當中。

網路上的任何人都可以很容易地去核驗它的答案。


為我們的區塊鏈來實現一個簡單的演算法。我們的規則與上面的示例類似:

找出一個數字 ,它與前一個區塊的答案進行哈希運算得到一個哈希值,這個哈希值的前四位必須是由 組成。

importhashlib

importjson

fromtimeimporttime

fromuuidimportuuid4

classBlockchain(object):

...

defproof_of_work(self,last_proof):

"""

Simple Proof of Work Algorithm:

- Find a number p" such that hash(pp") contains leading 4 zeroes, where p is the previous p"

- p is the previous proof, and p" is the new proof

:param last_proof:

:return:

"""

proof=

whileself.valid_proof(last_proof,proof)isFalse:

proof+=1

returnproof

@staticmethod

defvalid_proof(last_proof,proof):

"""

Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?

:param last_proof: Previous Proof

:param proof: Current Proof

:return: True if correct, False if not.

"""

guess=f"".encode()

guess_hash=hashlib.sha256(guess).hexdigest()

returnguess_hash[:4]=="0000"

為了調整演算法的難度,我們可以修改前導 0 的數量。但是 4 個零已經足夠難了。你會發現,將前導 0 的數量每增加一,那麼找到正確答案所需要的時間難度將大幅增加。

我們的類基本完成了,現在我們開始去使用 HTTP 請求與它交互。


第 2 步:以 API 方式去訪問我們的區塊鏈

我們將使用 Python Flask 框架。它是個微框架,使用它去做端點到 Python 函數的映射很容易。這樣我們可以使用 HTTP 請求基於 web 來與我們的區塊鏈對話。

我們將創建三個方法:

在一個區塊上創建一個新交易

告訴我們的伺服器去挖礦一個新區塊

返回完整的區塊鏈


我們的 「伺服器」 將在我們的區塊鏈網路中產生一個單個的節點。我們來創建一些樣板代碼:

importhashlib

importjson

fromtextwrapimportdedent

fromtimeimporttime

fromuuidimportuuid4

fromflaskimportFlask

classBlockchain(object):

...

# Instantiate our Node

app=Flask(__name__)

# Generate a globally unique address for this node

node_identifier=str(uuid4()).replace("-","")

# Instantiate the Blockchain

blockchain=Blockchain()

@app.route("/mine",methods=["GET"])

defmine():

return"We"ll mine a new Block"

@app.route("/transactions/new",methods=["POST"])

defnew_transaction():

return"We"ll add a new transaction"

@app.route("/chain",methods=["GET"])

deffull_chain():

response={

"chain":blockchain.chain,

"length":len(blockchain.chain),

}

returnjsonify(response),200

if__name__=="__main__":

app.run(host="0.0.0.0",port=5000)

對上面的代碼,我們做添加一些詳細的解釋:

Line 15:實例化我們的節點。更多關於 Flask 的知識讀 這裡。

Line 18:為我們的節點創建一個隨機的名字。

Line 21:實例化我們的區塊鏈類。

Line 24–26:創建端點,這是一個 GET 請求。

Line 28–30:創建端點,這是一個 POST 請求,因為我們要發送數據給它。

Line 32–38:創建端點,它返回全部區塊鏈。

Line 40–41:在 5000 埠上運行伺服器。


這就是對一個交易的請求,它是用戶發送給伺服器的:

{

"sender":"my address",

"recipient":"someone else"s address",

"amount":5

}

因為我們已經有了添加交易到塊中的類方法,剩下的就很容易了。讓我們寫個函數來添加交易:

importhashlib

importjson

fromtextwrapimportdedent

fromtimeimporttime

fromuuidimportuuid4

fromflaskimportFlask,jsonify,request

...

@app.route("/transactions/new",methods=["POST"])

defnew_transaction():

values=request.get_json()

# Check that the required fields are in the POST"ed data

required=["sender","recipient","amount"]

ifnotall(kinvaluesforkinrequired):

return"Missing values",400

# Create a new Transaction

index=blockchain.new_transaction(values["sender"],values["recipient"],values["amount"])

response={"message":f"Transaction will be added to Block "}

returnjsonify(response),201

創建交易的方法


我們的挖礦端點是見證奇蹟的地方,它實現起來很容易。它要做三件事情:

計算工作量證明

因為礦工(我們)添加一個交易而獲得報酬,獎勵礦工(我們) 1 個幣

通過將它添加到鏈上而打造一個新區塊

importhashlib

importjson

fromtimeimporttime

fromuuidimportuuid4

fromflaskimportFlask,jsonify,request

...

@app.route("/mine",methods=["GET"])

defmine():

# We run the proof of work algorithm to get the next proof...

last_block=blockchain.last_block

last_proof=last_block["proof"]

proof=blockchain.proof_of_work(last_proof)

# We must receive a reward for finding the proof.

# The sender is "0" to signify that this node has mined a new coin.

blockchain.new_transaction(

sender="0",

recipient=node_identifier,

amount=1,

)

# Forge the new Block by adding it to the chain

previous_hash=blockchain.hash(last_block)

block=blockchain.new_block(proof,previous_hash)

response={

"message":"New Block Forged",

"index":block["index"],

"transactions":block["transactions"],

"proof":block["proof"],

"previous_hash":block["previous_hash"],

}

returnjsonify(response),200

注意,挖掘出的區塊的接收方是我們的節點地址。現在,我們所做的大部分工作都只是與我們的 類的方法進行交互的。到目前為止,我們已經做完了,現在開始與我們的區塊鏈去交互。


第 3 步:與我們的區塊鏈去交互

你可以使用簡單的 cURL 或者 Postman 通過網路與我們的 API 去交互。

啟動伺服器:

$pythonblockchain.py

*Running onhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)

我們通過生成一個 請求到 去嘗試挖一個區塊:

使用 Postman 去生成一個 GET 請求

我們通過生成一個 請求到 去創建一個區塊,請求數據包含我們的交易結構:

使用 Postman 去生成一個 POST 請求

如果你不使用 Postman,也可以使用 cURL 去生成一個等價的請求:

$curl-XPOST-H"Content-Type: application/json"-d"{

"sender": "d4ee26eee15148ee92c6cd394edd974e",

"recipient": "someone-other-address",

"amount": 5

}""http://localhost:5000/transactions/new"

我重啟動我的伺服器,然後我挖到了兩個區塊,這樣總共有了 3 個區塊。我們通過請求來檢查整個區塊鏈:

{

"chain":[

{

"index":1,

"previous_hash":1,

"proof":100,

"timestamp":1506280650.770839,

"transactions":[]

},

{

"index":2,

"previous_hash":"c099bc...bfb7",

"proof":35293,

"timestamp":1506280664.717925,

"transactions":[

{

"amount":1,

"recipient":"8bbcb347e0634905b0cac7955bae152b",

"sender":"0"

}

]

},

{

"index":3,

"previous_hash":"eff91a...10f2",

"proof":35089,

"timestamp":1506280666.1086972,

"transactions":[

{

"amount":1,

"recipient":"8bbcb347e0634905b0cac7955bae152b",

"sender":"0"

}

]

}

],

"length":3

}


第 4 步:共識

這是很酷的一個地方。我們已經有了一個基本的區塊鏈,它可以接收交易並允許我們去挖掘出新區塊。但是區塊鏈的整個重點在於它是去中心化的decentralized。而如果它們是去中心化的,那我們如何才能確保它們表示在同一個區塊鏈上?這就是共識Consensus問題,如果我們希望在我們的網路上有多於一個的節點運行,那麼我們將必須去實現一個共識演算法。


在我們能實現一個共識演算法之前,我們需要一個辦法去讓一個節點知道網路上的鄰居節點。我們網路上的每個節點都保留有一個該網路上其它節點的註冊信息。因此,我們需要更多的端點:

以 URL 的形式去接受一個新節點列表

去實現我們的共識演算法,由它來解決任何的衝突 —— 確保節點有一個正確的鏈。

我們需要去修改我們的區塊鏈的構造函數,來提供一個註冊節點的方法:

...

fromurllib.parseimporturlparse

...

classBlockchain(object):

def__init__(self):

...

self.nodes=set()

...

defregister_node(self,address):

"""

Add a new node to the list of nodes

:param address: Address of node. Eg. "http://192.168.0.5:5000"

:return: None

"""

parsed_url=urlparse(address)

一個添加鄰居節點到我們的網路的方法

注意,我們將使用一個 去保存節點列表。這是一個非常合算的方式,它將確保添加的節點是冪等idempotent的 —— 這意味著不論你將特定的節點添加多少次,它都是精確地只出現一次。


正如前面提到的,當一個節點與另一個節點有不同的鏈時就會產生衝突。為解決衝突,我們制定一個規則,即最長的有效的鏈才是權威的鏈。換句話說就是,網路上最長的鏈就是事實上的區塊鏈。使用這個演算法,可以在我們的網路上節點之間達到共識。

...

importrequests

classBlockchain(object)

...

defvalid_chain(self,chain):

"""

Determine if a given blockchain is valid

:param chain:

A blockchain

:return: True if valid, False if not

"""

last_block=chain[]

current_index=1

whilecurrent_index

block=chain[current_index]

print(f"")

print(f"")

print("
-----------
")

# Check that the hash of the block is correct

ifblock["previous_hash"]!=self.hash(last_block):

returnFalse

# Check that the Proof of Work is correct

ifnotself.valid_proof(last_block["proof"],block["proof"]):

returnFalse

last_block=block

current_index+=1

returnTrue

defresolve_conflicts(self):

"""

This is our Consensus Algorithm, it resolves conflicts

by replacing our chain with the longest one in the network.

:return: True if our chain was replaced, False if not

"""

neighbours=self.nodes

new_chain=None

# We"re only looking for chains longer than ours

max_length=len(self.chain)

# Grab and verify the chains from all the nodes in our network

fornodeinneighbours:

response=requests.get(f"http:///chain")

ifresponse.status_code==200:

length=response.json()["length"]

chain=response.json()["chain"]

# Check if the length is longer and the chain is valid

iflength>max_lengthandself.valid_chain(chain):

max_length=length

new_chain=chain

# Replace our chain if we discovered a new, valid chain longer than ours

ifnew_chain:

self.chain=new_chain

returnTrue

returnFalse

第一個方法 是負責來檢查鏈是否有效,它通過遍歷區塊鏈上的每個區塊並驗證它們的哈希和工作量證明來檢查這個區塊鏈是否有效。

方法用於遍歷所有的鄰居節點,下載它們的鏈並使用上面的方法去驗證它們是否有效。如果找到有效的鏈,確定誰是最長的鏈,然後我們就用最長的鏈來替換我們的當前的鏈。

在我們的 API 上來註冊兩個端點,一個用於添加鄰居節點,另一個用於解決衝突:

@app.route("/nodes/register",methods=["POST"])

defregister_nodes():

values=request.get_json()

nodes=values.get("nodes")

ifnodesisNone:

return"Error: Please supply a valid list of nodes",400

fornodeinnodes:

blockchain.register_node(node)

response={

"message":"New nodes have been added",

"total_nodes":list(blockchain.nodes),

}

returnjsonify(response),201

@app.route("/nodes/resolve",methods=["GET"])

defconsensus():

replaced=blockchain.resolve_conflicts()

ifreplaced:

response={

"message":"Our chain was replaced",

"new_chain":blockchain.chain

}

else:

response={

"message":"Our chain is authoritative",

"chain":blockchain.chain

}

returnjsonify(response),200

這種情況下,如果你願意,可以使用不同的機器來做,然後在你的網路上啟動不同的節點。或者是在同一台機器上使用不同的埠啟動另一個進程。我是在我的機器上使用了不同的埠啟動了另一個節點,並將它註冊到了當前的節點上。因此,我現在有了兩個節點: 和 。

註冊一個新節點

我接著在節點 2 上挖出一些新區塊,以確保這個鏈是最長的。之後我在節點 1 上以 方式調用了 ,這時,節點 1 上的鏈被共識演算法替換成節點 2 上的鏈了:

工作中的共識演算法

然後將它們封裝起來 … 找一些朋友來幫你一起測試你的區塊鏈。

我希望以上內容能夠鼓舞你去創建一些新的東西。我是加密貨幣的狂熱擁護者,因此我相信區塊鏈將迅速改變我們對經濟、ZF 和記錄保存的看法。

【關於投稿】

如果大家有原創好文投稿,請直接給公號發送留言。

① 留言格式:

【投稿】+《 文章標題》+ 文章鏈接

② 示例:

【投稿】《不要自稱是程序員,我十多年的 IT 職場總結》:http://blog.jobbole.com/94148/

③ 最後請附上您的個人簡介哈~

看完本文有收穫?請轉發分享給更多人

關注「Python開發者」,提升Python技能


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Python 的精彩文章:

一隻健壯的Python爬蟲應當怎麼處理異常情況
大家都在學的編程語言 Python,可以用來幹什麼?

TAG:Python |