Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
3ba7442d
Commit
3ba7442d
authored
6 years ago
by
Pierre Krieger
Committed by
Arkadiy Paronyan
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Properly distinguish in/out connections in the PSM (#2102)
parent
418edcfb
Branches
Branches containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
substrate/core/peerset/src/lib.rs
+26
-14
26 additions, 14 deletions
substrate/core/peerset/src/lib.rs
with
26 additions
and
14 deletions
substrate/core/peerset/src/lib.rs
+
26
−
14
View file @
3ba7442d
...
...
@@ -31,14 +31,18 @@ pub struct Peerset {
struct
Inner
{
/// List of nodes that we know exist but we are not connected to.
/// Elements in this list must never be in `out_slots` or `in_slots`.
discovered
:
Vec
<
PeerId
>
,
/// List of reserved nodes.
reserved
:
HashSet
<
PeerId
>
,
/// If true, we only accept reserved nodes.
reserved_only
:
bool
,
/// Node slots. Each slot contains either `None` if the node is free, or `Some` if it is
/// assigned to a peer.
slots
:
Vec
<
Option
<
PeerId
>>
,
/// Node slots for outgoing connections. Each slot contains either `None` if the node is free,
/// or `Some` if it is assigned to a peer.
out_slots
:
Vec
<
Option
<
PeerId
>>
,
/// Node slots for incoming connections. Each slot contains either `None` if the node is free,
/// or `Some` if it is assigned to a peer.
in_slots
:
Vec
<
Option
<
PeerId
>>
,
}
/// Message that can be sent by the peer set manager (PSM).
...
...
@@ -111,7 +115,8 @@ impl Peerset {
discovered
:
config
.bootnodes
.into_iter
()
.collect
(),
reserved
:
Default
::
default
(),
reserved_only
:
config
.reserved_only
,
slots
:
(
0
..
(
config
.in_peers
+
config
.out_peers
))
.map
(|
_
|
None
)
.collect
(),
out_slots
:
(
0
..
config
.out_peers
)
.map
(|
_
|
None
)
.collect
(),
in_slots
:
(
0
..
config
.in_peers
)
.map
(|
_
|
None
)
.collect
(),
};
alloc_slots
(
&
mut
inner
,
&
tx
);
...
...
@@ -147,10 +152,15 @@ impl Peerset {
return
;
}
// Nothing more to do if we're already connected.
if
inner
.out_slots
.iter
()
.chain
(
inner
.in_slots
.iter
())
.any
(|
s
|
s
.as_ref
()
==
Some
(
&
peer_id
))
{
return
;
}
// Assign a slot for this reserved peer.
if
let
Some
(
pos
)
=
inner
.slots
.iter
()
.position
(|
s
|
s
.as_ref
()
.map
(|
n
|
!
inner
.reserved
.contains
(
n
))
.unwrap_or
(
true
))
{
if
let
Some
(
pos
)
=
inner
.
out_
slots
.iter
()
.position
(|
s
|
s
.as_ref
()
.map
(|
n
|
!
inner
.reserved
.contains
(
n
))
.unwrap_or
(
true
))
{
let
_
=
self
.tx
.unbounded_send
(
Message
::
Connect
(
peer_id
.clone
()));
inner
.slots
[
pos
]
=
Some
(
peer_id
);
inner
.
out_
slots
[
pos
]
=
Some
(
peer_id
);
}
else
{
// All slots are filled with reserved peers.
...
...
@@ -176,7 +186,7 @@ impl Peerset {
// Disconnect non-reserved nodes.
if
reserved_only
{
for
slot
in
&
mut
inner
.slots
{
for
slot
in
inner
.out_slots
.iter_mut
()
.chain
(
inner
.
in_
slots
.iter_mut
())
{
if
let
Some
(
peer
)
=
slot
.as_ref
()
{
if
inner
.reserved
.contains
(
peer
)
{
continue
;
...
...
@@ -201,7 +211,7 @@ fn alloc_slots(inner: &mut Inner, tx: &mpsc::UnboundedSender<Message>) {
return
;
}
for
slot
in
inner
.slots
.iter_mut
()
{
for
slot
in
inner
.
out_
slots
.iter_mut
()
{
if
slot
.is_some
()
{
continue
;
}
...
...
@@ -226,12 +236,12 @@ impl PeersetMut {
/// peerset is already connected to, in which case it must not answer.
pub
fn
incoming
(
&
self
,
peer_id
:
PeerId
,
index
:
IncomingIndex
)
{
let
mut
inner
=
self
.parent.inner
.lock
();
if
inner
.slots
.iter
()
.any
(|
s
|
s
.as_ref
()
==
Some
(
&
peer_id
))
{
if
inner
.
out_
slots
.iter
()
.
chain
(
inner
.in_slots
.iter
())
.
any
(|
s
|
s
.as_ref
()
==
Some
(
&
peer_id
))
{
return
}
if
let
Some
(
pos
)
=
inner
.slots
.iter
()
.position
(|
s
|
s
.is_none
())
{
inner
.slots
[
pos
]
=
Some
(
peer_id
);
if
let
Some
(
pos
)
=
inner
.
in_
slots
.iter
()
.position
(|
s
|
s
.is_none
())
{
inner
.
in_
slots
[
pos
]
=
Some
(
peer_id
);
let
_
=
self
.parent.tx
.unbounded_send
(
Message
::
Accept
(
index
));
}
else
{
if
inner
.discovered
.iter
()
.all
(|
p
|
*
p
!=
peer_id
)
{
...
...
@@ -247,6 +257,7 @@ impl PeersetMut {
/// `PeerId`, or accepted an incoming connection with this `PeerId`.
pub
fn
dropped
(
&
self
,
peer_id
:
&
PeerId
)
{
let
mut
inner
=
self
.parent.inner
.lock
();
let
inner
=
&
mut
*
inner
;
// Fixes a borrowing issue.
// Automatically connect back if reserved.
if
inner
.reserved
.contains
(
peer_id
)
{
...
...
@@ -255,9 +266,10 @@ impl PeersetMut {
}
// Otherwise, free the slot.
for
slot
in
inner
.slots
.iter_mut
()
{
for
slot
in
inner
.
out_
slots
.iter_mut
()
.chain
(
inner
.in_slots
.iter_mut
())
{
if
slot
.as_ref
()
==
Some
(
peer_id
)
{
*
slot
=
None
;
break
;
}
}
...
...
@@ -266,7 +278,7 @@ impl PeersetMut {
if
inner
.discovered
.iter
()
.all
(|
p
|
p
!=
peer_id
)
{
inner
.discovered
.push
(
peer_id
.clone
());
}
alloc_slots
(
&
mut
inner
,
&
self
.parent.tx
);
alloc_slots
(
inner
,
&
self
.parent.tx
);
}
/// Adds a discovered peer id to the PSM.
...
...
@@ -276,7 +288,7 @@ impl PeersetMut {
pub
fn
discovered
(
&
self
,
peer_id
:
PeerId
)
{
let
mut
inner
=
self
.parent.inner
.lock
();
if
inner
.slots
.iter
()
.any
(|
p
|
p
.as_ref
()
==
Some
(
&
peer_id
))
{
if
inner
.
out_
slots
.iter
()
.
chain
(
inner
.in_slots
.iter
())
.
any
(|
p
|
p
.as_ref
()
==
Some
(
&
peer_id
))
{
return
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment