Learn Blockchain by Building One

Before we get started…

  • 所謂的區塊鏈:不可變動連續紀錄鏈結 (區塊)
    • 其中可包含 交易檔案、任何資料
    • 這些都透過hash做鏈結
  • 工具:
    • Python 3.6+
      • venv (recommend)
      • Flask
      • requests
      • hashlib
      • json
      • time

Step1: Building a Blockchain

  • 新創一個檔案blockchain.py
  • 檔案中創建一個類別 Blockchain
    • 其中建構子:
      • 起始空串列 => 儲存區塊鏈
      • 其他儲存交易紀錄
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        # blockchain.py

        class Blockchain(object):
        def __init__(self):
        # Creates an initial empty list to store our blockchain
        self.chain = []
        self.current_transactions = []

        def new_block(self):
        # Creates a new Block and adds it to the chain
        pass

        def new_transaction(self):
        # Add a new transaction to the list of transactions
        pass

        @staticmethod
        def hash(block):
        # Hashes a Block
        pass

        @Property
        def last_block(self):
        # Returns the last Block in the chain
  • Blockchain類別負責管理整條鏈結:
    • 儲存交易紀錄
    • 提供新增區塊的有效方法

What does a Block look like?

  • 每個區塊都有:
    • index
    • timestamp
    • a list of transations
    • a proof
    • hash of the previous Block
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      block = {
      'index': 1,
      'timestamp': 1506057125.900785,
      'transactions': [
      {
      'sender': "8527147fe1f5426f9dd545de4b27ee00",
      'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
      'amount': 5,
      }
      ],
      'proof': 324984774000,
      'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
      }
  • 如此便可明瞭,之所以為鏈結正式因為:
    • 每個新的區塊都包含前一個區塊的hash值
    • 而這也是區塊鏈不變性的關鍵

Adding Transactions to a Block

  • 為了需要新增交易,由new_transationc()負責,其中非常直觀的去理解
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    # blockchain.py

    class Blockchain(object):

    # ...

    def new_transaction(self, sender, recipient, amount):
    """
    Creates a new transaction to go into the text mined Block

    :param sender: <str> Address of the Sender
    :param recipient: <str> Address of the Recipient
    :param amount: <int> Amount
    :return: <int> The index of the Block that will hold this transaction
    """

    self.current_transactions.append({
    'sender': sender,
    'recipient': recipient,
    'amount': amount,
    })

    # ...

  • 其中的參數含意:
    • sender: 寄送者的位址
    • recipient: 接收者的位址
    • amount: 金額
  • new_transaction()新增交易到串後,會回傳index到區塊,讓下一個能夠去挖掘

Creating new Blocks

  • Blockchain類別實例化後,我們需要給出起始區塊
  • 同時我們也要新增”proof”到起始區塊中 => 即挖掘結果
  • 為了新增起始區塊到建構子,我們需要寫new_block()new_transaction()hash()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    # blockchain.py

    import hashlib
    import json
    from time import time

    class Blockchain(object):
    def __init__(self):
    # Creates an initial empty list to store our blockchain
    self.current_transactions = []
    self.chain = []

    # Create the genesis block
    self.new_block(previous_hash = 1, proof = 100)

    def new_block(self, proof, previous_hash = None):
    """
    Create a new Block in the Blockchain

    :param proof: <int> The proof given by the Proof of Work algorithm
    :param previous_hash: (Optional) <str> Hash of previous Block
    :return: <dict> New Block
    """

    block = {
    'index': len(self.chain) + 1,
    'timestamp': time(),
    'transactios': self.current_transactions,
    'proof': proof,
    'previous_hash': previous_hash or self.hash(self.chain[-1]),
    }

    # Reset the current list of transactions
    self.current_transaction = []

    self.chain.append(block)

    return block

    def new_transaction(self, sender, recipient, amount):
    """
    Creates a new transaction to go into the next mined Block

    :param sender: <str> Address of the Sender
    :param recipient: <str> Address of the Recipient
    :param amount: <int> Amount
    :return: <int> The index of the Block that will hold this transaction
    """

    self.current_transactions.append({
    'sender': sender,
    'recipient': recipient,
    'amount': amount,
    })

    return self.last_block['index'] + 1

    @property
    def last_block(self):
    return self.chain[-1]

    @staticmethod
    def hash(block):
    """
    Creates a SHA-256 hash of a Block

    :param block: <dict> Block
    :return: <str>
    """

    # We must make sure that the Dictionay is Ordered, or we'll have inconsistent hashes
    block_string = json.dumps(block, sort_keys = True).encode()

    return hashlib.sha256(block_string).hexdigest()
  • 到此幾乎可以呈現區塊鏈,在此之前需要理解新的區塊如何創建、偽造、挖掘

Understanding Proof of Work

  • PoW => 創建新區塊 / 區塊鏈挖礦 的演算法
    • 目的:找一個能夠解決問題的數字(難找但容易驗證) - computationally speaking
    • 核心概念:
      • 決定一 hash ,用整數x * y(結尾為0)
        => 所以 hash(x * y) = ac23dc .... 0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        from hashlib import sha256

        x = 5
        y = 0 # We don't know what y should be yet.

        while sha256(f'{x * y}'.encode()).hexdigest()[-1] != "0":
        y += 1

        print(f'The solution is y = {y}')
      •  結果 => y = 21時,hash值的結尾就是0
        1
        hash(5 * 21) = 1253e9373e781b7500266caa55150e08e210bc8cd8cc70d89985e3600155e860
  • Bitcoin中,PoW演算法被稱為 *Hashcash*,概念跟上述範例相差不遠
    • 這是讓miner相競去解決題目,為了創建新的區塊 => 解決後會有獎勵回饋
    • 困難度由從字串中搜尋的字元數量決定

Implementing Basic Proof of Work

  • 接著運行類似的演算法在區塊鏈上,運算原則與上述類似:
    • 找一個數字p
    • hash包含前一區塊運算結果的hash值
    • 開頭為4個0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# blockchain.py
import hashlib
import json
from time import time
from uuid import uuid4

class Blockchain(object):

# ...

def proof_of_work(self, last_proof):
"""
Simple Proof of Work Algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeros, where p is the previous p'
- p is the previous proof, and p' is the new proof

:param last_proof: <int>
:return: <int>
"""

proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1

return proof

@staticmethod
def valid_proof(last_proof, proof):
"""
Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?

:param last_proof: <int> Previous Proof
:param proof: <int> Current Proof
:return: <bool> True if correct, False if not.
"""

guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()

return guess_hash[:4] == "0000"
```
* 在演算法中:
* 為了調整困難度 => 修改前幾位的零 (4位最有效率)
* 多一位零的調整就會有極大的差距

## **Step 2: Our Blockchain as an API**
* 使用Flask作為區塊鏈的網路基礎架構來應對HTTP requests
* 主要創建三個method:
* `/transactions/new` => 創立新的區塊交易
* `/mine` => 告知server挖掘新區塊
* `/chain` => 回傳所有區塊鏈

### *Setting up Flask*
* 目前使用單一節點作為區塊鏈網路,新建一個`application.py`
```python=
# application.py

import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4
from flask import Flask
from blockchain import *

# Instantiate our Node
app = Flask(__name__)

# Generate a globally unique address for this node,
# which means create a random name for our node
node_identifier = str(uuid4()).replace('-', '')

# Instantiate the Blockchain
blockchain = Blockchain()

# ---------------------------------------------------------
@app.route('/mine', methods = ['GET'])
def mine():
return "We'll mine a new Block."

@app.route('/transactions/new', methods = ['POST']
def new_transaction():
return "We'll add a new transaction."

@app.route('/chain', methods = ['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200

if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 5000)

The Transactions Endpoint

  • 交易內容主要如下:
    1
    2
    3
    4
    5
    {
    "sender": "my address",
    "recipient": "someone else's address",
    "amount": 5
    }
  • 程式如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # application.py

    # ...

    @app.route('/transactions/new', methods = ['POST'])
    def new_transaction():
    values = request.get_json()

    # Check that the required fields are in the POST'ed data
    required = ['sender', 'recipient', 'amount']
    if not all(k in values for k in required):
    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 {index}.'
    }
    return jsonify(response), 201

The Mining Endpoint

  • 在挖礦的函式中主要實現三件事:
    • 計算PoW
    • 回饋礦工 => 獎勵一枚幣
    • 生產新的區塊並加到鏈上
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      # application.py

      # ...

      @app.route('/mine', methods = ['GET'])
      def mine():
      # 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'],
      }
      return jsonify(response), 200
  • 至此,基本的區塊鏈架構已然完成大部分,可以實際進行運作

Step 3: Interacting with our Blockchain

  • 實際運行剛剛所架設的API (即運行Flask)

    Windows可以使用Postman進行API測試
    Linux則可以使用cURL指令

    1
    2
    3
    $ python application.py

    * Running on https://0.0.0.0:5000/ (Press CTRL+C to quit)
  • 首先連上http://127.0.0.1:5000/mine去嘗試挖礦 => 送出GET請求
  • 接著連上http://127.0.0.1:5000/transactions/new來交易 => 送出POST請求
    • 先連接http://127.0.0.1:5000/chain看整串區塊鏈
    • 找到在mining過後的幾個區塊中,recipient的部分
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      {
      "chain": [
      // ...
      {
      "index": 2,
      "previous_hash": "87f42077ebfddda1d736c649e5e8bdf40c392b95a4ce153cf5bbb2facbc7e36b",
      "proof": 35293,
      "timestamp": 1557847550.1349301,
      "transactions": [
      {
      "amount": 1,
      "recipient": "da4239608ef04acbb19869a8581a0fcc",
      "sender": "0"
      }
      ]
      },
      ]
      }
    • 再送出請求前,點到body的部分,點raw並選擇Json編碼,輸入交易內容:
      • 如範例中的位址為da4239608ef04acbb19869a8581a0fcc,替換 “my address”的部分
        1
        2
        3
        4
        5
        {
        "sender": "my address",
        "recipient": "someone else's address",
        "amount": 5
        }

        如果使用cURL,則輸入:

        1
        2
        3
        4
        5
        $ curl -X POST -H "Content-Type: application/json" -d '{
        "sender": "..(This is hash value)..",
        "recipient": "someone-other-address",
        "amount": 5
        }' "http://127.0.0.1:5000/transactions/new"
  • 重啟伺服器後,連接http://127.0.0.1:5000/chain來看整串鏈

Step 4: Consensus

  • 至此已經把整個基礎區塊鏈架構完成 => 交易、挖礦
    • 但是區塊鏈主要應該是 ==去中心化==
  • 要如何讓全世界都維護同一條鏈結 => 共識決問題 (Consensus)

Registering New Nodes

  • 在實作共識決演算法:
    • 需要讓節點知道鄰近節點
    • 每個節點都需要在整個區塊鏈網路上登記
  • 需要新增的function:
    • /nodes/register => 接受一串新節點,用URL註冊
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      # blockchain.py

      # ...
      from urlib.parse import urlparse

      class Blockchain(object):
      def __init__(self):
      # ...
      self.nodes = set()

      # ...

      def register_node(self, address):
      """
      Add a new node to the list of nodes

      :param address: <str> Address of node. e.g. 'http://192.168.0.5:5000'
      :return: None
      """

      parsed_url = urlparse(address)
      self.nodes.add(aprsed_url.netloc)
      • set()保留節點清單 => 保證新節點不變性最輕鬆的方法
    • /nodes/resulve => 實作共識決演算法,解決區塊衝突以保證每個節點維護的是正確的鏈結

Implementing the Consensus Algorithm

  • 衝突的原因:兩節點間維護不同的鏈結

    • 為了解決衝突 => 我們設定的原則是 鏈結的有效區塊愈長愈可靠
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    # blockchain.py

    # ...
    import requests

    class Blockchain(object):

    # ...

    def valid_chain(self, chain):
    """
    Determine if a given blockchain is valid.

    :param chain: <list> A blockchain
    :return: <bool> True if valid, False if not
    """

    last_block = chain[0]
    current_index = 1

    while current_index < len(chain):
    block = chain[current_index]
    print(f'{last_block}')
    print(f'{block}')
    print("\n-------------\n")

    # Check that the hash of the block is correct
    if block['previous_hash'] != self.hash(last_block):
    return False

    # Check that the Proof of Work is correct
    if not self.valid_proof(last_block['proof'], block['proof']):
    return False

    last_block = block
    current_index += 1

    return True

    def resolve_conflicts(self):
    """
    This is our Consensus Algorithm, it resolves conflicts by replacing our chain with the longest one in the netword.

    :return: <bool> 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
    for node in neighbours:
    response = request.get(f'http://{node}/chain')

    if response.status_code == 200:
    length = response.json().['length']
    chain = response.json().['chain']

    # Check if the length is longer and the chain is valid
    if length > max_length and self.valid_chain(chain):
    max_length = length
    new_chain = chain

    # Replace our chain if we discovered a new, valid chain longer than ours
    if new_chain:
    self.chain = new_chain
    return True
    return False
    • valid_chain() => 利用迴圈對每個區塊的hash跟proof值進行驗證以確認鏈結的有效性
    • resolve_conflicts() => 利用迴圈走周圍所有區塊,下載他們的鏈結並做驗證
      • 如果某個有效鏈結長度較長則去代現有的鏈結
  • 將兩個method新增到API中:

    • 一用於新增鄰近節點
    • 一用於解決衝突
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      # application.py

      # ...

      @app.route('/nodes/register', methods = ['POST'])
      def register_nodes():
      values = request.get_json()
      nodes = values.get('nodes')

      if nodes in nodes:
      blockchain.register_node(node)

      response = {
      'message': 'New nodes have been added',
      'total_nodes': list(blockchain.nodes),
      }
      return jsonify(response, 201)

      @app.route('/nodes/resolve', methods = ['GET'])
      def consensus():
      replaced = blockchain.resolve_conflicts()

      if replaced:
      response = {
      'message': Our chain was replaced',
      'new_chain': blockchain.chain
      }
      else:
      response = {
      'message': 'Our chain is authoritative',
      'chain': blockchain.chain
      }

      return jsonify(response), 200

Reference Websites:

  1. Learn Blockchains by Building One
  • Copyrights © 2019-2021 NIghTcAt

請我喝杯咖啡吧~