Here This is One of the easy Programming of Blockchain that give block hashes for different blocks. This full Programming make block and then connect as chain to execute and perform blockchain process.
import datetime
import hashlib
class Block:
blockNo = 0
data = None
next = None
hash = None
nonce = 0
previous_hash = 0x0
timestamp = datetime.datetime.now()
def __init__(self, data):
self.data = data
def hash(self):
h = hashlib.sha256()
h.update(
str(self.nonce).encode('utf-8') +
str(self.data).encode('utf-8') +
str(self.previous_hash).encode('utf-8') +
str(self.timestamp).encode('utf-8') +
str(self.blockNo).encode('utf-8')
)
return h.hexdigest()
def __str__(self):
return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------"
class Blockchain:
diff = 20
maxNonce = 2**32
target = 2 ** (256-diff)
block = Block("Genesis")
dummy = head = block
def add(self, block):
block.previous_hash = self.block.hash()
block.blockNo = self.block.blockNo + 1
self.block.next = block
self.block = self.block.next
def mine(self, block):
for n in range(self.maxNonce):
if int(block.hash(), 16) <= self.target:
self.add(block)
print(block)
break
else:
block.nonce += 1
blockchain = Blockchain()
for n in range(10):
blockchain.mine(Block("Block " + str(n+1)))
while blockchain.head != None:
print(blockchain.head)
blockchain.head = blockchain.head.next
Output:
(base) vatshayan@vatshayans-MacBook-Air ~ % /usr/local/bin/python3 "/Users/vatshayan/import datetime.py" Block Hash: 1f565112684bf07f6bfa10a35f6cba7a495924ce2637dfeaff343c895fb20284 BlockNo: 1 Block Data: Block 1
Hashes: 44791
Block Hash: aeeb97af293088e35d91181d385a89ba16c9cbea0ad9507995e1012eedf02b42 BlockNo: 2 Block Data: Block 2
Hashes: 2008229
Block Hash: 514703d9379c83567e4aa3b8b2fa119ff94bded382c5c1ea08df91dbb1204ab4 BlockNo: 3 Block Data: Block 3
Hashes: 684423
/usr/local/bin/python3 "/Users/vatshayan/import datetime.py" Block Hash: 426378ff4b0527bbbc9dd10f93a15e9f528b76b39b29e24bd45ee34d3e00eda6 BlockNo: 4 Block Data: Block 4
Hashes: 1699107
Block Hash: dde03f722a51a3dd1a8035f67820d6062ce81d9ce6eea91adda1d3114e0ed432 BlockNo: 5 Block Data: Block 5
Hashes: 536198
Block Hash: 14ad320c25cba5338e6fcb9c67013655a737160968197919f90f71c09ef0cc0a BlockNo: 6 Block Data: Block 6
Hashes: 1521230
Block Hash: 0b6f7f9d8c11f900c73e86a820c0279c34ccf3f384e7eeb23029a120bd5ff59d BlockNo: 7 Block Data: Block 7
Hashes: 679503
Block Hash: 04d1476940d44f63ac2db6463ee06ede54fb5e29fac863ca000b5f947ebb1085 BlockNo: 8 Block Data: Block 8
Hashes: 2353833
Block Hash: f67dff01cf0f50dca58b87b8ab8ab7f1c0200803dc8828497355b14c5dce1e87 BlockNo: 9 Block Data: Block 9
Hashes: 1885867
Block Hash: 83a3ee39e97ba96dab92965d65af248065175370d04ae0cfa9faa9a7fe9b9cad BlockNo: 10 Block Data: Block 10
Hashes: 189336
Block Hash: ba57e47b1c1b599cc8f746f052097f549377903a2e069c0b631f1c4d1a335a9a BlockNo: 0 Block Data: Genesis
Hashes: 0
Block Hash: 1f565112684bf07f6bfa10a35f6cba7a495924ce2637dfeaff343c895fb20284 BlockNo: 1 Block Data: Block 1
Hashes: 44791
Block Hash: aeeb97af293088e35d91181d385a89ba16c9cbea0ad9507995e1012eedf02b42 BlockNo: 2 Block Data: Block 2
Hashes: 2008229
Block Hash: 514703d9379c83567e4aa3b8b2fa119ff94bded382c5c1ea08df91dbb1204ab4 BlockNo: 3 Block Data: Block 3
Hashes: 684423
Block Hash: 426378ff4b0527bbbc9dd10f93a15e9f528b76b39b29e24bd45ee34d3e00eda6 BlockNo: 4 Block Data: Block 4
Hashes: 1699107
Block Hash: dde03f722a51a3dd1a8035f67820d6062ce81d9ce6eea91adda1d3114e0ed432 BlockNo: 5 Block Data: Block 5
Hashes: 536198
Block Hash: 14ad320c25cba5338e6fcb9c67013655a737160968197919f90f71c09ef0cc0a BlockNo: 6 Block Data: Block 6
Hashes: 1521230
Block Hash: 0b6f7f9d8c11f900c73e86a820c0279c34ccf3f384e7eeb23029a120bd5ff59d BlockNo: 7 Block Data: Block 7
Hashes: 679503
Block Hash: 04d1476940d44f63ac2db6463ee06ede54fb5e29fac863ca000b5f947ebb1085 BlockNo: 8 Block Data: Block 8
Hashes: 2353833
Block Hash: f67dff01cf0f50dca58b87b8ab8ab7f1c0200803dc8828497355b14c5dce1e87 BlockNo: 9 Block Data: Block 9
Hashes: 1885867
Block Hash: 83a3ee39e97ba96dab92965d65af248065175370d04ae0cfa9faa9a7fe9b9cad BlockNo: 10 Block Data: Block 10 Hashes: 189336
Contact for Blockchain Queries
Mail : mevatshayan007@gmail.com