Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
blockchain_CS765_HW1
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Saswat
blockchain_CS765_HW1
Commits
54e4d349
Commit
54e4d349
authored
Feb 04, 2024
by
Saswat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cmd args, init simulate, create network
parent
417aca85
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
153 additions
and
22 deletions
+153
-22
main.py
main.py
+27
-0
network.py
network.py
+72
-8
node.py
node.py
+11
-4
simulate.py
simulate.py
+43
-10
No files found.
main.py
0 → 100644
View file @
54e4d349
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
network.py
View file @
54e4d349
from
node
import
Node
import
random
class
Network
:
def
__init__
(
self
,
n
):
self
.
n
=
n
pass
def
__init__
(
self
,
args
):
self
.
args
=
args
self
.
initialise_nodes
()
self
.
create_connections
()
def
initialise_nodes
():
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
):
# Create a connected graph of peers
# Ensure each peer has 3 to 6 connections
pass
def
create_connections
(
self
):
"""
Create a bidirectional graph, 3-6 adjacent nodes per node
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
):
# Calculate latency based on provided formula
...
...
node.py
View file @
54e4d349
class
Node
:
def
__init__
(
self
,
ID
,
cpuType
,
speed
,
balance
):
def
__init__
(
self
,
ID
,
bandwidth_type
,
cpu_type
,
Tk
):
# Initialize node attributes
self
.
balance
=
0
# intialize random
self
.
nodeId
=
0
# initialize random
self
.
cpuType
=
CPU_type
self
.
balance
=
0
self
.
nodeId
=
ID
self
.
cpuType
=
cpu_type
self
.
bandwidth_type
=
bandwidth_type
self
.
Tk
=
Tk
self
.
neighbors
=
[]
def
generate_transaction
(
peer
):
# Generate a transaction for the given peer
# Format: "TxnID: IDx pays IDy C coins"
pass
def
set_neighbors
(
neighbors
):
# Set the neighbors of the node
pass
\ No newline at end of file
simulate.py
View file @
54e4d349
from
network
import
Network
import
time
import
random
class
Event
:
# time
# operation
def
__init__
(
self
,
time
,
operation
,
args
):
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
:
def
__init__
(
self
):
# max_time of simulation
# queue of events
# create and initialise a network
"""
Simulate the network
call startSimulation to start the simulation
"""
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
):
# run a loop till queue is empty / time runouts
# push new events from triggers into the queue
pass
"""
Start the simulation.
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment