abuzarAli commited on
Commit
c42ffd1
·
verified ·
1 Parent(s): 468b3ee

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hashlib
3
+ import time
4
+ import json
5
+
6
+ # Blockchain Classes
7
+ class Block:
8
+ def __init__(self, index, timestamp, transactions, previous_hash):
9
+ self.index = index
10
+ self.timestamp = timestamp
11
+ self.transactions = transactions
12
+ self.previous_hash = previous_hash
13
+ self.hash = self.calculate_hash()
14
+
15
+ def calculate_hash(self):
16
+ block_content = json.dumps({
17
+ "index": self.index,
18
+ "timestamp": self.timestamp,
19
+ "transactions": self.transactions,
20
+ "previous_hash": self.previous_hash
21
+ }, sort_keys=True).encode()
22
+ return hashlib.sha256(block_content).hexdigest()
23
+
24
+ class Blockchain:
25
+ def __init__(self):
26
+ self.chain = [self.create_genesis_block()]
27
+ self.pending_transactions = []
28
+
29
+ def create_genesis_block(self):
30
+ return Block(0, time.time(), [], "0")
31
+
32
+ def get_latest_block(self):
33
+ return self.chain[-1]
34
+
35
+ def add_transaction(self, transaction):
36
+ self.pending_transactions.append(transaction)
37
+
38
+ def mine_block(self):
39
+ if not self.pending_transactions:
40
+ return None
41
+
42
+ new_block = Block(
43
+ index=len(self.chain),
44
+ timestamp=time.time(),
45
+ transactions=self.pending_transactions,
46
+ previous_hash=self.get_latest_block().hash
47
+ )
48
+ self.chain.append(new_block)
49
+ self.pending_transactions = [] # Clear pending transactions
50
+ return new_block
51
+
52
+ def is_chain_valid(self):
53
+ for i in range(1, len(self.chain)):
54
+ current_block = self.chain[i]
55
+ previous_block = self.chain[i - 1]
56
+
57
+ if current_block.hash != current_block.calculate_hash():
58
+ return False
59
+
60
+ if current_block.previous_hash != previous_block.hash:
61
+ return False
62
+
63
+ return True
64
+
65
+ # Streamlit App
66
+ st.title("Simple Blockchain Demo")
67
+ st.sidebar.header("Add a Transaction")
68
+
69
+ # Initialize Blockchain
70
+ if "blockchain" not in st.session_state:
71
+ st.session_state.blockchain = Blockchain()
72
+
73
+ blockchain = st.session_state.blockchain
74
+
75
+ # Add a Transaction
76
+ sender = st.sidebar.text_input("Sender")
77
+ recipient = st.sidebar.text_input("Recipient")
78
+ amount = st.sidebar.number_input("Amount", min_value=0.01, step=0.01)
79
+
80
+ if st.sidebar.button("Add Transaction"):
81
+ if sender and recipient and amount:
82
+ blockchain.add_transaction({"from": sender, "to": recipient, "amount": amount})
83
+ st.sidebar.success("Transaction added to the pool!")
84
+ else:
85
+ st.sidebar.error("Please provide valid transaction details.")
86
+
87
+ # Mine a Block
88
+ if st.button("Mine a Block"):
89
+ new_block = blockchain.mine_block()
90
+ if new_block:
91
+ st.success(f"Block {new_block.index} mined successfully!")
92
+ else:
93
+ st.error("No transactions to mine.")
94
+
95
+ # Display Blockchain
96
+ st.subheader("Blockchain Ledger")
97
+ for block in blockchain.chain:
98
+ st.write({
99
+ "Index": block.index,
100
+ "Timestamp": time.ctime(block.timestamp),
101
+ "Transactions": block.transactions,
102
+ "Previous Hash": block.previous_hash,
103
+ "Hash": block.hash,
104
+ })
105
+
106
+ # Validate Blockchain
107
+ if st.button("Validate Blockchain"):
108
+ if blockchain.is_chain_valid():
109
+ st.success("Blockchain is valid!")
110
+ else:
111
+ st.error("Blockchain is invalid!")