Omnibus commited on
Commit
7e8dddc
·
1 Parent(s): 29346dc

Create blockchain.py

Browse files
Files changed (1) hide show
  1. blockchain.py +132 -0
blockchain.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Blockchain:
2
+
3
+ def __init__(self,load=None,create=None):
4
+
5
+ self.pending_transactions = []
6
+ if load == None or load=="":
7
+ self.chain = []
8
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
9
+ elif load != None and load !="":
10
+ #r = requests.get(load)
11
+ lod = json.loads(load)
12
+ self.chain = lod
13
+ def reset(self,create=None):
14
+ self.chain = []
15
+ self.pending_transactions = []
16
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
17
+ def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
18
+ if chain_r=="" or chain_r==None:
19
+ chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
20
+ if chain_n=="" or chain_n==None:
21
+ chain_n=chain_d
22
+ block = {'index': len(self.chain) + 1,
23
+ 'timestamp': str(datetime.datetime.now()),
24
+ 'transactions': self.pending_transactions,
25
+ 'proof': proof,
26
+ 'previous_hash': previous_hash}
27
+ if self.block_valid(block) == True:
28
+
29
+ self.pending_transactions = []
30
+ self.chain.append(block)
31
+ json_object = json.dumps(self.chain, indent=4)
32
+ with open("tmp.json", "w") as outfile:
33
+ outfile.write(json_object)
34
+ try:
35
+ api.upload_file(
36
+ path_or_fileobj="tmp.json",
37
+ path_in_repo=chain_n,
38
+ repo_id=chain_r,
39
+ token=token_self,
40
+ repo_type="dataset",
41
+ )
42
+ os.remove("tmp.json")
43
+
44
+ except Exception as e:
45
+ print(e)
46
+ pass
47
+ return block
48
+ else:
49
+ block = {"Not Valid"}
50
+ print("not Valid")
51
+ return block
52
+ def print_previous_block(self):
53
+ return self.chain[-1]
54
+ def new_transaction(self, sender, recipient, amount):
55
+ transaction = {
56
+ 'sender': sender,
57
+ 'recipient': recipient,
58
+ 'amount': amount
59
+ }
60
+ self.pending_transactions.append(transaction)
61
+ def proof_of_work(self, previous_proof):
62
+ new_proof = 1
63
+ check_proof = False
64
+ while check_proof is False:
65
+ hash_operation = hashlib.sha256(
66
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
67
+ if hash_operation[:5] == '00000':
68
+ check_proof = True
69
+ else:
70
+ new_proof += 1
71
+ return new_proof
72
+
73
+ def hash(self, block):
74
+ encoded_block = json.dumps(block, sort_keys=True).encode()
75
+ return hashlib.sha256(encoded_block).hexdigest()
76
+ def block_valid(self, block):
77
+ print (block)
78
+ #prev_block=len(self.chain)
79
+ if len(self.chain) > 0:
80
+ prev_block = len(self.chain)-1
81
+ previous_block = self.chain[prev_block]
82
+ print (previous_block)
83
+ out=True
84
+ ind=None
85
+ mes=None
86
+ if block['previous_hash'] != self.hash(previous_block):
87
+ out=False
88
+ #ind=block_index
89
+ mes='Hash'
90
+
91
+ previous_proof = previous_block['proof']
92
+ proof = block['proof']
93
+ hash_operation = hashlib.sha256(
94
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
95
+
96
+ if hash_operation[:5] != '00000':
97
+ out=False
98
+ #ind=block_index+1
99
+ mes='Proof'
100
+ previous_block = block
101
+ else:
102
+ out = True
103
+ return out
104
+
105
+ def chain_valid(self, chain):
106
+ previous_block = chain[0]
107
+ block_index = 1
108
+ out=True
109
+ ind=None
110
+ mes=None
111
+ while block_index < len(chain):
112
+ block = chain[block_index]
113
+ if block['previous_hash'] != self.hash(previous_block):
114
+ out=False
115
+ ind=block_index
116
+ mes='Hash'
117
+ break
118
+
119
+ previous_proof = previous_block['proof']
120
+ proof = block['proof']
121
+ hash_operation = hashlib.sha256(
122
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
123
+
124
+ if hash_operation[:5] != '00000':
125
+ out=False
126
+ ind=block_index+1
127
+ mes='Proof'
128
+ break
129
+ previous_block = block
130
+ block_index += 1
131
+
132
+ return out, ind, mes