Learn Blockchains by Building One Learn Blockchains by Building One


On October 24 last year, blockchain was elevated to a national strategic position, and many companies, including traditional large enterprises and central enterprises, have laid out blockchain. There is a wave of learning blockchain in the society, and blockchain talents have become “in demand”.


For programmers like us, the quickest way to learn blockchain is to create one ourselves. Below, we will lead you to build a simple blockchain, learn in practice, can not only deepen the understanding of blockchain, but also gain skills.


A blockchain is a chain of immutable, continuous blocks that record information. Blocks contain transaction information, files, or whatever data you want to record. Most importantly, the blocks are linked together by “hashes”.


To start, you need to install Python3.6+ (and PIP), Flask, and Requests Library:

PIP install Flask = = 0.12.2 requests = = 2.18.4Copy the code


You also need an HTTP client, either Postman or cURL.


Now we’re ready to go.


Step 1: Create a blockchain

Open a text editor or IDE, PyCharm is recommended.


Create a new file named blockchain.py. The entire process uses only one file, and if you lose it, you can find the source here: github.com/dvf/blockch…


Render blockchain

Create a blockchain class, in which constructor creates a raw blank list to store the blockchain and another to store transactions. Here is the blueprint for class:

class Blockchain(object):
    def __init__(self):
        self.chain = []
        self.current_transactions = []
        
    def new_block(self):
        # Creates a new Block and adds it to the chain
        pass
    
    def new_transaction(self):
        # Adds 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
        pass
Copy the code

Blockchain Class manages the chain, stores transactions, and assists in adding new blocks to the chain.


What does the block look like?

Each block has an index, a timestamp, a list of transactions, a proof, and a hash of the previous block. Here is an example:

block = {
    'index': 1,
    'timestamp': 1506057125.900785.'transactions': [{'sender': "8527147fe1f5426f9dd545de4b27ee00".'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f".'amount': 5,}],'proof': 324984774000,
    'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
Copy the code


As you can see from this example, the idea of “chain” is still very obvious — each new block contains the hash value of the previous block. This is crucial because it makes the blockchain “immutable” : if an attacker destroys an earlier block on the chain, all subsequent blocks will contain incorrect hashes.


Add a transaction to the block

Use the new_transaction () :

class Blockchain(object):
    ...
    
    def new_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,
        })

        return self.last_block['index'] + 1
Copy the code

After new_TRANSACTION () adds a transaction to the list, it returns the index of the next block to be mined. This is useful for users submitting transactions.


Create a new block

Once Blockchain is embodied, we need to create genesis Blocks. First, we need to add a proof to genesis blockchain, which is the result of mining (proof of work). We’ll talk about mining later.


In addition to adding a creation block to constructor, fill new_block(), new_transaction(), hash() :

import hashlib
import json
from time import time


class Blockchain(object):
    def __init__(self):
        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: 
       
         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_hash or self.hash(self.chain[-1]),
        }

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

        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: 
       
         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,
        })

        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: 
       
         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()
        return hashlib.sha256(block_string).hexdigest()
Copy the code


The example above should be pretty clear, but TO help you understand it, I’ve also added some comments and docstrings. We’re almost done with the blockchain demo. Next, let’s see how a new block is dug up.


Proof of Work

The proof-of-work algorithm is how blocks are created or dug up. The goal of proof of work is to find a “number” that solves the puzzle, a number that is hard to find but easy to verify through calculations by people in the network.


Let’s look at a very simple example to further understand this problem.


First define: the hash of some integer X times Y must end in 0, so hash (X * Y) =ac23dc… 0, where X is equal to 5. Implementation in Python:

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}')
Copy the code


The solution here is Y=21, because the resulting hash ends in 0:

hash(5 * 21) = 1253e9373e... 5e3600155e860Copy the code


In the Bitcoin blockchain, the proof-of-work algorithm is called Hashcash and is not much different from the example above. This is the algorithm that miners are competing to solve in order to create a new block, and the difficulty depends on the number of characters searched in the string. Miners who successfully dig out the blocks will then receive bitcoins as a reward.


Networks can easily validate their solutions.


Deploy basic proof of work

Next, deploy a similar algorithm for our blockchain with rules similar to the example above.


When we hash with the hash value 4 of the previous block, we find a number P and output “0s” :

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 zeroes, where p is the previous p' - p is the previous proof, and p' is the new proof :param last_proof: 
       
         :return: 
        
          "
        
       ""

        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: 
       
         Previous Proof :param proof: 
        
          Current Proof :return: 
         
           True if correct, False if not. "
         
        
       ""

        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"
Copy the code


To adjust the difficulty of the algorithm, we can modify the number of leading zeros. But four is enough, and you’ll find that adding a leading zero makes a huge difference in the time it takes to find a solution.


At this point, the class is almost complete, and you’re ready to start interacting with it using HTTP requests.


Step 2: Blockchain as an API

Using the Python Flask Framework, create three paths:

/transactions/new

/mine

/chain


Set the Flask

Our server is a node in the blockchain network, so let’s create some boilerplate code:

import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4

from flask import Flask


class Blockchain(object):
    ...


# Instantiate our Node
app = Flask(__name__)

# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace(The '-'.' ')

# 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)
Copy the code


A quick explanation of what was added above: Line15: has a materialized node

Line18: Create a random name for the node

Line21: Implement the Blockchain Class

Line24-26: Create /mine endpoint, which is a GET request.

Line28-30: Create the /transactions/new endpoint, which is a POST request because we are sending data to it.

Line32-38: Create the /chain endpoint and return the entire blockchain.

Line40-41: Run the server on port 5000.


Trading endpoint

This is an example of a trade request. Here’s what the user sends to the server:

{
 "sender": "my address"."recipient": "someone else's address"."amount": 5}Copy the code


Once you have the class path to add transactions to the block, the rest is simple. Write a function to add transactions:

import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4

from flask import Flask, jsonify, request

...

@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
Copy the code


Dig the endpoint

Mining endpoints are places where miracles happen, and they do three things:

Proof of work

Bonus 1 coin for successfully adding block absenteeism

Add the new block to the chain


import hashlib
import json

from time import time
from uuid import uuid4

from flask import Flask, jsonify, request

...

@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
Copy the code


Note that the recipient of the dug block is the address of our node. We did all this to interact with the path in the Blockchain class, and now we can interact with the Blockchain.


Step3: interact with Blockchain

You can use regular cURL or Postman to interact with the API over the web.


Start the server:

$python blockchain.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Copy the code


Let’s try to dig a block by a GET request to http://localhost:5000/mine:.



Create a GET request with Postman


Through a POST request to http://localhost:5000/transactions/new to create a deal.



Create POST request with Postman


Do you have something to cURL up with?

$ curl -X POST -H "Content-Type: application/json" -d '{ "sender": "d4ee26eee15148ee92c6cd394edd974e", "recipient": "someone-other-address", "amount": 5 }' "http://localhost:5000/transactions/new"
Copy the code


Restart the server, dug out two pieces, now has a total of three blocks, then by requesting http://localhost:5000/chain to check the whole chain.


{
  "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}Copy the code


Step4: consensus

We now have a basic blockchain that accepts transactions and mines blocks. But the core of blockchain is decentralization. And if they are decentralized, how do you ensure that they map the entire chain? This is where consensus comes in. If we want to be decentralized, we need to make sure that there are multiple nodes in the network, and with multiple nodes, we need consensus algorithms.


Registering a New node

Before deploying the consensus algorithm, we need to find a way for nodes to know their neighbors on the network. Each node on the network should hold registries for other nodes. Therefore, we need more endpoints:

/nodes/register Receives a list of new nodes in URL format

/ Nodes /resolve Deploys the consensus algorithm to resolve conflicts and ensure that all nodes are on the correct chain.


We need to modify the constructor of the blockchain and provide methods for registering nodes:

. from urllib.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.eg. 'http://192.168.0.5:5000' :return: None """

        parsed_url = urlparse(address)
        self.nodes.add(parsed_url.netloc)
Copy the code


Notice that we use set () to hold the list of nodes. This ensures that the addition of new nodes is idempotent, meaning that the same node occurs only once no matter how many times it is added.


Deploying consensus algorithms

Node conflict is when one node diverges from another node and has a different chain. To solve this problem, we make a rule that the longest chain is the correct chain. With this algorithm, nodes in the network reach a consensus.


. import requests class Blockchain(object) ... def valid_chain(self, chain):""" Determine if a given blockchain is valid :param chain: 
       
         A blockchain :return: 
        
          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 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
        for node in neighbours:
            response = requests.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
Copy the code


The first path, valid_chain(), checks that the chain is valid by “patrolling” each block and verifying the block’s hash and proof.


Resolve_conflicts () is responsible for “patrolling” all neighboring nodes, downloading their chain, and verifying that they use the above path. If we find a valid chain larger than ours, that chain will replace our chain.


Let’s register these two endpoints with our API, one for adding adjacent nodes and the other for resolving conflicts:

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

    nodes = values.get('nodes')
    if nodes is None:
        return "Error: Please supply a valid list of nodes", 400,for node 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
Copy the code



At this point, you can switch to a different computer if you like, or rotate different nodes in your network. Or start the process using a different port on the same computer. I create another node on another port on my computer and register it with the current node. Thus, I have two nodes:

http://localhost:5000

http://localhost:5001




(Register a new node)


I then cut out some new blocks on node 2 to make the chain longer. After that, I call GET/Nodes /resolve on node 1, where the chain is replaced by a consensus algorithm.




(Consensus algorithm at work)


At this point, a complete public blockchain is almost ready, and you can get some of your friends to test it out for you.


Wanxiang Blockchain has been involved in blockchain since 2015. If you are interested in blockchain, welcome to join us!