Blockchain
Blockchain
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
return the_block
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
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)
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: