Move networksim concerns

This commit is contained in:
Oskar Thoren 2019-01-11 00:08:49 -05:00
parent 9db4a91558
commit 5540b7e982
3 changed files with 47 additions and 46 deletions

45
data_sync/networksim.py Normal file
View File

@ -0,0 +1,45 @@
import random
# NOTE: Inspired by github.com/ethereum/research networksim.py.
class NetworkSimulator():
def __init__(self):
self.nodes = []
self.time = 0
self.queue = {}
self.peers = {}
# Global network reliability
self.reliability = 1 # 0.95? Dunno.
def tick(self):
if self.time in self.queue:
# XXX: Should sender be here?
for sender, receiver, msg in self.queue[self.time]:
if random.random() < self.reliability:
#print "*** message ok", sender.name, "->", receiver.name
receiver.on_receive(sender, msg)
#else:
#print "*** message dropped", sender.name, "->", receiver.name
#print ""
print "tick", self.time + 1
#print "-----------"
for n in self.nodes:
n.tick()
self.time += 1
# XXX: This should be normal distribution or Poisson
def latency_uniform_random(self):
# XXX: Hardcode for now, easier analyze
latency = 1
#latency = random.randint(1,3)
return latency
# NOTE: Direct message, no broadcast etc
def send_message(self, sender_id, receiver_id, message):
#print "*** (network) send_message", sender_id, receiver_id
# XXX: Assuming sender exists
sender = self.peers[sender_id]
receiver = self.peers[receiver_id]
recv_time = self.time + self.latency_uniform_random()
if recv_time not in self.queue:
self.queue[recv_time] = []
self.queue[recv_time].append((sender, receiver, message))

BIN
data_sync/networksim.pyc Normal file

Binary file not shown.

View File

@ -1,6 +1,7 @@
# Sync protocol PoC
import hashlib
import networksim
import random
import sync_pb2
import time
@ -17,51 +18,6 @@ GROUP_ID = "0xdeadbeef"
def log(message):
print message
# NOTE: Inspired by github.com/ethereum/research networksim.py.
# XXX: Break this out into separate namespace
class NetworkSimulator():
def __init__(self):
self.nodes = []
self.time = 0
self.queue = {}
self.peers = {}
# Global network reliability
self.reliability = 1 # 0.95? Dunno.
def tick(self):
if self.time in self.queue:
# XXX: Should sender be here?
for sender, receiver, msg in self.queue[self.time]:
if random.random() < self.reliability:
#print "*** message ok", sender.name, "->", receiver.name
receiver.on_receive(sender, msg)
#else:
#print "*** message dropped", sender.name, "->", receiver.name
#print ""
print "tick", self.time + 1
#print "-----------"
for n in self.nodes:
n.tick()
self.time += 1
# XXX: This should be normal distribution or Poisson
def latency_uniform_random(self):
# XXX: Hardcode for now, easier analyze
latency = 1
#latency = random.randint(1,3)
return latency
# NOTE: Direct message, no broadcast etc
def send_message(self, sender_id, receiver_id, message):
#print "*** (network) send_message", sender_id, receiver_id
# XXX: Assuming sender exists
sender = self.peers[sender_id]
receiver = self.peers[receiver_id]
recv_time = self.time + self.latency_uniform_random()
if recv_time not in self.queue:
self.queue[recv_time] = []
self.queue[recv_time].append((sender, receiver, message))
class Node():
def __init__(self, name, network, profile):
self.name = name
@ -280,7 +236,7 @@ def new_ack_record(id):
################################################################################
def run(steps=10):
n = NetworkSimulator()
n = networksim.NetworkSimulator()
a = Node("A", n, 'burstyMobile')
b = Node("B", n, 'burstyMobile')