Created
November 17, 2024 01:32
-
-
Save pyoneerC/6aff8366f90e8f02271eb2ddde645790 to your computer and use it in GitHub Desktop.
crypto dummy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
import time | |
import json | |
class Block: | |
def __init__(self, index, transactions, timestamp, previous_hash, nonce=0): | |
self.index = index | |
self.transactions = transactions | |
self.timestamp = timestamp | |
self.previous_hash = previous_hash | |
self.nonce = nonce | |
self.hash = self.calculate_hash() | |
def calculate_hash(self): | |
block_string = json.dumps(self.__dict__, sort_keys=True) | |
return hashlib.sha256(block_string.encode()).hexdigest() | |
def mine_block(self, difficulty): | |
target = '0' * difficulty | |
while not self.hash.startswith(target): | |
self.nonce += 1 | |
self.hash = self.calculate_hash() | |
class Blockchain: | |
def __init__(self): | |
self.chain = [self.create_genesis_block()] | |
self.difficulty = 4 | |
self.pending_transactions = [] | |
self.mining_reward = 10 | |
def create_genesis_block(self): | |
return Block(0, [], time.time(), "0") | |
def get_latest_block(self): | |
return self.chain[-1] | |
def add_transaction(self, transaction): | |
if not transaction['sender'] or not transaction['recipient'] or not transaction['amount']: | |
raise ValueError("Invalid transaction data") | |
self.pending_transactions.append(transaction) | |
def mine_pending_transactions(self, mining_reward_address): | |
block = Block( | |
len(self.chain), | |
self.pending_transactions, | |
time.time(), | |
self.get_latest_block().hash | |
) | |
block.mine_block(self.difficulty) | |
print(f"Block mined! Hash: {block.hash}") | |
self.chain.append(block) | |
self.pending_transactions = [{"sender": None, "recipient": mining_reward_address, "amount": self.mining_reward}] | |
def is_chain_valid(self): | |
for i in range(1, len(self.chain)): | |
current_block = self.chain[i] | |
previous_block = self.chain[i - 1] | |
if current_block.hash != current_block.calculate_hash(): | |
return False | |
if current_block.previous_hash != previous_block.hash: | |
return False | |
return True | |
# Test the cryptocurrency | |
if __name__ == "__main__": | |
my_coin = Blockchain() | |
# Adding transactions | |
my_coin.add_transaction({"sender": "Alice", "recipient": "Bob", "amount": 50}) | |
my_coin.add_transaction({"sender": "Bob", "recipient": "Alice", "amount": 20}) | |
# Mining transactions | |
print("Starting mining...") | |
my_coin.mine_pending_transactions("Miner1") | |
print("Starting mining again...") | |
my_coin.mine_pending_transactions("Miner2") | |
# Display the blockchain | |
for block in my_coin.chain: | |
print(f"Block {block.index}:\n{json.dumps(block.__dict__, indent=4)}") | |
# Validate the chain | |
print("Blockchain valid:", my_coin.is_chain_valid()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment