From b5fe4453ede0352b03f75f27246f348050f24622 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 22 Oct 2020 16:23:16 +0000 Subject: [PATCH] added graph simulation script --- scripts/graph-simulation.py | 51 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/scripts/graph-simulation.py b/scripts/graph-simulation.py index 304a59f1..1ab751a6 100644 --- a/scripts/graph-simulation.py +++ b/scripts/graph-simulation.py @@ -1,10 +1,14 @@ -from typing import NamedTuple, List +from typing import NamedTuple, List, Set +from collections import namedtuple from hashlib import sha3_256 from secrets import randbelow from random import SystemRandom +from threading import Thread +from time import sleep +import uuid -WeightedEdge = NamedTuple("WeightedEdge", "node", "weight") +WeightedEdge = namedtuple("WeightedEdge", ["node", "weight"]) message_pool: Set["Message"] = set() # This is just to save memory for the simulation @@ -19,8 +23,9 @@ class Node: edges: List[WeightedEdge], graph_ids=["main"]): self.hidden_node = hidden_node self.edges = edges + self.node_id = str(uuid.uuid4()) self._messages: List[str] = list() # hashes point to message_pool - self._hidden_messages = List[str] = list() + self._hidden_messages: List[str] = list() def sharing_messages(self): share = list(self._messages) @@ -31,16 +36,16 @@ class Node: def add_message(self, hash_id): if hash_id not in self._messages: - self._messages.append(message.hash_id) + self._messages.append(hash_id) def pick_edge(self): selected = self.edges[0] alt = [] for weighted_edge in self.edges[1:]: - if weighted_edge.weight > selected.weight: + if weighted_edge.weight[0] > selected.weight: selected = weighted_edge - elif weighted_edge.weight == selected.weight: + elif weighted_edge.weight[0] == selected.weight: alt.append(selected) alt.append(selected) SystemRandom().shuffle(alt) @@ -50,21 +55,23 @@ class Node: def share_to_edges(self, message_id): edge = self.pick_edge() edge.node.add_message(message_id) - edge.weight += 1 + edge.weight[0] += 1 def lookup_messages(self): edge = self.pick_edge() - for message in edge.node.sharing_messages: + for message in edge.node.sharing_messages(): if message.hash_id not in self._messages: self._messages.append(message.hash_id) - edge.weight += 1 + print(self.node_id, "found", message.hash_id) + edge.weight[0] += 1 def make_new_connections(self): edge = self.pick_edge() - for new_edge in edge.edges: + for new_edge in edge.node.edges: if new_edge not in self.edges: - self.edges.append(WeightedEdge(new_edge.node, 0)) - edge.weight += 1 + self.edges.append(WeightedEdge(new_edge.node, [0])) + print(self.node_id, "added peer", new_edge.node.node_id) + edge.weight[0] += 1 class Message: @@ -77,16 +84,22 @@ class Message: self.data = data bootstrap_node = Node(False, []) -graph = [bootstrap_node] +graph = [WeightedEdge(bootstrap_node, [0])] for i in range(10): - graph.append(Node(False, [bootstrap_node])) + graph.append(WeightedEdge(Node(False, [WeightedEdge(bootstrap_node, [0])]), [0])) -m = Message("hello world") +m = Message(b"hello world") message_pool.add(m) -bootstrap_node.add_message(m) -while True: - for node in graph: +bootstrap_node.add_message(m.hash_id) + +def node_driver(node): + while True: node.make_new_connections() node.lookup_messages() - bootstrap_node.add_message(m) + sleep(1) + +for edge in graph: + Thread(target=node_driver, args=[edge.node], daemon=True).start() +input() +