Spaces:
Runtime error
Runtime error
File size: 5,218 Bytes
f850533 |
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 |
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 Blockchain:
def __init__(self,chain_load,load=None,create=None):
global main_chain
main_chain=chain_load
self.pending_transactions = []
self.trans_data_out = []
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, 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}"
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,
'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, block):
print (f'block::{block}')
trans_data = {
'name': block[0]['name'],
'recipient': block[0]['recipient'],
'amount': block[0]['amount'],
'balance': block[0]['balance'],
'index': block[0]['index'],
'timestamp': block[0]['timestamp'],
'proof': block[0]['proof'],
'previous_hash': block[0]['previous_hash']
}
self.trans_data_out.append(trans_data)
self.pending_transactions.append(block)
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 |