-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockaviate.py
282 lines (235 loc) · 9.49 KB
/
blockaviate.py
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
'''
Block-Aviate: A Blockchain-ledger for a Weaviate Database
Created by Jason Weeks
'''
# Importing modules
import datetime # To keep track of time, as each block has its own timestamp (exact date and time at which the block is created)
import json # For encoding the blocks before hashing them
import hashlib # For finding hashes for the blocks
import weaviate
import timeit
file_name = "curChain.json"
# Initial blockchain implementation found here: https://github.com/krvaibhaw/blockchain/tree/main
# Building the blockchain architecture
class Blockchain:
def __init__(self):
# List of chains (to cryptographically link the blocks)
self.chain = []
# Imports current chain to instance
with open(file_name, mode='r', encoding='utf-8') as f:
for line in f:
block_json = json.loads(line)
self.chain.append(block_json)
# Creating the Genesis Block if no entries in BC
if not self.chain:
self.createblock(proof = 1, prevhash = "0", crud_id="-1")
def createblock(self, proof, prevhash, crud_id):
# Returns if block already exists (usually genesis block); uses prevhash since it's unique
if any(x['prevhash'] == prevhash for x in self.chain):
return
# Defining the structure of our block
block = {'index': len(self.chain) + 1,
'timestamp': str(datetime.datetime.now()),
'proof': proof,
'prevhash': prevhash,
'crud_id': crud_id} # crud_id: which operation was performed; 0 - create, 1 - read, 2 - update, 3 - delete
# Establishing a cryptographic link
self.chain.append(block)
# Exports block to file
with open(file_name, mode='a', encoding='utf-8') as f:
f.write(json.dumps(block) + "\n")
return block
def getprevblock(self):
return self.chain[-1]
def proofofwork(self, prevproof):
newproof = 1
checkproof = False
# Defining crypto puzzle for the miners and iterating until able to mine it
while checkproof is False:
op = hashlib.sha256(str(newproof**2 - prevproof**5).encode()).hexdigest()
if op[:5] == "00000":
checkproof = True
else:
newproof += 1
return newproof
def hash(self, block):
encodedblock = json.dumps(block, sort_keys = True).encode()
return hashlib.sha256(encodedblock).hexdigest()
def ischainvalid(self, chain):
prevblock = chain[0] # Initilized to Genesis block
blockindex = 1 # Initilized to Next block
while blockindex < len(chain):
# First Check : For each block check if the previous hash field is equal to the hash of the previous block
# i.e. to verify the cryptographic link
currentblock = chain[blockindex]
if currentblock['prevhash'] != self.hash(prevblock):
return False
# Second Check : To check if the proof of work for each block is valid according to problem defined in proofofwork() function
# i.e. to check if the correct block is mined or not
prevproof = prevblock['proof']
currentproof = currentblock['proof']
op = hashlib.sha256(str(currentproof**2 - prevproof**5).encode()).hexdigest()
if op[:5] != "00000":
return True
prevblock = currentblock
blockindex += 1
return True
# Creating a blockchain based on architecture defined
blockchain = Blockchain()
# Method for adding blocks to BC
def addBlock(crud_id):
prevblock = blockchain.getprevblock()
prevproof = prevblock['proof']
proof = blockchain.proofofwork(prevproof)
prevhash = blockchain.hash(prevblock)
blockchain.createblock(proof, prevhash, crud_id)
#################################################################################################
# Initializing Weaviate DB here
client = weaviate.Client(
url = "http://localhost:8080", # Replace with your endpoint
)
class_obj = {
"class": "GlobalCarbonBudget",
"vectorizer": "text2vec-contextionary",
"moduleConfig": {
"text2vec-contextionary": {
"vectorizeClassName": True
}
}
}
if not client.schema.exists(class_name=class_obj["class"]): # Checks if current class_obj class already exists within DB. If yes, skip insertion, otherwise insert the object
client.schema.create_class(class_obj) # Creates class based on this
json_file = open('./InputData/global-carbon-budget.json')
data = json.load(json_file);
client.batch.configure(batch_size=30, num_workers=2) # Configure batch
with client.batch as batch: # Initialize a batch process
# Imports data from .json file to VDB; each group of insertions given a block in BC
for (i, d) in enumerate(data): # Batch import data
print(f"importing entry: {i+1}")
properties = {
"year": d["Year"],
"fossilFuelandIndustry": d["Fossil-Fuel-And-Industry"],
"landUseChangeEmissions": d["Land-Use-Change-Emissions"],
"atmosphericGrowth": d["Atmospheric-Growth"],
"oceanSink": d["Ocean-Sink"],
"landSink": d["Land-Sink"],
"budgetImbalance": d["Budget-Imbalance"],
}
batch.add_data_object(
data_object=properties,
class_name="GlobalCarbonBudget",
)
if i % 10 == 0:
addBlock(0)
# end for
#end with
json_file.close()
def Query(): # A method used to test querying
print("Query: Print all entries in GlobalCarbonBudget in ascending order by year.")
response = (
client.query
.get("GlobalCarbonBudget",
["year",
"fossilFuelandIndustry",
"landUseChangeEmissions",
"atmosphericGrowth",
"oceanSink",
"landSink",
"budgetImbalance"])
.with_sort({
"path": ["year"],
"order": "asc",
})
.do()
)
addBlock(1)
print(json.dumps(response, indent=4))
"""
with open("GCBData.json", "w") as write:
json.dump(response, write, indent=4)"""
def Insert(n): # A method used to test object insertion
json_file = open('./InputData/global-carbon-budget20.json')
data = json.load(json_file);
client.batch.configure(batch_size=30, num_workers=2) # Configure batch
with client.batch as batch: # Initialize a batch process
# Imports data from .json file to VDB; each group of insertions given a block in BC
for (i, d) in enumerate(data): # Batch import data
print(f"importing entry: {i+1}")
properties = {
"year": d["Year"],
"fossilFuelandIndustry": d["Fossil-Fuel-And-Industry"],
"landUseChangeEmissions": d["Land-Use-Change-Emissions"],
"atmosphericGrowth": d["Atmospheric-Growth"],
"oceanSink": d["Ocean-Sink"],
"landSink": d["Land-Sink"],
"budgetImbalance": d["Budget-Imbalance"],
}
batch.add_data_object(
data_object=properties,
class_name="GlobalCarbonBudget",
)
if i % n == 0:
addBlock(0)
# end for
#end with
json_file.close()
''' # Leftover queries
print("Query: Print all entries in GlobalCarbonBudget in ascending order by year.")
response = (
client.query
.get("GlobalCarbonBudget",
["year",
"fossilFuelandIndustry",
"landUseChangeEmissions",
"atmosphericGrowth",
"oceanSink",
"landSink",
"budgetImbalance"])
.with_sort({
"path": ["year"],
"order": "asc",
})
.do()
)
addBlock(1)
print(json.dumps(response, indent=4))
with open("GCBData.json", "w") as write:
json.dump(response, write, indent=4)
================================================================================================================
print("Query: Years where the land sink and ocean sink are both greater than 1.5 in ascending order by year.")
response = (
client.query
.get("GlobalCarbonBudget",
["year",
"fossilFuelandIndustry",
"landUseChangeEmissions",
"atmosphericGrowth",
"oceanSink",
"landSink",
"budgetImbalance"])
.with_sort({
"path": ["year"],
"order": "asc",
})
.with_where({
"operator": "And",
"operands": [
{
"path": ["landSink"],
"operator": "GreaterThan",
"valueNumber": "1.5"
},
{
"path": ["oceanSink"],
"operator": "GreaterThan",
"valueNumber": "1.5"
}
]
})
.do()
)
addBlock(1)
print(json.dumps(response, indent=4))
with open("queryOut.json", "w") as write:
json.dump(response, write, indent=4)
'''