0% found this document useful (0 votes)
50 views4 pages

Name: Shubham Bhadaker Sathaye College - 02 Blockchain Practical 01

This document contains Python code that defines a Block class to create a blockchain with 14 blocks. It initializes the genesis block and then appends 13 additional blocks to the blockchain, each with the hash of the previous block, block number data, and current timestamp. It prints out the hash for each new block as it is added to the blockchain.

Uploaded by

shubham bhadekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Name: Shubham Bhadaker Sathaye College - 02 Blockchain Practical 01

This document contains Python code that defines a Block class to create a blockchain with 14 blocks. It initializes the genesis block and then appends 13 additional blocks to the blockchain, each with the hash of the previous block, block number data, and current timestamp. It prints out the hash for each new block as it is added to the blockchain.

Uploaded by

shubham bhadekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Shubham bhadaker

Sathaye College_ 02

Blockchain practical 01

import datetime

import hashlib

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()

@staticmethod

def create_genesis_block():

return Block('0','0',datetime.datetime.now())

def get_hash(self):

header = (str(self.previous_block_hash) + str(self.data) + str(self.timestamp))

inner_hash = hashlib.sha256(header.encode()).hexdigest().encode()

comp_hash = hashlib.sha256(inner_hash).hexdigest()
return comp_hash

number_of_blocks = 14

Blockchain = [Block.create_genesis_block()]

print("Pratik's Genesis block is created")

print("Hash: %s" % Blockchain[0].hash)

for i in range(1,number_of_blocks):

Blockchain.append(Block(Blockchain[i-1].hash, "Block number %d" %i,


datetime.datetime.now()))

print("Pratik's #%d block is created" %i)

print("Hash: %s" % Blockchain[-1].hash)

You might also like