Commit 54e4d349 authored by Saswat's avatar Saswat

cmd args, init simulate, create network

parent 417aca85
import argparse
from simulate import EventSimulator
import random
def parse_cmd():
"""
Parse command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num_nodes', type = int, default = 50, help="Number of nodes in the network")
parser.add_argument('--z0', type = int, default = 10, help="percentage of node with slow bandwidth")
parser.add_argument('--z1', type = int, default = 10, help="percentage of node with low CPU")
parser.add_argument('--T_tx', type = int, default = 10, help="Interarrival time between transactions")
parser.add_argument('--I', type = int, default = 100, help="Interarrival time between blocks")
parser.add_argument('--T_dij', type = int, default = 10, help="Mean of queuing delay")
#parser.add_argument('--pij', type = int, default = 10, help="speed of light propagation delay")
parser.add_argument('--sim_time', type = int, default = 1000, help="Simulation time in seconds")
parser.add_argument('--seed', type = int, default = 0, help="Seed for random number generator")
return parser.parse_args()
if __name__ == '__main__':
args = parse_cmd()
random.seed(args.seed)
print(args)
sim = EventSimulator(args)
exit(0)
\ No newline at end of file
from node import Node from node import Node
import random
class Network: class Network:
def __init__(self, n): def __init__(self, args):
self.n = n self.args = args
pass self.initialise_nodes()
self.create_connections()
def initialise_nodes():
pass pass
def initialise_nodes(self):
"""
Create a list of nodes with the specified parameters
"""
self.nodes = []
# Create a list of badwidth and cpu type based on z0 and z1
# 0 is slow and 1 is fast
bandwidth_type = [1] * self.args.num_nodes
num_slow_bandwidth = int((self.args.z0*self.args.num_nodes)/100)
rand_idx = random.sample(range(self.args.num_nodes), num_slow_bandwidth)
for idx in rand_idx:
bandwidth_type[idx] = 0
cpu_type = [1] * self.args.num_nodes
num_slow_cpu = int((self.args.z1*self.args.num_nodes)/100)
rand_idx = random.sample(range(self.args.num_nodes), num_slow_cpu)
for idx in rand_idx:
cpu_type[idx] = 0
# Based on CPU type calculate the PoW time Tk
# 10 * h_k * (n - num_slow) + h_k * num_slow = 1
# => h_k = 1 / (10 * n - 9 * num_slow)
h_k_slow = 1 / float(10*self.args.num_nodes - 9*num_slow_cpu)
Tk = []
for i in range(self.args.num_nodes):
if cpu_type[i] == 0:
Tk.append(h_k_slow)
else:
Tk.append(10 * (1 - h_k_slow))
for i in range(self.args.num_nodes):
self.nodes.append(Node(i, bandwidth_type[i], cpu_type[i], Tk[i]))
return
def create_network(peers): def create_connections(self):
# Create a connected graph of peers """
# Ensure each peer has 3 to 6 connections Create a bidirectional graph, 3-6 adjacent nodes per node
pass TODO(SM): Need a better way to create a valid graph
"""
num_nodes = self.args.num_nodes
valid_graph = False
cnt = 0
while valid_graph == False:
print("tyring to create a valid graph",cnt)
adjacency_list = [[] for _ in range(num_nodes)]
for i in range(num_nodes):
num_connections = random.randint(3, 6)
potential_neighbors = [x for x in range(num_nodes) if x != i and len(adjacency_list[x]) < 6]
neighbors = random.sample(potential_neighbors, min(num_connections, len(potential_neighbors)))
for j in neighbors:
adjacency_list[i].append(j)
adjacency_list[j].append(i)
valid_graph = True
for i in range(num_nodes):
if len(adjacency_list[i]) < 3 or len(adjacency_list[i]) > 6:
valid_graph = False
break
#print(adjacency_list)
cnt += 1
for i in range(num_nodes):
for j in adjacency_list[i]:
self.nodes[i].neighbors.append(id(self.nodes[j]))
print("Graph created")
return
def calculate_latency(sender, receiver, message_size): def calculate_latency(sender, receiver, message_size):
# Calculate latency based on provided formula # Calculate latency based on provided formula
......
class Node: class Node:
def __init__(self, ID, cpuType, speed, balance): def __init__(self, ID, bandwidth_type, cpu_type, Tk):
# Initialize node attributes # Initialize node attributes
self.balance = 0 # intialize random self.balance = 0
self.nodeId = 0 # initialize random self.nodeId = ID
self.cpuType = CPU_type self.cpuType = cpu_type
self.bandwidth_type = bandwidth_type
self.Tk = Tk
self.neighbors = []
def generate_transaction(peer): def generate_transaction(peer):
# Generate a transaction for the given peer # Generate a transaction for the given peer
# Format: "TxnID: IDx pays IDy C coins" # Format: "TxnID: IDx pays IDy C coins"
pass pass
def set_neighbors(neighbors):
# Set the neighbors of the node
pass
\ No newline at end of file
from network import Network
import time
import random
class Event: class Event:
# time def __init__(self, time, operation, args):
# operation self.time = time
self.operation = operation
self.args = args
def process(self):
print("Processing event at time", self.time)
return self.operation(self.args)
class EventSimulator: class EventSimulator:
def __init__(self): """
# max_time of simulation Simulate the network
# queue of events call startSimulation to start the simulation
# create and initialise a network """
def __init__(self, args):
self.nx = Network(args)
self.queue = []
self.sim_time = args.sim_time
self.curr_time = 0
self.pij = random.randrange(10, 500) # speed of light propagation delay in ms
def startSimulation(self): def startSimulation(self):
# run a loop till queue is empty / time runouts """
# push new events from triggers into the queue Start the simulation.
pass Run till the simulation time is reached or the queue is empty
"""
while len(self.queue) > 0 and self.queue[0].time < self.sim_time:
event = self.queue.pop(0)
new_events = event.process()
self.push_new_events(new_events)
return
def push_new_events(self, events):
"""
Add each event to the queue just before the time it is supposed to occur
"""
for event in events:
for i in range(len(self.queue)):
if self.queue[i].time > event.time:
self.queue.insert(i, event)
break
else:
self.queue.append(event)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment