Commit ad275da9 authored by Saswat's avatar Saswat

Random transaction generation

parent 54e4d349
__pycache__/
\ No newline at end of file
class Block: class Block:
def __init__(self, ID, transactions, previous_block_ID): def __init__(self, ID, transactions, previous_block_ID):
# Initialize block attributes # Initialize block attributes
return
class Blockchain: class Blockchain:
def __init__(self): def __init__(self):
# Initialize blockchain attributes # Initialize blockchain attributes
pass
def mine_block(peer, blockchain, longest_chain): def mine_block(peer, blockchain, longest_chain):
# Simulate PoW mining and block creation # Simulate PoW mining and block creation
pass
def validate_block(peer, block): def validate_block(peer, block):
# Validate transactions in the block # Validate transactions in the block
pass
def resolve_forks(peer, blockchain, longest_chain): def resolve_forks(peer, blockchain, longest_chain):
# Resolve forks and update the longest chain # Resolve forks and update the longest chain
pass
def update_tree_file(peer, blockchain): def update_tree_file(peer, blockchain):
# Update tree file with block information for each node # Update tree file with block information for each node
pass
\ No newline at end of file
import argparse import argparse
from simulate import EventSimulator from simulate import EventSimulator
import random import random
from network import Network
def parse_cmd(): def parse_cmd():
""" """
...@@ -14,7 +15,7 @@ def parse_cmd(): ...@@ -14,7 +15,7 @@ def parse_cmd():
parser.add_argument('--I', type = int, default = 100, help="Interarrival time between blocks") 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('--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('--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('--sim_time', type = int, default = 100, help="Simulation time in seconds")
parser.add_argument('--seed', type = int, default = 0, help="Seed for random number generator") parser.add_argument('--seed', type = int, default = 0, help="Seed for random number generator")
return parser.parse_args() return parser.parse_args()
...@@ -23,5 +24,9 @@ if __name__ == '__main__': ...@@ -23,5 +24,9 @@ if __name__ == '__main__':
random.seed(args.seed) random.seed(args.seed)
print(args) print(args)
sim = EventSimulator(args) sim = EventSimulator(args)
nx = Network(sim, args)
sim.startSimulation()
exit(0) exit(0)
\ No newline at end of file
from node import Node from node import Node
import random import random
from simulate import Event
from utils import *
class Network: class Network:
def __init__(self, args): def __init__(self, sim, args):
self.args = args self.args = args
self.initialise_nodes() self.sim = sim
self.pij = random.randrange(10, 500) # speed of light propagation delay in ms
self.create_nodes()
self.create_connections() self.create_connections()
self.sim.push_event(Event(0, self.init_simulation))
pass return
def initialise_nodes(self): def init_simulation(self):
for node in self.nodes:
node.init_simulation()
return
def create_nodes(self):
""" """
Create a list of nodes with the specified parameters Create a list of nodes with the specified parameters
""" """
...@@ -42,7 +51,8 @@ class Network: ...@@ -42,7 +51,8 @@ class Network:
Tk.append(10 * (1 - h_k_slow)) Tk.append(10 * (1 - h_k_slow))
for i in range(self.args.num_nodes): for i in range(self.args.num_nodes):
self.nodes.append(Node(i, bandwidth_type[i], cpu_type[i], Tk[i])) self.nodes.append(Node(self.sim, i, bandwidth_type[i],\
cpu_type[i], Tk[i], self.args.T_tx, self.args.num_nodes))
return return
def create_connections(self): def create_connections(self):
...@@ -54,11 +64,11 @@ class Network: ...@@ -54,11 +64,11 @@ class Network:
valid_graph = False valid_graph = False
cnt = 0 cnt = 0
while valid_graph == False: while valid_graph == False:
print("tyring to create a valid graph",cnt) #print("tyring to create a valid graph",cnt)
adjacency_list = [[] for _ in range(num_nodes)] adjacency_list = [[] for _ in range(num_nodes)]
for i in range(num_nodes): for i in range(num_nodes):
num_connections = random.randint(3, 6) num_connections = random.randint(3, 6)
potential_neighbors = [x for x in range(num_nodes) if x != i and len(adjacency_list[x]) < 6] potential_neighbors = [x for x in range(num_nodes) if x != i and len(adjacency_list[x]) < 6 and x not in adjacency_list[i]]
neighbors = random.sample(potential_neighbors, min(num_connections, len(potential_neighbors))) neighbors = random.sample(potential_neighbors, min(num_connections, len(potential_neighbors)))
for j in neighbors: for j in neighbors:
adjacency_list[i].append(j) adjacency_list[i].append(j)
...@@ -69,8 +79,9 @@ class Network: ...@@ -69,8 +79,9 @@ class Network:
if len(adjacency_list[i]) < 3 or len(adjacency_list[i]) > 6: if len(adjacency_list[i]) < 3 or len(adjacency_list[i]) > 6:
valid_graph = False valid_graph = False
break break
#print(adjacency_list)
cnt += 1 cnt += 1
for neighbors in adjacency_list:
print(neighbors)
for i in range(num_nodes): for i in range(num_nodes):
for j in adjacency_list[i]: for j in adjacency_list[i]:
self.nodes[i].neighbors.append(id(self.nodes[j])) self.nodes[i].neighbors.append(id(self.nodes[j]))
......
from blockchain import Blockchain
from simulate import Event
from utils import *
class Node: class Node:
def __init__(self, ID, bandwidth_type, cpu_type, Tk): def __init__(self, sim, ID, bandwidth_type, cpu_type, Tk, T_tx, num_nodes):
# Initialize node attributes # Initialize node attributes
self.sim = sim
self.balance = 0 self.balance = 0
self.nodeId = ID self.nodeId = ID
self.cpuType = cpu_type self.cpuType = cpu_type
self.bandwidth_type = bandwidth_type self.bandwidth_type = bandwidth_type
self.Tk = Tk self.Tk = Tk
self.T_tx = T_tx
self.num_nodes = num_nodes
self.neighbors = [] self.neighbors = []
def generate_transaction(peer):
self.blockchain = Blockchain()
self.pending_transactions = []
def init_simulation(self):
# Initialize simulation
# schedule the first transaction
self.sim.push_event(Event(self.sim.curr_time + sample_exponential(self.T_tx),\
self.generate_random_transaction))
pass
def generate_random_transaction(self):
# 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 print("Generating a transaction for node", self.nodeId)
def set_neighbors(neighbors): # Select a random destination node and amount
# Set the neighbors of the node dest = self.nodeId
pass while dest == self.nodeId:
\ No newline at end of file dest = random.randint(0, self.num_nodes)
amount = random.randint(1, self.balance + 10)
txn = "TxnID: " + str(self.nodeId) + " pays " + str(dest) + " " + str(amount) + " coins"
self.pending_transactions.append(txn)
# schedule the next transaction
self.sim.push_event(Event(self.sim.curr_time + sample_exponential(self.T_tx),\
self.generate_random_transaction))
pass
from network import Network
import time import time
import random import random
event_id = 0 # for debug purposes
class Event: class Event:
def __init__(self, time, operation, args): def __init__(self, time, operation):
self.time = time self.time = time
self.operation = operation self.operation = operation
self.args = args
def process(self): def process(self):
print("Processing event at time", self.time) print("Processing event at time", self.time)
return self.operation(self.args) return self.operation()
class EventSimulator: class EventSimulator:
""" """
...@@ -17,31 +16,37 @@ class EventSimulator: ...@@ -17,31 +16,37 @@ class EventSimulator:
call startSimulation to start the simulation call startSimulation to start the simulation
""" """
def __init__(self, args): def __init__(self, args):
self.nx = Network(args) """
Initialize the simulator with the given parameters
"""
self.queue = [] self.queue = []
self.sim_time = args.sim_time self.sim_time = args.sim_time
self.curr_time = 0 self.curr_time = 0
self.pij = random.randrange(10, 500) # speed of light propagation delay in ms print("Created an Event Simulator with sim_time", self.sim_time)
def startSimulation(self): def startSimulation(self):
""" """
Start the simulation. Start the simulation.
Run till the simulation time is reached or the queue is empty Run till the simulation time is reached or the queue is empty
""" """
print("Starting the simulation")
while len(self.queue) > 0 and self.queue[0].time < self.sim_time: while len(self.queue) > 0 and self.queue[0].time < self.sim_time:
event = self.queue.pop(0) event = self.queue.pop(0)
new_events = event.process() self.curr_time = event.time
self.push_new_events(new_events) event.process()
return return
def push_new_events(self, events): def push_event(self, event):
""" """
Add each event to the queue just before the time it is supposed to occur Add event to the queue just before the time it is supposed to occur
""" """
for event in events: print(self.curr_time, "Pushing event at time", event.time)
for i in range(len(self.queue)): time.sleep(2)
if self.queue[i].time > event.time: for i in range(len(self.queue)):
self.queue.insert(i, event) if self.queue[i].time > event.time:
break self.queue.insert(i, event)
else: break
self.queue.append(event) else:
self.queue.append(event)
return
\ No newline at end of file
import random
def sample_exponential(mean):
return random.expovariate(1/mean)
\ No newline at end of file
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