Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import hashlib
  2. import pickle
  3. import csv
  4. from binascii import unhexlify
  5.  
  6.  
  7. def swap_order(d, wsz=4, gsz=2 ):
  8. return "".join(["".join([m[i:i+gsz] for i in range(wsz-gsz,-gsz,-gsz)]) for m in [d[i:i+wsz] for i in range(0,len(d),wsz)]])
  9.  
  10.  
  11. def calculate_blockhash(serialized_block):
  12. a = hashlib.sha256(hashlib.sha256(serialized_block).digest()).hexdigest()
  13. return swap_order(a, len(a))
  14.  
  15.  
  16. def read_csv(path):
  17. result = []
  18. with open(path, newline="\n") as csv_file:
  19. reader = csv.reader(csv_file, delimiter=',')
  20. for row in reader:
  21. result.append(row)
  22. return result
  23.  
  24.  
  25. # Block #400000
  26.  
  27.  
  28. class Block:
  29. def __init__(self, transactions, lastblockhash, difficulty):
  30. self.transactions = transactions
  31. self.blockhash = ""
  32. self.lastblockhash = lastblockhash
  33. self.difficulty = difficulty
  34. self.nonce = 0
  35.  
  36. def proof_of_work(self):
  37. current_hash = calculate_blockhash(pickle.dumps(self))
  38. while int(current_hash, 16) >= self.difficulty:
  39. self.nonce += 1
  40. # print(current_hash)
  41. current_hash = calculate_blockhash(pickle.dumps(self))
  42. self.blockhash = current_hash
  43. return current_hash
  44.  
  45. def __str__(self):
  46. result = ""
  47. result += "Last Blockhash: " + str(self.lastblockhash) + "\n"
  48. result += "Own Blockhash : " + str(self.blockhash) + "\n"
  49. result += "Nonce : " + str(self.nonce) +"\n"
  50. result += "Transactions : " +"\n"
  51. for t in self.transactions:
  52. result += t[0] + ", " + t[1] + ", " + t[2]
  53. result += "\n"
  54. return result
  55.  
  56.  
  57. # Read the transactions
  58. transactions = read_csv("buchhaltung.csv")
  59.  
  60. # Define the difficulty
  61. mant = 3
  62. exp = 71
  63. difficulty = mant * 10**exp
  64.  
  65. # Create the Genesis Block
  66. genesis = Block([], 0, difficulty)
  67. current_blockhash = genesis.proof_of_work()
  68. print("Genesis Blockhash: ")
  69. print(genesis)
  70. blocks = [genesis]
  71. for transaction in transactions:
  72. current_block = Block([transaction], current_blockhash, difficulty)
  73. current_blockhash = current_block.proof_of_work()
  74. print(current_block)
  75. blocks.append(current_block)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement