-
Notifications
You must be signed in to change notification settings - Fork 4
/
solution_rishita.py
48 lines (40 loc) · 1.53 KB
/
solution_rishita.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
import hashlib
import json
import time
class Block:
def __init__(self, index, previous_hash, timestamp, data, hash):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash = hash
def calculate_hash(index, previous_hash, timestamp, data):
value = str(index) + str(previous_hash) + str(timestamp) + json.dumps(data)
return hashlib.sha256(value.encode('utf-8')).hexdigest()
def create_genesis_block():
return Block(0, "0", int(time.time()), [], "0000000000000000000000000000000000000000000000000000000000000000")
def create_new_block(previous_block, data):
index = previous_block.index + 1
timestamp = int(time.time())
hash = calculate_hash(index, previous_block.hash, timestamp, data)
return Block(index, previous_block.hash, timestamp, data, hash)
# Initialize blockchain with a genesis block
blockchain = [create_genesis_block()]
current_block = blockchain[-1]
# Example data for lost and found system
lost_item_data = {
"item_name": "Phone",
"description": "iPhone X",
"owner_name": "Alice",
"contact_info": "123-456-7890"
}
# Add a lost item to the blockchain
current_block = create_new_block(current_block, lost_item_data)
blockchain.append(current_block)
# Print the blockchain
for block in blockchain:
print(f"Index: {block.index}")
print(f"Previous Hash: {block.previous_hash}")
print(f"Timestamp: {block.timestamp}")
print(f"Data: {json.dumps(block.data)}")
print(f"Hash: {block.hash}\n")