Skip to content

Instantly share code, notes, and snippets.

@hclivess
Last active July 28, 2019 18:22
Show Gist options
  • Save hclivess/a6c0cf621fe426774487e82b4b468417 to your computer and use it in GitHub Desktop.
Save hclivess/a6c0cf621fe426774487e82b4b468417 to your computer and use it in GitHub Desktop.
peerlist proposal
def peersync(self, subdata: str) -> int:
"""Got a peers list from a peer, process. From worker().
returns the number of added peers, -1 if it was locked or not accepting new peers
subdata is a dict, { 'ip': 'port'}"""
with self.peersync_lock:
try:
total_added = 0
subdata = self.dict_validate(subdata)
data_dict = json.loads(subdata)
self.app_log.info(f"Received {len(data_dict)} peers.")
# Simplified the log, every peers then has a ok or ko status anyway.
for ip, port in data_dict.items():
if ip not in self.peer_dict:
self.app_log.info(f"Outbound: {ip}:{port} is a new peer, saving if connectible")
try:
s_purge = socks.socksocket()
s_purge.settimeout(5)
if self.config.tor:
s_purge.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
s_purge.connect((ip, int(port))) # save a new peer file with only active nodes
s_purge.close()
# This only adds to our local dict, does not force save.
if ip not in self.peer_dict:
total_added += 1
self.peer_dict[ip] = port
self.app_log.info(f"Inbound: Peer {ip}:{port} saved to local peers")
except:
self.app_log.info("Not connectible")
else:
self.app_log.info(f"Outbound: {ip}:{port} is not a new peer")
except Exception as e:
self.app_log.warning(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
raise
return total_added
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment