Skip to content

Commit b4419b7

Browse files
authored
Update proof_of_work.py
1 parent 75a0ffb commit b4419b7

File tree

1 file changed

+13
-48
lines changed

1 file changed

+13
-48
lines changed

blockchain/proof_of_work.py

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,35 @@
11
"""
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
53
"""
64

7-
from datetime import datetime
5+
from datetime import datetime, timezone
86
import hashlib
97

108

119
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):
1511
self.index = index
1612
self.previous_hash = previous_hash
1713
self.data = data
1814
self.timestamp = timestamp
19-
self.nonce = 0
20-
self.hash = self.calculate_hash()
2115

2216
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}"
3718
return hashlib.sha256(value.encode("utf-8")).hexdigest()
3819

39-
def mine_block(self, difficulty: int) -> None:
20+
def mine_block(self) -> None:
4021
"""
4122
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
4623
"""
47-
# Implementation of mining logic
48-
print(f"Block mined: {self.hash}")
24+
# Implementation of mining logic goes here
4925

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":
5728
"""
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
6330
"""
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())
6832

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

Comments
 (0)