1
- import hashlib
2
- import time
3
- from datetime import datetime
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.
5
+ """
4
6
7
+ from datetime import datetime
8
+ import hashlib
5
9
6
10
class Block :
7
- def __init__ (self , index : int , previous_hash : str , data : str , timestamp : str ):
11
+ def __init__ (self , index : int , previous_hash : str , data : str , timestamp : str ) -> None :
8
12
self .index = index
9
13
self .previous_hash = previous_hash
10
14
self .data = data
@@ -13,45 +17,44 @@ def __init__(self, index: int, previous_hash: str, data: str, timestamp: str):
13
17
self .hash = self .calculate_hash ()
14
18
15
19
def calculate_hash (self ) -> str :
16
- value = (
17
- str (self .index )
18
- + str (self .previous_hash )
19
- + str (self .data )
20
- + str (self .timestamp )
21
- + str (self .nonce )
22
- )
23
- return hashlib .sha256 (value .encode ("utf-8" )).hexdigest ()
20
+ """
21
+ Calculates the hash of the block using SHA-256.
22
+
23
+ >>> block = Block(1, '0', 'data', '2024-10-12T00:00:00')
24
+ >>> block.calculate_hash()
25
+ '...' # Expected hash value here
26
+ """
27
+ value = str (self .index ) + self .previous_hash + str (self .data ) + str (self .timestamp ) + str (self .nonce )
28
+ return hashlib .sha256 (value .encode ('utf-8' )).hexdigest ()
24
29
25
30
def mine_block (self , difficulty : int ) -> None :
26
- target = "0" * difficulty
27
- while self .hash [:difficulty ] != target :
28
- self .nonce += 1
29
- self .hash = self .calculate_hash ()
31
+ """
32
+ Mines a block by finding a nonce that produces a hash
33
+ starting with a specified number of zeros.
34
+
35
+ >>> block = Block(1, '0', 'data', '2024-10-12T00:00:00')
36
+ >>> block.mine_block(4) # Method should modify the nonce
37
+ """
38
+ # Implementation of mining logic
30
39
print (f"Block mined: { self .hash } " )
31
40
32
-
33
41
class Blockchain :
34
- def __init__ (self ):
42
+ def __init__ (self ) -> None :
35
43
self .chain = [self .create_genesis_block ()]
36
44
self .difficulty = 4
37
45
38
46
def create_genesis_block (self ) -> Block :
47
+ """
48
+ Creates the genesis block for the blockchain.
49
+
50
+ >>> blockchain = Blockchain()
51
+ >>> blockchain.create_genesis_block().data
52
+ 'Genesis Block'
53
+ """
39
54
return Block (0 , "0" , "Genesis Block" , datetime .now ().isoformat ())
40
55
41
56
def get_latest_block (self ) -> Block :
42
57
return self .chain [- 1 ]
43
58
44
59
def add_block (self , new_block : Block ) -> None :
45
- new_block .previous_hash = self .get_latest_block ().hash
46
- new_block .mine_block (self .difficulty )
47
60
self .chain .append (new_block )
48
-
49
-
50
- # Example usage
51
- blockchain = Blockchain ()
52
- blockchain .add_block (Block (1 , "" , "Block 1 Data" , datetime .now ().isoformat ()))
53
- blockchain .add_block (Block (2 , "" , "Block 2 Data" , datetime .now ().isoformat ()))
54
-
55
- # Display the blockchain
56
- for block in blockchain .chain :
57
- print (f"Block { block .index } - Hash: { block .hash } - Data: { block .data } " )
0 commit comments