Spaces:
Runtime error
Runtime error
Update chatchain.py
Browse files- chatchain.py +125 -0
chatchain.py
CHANGED
@@ -11,6 +11,131 @@ chain_d="chain1.json"
|
|
11 |
chain_t="trans1.json"
|
12 |
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def store_image(img,chain_load,address):
|
15 |
main_chain_im=chain_load
|
16 |
chain_r=f"{main_chain_im.split('datasets/',1)[1].split('/raw',1)[0]}"
|
|
|
11 |
chain_t="trans1.json"
|
12 |
|
13 |
|
14 |
+
|
15 |
+
def get_my_chain_send(sender_name=None):
|
16 |
+
global mychain_send
|
17 |
+
mes_error = ""
|
18 |
+
try:
|
19 |
+
r = requests.get(f'{main_balance}{sender_name}.json')
|
20 |
+
|
21 |
+
#print (f'r={r.text}')
|
22 |
+
mychain_send = MyChainSend(chain_load=main_balance,load=r.text)
|
23 |
+
|
24 |
+
response = {'chain': mychain_send.chain,
|
25 |
+
'length': len(mychain_send.chain)}
|
26 |
+
#print (f'response={response}')
|
27 |
+
|
28 |
+
|
29 |
+
message = f"Blockchain loaded from: {main_balance}{sender_name}.json"
|
30 |
+
return response,message
|
31 |
+
except Exception:
|
32 |
+
message = f"Error loading from: {sender_name}"
|
33 |
+
print (message)
|
34 |
+
return ["Error Loading Chain"],message
|
35 |
+
|
36 |
+
def get_my_chain_rec(recipient_name=None):
|
37 |
+
global mychain_rec
|
38 |
+
|
39 |
+
try:
|
40 |
+
r = requests.get(f'{main_balance}{recipient_name}.json')
|
41 |
+
mychain_rec = MyChainRec(chain_load=main_balance,load=r.text)
|
42 |
+
response = {'chain': mychain_rec.chain,
|
43 |
+
'length': len(mychain_rec.chain)}
|
44 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
45 |
+
return response,message
|
46 |
+
except Exception:
|
47 |
+
try:
|
48 |
+
mychain_rec = MyChainRec(chain_load=main_balance,create=recipient_name)
|
49 |
+
response = {'chain': mychain_rec.chain,
|
50 |
+
'length': len(mychain_rec.chain)}
|
51 |
+
|
52 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
53 |
+
return response,message
|
54 |
+
except Exception:
|
55 |
+
message = f"Error loading from: {recipient_name}"
|
56 |
+
return ["Error Loading Chain"],message
|
57 |
+
|
58 |
+
def display_chain_send():
|
59 |
+
response = {'chain': mychain_send.chain,
|
60 |
+
'length': len(mychain_send.chain)}
|
61 |
+
return response
|
62 |
+
|
63 |
+
def display_chain_rec():
|
64 |
+
response = {'chain': mychain_rec.chain,
|
65 |
+
'length': len(mychain_rec.chain)}
|
66 |
+
return response
|
67 |
+
|
68 |
+
def valid_send():
|
69 |
+
valid,ind,mes = mychain_send.chain_valid(mychain_send.chain)
|
70 |
+
if valid:
|
71 |
+
response = 'The Blockchain is valid.'
|
72 |
+
z=True
|
73 |
+
else:
|
74 |
+
response = f'Sender Blockchain is not valid. {mes} at Index {ind}'
|
75 |
+
z=False
|
76 |
+
return response,z,mes,ind
|
77 |
+
|
78 |
+
def valid_rec():
|
79 |
+
valid,ind,mes = mychain_rec.chain_valid(mychain_rec.chain)
|
80 |
+
if valid:
|
81 |
+
response = 'The Blockchain is valid.'
|
82 |
+
z=True
|
83 |
+
else:
|
84 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
85 |
+
z=False
|
86 |
+
return response,z, mes, ind
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
def mychain_mine_block_send(chain_r=None,chain_n=None):
|
91 |
+
previous_block = mychain_send.print_previous_block()
|
92 |
+
previous_proof = previous_block['proof']
|
93 |
+
proof = mychain_send.proof_of_work(previous_proof)
|
94 |
+
previous_hash = mychain_send.hash(previous_block)
|
95 |
+
block = mychain_send.create_block(proof, previous_hash,chain_r,chain_n)
|
96 |
+
|
97 |
+
response = {'message': 'A block is MINED',
|
98 |
+
'index': block['index'],
|
99 |
+
'timestamp': block['timestamp'],
|
100 |
+
'proof': block['proof'],
|
101 |
+
'previous_hash': block['previous_hash']
|
102 |
+
}
|
103 |
+
|
104 |
+
message = "A block is MINED"
|
105 |
+
show_chain = display_chain_send()
|
106 |
+
|
107 |
+
if len(mychain_send.chain) > 1000:
|
108 |
+
mychain_send.reset()
|
109 |
+
response = None
|
110 |
+
show_chain=display_chain_send()
|
111 |
+
message = "New Chain Created at Max 20 Blocks"
|
112 |
+
|
113 |
+
return response, show_chain,message
|
114 |
+
|
115 |
+
def mychain_mine_block_rec(chain_r=None,chain_n=None):
|
116 |
+
previous_block = mychain_rec.print_previous_block()
|
117 |
+
previous_proof = previous_block['proof']
|
118 |
+
proof = mychain_rec.proof_of_work(previous_proof)
|
119 |
+
previous_hash = mychain_rec.hash(previous_block)
|
120 |
+
block = mychain_rec.create_block(proof, previous_hash,chain_r,chain_n)
|
121 |
+
|
122 |
+
response = {'message': 'A block is MINED',
|
123 |
+
'index': block['index'],
|
124 |
+
'timestamp': block['timestamp'],
|
125 |
+
'proof': block['proof'],
|
126 |
+
'previous_hash': block['previous_hash']
|
127 |
+
}
|
128 |
+
message = "A block is MINED"
|
129 |
+
show_chain = display_chain_rec()
|
130 |
+
if len(mychain_rec.chain) > 1000:
|
131 |
+
mychain_rec.reset()
|
132 |
+
response = None
|
133 |
+
show_chain=display_chain_rec()
|
134 |
+
message = "New Chain Created at Max 20 Blocks"
|
135 |
+
return response, show_chain, message
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
def store_image(img,chain_load,address):
|
140 |
main_chain_im=chain_load
|
141 |
chain_r=f"{main_chain_im.split('datasets/',1)[1].split('/raw',1)[0]}"
|