How to develop a custom blockchain?
Define the block
Libraries required • Datetime (This module supplies classes for manipulating dates and times.) • Hashlib (Secure hashes and message digests) Import these libraries
Step 1: Define the structure of a block • Define the structure for the block as a class: class Block: def __init__(self, previous_block_hash, data, timestamp): self.previous_block_hash = previous_block_hash self.data = data self.timestamp = timestamp self.hash = self.get_hash() • #function to create our own hash this hash will be take all the data in the block header and run SHA 256 two time (inner and outer encryption) • #Only considered the previous block hash, data and timestamp to be part of block for simplicity.
Step 1: Define the structure of a block • Define a class:
Step 2: Define the function to perform the hashing of block components def get_hash(self): #will take only object self as it has all we need for this function #generating binary representation of the header header_bin = (str(self.previous_block_hash) + str(self.data) + str(self.timestamp)).encode() #encode function #we convert all the data in strings to feed it as input for hash function SHA256 (in this function two-level hashing is illustrated in the from of inner hash and outer hash) #encode() is used to encode the code into unicode inner_hash = hashlib.sha256(header_bin.encode()).hexdigest().encode() #hexdigest() convert the data in hexadecimal format #hashlib.sha256 is used for hashing using SHA256 from library hashlib outer_hash = hashlib.sha256(inner_hash).hexdigest() return outer_hash
Step 2: Define the function to perform the hashing of block components
Step 3: Creating a genesis block (Static Coding Method) def create_genesis_block(): return Block("0", "0", datetime.datetime.now()) A static method is bound to a class rather than the objects for that class. This means that a static method can be called without an object for that class. This also means that static methods cannot modify the state of an object as they are not bound to it.
Step 3: Creating a genesis block (Static Coding Method)
Resultant of Step1, 2 and 3 (block.py) • save this as block.py
Create a Blockchain
Creating Blockchain • Create a file named Blockchain.py • Import the structure of block defined in block.py file from block import Block
Creating Blockchain • Generate the Genesis block and print its hash on screen b1 = Block.create_genesis_block() Print(b1.hash)
Creating Blockchain • Generate the Genesis block and print its hash on screen
Creating Blockchain • Lets add some elements to our blockchain • For simplicity, lets fix the number of blocks num_blocks_to_add = 10
Creating Blockchain • Lets add some elements to our blockchain • Initialize the loop and append the blocks for i in range(1, num_blocks_to_add): block_chain.append(Block(block_chain[i-1].hash, "Block number %d" % i, datetime.datetime.now()))
Creating Blockchain • Lets add some elements to our blockchain • Initialize the loop and append the blocks
Adding more blocks to Blockchain Specifying the number of blocks to add into blockchain
Adding more blocks to Blockchain Creating the genesis block and adding into blockchain
Adding more blocks to Blockchain Printing the value of hashed genesis block
Adding more blocks to Blockchain Running a loop to generate the given number of blocks
Adding more blocks to Blockchain Appending new blocks
Adding more blocks to Blockchain Printing the hash of the generated block
Adding more blocks to Blockchain
A single file

Lecture 17 (Blockchain Implementation using Python).pptx

  • 1.
    How to developa custom blockchain?
  • 2.
  • 3.
    Libraries required • Datetime(This module supplies classes for manipulating dates and times.) • Hashlib (Secure hashes and message digests) Import these libraries
  • 4.
    Step 1: Definethe structure of a block • Define the structure for the block as a class: class Block: def __init__(self, previous_block_hash, data, timestamp): self.previous_block_hash = previous_block_hash self.data = data self.timestamp = timestamp self.hash = self.get_hash() • #function to create our own hash this hash will be take all the data in the block header and run SHA 256 two time (inner and outer encryption) • #Only considered the previous block hash, data and timestamp to be part of block for simplicity.
  • 5.
    Step 1: Definethe structure of a block • Define a class:
  • 6.
    Step 2: Definethe function to perform the hashing of block components def get_hash(self): #will take only object self as it has all we need for this function #generating binary representation of the header header_bin = (str(self.previous_block_hash) + str(self.data) + str(self.timestamp)).encode() #encode function #we convert all the data in strings to feed it as input for hash function SHA256 (in this function two-level hashing is illustrated in the from of inner hash and outer hash) #encode() is used to encode the code into unicode inner_hash = hashlib.sha256(header_bin.encode()).hexdigest().encode() #hexdigest() convert the data in hexadecimal format #hashlib.sha256 is used for hashing using SHA256 from library hashlib outer_hash = hashlib.sha256(inner_hash).hexdigest() return outer_hash
  • 7.
    Step 2: Definethe function to perform the hashing of block components
  • 8.
    Step 3: Creatinga genesis block (Static Coding Method) def create_genesis_block(): return Block("0", "0", datetime.datetime.now()) A static method is bound to a class rather than the objects for that class. This means that a static method can be called without an object for that class. This also means that static methods cannot modify the state of an object as they are not bound to it.
  • 9.
    Step 3: Creatinga genesis block (Static Coding Method)
  • 10.
    Resultant of Step1,2 and 3 (block.py) • save this as block.py
  • 11.
  • 12.
    Creating Blockchain • Createa file named Blockchain.py • Import the structure of block defined in block.py file from block import Block
  • 13.
    Creating Blockchain • Generatethe Genesis block and print its hash on screen b1 = Block.create_genesis_block() Print(b1.hash)
  • 14.
    Creating Blockchain • Generatethe Genesis block and print its hash on screen
  • 15.
    Creating Blockchain • Letsadd some elements to our blockchain • For simplicity, lets fix the number of blocks num_blocks_to_add = 10
  • 16.
    Creating Blockchain • Letsadd some elements to our blockchain • Initialize the loop and append the blocks for i in range(1, num_blocks_to_add): block_chain.append(Block(block_chain[i-1].hash, "Block number %d" % i, datetime.datetime.now()))
  • 17.
    Creating Blockchain • Letsadd some elements to our blockchain • Initialize the loop and append the blocks
  • 18.
    Adding more blocksto Blockchain Specifying the number of blocks to add into blockchain
  • 19.
    Adding more blocksto Blockchain Creating the genesis block and adding into blockchain
  • 20.
    Adding more blocksto Blockchain Printing the value of hashed genesis block
  • 21.
    Adding more blocksto Blockchain Running a loop to generate the given number of blocks
  • 22.
    Adding more blocksto Blockchain Appending new blocks
  • 23.
    Adding more blocksto Blockchain Printing the hash of the generated block
  • 24.
    Adding more blocksto Blockchain
  • 25.