added graph simulation script
parent
77de7e318d
commit
b5fe4453ed
|
@ -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 hashlib import sha3_256
|
||||||
from secrets import randbelow
|
from secrets import randbelow
|
||||||
from random import SystemRandom
|
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
|
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"]):
|
edges: List[WeightedEdge], graph_ids=["main"]):
|
||||||
self.hidden_node = hidden_node
|
self.hidden_node = hidden_node
|
||||||
self.edges = edges
|
self.edges = edges
|
||||||
|
self.node_id = str(uuid.uuid4())
|
||||||
self._messages: List[str] = list() # hashes point to message_pool
|
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):
|
def sharing_messages(self):
|
||||||
share = list(self._messages)
|
share = list(self._messages)
|
||||||
|
@ -31,16 +36,16 @@ class Node:
|
||||||
|
|
||||||
def add_message(self, hash_id):
|
def add_message(self, hash_id):
|
||||||
if hash_id not in self._messages:
|
if hash_id not in self._messages:
|
||||||
self._messages.append(message.hash_id)
|
self._messages.append(hash_id)
|
||||||
|
|
||||||
def pick_edge(self):
|
def pick_edge(self):
|
||||||
selected = self.edges[0]
|
selected = self.edges[0]
|
||||||
alt = []
|
alt = []
|
||||||
|
|
||||||
for weighted_edge in self.edges[1:]:
|
for weighted_edge in self.edges[1:]:
|
||||||
if weighted_edge.weight > selected.weight:
|
if weighted_edge.weight[0] > selected.weight:
|
||||||
selected = weighted_edge
|
selected = weighted_edge
|
||||||
elif weighted_edge.weight == selected.weight:
|
elif weighted_edge.weight[0] == selected.weight:
|
||||||
alt.append(selected)
|
alt.append(selected)
|
||||||
alt.append(selected)
|
alt.append(selected)
|
||||||
SystemRandom().shuffle(alt)
|
SystemRandom().shuffle(alt)
|
||||||
|
@ -50,21 +55,23 @@ class Node:
|
||||||
def share_to_edges(self, message_id):
|
def share_to_edges(self, message_id):
|
||||||
edge = self.pick_edge()
|
edge = self.pick_edge()
|
||||||
edge.node.add_message(message_id)
|
edge.node.add_message(message_id)
|
||||||
edge.weight += 1
|
edge.weight[0] += 1
|
||||||
|
|
||||||
def lookup_messages(self):
|
def lookup_messages(self):
|
||||||
edge = self.pick_edge()
|
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:
|
if message.hash_id not in self._messages:
|
||||||
self._messages.append(message.hash_id)
|
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):
|
def make_new_connections(self):
|
||||||
edge = self.pick_edge()
|
edge = self.pick_edge()
|
||||||
for new_edge in edge.edges:
|
for new_edge in edge.node.edges:
|
||||||
if new_edge not in self.edges:
|
if new_edge not in self.edges:
|
||||||
self.edges.append(WeightedEdge(new_edge.node, 0))
|
self.edges.append(WeightedEdge(new_edge.node, [0]))
|
||||||
edge.weight += 1
|
print(self.node_id, "added peer", new_edge.node.node_id)
|
||||||
|
edge.weight[0] += 1
|
||||||
|
|
||||||
|
|
||||||
class Message:
|
class Message:
|
||||||
|
@ -77,16 +84,22 @@ class Message:
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
bootstrap_node = Node(False, [])
|
bootstrap_node = Node(False, [])
|
||||||
graph = [bootstrap_node]
|
graph = [WeightedEdge(bootstrap_node, [0])]
|
||||||
|
|
||||||
for i in range(10):
|
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)
|
message_pool.add(m)
|
||||||
bootstrap_node.add_message(m)
|
bootstrap_node.add_message(m.hash_id)
|
||||||
|
|
||||||
|
def node_driver(node):
|
||||||
while True:
|
while True:
|
||||||
for node in graph:
|
|
||||||
node.make_new_connections()
|
node.make_new_connections()
|
||||||
node.lookup_messages()
|
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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue