0% found this document useful (0 votes)
5 views3 pages

Blockchain

The document outlines the steps to create a simple blockchain using Python, detailing the block structure, genesis block creation, hashing function, and transaction handling. It includes a sample implementation of a blockchain class with methods for adding blocks and transactions. The result confirms the successful implementation of the blockchain.

Uploaded by

720822106055
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)
5 views3 pages

Blockchain

The document outlines the steps to create a simple blockchain using Python, detailing the block structure, genesis block creation, hashing function, and transaction handling. It includes a sample implementation of a blockchain class with methods for adding blocks and transactions. The result confirms the successful implementation of the blockchain.

Uploaded by

720822106055
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/ 3

Create a Simple Block chain in any suitable programming language.

Aim:
To Create a Simple Block chain in any suitable programming language.
Algorithm/Procedure:
Step 1: Define the Block Structure
Index: A unique identifier for the block.
Timestamp: The time when the block is created.
Data: The actual information to be stored in the block (e.g., transactions).
Previous Hash: The hash of the previous block in the chain.
Nonce: A random number used in mining (proof-of-work).
Step 2: Create the Genesis Block
Step 3: Implement Hashing Function
Step 4: Mining and Proof-of-Work (Optional)
Step 5: Add New Blocks to the Chain
Step 6: Validate the Blockchain (Optional)
Step 7: Test the Blockchain
Step 8: Run the Blockchain

Program:
# importing the required libraries
import hashlib
import json
from time import time

# creating the Block_chain class


class Block_chain(object):
def __init__(self):
self.chain = []
self.pendingTransactions = []

self.newBlock(previousHash = "The Times 03/Jan/2009 Chancellor on brink of second bail


out for banks.", the_proof = 100)

# Creating a new block listing key/value pairs of


# block information in a JSON object.
# Reset the list of pending transactions &
# append the newest block to the chain.
def newBlock(self, the_proof, previousHash = None):
the_block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.pendingTransactions,
'proof': the_proof,
'previous_hash': previousHash or self.hash(self.chain[-1]),
}
self.pendingTransactions = []
self.chain.append(the_block)

return the_block

#Searching the blockchain for the most recent block.


@property
def lastBlock(self):

return self.chain[-1]

# Adding a transaction with relevant info to the 'blockpool' - list of pending tx's.
def newTransaction(self, the_sender, the_recipient, the_amount):
the_transaction = {
'sender': the_sender,
'recipient': the_recipient,
'amount': the_amount
}
self.pendingTransactions.append(the_transaction)
return self.lastBlock['index'] + 1

# receiving one block. Turning it into a string, turning that into


# Unicode (for hashing). Hashing with SHA256 encryption,
# then translating the Unicode into a hexidecimal string.
def hash(self, the_block):
stringObject = json.dumps(the_block, sort_keys = True)
blockString = stringObject.encode()
rawHash = hashlib.sha256(blockString)
hexHash = rawHash.hexdigest()
return hexHash

block_chain = Block_chain()
transaction1 = block_chain.newTransaction("Satoshi", "Alex", '10 BTC')
transaction2 = block_chain.newTransaction("Alex", "Satoshi", '2 BTC')
transaction3 = block_chain.newTransaction("Satoshi", "James", '10 BTC')
block_chain.newBlock(10123)
transaction4 = block_chain.newTransaction("Alex", "Lucy", '2 BTC')
transaction5 = block_chain.newTransaction("Lucy", "Justin", '1 BTC')
transaction6 = block_chain.newTransaction("Justin", "Alex", '1 BTC')
block_chain.newBlock(10384)

print("Genesis block: ", block_chain.chain)

OUTPUT:
Genesis block: [{'index': 1, 'timestamp': 1687764219.8479066, 'transactions': [], 'proof': 100, 'previous_hash':
'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.'},
{'index': 2, 'timestamp': 1687764219.848139, 'transactions': [{'sender': 'Satoshi', 'recipient': 'Alex', 'amount': '10
BTC'}, {'sender': 'Alex', 'recipient': 'Satoshi', 'amount': '2 BTC'}, {'sender': 'Satoshi', 'recipient': 'James',
'amount': '10 BTC'}], 'proof': 10123,
'previous_hash':'89c0ba5c01624f2a06a41bf0e520de5bf375a79fc6b2bbabe30551f6e2504405'},
{'index': 3, 'timestamp': 1687764219.8484092, 'transactions': [{'sender': 'Alex', 'recipient': 'Lucy', 'amount': '2
BTC'}, {'sender': 'Lucy', 'recipient': 'Justin', 'amount': '1 BTC'}, {'sender': 'Justin', 'recipient': 'Alex', 'amount': '1
BTC'}], 'proof': 10384, 'previous_hash':
'157dba56c6194d09755b8778605e523696aec4e6a53d1880900fd16ec70304cc'}]

Result:

Thus to Create a Simple Block chain in any suitable programming language is


implemented successfully.

You might also like