@@ -27,7 +27,9 @@ class Block:
2727 hash (str): SHA256 hash of the block's content.
2828 """
2929
30- def __init__ (self , index : int , data : str , previous_hash : str , difficulty : int = 2 ) -> None :
30+ def __init__ (
31+ self , index : int , data : str , previous_hash : str , difficulty : int = 2
32+ ) -> None :
3133 self .index = index
3234 self .timestamp = time ()
3335 self .data = data
@@ -44,7 +46,9 @@ def compute_hash(self, nonce: int) -> str:
4446 Returns:
4547 str: Hexadecimal hash string.
4648 """
47- block_string = f"{ self .index } { self .timestamp } { self .data } { self .previous_hash } { nonce } "
49+ block_string = (
50+ f"{ self .index } { self .timestamp } { self .data } { self .previous_hash } { nonce } "
51+ )
4852 return hashlib .sha256 (block_string .encode ()).hexdigest ()
4953
5054 def mine_block (self , difficulty : int ) -> Tuple [int , str ]:
@@ -64,7 +68,7 @@ def mine_block(self, difficulty: int) -> Tuple[int, str]:
6468 if difficulty < 1 :
6569 raise ValueError ("Difficulty must be at least 1" )
6670 nonce = 0
67- target = '0' * difficulty
71+ target = "0" * difficulty
6872 while True :
6973 hash_result = self .compute_hash (nonce )
7074 if hash_result .startswith (target ):
@@ -152,7 +156,7 @@ def is_valid(self) -> bool:
152156 prev = self .chain [i - 1 ]
153157 if current .previous_hash != prev .hash :
154158 return False
155- if not current .hash .startswith ('0' * self .difficulty ):
159+ if not current .hash .startswith ("0" * self .difficulty ):
156160 return False
157161 if current .hash != current .compute_hash (current .nonce ):
158162 return False
0 commit comments