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
ad275da9
Commit
ad275da9
authored
Feb 04, 2024
by
Saswat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Random transaction generation
parent
54e4d349
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
96 additions
and
41 deletions
+96
-41
.gitignore
.gitignore
+1
-0
blockchain.py
blockchain.py
+8
-5
main.py
main.py
+6
-1
network.py
network.py
+21
-10
node.py
node.py
+33
-7
simulate.py
simulate.py
+23
-18
utils.py
utils.py
+4
-0
No files found.
.gitignore
0 → 100644
View file @
ad275da9
__pycache__/
\ No newline at end of file
blockchain.py
View file @
ad275da9
class
Block
:
def
__init__
(
self
,
ID
,
transactions
,
previous_block_ID
):
# Initialize block attributes
return
class
Blockchain
:
def
__init__
(
self
):
# Initialize blockchain attributes
pass
def
mine_block
(
peer
,
blockchain
,
longest_chain
):
# Simulate PoW mining and block creation
pass
def
validate_block
(
peer
,
block
):
# Validate transactions in the block
pass
def
resolve_forks
(
peer
,
blockchain
,
longest_chain
):
# Resolve forks and update the longest chain
pass
def
update_tree_file
(
peer
,
blockchain
):
# Update tree file with block information for each node
pass
\ No newline at end of file
main.py
View file @
ad275da9
import
argparse
from
simulate
import
EventSimulator
import
random
from
network
import
Network
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
(
'--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
=
100
0
,
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"
)
return
parser
.
parse_args
()
...
...
@@ -23,5 +24,9 @@ if __name__ == '__main__':
random
.
seed
(
args
.
seed
)
print
(
args
)
sim
=
EventSimulator
(
args
)
nx
=
Network
(
sim
,
args
)
sim
.
startSimulation
()
exit
(
0
)
\ No newline at end of file
network.py
View file @
ad275da9
from
node
import
Node
import
random
from
simulate
import
Event
from
utils
import
*
class
Network
:
def
__init__
(
self
,
args
):
def
__init__
(
self
,
sim
,
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
()
pass
def
initialise_nodes
(
self
):
self
.
sim
.
push_event
(
Event
(
0
,
self
.
init_simulation
))
return
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
"""
...
...
@@ -42,7 +51,8 @@ class Network:
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
]))
self
.
nodes
.
append
(
Node
(
self
.
sim
,
i
,
bandwidth_type
[
i
],
\
cpu_type
[
i
],
Tk
[
i
],
self
.
args
.
T_tx
,
self
.
args
.
num_nodes
))
return
def
create_connections
(
self
):
...
...
@@ -54,11 +64,11 @@ class Network:
valid_graph
=
False
cnt
=
0
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
)]
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
]
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
)))
for
j
in
neighbors
:
adjacency_list
[
i
]
.
append
(
j
)
...
...
@@ -69,8 +79,9 @@ class Network:
if
len
(
adjacency_list
[
i
])
<
3
or
len
(
adjacency_list
[
i
])
>
6
:
valid_graph
=
False
break
#print(adjacency_list)
cnt
+=
1
for
neighbors
in
adjacency_list
:
print
(
neighbors
)
for
i
in
range
(
num_nodes
):
for
j
in
adjacency_list
[
i
]:
self
.
nodes
[
i
]
.
neighbors
.
append
(
id
(
self
.
nodes
[
j
]))
...
...
node.py
View file @
ad275da9
from
blockchain
import
Blockchain
from
simulate
import
Event
from
utils
import
*
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
self
.
sim
=
sim
self
.
balance
=
0
self
.
nodeId
=
ID
self
.
cpuType
=
cpu_type
self
.
bandwidth_type
=
bandwidth_type
self
.
Tk
=
Tk
self
.
T_tx
=
T_tx
self
.
num_nodes
=
num_nodes
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
# Format: "TxnID: IDx pays IDy C coins"
p
ass
p
rint
(
"Generating a transaction for node"
,
self
.
nodeId
)
def
set_neighbors
(
neighbors
):
# Set the neighbors of the node
pass
\ No newline at end of file
# Select a random destination node and amount
dest
=
self
.
nodeId
while
dest
==
self
.
nodeId
:
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
simulate.py
View file @
ad275da9
from
network
import
Network
import
time
import
random
event_id
=
0
# for debug purposes
class
Event
:
def
__init__
(
self
,
time
,
operation
,
args
):
def
__init__
(
self
,
time
,
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
)
return
self
.
operation
()
class
EventSimulator
:
"""
...
...
@@ -17,31 +16,37 @@ class EventSimulator:
call startSimulation to start the simulation
"""
def
__init__
(
self
,
args
):
self
.
nx
=
Network
(
args
)
"""
Initialize the simulator with the given parameters
"""
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
print
(
"Created an Event Simulator with sim_time"
,
self
.
sim_time
)
def
startSimulation
(
self
):
"""
Start the simulation.
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
:
event
=
self
.
queue
.
pop
(
0
)
new_events
=
event
.
process
()
self
.
push_new_events
(
new_events
)
self
.
curr_time
=
event
.
time
event
.
process
(
)
return
def
push_
new_events
(
self
,
events
):
def
push_
event
(
self
,
event
):
"""
Add e
ach e
vent 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
:
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
)
print
(
self
.
curr_time
,
"Pushing event at time"
,
event
.
time
)
time
.
sleep
(
2
)
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
)
return
\ No newline at end of file
utils.py
0 → 100644
View file @
ad275da9
import
random
def
sample_exponential
(
mean
):
return
random
.
expovariate
(
1
/
mean
)
\ No newline at end of file
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