Skip to Content
I'm available for work

7 mins read

Creating your own blockchain network

Create a blockchain network from scratch and understand the working of blockchain

Starting with the most basic question "What is Block Chain ?" The blockchain is another revolutionary technology that can change the ways of the internet just like open-sourced software did. Just like them, blockchain will take up some time to cool off and get reasonably priced for everyone to use in modern developments.

It could take years to reach the real potential that lies within. As blockchain is a distributed P2P ledger system, anyone can see other users’ entries, but undoubtedly no one can alter it.

You can only update the blockchain using a consensus algorithm. So, when a new set of information gets uploaded to the system, no one can alter it. The blockchain will contain accurate and reliable information on the ledger.

The core idea behind blockchains is their decentralized nature. You will be fascinated by the fact of how it all works inside. Blockchain might sound simple, but inside there are a lot of protocols and algorithms that make it happen.

For more info, refer to this BlockChain Technology Guide

How does a blockchain work
How does a blockchain work
How to build a blockchain
How to build a blockchain

Below I attach my code on How I created my blockchain network in flask (python) and nodejs (javascript)
import json
from hashlib import sha256
from time import time
class Blockchain(object):
    def __init__(self):
        self.chain = []
        self.current_transactions = []
        self.new_block(previous_hash=1, proof=100)
    def proof_of_work(self, last_proof):
        # This is where the consensus algorithm is implemented
        proof = 0
        while self.valid_proof(last_proof, proof) is False:
            proof += 1
        return proof
    def valid_proof(last_proof, proof):
        # This validates the block
        guess = f'{last_proof}{proof}'.encode()
        guess_hash = sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"
    def new_block(self, proof, previous_hash=None):
        # This creates new blocks
        # and then adds to the existing chain
        block = {
            'index': len(self.chain) + 1,
            'timestamp': time(),
            'proof': proof,
            previous_hash: previous_hash or self.hash(self.chain[-1]),
        # Set the current transaction list to empty.
        self.current_transactions = []
        return block
    def new_transaction(self, sender, recipient, amount):
        # This adds a new transaction to already existing transactions
        # This creates a new transaction
        # which will be sent to the next block.
        # It will contain three variables
        # including sender, recipient and amount
                'sender': sender,
                'recipient': recipient,
                'amount': amount,
        return self.last_block['index'] + 1
    def hash(block):
        # Used for hashing a block
        # The follow code will create a SHA - 256 block hash
        # and also ensure that the dictionary is ordered
        block_string = json.dumps(block, sort_keys=True).encode()
        return sha256(block_string).hexdigest()
    def last_block(self):
        # Calls and returns the last block of the chain
        return self.chain[-1]
from uuid import uuid4
from flask import Flask, request, jsonify
from blockchain import Blockchain
app = Flask(__name__)
node_identifier = str(uuid4()).replace('-', '')
# Initializing blockchain
blockchain = Blockchain()
@app.route('/mine', methods=['GET'])
def mine():
    # Here we make the proof of work algorithm work
    last_block = blockchain.last_block
    last_proof = last_block['proof']
    proof = blockchain.proof_of_work(last_proof)
    # rewarding the miner for his contribution.
    # 0 specifies new coin has been mined
    # now create the new block and add it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)
    response = {
        'message': 'The new block has been forged',
        'index': block['index'],
        'transactions': block['transactions'],
        'proof': block['proof'],
        'previous_hash': block['previous_hash']
    return jsonify(response), 200
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
    values = request.get_json()
    # Checking if the required data is there or not
    required = ['sender', 'recipient', 'amount']
    if not all(k in values for k in required):
        return 'Missing values', 400
    # creating a new transaction
    index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
    response = {'message': f'Transaction is scheduled to be added to Block No. {index}'}
    return jsonify(response), 201
@app.route('/chain', methods=['GET'])
def full_chain():
    response = {
        'chain': blockchain.chain,
        'length': len(blockchain.chain)
    return jsonify(response), 200
if __name__ == '__main__':"", port=5000)