|  | 
| 1 | 1 | """ | 
| 2 |  | -This module implements the Proof of Work algorithm for a blockchain. | 
| 3 |  | -It includes classes for Block and Blockchain, demonstrating how to mine a block | 
| 4 |  | -by finding a nonce that satisfies a given difficulty level. | 
|  | 2 | +Proof of Work Module | 
| 5 | 3 | """ | 
| 6 | 4 | 
 | 
| 7 |  | -from datetime import datetime | 
|  | 5 | +from datetime import datetime, timezone | 
| 8 | 6 | import hashlib | 
| 9 | 7 | 
 | 
| 10 | 8 | 
 | 
| 11 | 9 | class Block: | 
| 12 |  | -    def __init__( | 
| 13 |  | -        self, index: int, previous_hash: str, data: str, timestamp: str | 
| 14 |  | -    ) -> None: | 
|  | 10 | +    def __init__(self, index: int, previous_hash: str, data: str, timestamp: str): | 
| 15 | 11 |         self.index = index | 
| 16 | 12 |         self.previous_hash = previous_hash | 
| 17 | 13 |         self.data = data | 
| 18 | 14 |         self.timestamp = timestamp | 
| 19 |  | -        self.nonce = 0 | 
| 20 |  | -        self.hash = self.calculate_hash() | 
| 21 | 15 | 
 | 
| 22 | 16 |     def calculate_hash(self) -> str: | 
| 23 |  | -        """ | 
| 24 |  | -        Calculates the hash of the block using SHA-256. | 
| 25 |  | -
 | 
| 26 |  | -        >>> block = Block(1, '0', 'data', '2024-10-12T00:00:00') | 
| 27 |  | -        >>> block.calculate_hash() | 
| 28 |  | -        '...'  # Expected hash value here | 
| 29 |  | -        """ | 
| 30 |  | -        value = ( | 
| 31 |  | -            str(self.index) | 
| 32 |  | -            + self.previous_hash | 
| 33 |  | -            + str(self.data) | 
| 34 |  | -            + str(self.timestamp) | 
| 35 |  | -            + str(self.nonce) | 
| 36 |  | -        ) | 
|  | 17 | +        value = f"{self.index}{self.previous_hash}{self.data}{self.timestamp}" | 
| 37 | 18 |         return hashlib.sha256(value.encode("utf-8")).hexdigest() | 
| 38 | 19 | 
 | 
| 39 |  | -    def mine_block(self, difficulty: int) -> None: | 
|  | 20 | +    def mine_block(self) -> None: | 
| 40 | 21 |         """ | 
| 41 | 22 |         Mines a block by finding a nonce that produces a hash | 
| 42 |  | -        starting with a specified number of zeros. | 
| 43 |  | -
 | 
| 44 |  | -        >>> block = Block(1, '0', 'data', '2024-10-12T00:00:00') | 
| 45 |  | -        >>> block.mine_block(4)  # Method should modify the nonce | 
| 46 | 23 |         """ | 
| 47 |  | -        # Implementation of mining logic | 
| 48 |  | -        print(f"Block mined: {self.hash}") | 
|  | 24 | +        # Implementation of mining logic goes here | 
| 49 | 25 | 
 | 
| 50 |  | - | 
| 51 |  | -class Blockchain: | 
| 52 |  | -    def __init__(self) -> None: | 
| 53 |  | -        self.chain = [self.create_genesis_block()] | 
| 54 |  | -        self.difficulty = 4 | 
| 55 |  | - | 
| 56 |  | -    def create_genesis_block(self) -> Block: | 
|  | 26 | +    @staticmethod | 
|  | 27 | +    def create_genesis_block() -> "Block": | 
| 57 | 28 |         """ | 
| 58 |  | -        Creates the genesis block for the blockchain. | 
| 59 |  | -
 | 
| 60 |  | -        >>> blockchain = Blockchain() | 
| 61 |  | -        >>> blockchain.create_genesis_block().data | 
| 62 |  | -        'Genesis Block' | 
|  | 29 | +        Creates the genesis block | 
| 63 | 30 |         """ | 
| 64 |  | -        return Block(0, "0", "Genesis Block", datetime.now().isoformat()) | 
| 65 |  | - | 
| 66 |  | -    def get_latest_block(self) -> Block: | 
| 67 |  | -        return self.chain[-1] | 
|  | 31 | +        return Block(0, "0", "Genesis Block", datetime.now(timezone.utc).isoformat()) | 
| 68 | 32 | 
 | 
| 69 |  | -    def add_block(self, new_block: Block) -> None: | 
| 70 |  | -        self.chain.append(new_block) | 
|  | 33 | +    def get_latest_block(self) -> "Block": | 
|  | 34 | +        # Logic to get the latest block | 
|  | 35 | +        pass | 
0 commit comments