medmond919

Expanded BlockChain example

Feb 21st, 2025 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | Software | 0 0
  1.  
  2. """
  3.  
  4. import datetime as date
  5. import hashlib as hasher
  6. import random
  7.  
  8. # Users of the blockchain
  9. users = ["Alice", "Bob", "Charlie", "You"]
  10.  
  11. class Transaction:
  12. def __init__(self, sender, receiver, amount):
  13. self.sender = sender
  14. self.receiver = receiver
  15. self.amount = amount
  16.  
  17. class Block:
  18. def __init__(self, index, timestamp, data, previous_hash, nonce):
  19. self.index = index
  20. self.timestamp = timestamp
  21. self.data = data
  22. self.previous_hash = previous_hash
  23. self.nonce = nonce
  24. self.hash = self.hash_block()
  25.  
  26. def hash_block(self):
  27. sha = hasher.sha256()
  28. sha.update((str(self.index) + str(self.timestamp) + str(self.data) + str(self.nonce) + str(self.previous_hash)).encode())
  29. return sha.hexdigest()
  30.  
  31. def create_genesis_block():
  32. return Block(0, date.datetime.now(), "Genesis Block", "0", 1)
  33.  
  34. def next_block(last_block):
  35. this_index = last_block.index + 1
  36. this_timestamp = date.datetime.now()
  37.  
  38. rnd = random.Random()
  39. user_transactions = users.copy()
  40. sender = rnd.choice(user_transactions)
  41. user_transactions.remove(sender)
  42. receiver = rnd.choice(user_transactions)
  43. amount = rnd.randint(1, 100)
  44.  
  45. this_data = Transaction(sender, receiver, amount)
  46. previous_hash = last_block.hash
  47. this_nonce = proof_of_work(last_block.nonce, this_index, this_timestamp, this_data, previous_hash)
  48.  
  49. return Block(this_index, this_timestamp, this_data, previous_hash, this_nonce)
  50.  
  51. def proof_of_work(last_nonce, this_index, this_timestamp, this_data, previous_hash):
  52. incrementor = 0
  53. block = Block(this_index, this_timestamp, this_data, previous_hash, incrementor)
  54. while str(block.hash)[:4] != '0000':
  55. incrementor += 1
  56. block = Block(this_index, this_timestamp, this_data, previous_hash, incrementor)
  57. return incrementor
  58.  
  59. # Initialize the blockchain
  60. blockchain = [create_genesis_block()]
  61. previous_block = blockchain[0]
  62.  
  63. # Generate a number of blocks with fake transactions
  64. num_of_blocks_to_add = 100
  65. for i in range(num_of_blocks_to_add):
  66. block_to_add = next_block(previous_block)
  67. blockchain.append(block_to_add)
  68. previous_block = block_to_add
  69.  
  70. # Calculate the balance of each user
  71. balances = {user: 0 for user in users}
  72. for block in blockchain:
  73. if block.index == 0:
  74. continue
  75. transaction = block.data
  76. balances[transaction.sender] -= transaction.amount
  77. balances[transaction.receiver] += transaction.amount
  78.  
  79. # Display the balance of each user
  80. for user, balance in balances.items():
  81. print(f"{user}'s balance: {balance}")
Tags: blockchain
Advertisement
Add Comment
Please, Sign In to add comment