-
Notifications
You must be signed in to change notification settings - Fork 0
/
biggossip.py
224 lines (177 loc) · 8.03 KB
/
biggossip.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
import asyncio
import random
from typing import Callable, Union
from deccom.peers.peer import Peer
from deccom.protocols.peerdiscovery.abstractpeerdiscovery import AbstractPeerDiscovery
from deccom.protocols.wrappers import *
class BigGossip(AbstractPeerDiscovery):
INTRODUCTION = int.from_bytes(b'\xe1', byteorder="big") # english opening king's variation
RESPOND_FIND = int.from_bytes(b'\xc4', byteorder="big")
PULL = int.from_bytes(b'\xe5', byteorder="big")
PULLED = int.from_bytes(b'\xc3', byteorder="big")
FIND = int.from_bytes(b'\xf6', byteorder="big")
ASK_FOR_ID = int.from_bytes(b'\xf3',byteorder="big")
offers = dict(AbstractPeerDiscovery.offers, **{
"send_ping": "send_ping"
})
bindings = dict(AbstractPeerDiscovery.bindings, **{
"_lower_ping": "send_ping"
})
required_lower = AbstractPeerDiscovery.required_lower + ["send_ping"]
def __init__(self, bootstrap_peers: list[Peer] = [], interval: int = 60, k: int = 20, submodule=None, callback: Callable[[tuple[str, int], bytes], None] = None, disconnected_callback=lambda *args:..., connected_callback: Callable[[Peer], None] =lambda *args:...):
super().__init__(bootstrap_peers, interval, submodule, callback, disconnected_callback, connected_callback)
self.k = k
self.peer_crawls = dict()
self.sent_finds = dict()
self.warmup = 0
self.refresh_loop = None
self.max_warmup = 60
self.searches: dict[bytes,bytes] = dict()
async def start(self, p: Peer):
await super().start(p)
for p in self.bootstrap_peers:
await self.introduce_to_peer(p)
msg = bytearray([BigGossip.ASK_FOR_ID])
await self.send_datagram(msg,p.addr)
loop = asyncio.get_event_loop()
self.refresh_loop = loop.call_later(2, self.refresh_table)
def refresh_table(self):
loop = asyncio.get_event_loop()
loop.create_task(self._refresh_table())
async def _refresh_table(self):
# print("refreshing")
loop = asyncio.get_running_loop()
if len(self.peers.items()) == 0:
print("i dont know anyone still")
for p in self.bootstrap_peers:
await self.introduce_to_peer(p)
await asyncio.sleep(1)
msg = bytearray([BigGossip.ASK_FOR_ID])
await self.send_datagram(msg,p.addr)
self.refresh_loop = loop.call_later(2, self.refresh_table)
return
prs = list(self.peers.items())
if len(prs) > 0:
prs = random.sample(prs,min(len(prs),2))
for k,v in prs:
msg = bytearray([BigGossip.PULL])
await self.send_datagram(msg,v.addr)
self.refresh_loop = loop.call_later(self.interval, self.refresh_table)
async def stop(self):
if self.refresh_loop != None:
self.refresh_loop.cancel()
self.refresh_loop = None
self.searches.clear()
self.peer_crawls.clear()
self.sent_finds.clear()
return await super().stop()
def remove_peer(self, addr: tuple[str, int], node_id: bytes):
if self.peers.get(node_id) == None:
return
del self.peers[node_id]
return super().remove_peer(addr, node_id)
async def introduce_to_peer(self, peer: Peer):
# print("introducing to", peer.id_node)
msg = bytearray([BigGossip.INTRODUCTION])
msg = msg + bytes(self.peer)
await self.send_datagram(msg, peer.addr)
def process_datagram(self, addr: tuple[str, int], data: bytes):
# print("processing...")
asyncio.set_event_loop(self.loop)
if data[0] == BigGossip.INTRODUCTION:
other, i = Peer.from_bytes(data[1:])
# print(self.peer.pub_key,": introduction form", other.pub_key)
other.addr = addr
self.connection_approval(addr,other,self.add_peer,self.ban_peer)
elif data[0] == BigGossip.PULL:
prs = list(self.peers.items())
if len(prs) > 0:
prs = random.sample(prs,min(len(prs),self.k))
i = 0
while i < len(prs):
msg = bytearray([BigGossip.PULLED])
for k,v in prs[i:i+10]:
msg += bytes(v)
loop = asyncio.get_event_loop()
loop.create_task(self.send_datagram(msg, addr))
i += 10
elif data[0] == BigGossip.PULLED:
i = 1
loop = asyncio.get_event_loop()
while i < len(data):
#print(i, len(data))
other, k = Peer.from_bytes(data[i:])
i+= k
if other.id_node == self.peer.id_node:
continue
if self.peers.get(other.id_node) == None:
loop.create_task(self.introduce_to_peer(other))
msg = bytearray([BigGossip.ASK_FOR_ID])
loop.create_task(self.send_datagram(msg, other.addr))
elif data[0] == BigGossip.FIND:
if self.sent_finds.get(data) != None:
return
seeker, i = Peer.from_bytes(data[1:])
id = data[i+1:]
# print(seeker.id_node," is looking for ",id)
self.sent_finds[data] = i
if id == self.peer.id_node:
# print("THATS ME")
loop = asyncio.get_running_loop()
loop.create_task(self.introduce_to_peer(seeker))
self.connection_approval(addr,seeker,self.add_peer,self.ban_peer)
elif self.peers.get(id) == None:
l = list(self.get_peers())
for p in l:
if self.get_peers().get(p) is None:
continue
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(
data, self.peers[p].addr))
else:
peer = self.peers.get(id)
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(data, peer.addr))
elif data[0] == BigGossip.ASK_FOR_ID:
# print("ASKING FOR ID")
msg = bytearray([BigGossip.INTRODUCTION])
# print("PUBKEY", self.peer)
# pprint(vars(self.peer))
msg = msg + bytes(self.peer)
loop = asyncio.get_running_loop()
loop.create_task(self.send_datagram(msg, addr))
else:
return self.callback(addr, data[1:])
async def send_ping(self, addr, success, fail, timeout):
await self._lower_ping(addr, success, fail, timeout)
def add_peer(self, addr: tuple[str,int], p: Peer):
# print(p)
self.peers[p.id_node] = p
return super().add_peer(addr, p)
async def _find_peer(self, fut, id: bytes):
self.peer_crawls[id] = fut
msg = bytearray([BigGossip.FIND])
msg = msg + bytes(self.peer) + id
l = list(self.get_peers())
for p in l:
if self.get_peers().get(p) is None:
continue
await self.send_datagram(msg, self.peers[p].addr)
return
async def find_peer(self, id) -> Peer:
if id == self.peer.id_node:
return self.peer
if self.peers.get(id) == None:
if self.peer_crawls.get(id) == None:
loop = asyncio.get_running_loop()
fut = loop.create_future()
await self._find_peer(fut, id)
await fut
else:
await self.peer_crawls.get(id)
return self.get_peer(id)
def get_peer(self, id) -> Union[Peer,None]:
return self.peers.get(id)
@bindto("send_ping")
async def _lower_ping(self, addr, success, failure, timeout):
return