Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import pickle
- import csv
- from binascii import unhexlify
- def swap_order(d, wsz=4, gsz=2 ):
- 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)]])
- def calculate_blockhash(serialized_block):
- a = hashlib.sha256(hashlib.sha256(serialized_block).digest()).hexdigest()
- return swap_order(a, len(a))
- def read_csv(path):
- result = []
- with open(path, newline="\n") as csv_file:
- reader = csv.reader(csv_file, delimiter=',')
- for row in reader:
- result.append(row)
- return result
- # Block #400000
- class Block:
- def __init__(self, transactions, lastblockhash, difficulty):
- self.transactions = transactions
- self.blockhash = ""
- self.lastblockhash = lastblockhash
- self.difficulty = difficulty
- self.nonce = 0
- def proof_of_work(self):
- current_hash = calculate_blockhash(pickle.dumps(self))
- while int(current_hash, 16) >= self.difficulty:
- self.nonce += 1
- # print(current_hash)
- current_hash = calculate_blockhash(pickle.dumps(self))
- self.blockhash = current_hash
- return current_hash
- def __str__(self):
- result = ""
- result += "Last Blockhash: " + str(self.lastblockhash) + "\n"
- result += "Own Blockhash : " + str(self.blockhash) + "\n"
- result += "Nonce : " + str(self.nonce) +"\n"
- result += "Transactions : " +"\n"
- for t in self.transactions:
- result += t[0] + ", " + t[1] + ", " + t[2]
- result += "\n"
- return result
- # Read the transactions
- transactions = read_csv("buchhaltung.csv")
- # Define the difficulty
- mant = 3
- exp = 71
- difficulty = mant * 10**exp
- # Create the Genesis Block
- genesis = Block([], 0, difficulty)
- current_blockhash = genesis.proof_of_work()
- print("Genesis Blockhash: ")
- print(genesis)
- blocks = [genesis]
- for transaction in transactions:
- current_block = Block([transaction], current_blockhash, difficulty)
- current_blockhash = current_block.proof_of_work()
- print(current_block)
- blocks.append(current_block)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement