Featured Post

What is Paxful Wallet? | How do I Make a Paxful Account?

Paxful was set up in 2015 by a group of experienced engineers whose objective was to give a safe and clear trade stage for anybody to pu...

Learn Blockchains by Building One | Can You Create Your Own Blockchain?


You're here in light of the fact that, similar to me, you're psyched about the ascent of Cryptocurrencies. Furthermore, you need to know how Blockchains work—the central innovation behind them.

Be that as it may, understanding Blockchains isn't simple—or if nothing else wasn't for me. I walked through thick recordings, followed permeable instructional exercises, and managed the enhanced dissatisfaction of too barely any models.

I like learning by doing. It drives me to manage the topic at a code level, which makes it stick. In the event that you do likewise, toward the finish of this guide you'll have a working Blockchain with a strong handle of how they work.


Speaking to a Blockchain

We'll make a Blockchain class whose constructor makes an underlying void rundown (to store our blockchain), and another to store exchanges. Here's the outline for our group:

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


Our Blockchain class is liable for dealing with the chain. It will store exchanges and have some assistant techniques for adding new squares to the chain. How about we begin fleshing out certain strategies.

What does a Block resemble?

Each Block has a record, a timestamp (in Unix time), a rundown of exchanges, a proof (more on that later), and the hash of the past Block.
Here's a case of what a solitary Block resembles:

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


Now, the possibility of a chain ought to be evident—each new square contains inside itself, the hash of the past Block. This is vital in light of the fact that it's what gives blockchains unchanging nature: If an aggressor defiled a prior Block in the chain then all ensuing squares will contain off base hashes.

Does this bode well? On the off chance that it doesn't, take some effort to let it hit home—it's the center thought behind blockchains.

Adding Transactions to a Block

We'll require a method for adding exchanges to a Block. Our new_transaction() strategy is answerable for this, and it's entirely straight-forward:

class Blockchain(object):
    ...
   
    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



After new_transaction() adds an exchange to the rundown, it restores the list of the square which the exchange will be added to—the following one to be mined. This will be helpful later on, to the client presenting the exchange.

Making new Blocks

At the point when our Blockchain is started up we'll have to seed it with a beginning square—a square without any forerunners. We'll additionally need to include a "proof" to our beginning square which is the aftereffect of mining (or evidence of work). We'll speak increasingly about mining later.
Notwithstanding making the beginning square in our constructor, we'll additionally tissue out the strategies for new_block(), new_transaction() and hash():


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: <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(),
            '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: <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 Dictionary is Ordered, or we'll have inconsistent hashes
        block_string = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()

 
The above ought to be straight-forward—I've added a few remarks and docstrings to help keep it clear. We're nearly finished with speaking to our blockchain. Be that as it may, now, you should be thinking about how new squares are made, manufactured or mined.

Understanding Proof of Work

A Proof of Work calculation (PoW) is the way new Blocks are made or mined on the blockchain. The objective of PoW is to find a number which takes care of an issue. The number must be hard to discover however simple to confirm—computationally—by anybody on the system. This is the center thought behind Proof of Work.

We'll take a gander at an extremely basic guide to assist this with soaking in.
How about we conclude that the hash of some number x duplicated by another y must end in 0. In this way, hash(x * y) = ac23dc...0. Also, for this disentangled model, we should fix x = 5. Executing this in Python:

rom hashlib import sha256x = 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}')

The solution here is . Since, the produced hash ends in :

hash(5 * 21) = 1253e9373e...5e3600155e860 In Bitcoin, the Proof of Work calculation is called Hashcash. What's more, it's not very not quite the same as our fundamental model above. The calculation excavators race to understand so as to make another square. All in all, the trouble is controlled by the quantity of characters scanned for in a string. The excavators are then remunerated for their answer by getting a coin—in an exchange.
The system can without much of a stretch confirm their answer. 

Actualizing essential Proof of Work

We should actualize a comparable calculation for our blockchain. Our standard will be like the model above:
Locate a number p that when hashed with the past square's answer a hash with 4 driving 0s is delivered.

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: <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"
To alter the trouble of the calculation, we could change the quantity of driving zeroes. Be that as it may, 4 is adequate. You'll discover that the expansion of a solitary driving zero has a mammoth effect to the time required to discover an answer. 

Our class is practically finished and we're prepared to start cooperating with it utilizing HTTP demands.

Stage 2: Our Blockchain as an API


We're going to utilize the Python Flask Framework. It's a smaller scale structure and it makes it simple to delineate to Python capacities. This permits us converse with our blockchain over the web utilizing HTTP demands. 

We'll make three techniques:
  • to create a new transaction to a block
  • to tell our server to mine a new block.
  • to return the full Blockchain.
,

Setting up Flask

Our "server" will frame a solitary hub in our blockchain organize. How about we make some standard 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('-', '')

# 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)
A concise clarification of what we've included previously: 

  • Line 15: Instantiates our Node. Peruse increasingly about Flask here.
  • Line 18: Create an irregular name for our hub.
  • Line 21: Instantiate our Blockchain class.
  • Line 24–26: Create the/mine endpoint, which is a GET demand.
  • Line 28–30: Create the/exchanges/new endpoint, which is a POST demand, since we'll be sending information to it.
  • Line 32–38: Create the/chain endpoint, which restores the full Blockchain.
  • Line 40–41: Runs the server on port 5000.

The Transactions Endpoint

This is the thing that the solicitation for an exchange will resemble. It's what the client sends to the server:

{
"sender": "my address",
"recipient": "someone else's address",
"amount": 5
} Since we as of now have our class strategy for adding exchanges to a square, the rest is simple. We should compose the capacity for including exchanges:

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
The Mining Endpoint

Our mining endpoint is the place the enchantment occurs, and it's simple. It needs to complete three things:
  • Ascertain the Proof of Work
  • Prize the digger (us) by including an exchange conceding us 1 coin
  • Manufacture the new Block by adding it 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
Note that the beneficiary of the mined square is the location of our hub. What's more, the greater part of what we've done here is simply collaborate with the strategies on our Blockchain class. Now, we're done, and can begin communicating with our blockchain.

Step 3: Interacting with our Blockchain

You can use plain old cURL or Postman to interact with our API over a network.
Fire up the server:
$ python blockchain.py* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Let’s try mining a block by making a request to :

Let’s create a new transaction by making a request to with a body containing our transaction structure:

On the off chance that you aren't utilizing Postman, at that point you can make the proportionate solicitation utilizing cURL:

$ curl -X POST -H "Content-Type: application/json" -d '{
"sender": "d4ee26eee15148ee92c6cd394edd974e",
"recipient": "someone-other-address",
"amount": 5
}' "http://localhost:5000/transactions/new"
I restarted my server, and mined two squares, to give 3 altogether. How about we assess the full chain by mentioning http://localhost:5000/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
} Stage 4: Consensus

This is extremely cool. We have a fundamental Blockchain that acknowledges exchanges and permits us to mine new Blocks. In any case, the general purpose of Blockchains is that they ought to be decentralized. What's more, on the off chance that they're decentralized, how on earth do we guarantee that they all mirror a similar chain? This is known as the issue of Consensus, and we'll need to actualize a Consensus Algorithm in the event that we need more than one hub in our system.

Enlisting new Nodes

Before we can execute a Consensus Algorithm, we need an approach to tell a hub about neighboring hubs on the system. Every hub on our system should keep a library of different hubs on the system. Along these lines, we'll need some more endpoints: 

  1. to accept a list of new nodes in the form of URLs.
  2. to implement our Consensus Algorithm, which resolves any conflicts—to ensure a node has the correct chain.

We’ll need to modify our Blockchain’s constructor and provide a method 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)
Note that we've utilized a set() to hold the rundown of hubs. This is a modest method for guaranteeing that the option of new hubs is idempotent—implying that regardless of how frequently we include a particular hub, it shows up precisely once.

Actualizing the Consensus Algorithm

As referenced, a contention is the point at which one hub has an alternate chain to another hub. To determine this, we'll make the standard that the longest legitimate chain is definitive. At the end of the day, the longest chain on the system is the accepted one. Utilizing this calculation, we arrive at Consensus among the hubs in our system.

...
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 network.

        :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 = 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

The first method is responsible for checking if a chain is valid by looping through each block and verifying both the hash and the proof.
is a method which loops through all our neighbouring nodes, downloads their chains and verifies them using the above method. If a valid chain is found, whose length is greater than ours, we replace ours.
Let’s register the two endpoints to our API, one for adding neighbouring nodes and the another 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
Now you can snatch an alternate machine on the off chance that you like, and turn up various hubs on your system. Or on the other hand turn up forms utilizing various ports on a similar machine. I spun up another hub on my machine, on an alternate port, and enlisted it with my present hub. Along these lines, I have two hubs: http://localhost:5000 and http://localhost:5001.

I then mined some new Blocks on node 2, to ensure the chain was longer. Afterward, I called on node 1, where the chain was replaced by the Consensus Algorithm:

And that’s a wrap... Go get some friends together to help test out your Blockchain.
I trust this has enlivened you to make something new. I'm happy about Cryptocurrencies since I accept that Blockchains will quickly change the manner in which we consider economies, governments and record-keeping.
Update: I'm anticipating catching up with a Part 2, where we'll stretch out our Blockchain to have a Transaction Validation Mechanism just as talk about certain manners by which you can productionize your Blockchain.





1 comment: