From 52f0a4171bbe69cee0a77143bdfdda4a3e93bcfa Mon Sep 17 00:00:00 2001 From: Wutche Date: Wed, 16 Jul 2025 07:03:35 -0500 Subject: [PATCH] updated --- main.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 12bba56..7f12aa5 100644 --- a/main.py +++ b/main.py @@ -6,68 +6,73 @@ def parse_txid(txid: str) -> tuple: Example: 'deadbeef' -> ('de', 'ad', 'be', 'ef') """ # TODO: Return a tuple of 2-character segments from txid - pass + return tuple(txid[i:i+2] for i in range(0, len(txid), 2)) def create_utxo(txid: str, vout: int, amount: int) -> dict: """ Creates a dictionary representing a UTXO with the given txid, vout, and amount. """ # TODO: Return a dict with keys 'txid', 'vout', and 'amount' - pass + return {"txid": txid, "vout": vout, "amount": amount} def update_utxo(utxo: dict, new_amount: int) -> None: """ Updates the 'amount' field in a UTXO dictionary to a new value. """ # TODO: Use update to set new 'amount' - pass + utxo.update({"amount": new_amount}) def unpack_utxo(utxo: dict) -> str: """ Unpacks a UTXO dictionary and returns a formatted string representation. """ - # TODO: Unpack the dictionary and return formatted string - pass + txid = utxo.get("txid") + vout = utxo.get("vout") + amount = utxo.get("amount") + return f"UTXO - TXID: {txid}, VOUT: {vout}, AMOUNT: {amount}" def swap_addresses(addr1: str, addr2: str) -> tuple: """ Swaps two Bitcoin addresses and returns them in reversed order. """ # TODO: Swap the values and return as tuple - pass + return (addr2, addr1) def unique_addresses(addresses: list) -> set: """ Returns a set of unique Bitcoin addresses from the provided list. """ # TODO: Convert the list to a set - pass + return set(addresses) class BitcoinWallet: def __init__(self): """ Initializes the wallet with an empty UTXO set. """ + self.utxos: Dict[str, dict] = {} def add_utxo(self, utxo: Dict) -> None: """ Adds a UTXO to the wallet using a unique txid:vout key. """ # TODO: Create key from 'txid' and 'vout', store UTXO in self.utxos - pass + key = f"{utxo['txid']}:{utxo['vout']}" + self.utxos[key] = utxo def get_balance(self) -> float: """ Returns the total balance of all UTXOs in the wallet. """ # TODO: Sum the 'amount' of all UTXOs - pass + return sum(utxo['amount'] for utxo in self.utxos.values()) class TransactionPool: def __init__(self): """ Initializes an empty transaction pool. """ + self.tx_pool: Set[str] = set() def add_transaction(self, txid: str) -> bool: """ @@ -75,18 +80,22 @@ def add_transaction(self, txid: str) -> bool: Returns True if it was not already present, False otherwise. """ # TODO: Check for presence and add if not present - pass + if txid not in self.tx_pool: + self.tx_pool.add(txid) + return True + return False def get_pool_size(self) -> int: """ Returns the total number of unique transactions in the pool. """ # TODO: Return length of tx_pool - pass + return len(self.tx_pool) def block_height_generator(start: int, end: int) -> Generator[int, None, None]: """ Yields block heights from start to end (exclusive). """ # TODO: Use a generator loop to yield block heights - pass + for height in range(start, end): + yield height