-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_stub.py
More file actions
102 lines (79 loc) · 3.13 KB
/
node_stub.py
File metadata and controls
102 lines (79 loc) · 3.13 KB
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
import time
import sys
from threading import Lock
sys.path.append(".")
import node
import random
class NodeStub(node.Node):
def __init__(self, node, config = None, network_delay = 0, disable_primary = False, drop_ratio = 0, byzantine = False):
self.node = node
self.network_delay = network_delay
self.drop_ratio = drop_ratio
self.config = config
self.disable_primary = disable_primary
self._key_lock = Lock()
# just want to know when we initialize
self.primary = node.is_primary()
self.byzantine = byzantine
def get_node(self):
return self.node
def get_pk(self):
return self.node.get_pk()
def is_primary(self):
return self.node.is_primary()
def set_byzantine(self, new_value):
self.byzantine = new_value
def is_byzantine(self):
return self.byzantine
def __random_drop(self, drop_primary = False):
if drop_primary and self.disable_primary and self.primary:
return True if random.randint(1, 10) < 5 else False
elif self.drop_ratio > 0 and random.randint(1, 1000) <= self.drop_ratio:
print('↓', self.get_pk())
return True
return False
def __random_sleep(self, times = 1):
if self.network_delay == 0:
return
time.sleep(times*random.randint(0, self.network_delay)/1000)
async def client_request(self, request, signature):
if self.byzantine or self.__random_drop(True):
return
self._key_lock.acquire()
await self.node.client_request(request, signature)
self._key_lock.release()
async def pre_prepare(self, view, sequence, request, signature):
if self.byzantine or self.__random_drop():
return
self.__random_sleep()
self._key_lock.acquire()
await self.node.pre_prepare(view, sequence, request, signature)
self._key_lock.release()
async def prepare(self, view, sequence, request, signature):
if self.byzantine or self.__random_drop():
return
self.__random_sleep()
self._key_lock.acquire()
await self.node.prepare(view, sequence, request, signature)
self._key_lock.release()
async def commit(self, view, sequence, request, signature):
if self.byzantine or self.__random_drop():
return
self.__random_sleep()
self._key_lock.acquire()
await self.node.commit(view, sequence, request, signature)
self._key_lock.release()
async def view_change(self, new_view, prepared_certs, signature):
if self.byzantine or self.__random_drop():
return
self.__random_sleep()
self._key_lock.acquire()
await self.node.view_change(new_view, prepared_certs, signature)
self._key_lock.release()
async def new_view(self, new_view, omicron, theta, signature):
if self.byzantine or self.__random_drop():
return
self.__random_sleep()
self._key_lock.acquire()
await self.node.new_view(new_view, omicron, theta, signature)
self._key_lock.release()