-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.py
58 lines (45 loc) · 1.78 KB
/
blockchain.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
52
53
54
55
56
57
58
import hashlib
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) + str(data)
return hashlib.sha256(value.encode()).hexdigest()
def create_genesis_block():
return Block(0, "0", int(time.time()), "Genesis Block", calculate_hash(0, "0", int(time.time()), "Genesis Block"))
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)
def is_chain_valid(chain):
for i in range(1, len(chain)):
current_block = chain[i]
previous_block = chain[i - 1]
if current_block.hash != calculate_hash(current_block.index, previous_block.hash, current_block.timestamp, current_block.data):
return False
return True
# Create a simple blockchain
blockchain = [create_genesis_block()]
previous_block = blockchain[0]
# Add blocks to the blockchain
for i in range(1, 4):
new_data = f"Block #{i}"
new_block = create_new_block(previous_block, new_data)
blockchain.append(new_block)
previous_block = new_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: {block.data}")
print(f"Hash: {block.hash}")
print("\n")
# Validate the blockchain
print("Is the blockchain valid?", is_chain_valid(blockchain))