Spaces:
Runtime error
Runtime error
File size: 9,785 Bytes
3b58eb0 1848074 a5a5b67 1848074 1ae7fe8 1848074 d9ca5cd 1848074 d9ca5cd 1848074 1ae7fe8 1848074 2546ed8 1848074 2546ed8 1848074 2546ed8 1848074 3b58eb0 1848074 3b58eb0 1848074 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
import os
import json
import hashlib
import datetime
from huggingface_hub import (upload_file,HfApi)
token_self = os.environ['HF_TOKEN']
api = HfApi(token=token_self)
class MyChainSend:
def __init__(self,chain_load,load=None,create=None):
global main_chain
main_chain=chain_load
self.pending_transactions = []
if load == None or load=="":
self.chain = []
self.create_block(proof=1, previous_hash='0',chain_n=create)
elif load != None and load !="":
#r = requests.get(load)
lod = json.loads(load)
self.chain = lod
def reset(self,create=None):
self.chain = []
self.pending_transactions = []
self.create_block(proof=1, previous_hash='0',chain_n=create)
def create_block(self, balance, proof, previous_hash,chain_r=None,chain_n=None):
if chain_r=="" or chain_r==None:
chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
if chain_n !="" and chain_n !=None:
chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}.json"
if chain_n=="" or chain_n==None:
chain_n=chain_d
block = {'index': len(self.chain) + 1,
'timestamp': str(datetime.datetime.now()),
'transactions': self.pending_transactions,
'balance': balance,
'proof': proof,
'previous_hash': previous_hash}
if self.block_valid(block) == True:
self.pending_transactions = []
self.chain.append(block)
json_object = json.dumps(self.chain, indent=4)
with open("tmp.json", "w") as outfile:
outfile.write(json_object)
try:
api.upload_file(
path_or_fileobj="tmp.json",
path_in_repo=chain_n,
repo_id=chain_r,
token=token_self,
repo_type="dataset",
)
os.remove("tmp.json")
except Exception as e:
print(e)
pass
return block
else:
block = {"Not Valid"}
print("not Valid")
return block
def print_previous_block(self):
return self.chain[-1]
def new_transaction(self, sender, recipient, amount, balance):
transaction = {
'sender': sender,
'recipient': recipient,
'amount': amount,
'balance': balance
}
self.pending_transactions.append(transaction)
def proof_of_work(self, previous_proof):
new_proof = 1
check_proof = False
while check_proof is False:
hash_operation = hashlib.sha256(
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] == '00000':
check_proof = True
else:
new_proof += 1
return new_proof
def hash(self, block):
encoded_block = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(encoded_block).hexdigest()
def block_valid(self, block):
print (block)
#prev_block=len(self.chain)
if len(self.chain) > 0:
prev_block = len(self.chain)-1
previous_block = self.chain[prev_block]
print (previous_block)
out=True
ind=None
mes=None
if block['previous_hash'] != self.hash(previous_block):
out=False
#ind=block_index
mes='Hash'
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(
str(proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] != '00000':
out=False
#ind=block_index+1
mes='Proof'
previous_block = block
else:
out = True
return out
def chain_valid(self, chain):
previous_block = chain[0]
block_index = 1
out=True
ind=None
mes=None
while block_index < len(chain):
block = chain[block_index]
if block['previous_hash'] != self.hash(previous_block):
out=False
ind=block_index
mes='Hash'
break
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(
str(proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] != '00000':
out=False
ind=block_index+1
mes='Proof'
break
previous_block = block
block_index += 1
return out, ind, mes
class MyChainRec:
def __init__(self,chain_load,load=None,create=None):
global main_chain
main_chain=chain_load
self.pending_transactions = []
if load == None or load=="":
self.chain = []
self.create_block(proof=1, previous_hash='0',chain_n=create)
elif load != None and load !="":
#r = requests.get(load)
lod = json.loads(load)
self.chain = lod
def reset(self,create=None):
self.chain = []
self.pending_transactions = []
self.create_block(proof=1, previous_hash='0',chain_n=create)
def create_block(self, balance, proof, previous_hash,chain_r=None,chain_n=None):
if chain_r=="" or chain_r==None:
chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
if chain_n !="" and chain_n !=None:
chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}.json"
if chain_n=="" or chain_n==None:
chain_n=chain_d
block = {'index': len(self.chain) + 1,
'timestamp': str(datetime.datetime.now()),
'transactions': self.pending_transactions,
'balance': balance,
'proof': proof,
'previous_hash': previous_hash}
if self.block_valid(block) == True:
self.pending_transactions = []
self.chain.append(block)
json_object = json.dumps(self.chain, indent=4)
with open("tmp1.json", "w") as outfile:
outfile.write(json_object)
try:
api.upload_file(
path_or_fileobj="tmp1.json",
path_in_repo=chain_n,
repo_id=chain_r,
token=token_self,
repo_type="dataset",
)
os.remove("tmp1.json")
except Exception as e:
print(e)
pass
return block
else:
block = {"Not Valid"}
print("not Valid")
return block
def print_previous_block(self):
return self.chain[-1]
def new_transaction(self, sender, recipient, amount, balance):
transaction = {
'sender': sender,
'recipient': recipient,
'amount': amount,
'balance': balance
}
self.pending_transactions.append(transaction)
def proof_of_work(self, previous_proof):
new_proof = 1
check_proof = False
while check_proof is False:
hash_operation = hashlib.sha256(
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] == '00000':
check_proof = True
else:
new_proof += 1
return new_proof
def hash(self, block):
encoded_block = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(encoded_block).hexdigest()
def block_valid(self, block):
print (block)
#prev_block=len(self.chain)
if len(self.chain) > 0:
prev_block = len(self.chain)-1
previous_block = self.chain[prev_block]
print (previous_block)
out=True
ind=None
mes=None
if block['previous_hash'] != self.hash(previous_block):
out=False
#ind=block_index
mes='Hash'
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(
str(proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] != '00000':
out=False
#ind=block_index+1
mes='Proof'
previous_block = block
else:
out = True
return out
def chain_valid(self, chain):
previous_block = chain[0]
block_index = 1
out=True
ind=None
mes=None
while block_index < len(chain):
block = chain[block_index]
if block['previous_hash'] != self.hash(previous_block):
out=False
ind=block_index
mes='Hash'
break
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(
str(proof**2 - previous_proof**2).encode()).hexdigest()
if hash_operation[:5] != '00000':
out=False
ind=block_index+1
mes='Proof'
break
previous_block = block
block_index += 1
return out, ind, mes |