-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchainingBlocks.py
51 lines (38 loc) · 1.19 KB
/
chainingBlocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Imports
from dataclasses import dataclass
from datetime import datetime
from typing import Any
import hashlib
# The Block data class
@dataclass
class Block:
data: Any
creator_id: int
prev_hash: str = "0"
timestamp: str = datetime.utcnow().strftime("%H:%M:%S")
def hash_block(self):
sha = hashlib.sha256()
data = str(self.data).encode()
sha.update(data)
timestamp = str(self.timestamp).encode()
sha.update(timestamp)
prev_hash = str(self.prev_hash).encode()
sha.update(prev_hash)
return sha.hexdigest()
# Creating a class called PyChain
@dataclass
class PyChain:
chain: list[Block]
def add_block(self, block):
self.chain += [block]
# Initiating a new chain with the Genesis block
pychain = PyChain([Block(data="Genesis", creator_id=0)])
# Access the last block in the chain
prev_block = pychain.chain[-1]
# Calculate the hash for the last block in the chain
prev_block_hash = prev_block.hash_block()
# Create a new instance of the Block class
new_block = Block(data="new_block", creator_id=42, prev_hash=prev_block_hash)
# Add the new block to the chain
pychain.add_block(new_block)
print(pychain)