-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
83 lines (67 loc) · 2.15 KB
/
controller.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
from nodes import User
from network import Network
from const import ErrorCodes, PuddingType, N
import json
results = dict()
def main():
with open('test_config_.json', 'rb') as file:
data = file.read()
scenarios = json.loads(data)
count = 0
print(scenarios)
for key in scenarios.keys():
if count == 50:
break
scenario = scenarios[key]
print(scenario)
scenario_reader(key, scenario)
count += 1
with open('test_results.json', 'w') as file:
json.dump(results, file)
def scenario_reader(key, scenario):
global resuls
pudding_type = PuddingType[scenario['pudding_type']]
num_discovery_nodes = scenario['n']
threshold = scenario['k']
feasible = scenario['feasible']
network = Network(
pudding_type, num_discovery_nodes=num_discovery_nodes, threshold=threshold)
spare_key_pairs = network.spare_key_pairs
scenario_actors = list()
for user_event in scenario['user_scenario']:
action = user_event.pop()
actors = list()
for actor in user_event:
if actor not in scenario_actors or action == 'register':
try:
actors.append(
User(network, actor, spare_key_pairs.pop(), spare_key_pairs.pop()))
except:
print('NOT ENOUGH KEYS')
actors.append(User(network, actor))
res = execute_action(actors, action)
print(f'Result: {res}')
result = not res
if feasible != result:
results[key] = {
'feasible': feasible,
'result': result
}
def execute_action(users, action):
if action == 'register':
return users[0].register()
elif action == 'update':
return users[0].update_user_data()
elif action == 'discover':
return users[0].discover_user(users[1].id)
"""
scenario_dict = {
'pudding_type': network_scenario[0],
'k': network_scenario[1],
'n': network_scenario[2],
'user_scenario': user_scenario,
'feasible': user_scenarios[user_scenario]
}
"""
if __name__ == "__main__":
main()