-
Notifications
You must be signed in to change notification settings - Fork 0
/
DolevStrong.py
57 lines (51 loc) · 1.85 KB
/
DolevStrong.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
from Messages.Message import Message
class DolevStrong:
name = "Dolev-Strong Protocol"
SENDER = 0
def __init__(self, **kargs):
self.env = kargs["env"]
self.pki = kargs["pki"]
self.pki.register(self)
self.input = None
self.seen = [False, False]
def my_verify(self, round, msg):
sigs = []
mark = set()
tmp = msg
for i in range(round):
if type(tmp) is not Message:
return False
s = tmp.get_sender()
sigs.append(s)
tmp = tmp.get_content()
if s in mark:
return False
mark.add(s)
if sigs[-1] != DolevStrong.SENDER:
return False
return True
def run_node(self):
round = self.env.get_round()
myid = self.env.get_id(self)
if round == 0:
if self.env.get_id(self) == DolevStrong.SENDER:
self.input = self.env.get_input(self)
self.env.put_broadcast(self, self.pki.sign(
self, Message(myid, self.input)))
self.seen[self.input] = True
elif 1 <= round <= self.env.get_tf() + 1:
msgs = self.env.get_input_msgs(self)
for msg in msgs:
if self.pki.verify(msg) and self.my_verify(round, msg):
b = msg.get_extraction()
if (b == 0 or b == 1) and not self.seen[b]:
self.seen[b] = True
self.env.put_broadcast(
self, self.pki.sign(self, Message(myid, msg)))
if round == self.env.get_tf() + 1:
if self.seen[1] and not self.seen[0]:
self.env.put_output(self, 1)
else:
self.env.put_output(self, 0)
else:
raise RuntimeError