Onionr/scripts/graph-simulation.py

106 lines
3.0 KiB
Python
Raw Normal View History

2020-10-22 16:23:16 +00:00
from typing import NamedTuple, List, Set
2020-10-19 07:32:34 +00:00
2020-10-22 16:23:16 +00:00
from collections import namedtuple
2020-10-19 07:32:34 +00:00
from hashlib import sha3_256
from secrets import randbelow
from random import SystemRandom
2020-10-22 16:23:16 +00:00
from threading import Thread
from time import sleep
import uuid
2020-10-19 07:32:34 +00:00
2020-10-22 16:23:16 +00:00
WeightedEdge = namedtuple("WeightedEdge", ["node", "weight"])
2020-10-19 07:32:34 +00:00
message_pool: Set["Message"] = set() # This is just to save memory for the simulation
def get_message(hash_id):
for m in message_pool:
if hash_id == m.hash_id:
return m
raise ValueError
class Node:
def __init__(self, hidden_node: bool,
edges: List[WeightedEdge], graph_ids=["main"]):
self.hidden_node = hidden_node
self.edges = edges
2020-10-22 16:23:16 +00:00
self.node_id = str(uuid.uuid4())
2020-10-19 07:32:34 +00:00
self._messages: List[str] = list() # hashes point to message_pool
2020-10-22 16:23:16 +00:00
self._hidden_messages: List[str] = list()
2020-10-19 07:32:34 +00:00
def sharing_messages(self):
share = list(self._messages)
for m in self._hidden_messages:
share.remove(m)
return share
def add_message(self, hash_id):
if hash_id not in self._messages:
2020-10-22 16:23:16 +00:00
self._messages.append(hash_id)
2020-10-19 07:32:34 +00:00
def pick_edge(self):
selected = self.edges[0]
alt = []
for weighted_edge in self.edges[1:]:
2020-10-22 16:23:16 +00:00
if weighted_edge.weight[0] > selected.weight:
2020-10-19 07:32:34 +00:00
selected = weighted_edge
2020-10-22 16:23:16 +00:00
elif weighted_edge.weight[0] == selected.weight:
2020-10-19 07:32:34 +00:00
alt.append(selected)
alt.append(selected)
SystemRandom().shuffle(alt)
return alt[0]
def share_to_edges(self, message_id):
edge = self.pick_edge()
edge.node.add_message(message_id)
2020-10-22 16:23:16 +00:00
edge.weight[0] += 1
2020-10-19 07:32:34 +00:00
def lookup_messages(self):
edge = self.pick_edge()
2020-10-22 16:23:16 +00:00
for message in edge.node.sharing_messages():
2020-10-19 07:32:34 +00:00
if message.hash_id not in self._messages:
self._messages.append(message.hash_id)
2020-10-22 16:23:16 +00:00
print(self.node_id, "found", message.hash_id)
edge.weight[0] += 1
2020-10-19 07:32:34 +00:00
def make_new_connections(self):
edge = self.pick_edge()
2020-10-22 16:23:16 +00:00
for new_edge in edge.node.edges:
2020-10-19 07:32:34 +00:00
if new_edge not in self.edges:
2020-10-22 16:23:16 +00:00
self.edges.append(WeightedEdge(new_edge.node, [0]))
print(self.node_id, "added peer", new_edge.node.node_id)
edge.weight[0] += 1
2020-10-19 07:32:34 +00:00
class Message:
def __init__(self, data):
def _do_hash(data):
h = sha3_256()
h.update(data)
return h.digest()
self.hash_id = _do_hash(data)
self.data = data
bootstrap_node = Node(False, [])
2020-10-22 16:23:16 +00:00
graph = [WeightedEdge(bootstrap_node, [0])]
2020-10-19 07:32:34 +00:00
for i in range(10):
2020-10-22 16:23:16 +00:00
graph.append(WeightedEdge(Node(False, [WeightedEdge(bootstrap_node, [0])]), [0]))
2020-10-19 07:32:34 +00:00
2020-10-22 16:23:16 +00:00
m = Message(b"hello world")
2020-10-19 07:32:34 +00:00
message_pool.add(m)
2020-10-22 16:23:16 +00:00
bootstrap_node.add_message(m.hash_id)
def node_driver(node):
while True:
2020-10-19 07:32:34 +00:00
node.make_new_connections()
node.lookup_messages()
2020-10-22 16:23:16 +00:00
sleep(1)
for edge in graph:
Thread(target=node_driver, args=[edge.node], daemon=True).start()
input()