Skip to content

blockchain implementaion #11565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ So this is all about introduction to blockchain technology. To learn more about
* <https://en.wikipedia.org/wiki/Chinese_remainder_theorem>
* <https://en.wikipedia.org/wiki/Diophantine_equation>
* <https://www.geeksforgeeks.org/modular-division/>

## about blockchain_main file:
This python file contains the code for basic implementaion of blockchain in python with proof of work algorithm.
112 changes: 112 additions & 0 deletions blockchain/blockchain_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import hashlib
import json
from time import time
from uuid import uuid4

Check failure on line 4 in blockchain/blockchain_main.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

blockchain/blockchain_main.py:4:18: F401 `uuid.uuid4` imported but unused


class Blockchain:
def __init__(self):
# Initialize blockchain as an empty list and pending transactions
self.chain = []
self.current_transactions = []

# Create the genesis block
self.new_block(previous_hash="1", proof=100)

def new_block(self, proof, previous_hash=None):
"""
Creates a new block and adds it to the chain.
:param proof: The proof given by the Proof of Work algorithm
:param previous_hash: (Optional) Hash of previous block
:return: New Block
"""
block = {
"index": len(self.chain) + 1,
"timestamp": time(),
"transactions": self.current_transactions,
"proof": proof,
"previous_hash": previous_hash or self.hash(self.chain[-1]),
}

# Reset the current list of transactions
self.current_transactions = []

self.chain.append(block)
return block

def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next mined block.
:param sender: Address of the sender
:param recipient: Address of the recipient
:param amount: Amount of the transaction
:return: The index of the block that will hold this transaction
"""
self.current_transactions.append(
{"sender": sender, "recipient": recipient, "amount": amount}
)

return self.last_block["index"] + 1

@staticmethod
def hash(block):
"""
Creates a SHA-256 hash of a block.
:param block: Block
:return: Hash as a hex string
"""
# Ensure the dictionary is ordered to avoid inconsistent hashes
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()

@property
def last_block(self):
"""
Returns the last block in the chain.
:return: Last Block
"""
return self.chain[-1]

def proof_of_work(self, last_proof):
"""
Simple Proof of Work algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeroes
- p is the previous proof, and p' is the new proof
:param last_proof: Previous proof
:return: New proof
"""
proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1

# Add the mining reward here
self.new_transaction(
sender="0", # System or network
recipient="Miner1", # Replace with the miner's address
amount=1, # Reward amount
)
return proof

@staticmethod
def valid_proof(last_proof, proof):
"""
Validates the proof: Does hash(last_proof, proof) contain 4 leading zeroes?
:param last_proof: Previous proof
:param proof: Current proof
:return: True if correct, False if not.
"""
guess = f"{last_proof}{proof}".encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"


# Create a new blockchain instance
blockchain = Blockchain()

# Example usage: Add a new transaction and mine a new block
blockchain.new_transaction(sender="Alice", recipient="Bob", amount=50)
last_proof = blockchain.last_block["proof"]
proof = blockchain.proof_of_work(last_proof)
block = blockchain.new_block(proof)

print(f"New block created: {block}")
Loading