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
1406a81e
Commit
1406a81e
authored
Feb 05, 2024
by
Saswat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix broadcast and latency
parent
2e11cbbd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
12 deletions
+23
-12
network.py
network.py
+1
-1
node.py
node.py
+22
-11
No files found.
network.py
View file @
1406a81e
...
...
@@ -7,7 +7,7 @@ class Network:
def
__init__
(
self
,
sim
,
args
):
self
.
args
=
args
self
.
sim
=
sim
self
.
pij
=
random
.
randrange
(
10
,
500
)
# speed of light propagation delay in ms
self
.
pij
=
float
(
random
.
randrange
(
10
,
500
))
*
0.001
# speed of light propagation delay in ms
self
.
create_nodes
()
self
.
create_connections
()
self
.
sim
.
push_event
(
Event
(
0
,
self
.
init_simulation
,))
...
...
node.py
View file @
1406a81e
...
...
@@ -5,11 +5,14 @@ from utils import *
#TODO(SM): Need to bind each transaction with a time of arrival at a node
txn_id
=
0
def
ge
t
_txn_id
():
def
ge
n
_txn_id
():
global
txn_id
txn_id
+=
1
return
txn_id
def
get_txn_id
(
txn
):
return
txn
.
split
(
":"
)[
0
]
class
Node
:
def
__init__
(
self
,
sim
,
ID
,
bandwidth_type
,
cpu_type
,
Tk
,
T_tx
,
num_nodes
,
pij
):
# Initialize node attributes
...
...
@@ -49,8 +52,9 @@ class Node:
cij
=
100
else
:
cij
=
5
cij
*=
1e6
dij
=
sample_exponential
(
96
/
cij
)
dij
=
sample_exponential
(
(
96
*
1e3
)
/
cij
)
latency
=
dij
+
self
.
pij
if
(
type
(
data
)
==
str
):
# transaction
...
...
@@ -65,19 +69,26 @@ class Node:
"""
Event to receive a transaction from a neighbor
"""
print
(
self
.
nodeId
,
"Received transaction"
,
txn
)
txn
=
txn
[
0
]
print
(
self
.
sim
.
curr_time
,
self
.
nodeId
,
"Received transaction"
,
get_txn_id
(
txn
))
if
txn
not
in
self
.
pending_transactions
:
self
.
pending_transactions
.
append
(
txn
)
# Broadcast the received transaction to the neighbors immediately
self
.
broadcast_transaction
(
txn
)
else
:
print
(
self
.
sim
.
curr_time
,
self
.
nodeId
,
"Already exist, discarding "
,
get_txn_id
(
txn
))
return
def
_event_
broadcast_transaction
(
self
,
txn
):
def
broadcast_transaction
(
self
,
txn
):
"""
Broadcast the transaction to the neighbors
"""
print
(
self
.
nodeId
,
"Broadcasting txn"
,
txn
)
print
(
self
.
sim
.
curr_time
,
self
.
nodeId
,
"Broadcasting txn"
,
get_txn_id
(
txn
)
)
for
neighbor
in
self
.
neighbors
:
print
(
self
.
nodeId
,
"Sending txn"
,
txn
,
"to"
,
neighbor
.
nodeId
)
print
(
self
.
sim
.
curr_time
,
self
.
nodeId
,
"Sending txn"
,
get_txn_id
(
txn
),
\
"to"
,
neighbor
.
nodeId
,
self
.
sim
.
curr_time
\
+
self
.
get_latency
(
neighbor
,
txn
))
self
.
sim
.
push_event
(
Event
(
self
.
sim
.
curr_time
+
self
.
get_latency
(
neighbor
,
txn
),
\
neighbor
.
_event_receive_transaction
,
txn
))
return
...
...
@@ -95,9 +106,9 @@ class Node:
amount
=
random
.
randint
(
1
,
self
.
balance
+
10
)
# Create a transaction and it to current nodes pending transactions
txn
=
str
(
ge
t
_txn_id
())
+
": "
+
str
(
self
.
nodeId
)
+
" pays "
\
txn
=
str
(
ge
n
_txn_id
())
+
": "
+
str
(
self
.
nodeId
)
+
" pays "
\
+
str
(
dest
)
+
" "
+
str
(
amount
)
+
" coins"
print
(
self
.
nodeId
,
"Generated a txn:"
,
txn
)
print
(
self
.
sim
.
curr_time
,
self
.
nodeId
,
"Generated a txn:"
,
txn
)
self
.
pending_transactions
.
append
(
txn
)
...
...
@@ -106,5 +117,5 @@ class Node:
self
.
_event_generate_transaction
))
# broadcast the transaction to the neighbors
self
.
_event_
broadcast_transaction
(
txn
)
self
.
broadcast_transaction
(
txn
)
pass
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