-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathledgerFunctions.py
220 lines (162 loc) · 4.53 KB
/
ledgerFunctions.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
import json
from os import path
from random import randint
# macros
LEDGER_PATH = "ledger.json"
#
# Function to add a node to the ledger file
#
def add_node(ip, pubKey):
# checks if ledger file exists
if (path.exists(LEDGER_PATH) == False):
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# new node variable
node = {"IP" : ip, "Key" : pubKey}
# add it to the ledger
ledger["Nodes"].append(node)
# Serializing json
ledger_object = json.dumps(ledger, indent = 4)
# Writing to sample.json
with open(LEDGER_PATH, "w") as outfile:
outfile.write(ledger_object)
#
# Function to add a node to the ledger file
#
def add_first_node(ip, pubKey):
# read the ledger file into a dictionary
ledger = {}
# new node variable
node = {"IP" : ip, "Key" : pubKey}
# add it to the ledger
ledger["Nodes"] = [node]
# add en empty files list to the ledger
ledger["Files"] = []
# Serializing json
ledger_object = json.dumps(ledger, indent = 4)
# Writing to sample.json
with open(LEDGER_PATH, "w") as outfile:
outfile.write(ledger_object)
#
# Function to add a file to the ledger file
#
def add_file(filename, ownerIP):
# checks if ledger file exists
if (path.exists(LEDGER_PATH) == False):
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# new node variable
newfile = { "Filename" : filename, "Owner IP" : ownerIP, "Shards" : get_ips()}
# check if client is the atual owner of the file
for index, file in enumerate(ledger["Files"]):
if file["Filename"] == filename:
del ledger["Files"][index]
# add it to the ledger
ledger["Files"].append(newfile)
# Serializing json
ledger_object = json.dumps(ledger, indent = 4)
# Writing to sample.json
with open(LEDGER_PATH, "w") as outfile:
outfile.write(ledger_object)
#
# Function that returns the shard fiename as stored on the server
#
def get_shard(filename, IP):
# checks if ledger file exists
if path.exists(LEDGER_PATH) == False:
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# list to store the ips for a file
ips = []
# check if client is the atual owner of the file
for file in ledger["Files"]:
if file["Filename"] == filename:
return filename + str(file["Shards"].index(IP))
#
# Function to check if the filename is owned by the IP
#
def check_owner(filename, IP):
# checks if ledger file exists
if path.exists(LEDGER_PATH) == False:
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# check if client is the atual owner of the file
for file in ledger["Files"]:
if file["Filename"] == filename and file["Owner IP"] == IP:
return True
return False
#
# Function to return the public key of an IP
#
def get_pubkey(IP):
# checks if ledger file exists
if path.exists(LEDGER_PATH) == False:
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# check if client is the atual owner of the file
for node in ledger["Nodes"]:
if node["IP"] == IP:
return node["Key"]
print("Key not found for IP")
return None
#
# Returns all the ips that store a file
#
def get_ips_for_file(filename):
# checks if ledger file exists
if path.exists(LEDGER_PATH) == False:
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# list to store the ips for a file
ips = []
# check if client is the atual owner of the file
for file in ledger["Files"]:
if file["Filename"] == filename:
ips = file["Shards"]
return ips
#
# Function that returns a list of files owned by a client
#
def get_files_for_owner(IP):
# checks if ledger file exists
if path.exists(LEDGER_PATH) == False:
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# list to store the filenames
files = []
# check if client is the atual owner of the file
for file in ledger["Files"]:
if file["Owner IP"] == IP:
files.append(file["Filename"])
return files
#
# Function to get the list of ips from the ledger
#
def get_ips():
# checks if ledger file exists
if (path.exists(LEDGER_PATH) == False):
return False
# read the ledger file into a dictionary
with open(LEDGER_PATH) as f:
ledger = json.load(f)
# a list of just the ips
ips = []
# going through the data and getting all the ips
for node in ledger["Nodes"]:
ips.append(node["IP"])
# returns the new list of ips
return ips