blockchain.proof_of_work ======================== .. py:module:: blockchain.proof_of_work Classes ------- .. autoapisummary:: blockchain.proof_of_work.Block blockchain.proof_of_work.Blockchain Functions --------- .. autoapisummary:: blockchain.proof_of_work.test_blockchain Module Contents --------------- .. py:class:: Block(index: int, previous_hash: str, transactions: str, timestamp: float, difficulty: int) .. py:method:: compute_hash() -> str Generates the hash of the block content. Combines index, previous hash, transactions, timestamp, and nonce into a string, which is then hashed using SHA-256. Returns: - str: The hash of the block. .. py:method:: mine_block() -> None Performs Proof of Work by adjusting the nonce until a valid hash is found. A valid hash has the required number of leading zeros based on the difficulty level. Returns: - None .. py:attribute:: difficulty .. py:attribute:: hash :value: '' .. py:attribute:: index .. py:attribute:: nonce :value: 0 .. py:attribute:: previous_hash .. py:attribute:: timestamp .. py:attribute:: transactions .. py:class:: Blockchain(difficulty: int) .. py:method:: add_block(transactions: str) -> None Adds a new block to the blockchain after performing Proof of Work. Parameters: - transactions (str): The list of transactions to be added in the new block. Returns: - None .. py:method:: create_genesis_block() -> None Creates the first block in the blockchain (the Genesis block). Returns: - None .. py:method:: is_chain_valid() -> bool Verifies the integrity of the blockchain by ensuring each block's previous hash matches and that all blocks meet the Proof of Work requirement. Returns: - bool: True if the blockchain is valid, False otherwise. .. py:attribute:: chain :type: list[Block] :value: [] .. py:attribute:: difficulty .. py:function:: test_blockchain() -> None Test cases for the Blockchain proof of work algorithm. Returns: - None