Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,186 +24,7 @@ node_file='node_file.json'
|
|
| 24 |
space='blockchain-simulator-dev1'
|
| 25 |
api = HfApi(token=token_self)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
def create_block(self, proof, previous_hash,chain_r=None,chain_n=None,balance=None):
|
| 29 |
-
if chain_r=="" or chain_r==None:
|
| 30 |
-
chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
|
| 31 |
-
if chain_n=="" or chain_n==None:
|
| 32 |
-
chain_n=chain_d
|
| 33 |
-
block = {'index': len(self.chain) + 1,
|
| 34 |
-
'timestamp': str(datetime.datetime.now()),
|
| 35 |
-
'transactions': self.pending_transactions,
|
| 36 |
-
#'balance':self.balance
|
| 37 |
-
'proof': proof,
|
| 38 |
-
'previous_hash': previous_hash}
|
| 39 |
-
if self.block_valid(block) == True:
|
| 40 |
-
|
| 41 |
-
self.pending_transactions = []
|
| 42 |
-
self.chain.append(block)
|
| 43 |
-
json_object = json.dumps(self.chain, indent=4)
|
| 44 |
-
with open("tmp.json", "w") as outfile:
|
| 45 |
-
outfile.write(json_object)
|
| 46 |
-
try:
|
| 47 |
-
api.upload_file(
|
| 48 |
-
path_or_fileobj="tmp.json",
|
| 49 |
-
path_in_repo=chain_n,
|
| 50 |
-
repo_id=chain_r,
|
| 51 |
-
token=token_self,
|
| 52 |
-
repo_type="dataset",
|
| 53 |
-
)
|
| 54 |
-
os.remove("tmp.json")
|
| 55 |
-
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(e)
|
| 58 |
-
pass
|
| 59 |
-
return block
|
| 60 |
-
else:
|
| 61 |
-
block = {"Not Valid"}
|
| 62 |
-
print("not Valid")
|
| 63 |
-
return block
|
| 64 |
-
def print_previous_block(self):
|
| 65 |
-
return self.chain[-1]
|
| 66 |
-
def new_transaction(self, sender, recipient, amount):
|
| 67 |
-
transaction = {
|
| 68 |
-
'sender': sender,
|
| 69 |
-
'recipient': recipient,
|
| 70 |
-
'amount': amount
|
| 71 |
-
}
|
| 72 |
-
self.pending_transactions.append(transaction)
|
| 73 |
-
|
| 74 |
-
class Blockchain1:
|
| 75 |
-
|
| 76 |
-
def __init__(self,load=None,create=None):
|
| 77 |
-
|
| 78 |
-
self.pending_transactions = []
|
| 79 |
-
if load == None or load=="":
|
| 80 |
-
self.chain = []
|
| 81 |
-
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
| 82 |
-
elif load != None and load !="":
|
| 83 |
-
#r = requests.get(load)
|
| 84 |
-
lod = json.loads(load)
|
| 85 |
-
self.chain = lod
|
| 86 |
-
def reset(self,create=None):
|
| 87 |
-
self.chain = []
|
| 88 |
-
self.pending_transactions = []
|
| 89 |
-
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
| 90 |
-
def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
|
| 91 |
-
if chain_r=="" or chain_r==None:
|
| 92 |
-
chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
|
| 93 |
-
if chain_n=="" or chain_n==None:
|
| 94 |
-
chain_n=chain_d
|
| 95 |
-
block = {'index': len(self.chain) + 1,
|
| 96 |
-
'timestamp': str(datetime.datetime.now()),
|
| 97 |
-
'transactions': self.pending_transactions,
|
| 98 |
-
'proof': proof,
|
| 99 |
-
'previous_hash': previous_hash}
|
| 100 |
-
if self.block_valid(block) == True:
|
| 101 |
-
|
| 102 |
-
self.pending_transactions = []
|
| 103 |
-
self.chain.append(block)
|
| 104 |
-
json_object = json.dumps(self.chain, indent=4)
|
| 105 |
-
with open("tmp.json", "w") as outfile:
|
| 106 |
-
outfile.write(json_object)
|
| 107 |
-
try:
|
| 108 |
-
api.upload_file(
|
| 109 |
-
path_or_fileobj="tmp.json",
|
| 110 |
-
path_in_repo=chain_n,
|
| 111 |
-
repo_id=chain_r,
|
| 112 |
-
token=token_self,
|
| 113 |
-
repo_type="dataset",
|
| 114 |
-
)
|
| 115 |
-
os.remove("tmp.json")
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
print(e)
|
| 119 |
-
pass
|
| 120 |
-
return block
|
| 121 |
-
else:
|
| 122 |
-
block = {"Not Valid"}
|
| 123 |
-
print("not Valid")
|
| 124 |
-
return block
|
| 125 |
-
def print_previous_block(self):
|
| 126 |
-
return self.chain[-1]
|
| 127 |
-
def new_transaction(self, sender, recipient, amount):
|
| 128 |
-
transaction = {
|
| 129 |
-
'sender': sender,
|
| 130 |
-
'recipient': recipient,
|
| 131 |
-
'amount': amount
|
| 132 |
-
}
|
| 133 |
-
self.pending_transactions.append(transaction)
|
| 134 |
-
def proof_of_work(self, previous_proof):
|
| 135 |
-
new_proof = 1
|
| 136 |
-
check_proof = False
|
| 137 |
-
while check_proof is False:
|
| 138 |
-
hash_operation = hashlib.sha256(
|
| 139 |
-
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
|
| 140 |
-
if hash_operation[:5] == '00000':
|
| 141 |
-
check_proof = True
|
| 142 |
-
else:
|
| 143 |
-
new_proof += 1
|
| 144 |
-
return new_proof
|
| 145 |
-
|
| 146 |
-
def hash(self, block):
|
| 147 |
-
encoded_block = json.dumps(block, sort_keys=True).encode()
|
| 148 |
-
return hashlib.sha256(encoded_block).hexdigest()
|
| 149 |
-
def block_valid(self, block):
|
| 150 |
-
print (block)
|
| 151 |
-
#prev_block=len(self.chain)
|
| 152 |
-
if len(self.chain) > 0:
|
| 153 |
-
prev_block = len(self.chain)-1
|
| 154 |
-
previous_block = self.chain[prev_block]
|
| 155 |
-
print (previous_block)
|
| 156 |
-
out=True
|
| 157 |
-
ind=None
|
| 158 |
-
mes=None
|
| 159 |
-
if block['previous_hash'] != self.hash(previous_block):
|
| 160 |
-
out=False
|
| 161 |
-
#ind=block_index
|
| 162 |
-
mes='Hash'
|
| 163 |
-
|
| 164 |
-
previous_proof = previous_block['proof']
|
| 165 |
-
proof = block['proof']
|
| 166 |
-
hash_operation = hashlib.sha256(
|
| 167 |
-
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
| 168 |
-
|
| 169 |
-
if hash_operation[:5] != '00000':
|
| 170 |
-
out=False
|
| 171 |
-
#ind=block_index+1
|
| 172 |
-
mes='Proof'
|
| 173 |
-
previous_block = block
|
| 174 |
-
else:
|
| 175 |
-
out = True
|
| 176 |
-
return out
|
| 177 |
-
|
| 178 |
-
def chain_valid(self, chain):
|
| 179 |
-
previous_block = chain[0]
|
| 180 |
-
block_index = 1
|
| 181 |
-
out=True
|
| 182 |
-
ind=None
|
| 183 |
-
mes=None
|
| 184 |
-
while block_index < len(chain):
|
| 185 |
-
block = chain[block_index]
|
| 186 |
-
if block['previous_hash'] != self.hash(previous_block):
|
| 187 |
-
out=False
|
| 188 |
-
ind=block_index
|
| 189 |
-
mes='Hash'
|
| 190 |
-
break
|
| 191 |
-
|
| 192 |
-
previous_proof = previous_block['proof']
|
| 193 |
-
proof = block['proof']
|
| 194 |
-
hash_operation = hashlib.sha256(
|
| 195 |
-
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
| 196 |
-
|
| 197 |
-
if hash_operation[:5] != '00000':
|
| 198 |
-
out=False
|
| 199 |
-
ind=block_index+1
|
| 200 |
-
mes='Proof'
|
| 201 |
-
break
|
| 202 |
-
previous_block = block
|
| 203 |
-
block_index += 1
|
| 204 |
-
|
| 205 |
-
return out, ind, mes
|
| 206 |
-
#def check_balance(sender,amount):
|
| 207 |
|
| 208 |
######################## BLOCKCHAIN START ###############################
|
| 209 |
|
|
@@ -277,6 +98,7 @@ def sort_valid():
|
|
| 277 |
leng=len(json.loads(ggg))
|
| 278 |
print(f'Blockchain {cnt}: Length {leng} ')
|
| 279 |
valid,ind,mes = blockchain.chain_valid(json.loads(ggg))
|
|
|
|
| 280 |
except Exception:
|
| 281 |
valid=False
|
| 282 |
leng="No Data"
|
|
|
|
| 24 |
space='blockchain-simulator-dev1'
|
| 25 |
api = HfApi(token=token_self)
|
| 26 |
|
| 27 |
+
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
######################## BLOCKCHAIN START ###############################
|
| 30 |
|
|
|
|
| 98 |
leng=len(json.loads(ggg))
|
| 99 |
print(f'Blockchain {cnt}: Length {leng} ')
|
| 100 |
valid,ind,mes = blockchain.chain_valid(json.loads(ggg))
|
| 101 |
+
|
| 102 |
except Exception:
|
| 103 |
valid=False
|
| 104 |
leng="No Data"
|