From 9bb203e5bdea9fc793b2c5a878117c5a13896171 Mon Sep 17 00:00:00 2001 From: Mirko Date: Tue, 7 Nov 2023 09:43:33 +0100 Subject: [PATCH] Fixed issue with single client (ISSUE #9) --- gossipy/core.py | 4 ++-- gossipy/node.py | 5 +++++ gossipy/simul.py | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gossipy/core.py b/gossipy/core.py index a10f271..c6206f5 100644 --- a/gossipy/core.py +++ b/gossipy/core.py @@ -339,8 +339,8 @@ def __init__(self, num_nodes: int, topology: Optional[Union[np.ndarray, csr_matr for node in range(num_nodes): self._topology[node] = list(topology.getrow(node).nonzero()[-1]) else: - #self._topology = {i: None for i in range(num_nodes)} - self._topology = defaultdict(lambda: range(num_nodes)) + self._topology = {i: [j for j in range(num_nodes) if j != i] for i in range(num_nodes)} + # self._topology = defaultdict(lambda: range(num_nodes)) # docstr-coverage:inherited def size(self, node: Optional[int]=None) -> int: diff --git a/gossipy/node.py b/gossipy/node.py index 4eb047a..8a22174 100644 --- a/gossipy/node.py +++ b/gossipy/node.py @@ -103,6 +103,9 @@ def get_peer(self) -> int: """ peers = self.p2p_net.get_peers(self.idx) + if not peers: + LOG.warning("Node %d has no peers.", self.idx) + return None return random.choice(peers) if peers else choice_not_n(0, self.p2p_net.size(), self.idx) def timed_out(self, t: int) -> bool: @@ -737,6 +740,8 @@ def timed_out(self, t: int) -> int: def get_peer(self) -> int: if self.step == 1 or not self.best_nodes: peer = super().get_peer() + if peer is None: + return None if self.step == 1: self.selected[peer] += 1 return peer diff --git a/gossipy/simul.py b/gossipy/simul.py index fcb2a98..a9b87ba 100644 --- a/gossipy/simul.py +++ b/gossipy/simul.py @@ -395,6 +395,8 @@ def start(self, n_rounds: int=100) -> None: if node.timed_out(t): peer = node.get_peer() + if peer is None: + break msg = node.send(t, peer, self.protocol) self.notify_message(False, msg) if msg: @@ -599,6 +601,8 @@ def start(self, n_rounds: int=100) -> Tuple[List[float], List[float]]: if node.timed_out(t): if random() < self.accounts[i].proactive(): peer = node.get_peer() + if peer is None: + break msg = node.send(t, peer, self.protocol) self.notify_message(False, msg) if msg: @@ -632,6 +636,8 @@ def start(self, n_rounds: int=100) -> Tuple[List[float], List[float]]: self.accounts[msg.receiver].sub(reaction) for _ in range(reaction): peer = node.get_peer() + if peer is None: + break msg = node.send(t, peer, self.protocol) self.notify_message(False, msg) if msg: