KOHENOOR

KOHENOOR TECH NEWS & UPDATES


A simple example of Blockchain coding.

It is quite a huge ask but let’s try with a simple code in Python to explain the process of block and how it works:

//–KGS Global Example Code Starts–//

import hashlib
import datetime as date

class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()

def calculate_hash(self):
sha = hashlib.sha256()
sha.update(str(self.index).encode(‘utf-8’) +
str(self.timestamp).encode(‘utf-8’) +
str(self.data).encode(‘utf-8’) +
str(self.previous_hash).encode(‘utf-8’))
return sha.hexdigest()

class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]

def create_genesis_block(self):
return Block(0, date.datetime.now(), “Genesis Block”, “0”)

def get_latest_block(self):
return self.chain[-1]

def add_block(self, new_block):
new_block.previous_hash = self.get_latest_block().hash
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)

blockchain = Blockchain()
blockchain.add_block(Block(1, date.datetime.now(), “Transaction data”, “”))
blockchain.add_block(Block(2, date.datetime.now(), “Another transaction data”, “”))

//–Code Ends–//
In this program, we define two classes: Block and Blockchain. The Block class represents a single block in the blockchain and contains information such as the block index, timestamp, transaction data, and the previous block’s hash. The Blockchain class represents the entire blockchain and contains a list of blocks.

We start by creating the create_genesis_block method that creates the first block in the blockchain with an index of 0 and a timestamp of the current time. We then define the get_latest_block method that returns the last block in the chain.

The add_block method is responsible for adding a new block to the blockchain. It takes a new block as input and sets the previous hash to the hash of the latest block in the chain. It then calculates the hash of the new block and appends it to the chain.

In the main program, we create a new instance of the Blockchain class and add two new blocks to the chain using the add_block method. The first block contains the transaction data “Transaction data”, and the second block contains the transaction data “Another transaction data”. Each block’s hash is calculated and stored in the blockchain to maintain the chain’s integrity.

Note that this is a simple example to demonstrate the basic concepts of a blockchain. In practice, blockchain implementations are more complex and include additional features such as consensus algorithms, mining, and peer-to-peer network communication. P.S: For ease of testing, I have given the code hereunder separately:

import hashlib
import datetime as date

class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()

def calculate_hash(self):
sha = hashlib.sha256()
sha.update(str(self.index).encode(‘utf-8’) +
str(self.timestamp).encode(‘utf-8’) +
str(self.data).encode(‘utf-8’) +
str(self.previous_hash).encode(‘utf-8’))
return sha.hexdigest()

class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]

def create_genesis_block(self):
return Block(0, date.datetime.now(), “Genesis Block”, “0”)

def get_latest_block(self):
return self.chain[-1]

def add_block(self, new_block):
new_block.previous_hash = self.get_latest_block().hash
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)

blockchain = Blockchain()
blockchain.add_block(Block(1, date.datetime.now(), “Transaction data”, “”))
blockchain.add_block(Block(2, date.datetime.now(), “Another transaction data”, “”))



Leave a comment