Blockchain in python using functions

There are a total of 10 functions involved in this project ,per-requisites basic blockchain lingo should be known , The project is divided into sub parts , we will traverse sub -parts wise ;

1.Genesis_block :

#Gensis block initialization
genesis_block = {

   'previous_hash': '',

   'index': 0,

   'transaction': [],

   'nonce': 0
   #It is used in mining process

}
#Declaring Gensis Block
blockchain = [genesis_block]

2.Adding a Transaction

tx_data = get_transaction_value()

       sender,recipient, amount = tx_data

       add_value(recipient,sender, amount=amount)

       print(open_transactions)
       #open_transactions is a list of all the transactions that have happened and mined 

A.Now here get_transaction_value is used to take input from the user

def get_transaction_value():

   tx_sender=input('Enter the sender of the transaction:')
   
   tx_recipient = input('Enter the recipient of the transaction: ')

   tx_amount = float(input('Enter your transaction amount '))

   return tx_sender,tx_recipient, tx_amount

B.Add transaction , which will later be verified by Miners . Here we use a dictionary as key – value paring is required ,after which we append transaction into open_transaction list

def add_value(recipient, sender, amount=1.0):

   transaction = {'sender': sender,

   'recipient': recipient,

   'amount': amount}

   open_transactions.append(transaction)

3.Mining blocks , It is used to verify the transaction(i.e -> proof of work ) and then if everything works ,then add transaction to the longest Blockchain .

def mine_block():

   last_block = blockchain[-1]

   hashed_block = hash_block(last_block)
    #we compare the value of hash of the last block and value of hash stored in the present block of the last block , 
    # if they are not same that means blockchain has been modified 
   nonce = pow()
    #To proof of work to check for transaction verification 

   reward_transaction = {

           'sender': 'MINING',

           'recipient': owner,

           'amount': reward

       }
    #Adding the mining transaction 
   open_transactions.append(reward_transaction)

   block = {

       'previous_hash': hashed_block,

       'index': len(blockchain),

       'transaction': open_transactions,

       'nonce': nonce

   }

   blockchain.append(block)

A.Proof of Work or pow :It is used to return the value of nonce (unique id ) , after each transaction is valid , if not valid it keeps on increasing the value of nonce.

last_block = blockchain[-1]

   last_hash = hash_block(last_block)
   #storing details of previous hash
   nonce = 0
   #initialized the nonce to zero
   while not valid_proof(open_transactions, last_hash, nonce):
       #value of nonce is incremented when the transaction is verified 
       nonce += 1

   return nonce

A.1 valid_proof : There is a pattern in hash value of blocks of same Blockchain, We verify that pattern to check which blockchain does block belong

def valid_proof(transactions, last_hash, nonce):

   guess = (str(transactions) + str(last_hash) + str(nonce)).encode()

   guess_hash = hashlib.sha256(guess).hexdigest()
    #generating a Hashcode for a particular verified Transaction
   last_chars = guess_hash[-10:]
   #We have reduced the print to last 10 characters of each verification
   print(last_chars)
   #if first 4 characters is equal to 2018 , it means it belongs to this specific blockchain
   return guess_hash[-4:] == '2018'

4. Hashing : We use sha_256 hashing as it is standard hashing function , you can also Use sha3_256 it is modified function of sha_256

def hash_block(block):

   return hashlib.sha256(json.dumps(block).encode()).hexdigest()
    #1.sha 256-> SHA-256 is a one-way function that converts a text of any length into a string of 256 bits.
    #2.encode()-> Used to encode the string into hexidigest format
    #3.hexidigest()-> string is returned as a string of double length , containg only hexadecimal digits.
 

You can integrate it with Web framework to make an executable version of this , e.x -> Flask , Django ,etc.

GitHub Link: https://github.com/kakabisht/Blockchain-implementation-without-networkingpart

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s