Skip to content

Commit

Permalink
Autofix wrong type from distant peer
Browse files Browse the repository at this point in the history
  • Loading branch information
EggPool committed Nov 29, 2019
1 parent cafedbb commit dfb2932
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions peershandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

import json
import os
import random
import shutil
# import re
import sys
import threading
from time import time
import random

import socks

import connections
import regnet
from essentials import most_common_dict, percentage_in

__version__ = "0.0.18"
__version__ = "0.0.19"


class Peers:
Expand Down Expand Up @@ -296,7 +296,6 @@ 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'}"""

# early exit to reduce future levels
if not self.config.accept_peers:
return -1
Expand All @@ -305,12 +304,15 @@ def peersync(self, subdata: str) -> int:
# TODO: buffer, and keep track of recently tested peers.
self.app_log.info("Outbound: Peer sync occupied")
return -1
# Temp fix: subdata is typed str, but we have a dict sometimes.
if type(subdata) == dict:
# Enforce expected type.
self.app_log.warning("Enforced expected type for peersync subdata")
subdata = json.dumps(subdata)
with self.peersync_lock:
try:
total_added = 0

# json format

subdata = self.dict_validate(subdata)
data_dict = json.loads(subdata)

Expand Down Expand Up @@ -471,7 +473,7 @@ def client_loop(self, node, this_target):
"""Manager loop called every 30 sec. Handles maintenance"""
try:
for key, value in dict(self.dict_shuffle(self.peer_dict)).items():
# The dict() above is not an error or a cast,
# The dict() above is not an error or a cast,
# it's to make a copy of the dict and avoid "dictionary changed size during iteration"
host = key
port = int(value)
Expand Down

0 comments on commit dfb2932

Please sign in to comment.