From 54c81810ee48ab1c5970a7c6c12b9b84d4019257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrderer?= Date: Wed, 20 Mar 2024 22:59:44 +0100 Subject: [PATCH] Allow clients to transmit payments --- blockchain.py | 6 +++--- node.py | 9 +++++++++ open_transactions.py | 0 3 files changed, 12 insertions(+), 3 deletions(-) delete mode 100644 open_transactions.py diff --git a/blockchain.py b/blockchain.py index a044dda..85d5906 100644 --- a/blockchain.py +++ b/blockchain.py @@ -232,7 +232,7 @@ class OpenTransactions: used_transaction_ids = latest_block.used_transaction_ids.copy() balances = latest_block.balances.copy() def is_valid(transaction): - sender_tuple = (transaction.sender, transation.id) + sender_tuple = (transaction.sender, transaction.id) if sender_tuple in used_transaction_ids: return False balance = balances.get(transaction.sender) or 0 @@ -254,7 +254,7 @@ class OpenTransactions: else: transaction = self.__open_transactions[i] transaction_data = transaction.transaction_fee.to_bytes(8, "big") + \ - transaction.sender_pubkey + \ + transaction.sender + \ transaction.id.to_bytes(4, "big") current_hash = hashlib.sha256(current_hash + transaction_data).digest() self.__hashes.append(current_hash) @@ -265,8 +265,8 @@ class Blockchain: self.__block_map = {} self.__latest_block_hash = None self.__lock = Lock() - self.__load_blocks_from_disk() self.open_transactions = OpenTransactions(self) + self.__load_blocks_from_disk() def __load_blocks_from_disk(self): last_valid = None try: diff --git a/node.py b/node.py index c98130a..d9d2e2b 100755 --- a/node.py +++ b/node.py @@ -360,6 +360,15 @@ def receiver(node, b): } identifier = (addr[0:2], "transaction") receive_observer.publish(identifier, event_obj) + elif msg_type == 9: + # payment request + if msg_len != 153: + log(f"Got a payment of wrong length ({msg_len} bytes from {sender}, but expected 153 bytes)") + continue + parsed_transaction = blockchain.Transaction.from_bytes(msg[5:153]) + if not parsed_transaction.is_valid(): + continue + b.open_transactions.add(parsed_transaction) else: log(f"Got a udp message of unknown type from {sender}. (type {msg_type})") diff --git a/open_transactions.py b/open_transactions.py deleted file mode 100644 index e69de29..0000000