Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
d37d91cc
Commit
d37d91cc
authored
Mar 24, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
set up a blocked map sidecar for better performance
parent
81a51448
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
11 deletions
+77
-11
ConnectionGater.go
op-node/p2p/mocks/ConnectionGater.go
+14
-0
PeerGater.go
op-node/p2p/mocks/PeerGater.go
+14
-0
peer_gater.go
op-node/p2p/peer_gater.go
+21
-6
peer_gater_test.go
op-node/p2p/peer_gater_test.go
+28
-5
No files found.
op-node/p2p/mocks/ConnectionGater.go
View file @
d37d91cc
...
...
@@ -142,6 +142,20 @@ func (_m *ConnectionGater) InterceptUpgraded(_a0 network.Conn) (bool, control.Di
return
r0
,
r1
}
// IsBlocked provides a mock function with given fields: p
func
(
_m
*
ConnectionGater
)
IsBlocked
(
p
peer
.
ID
)
bool
{
ret
:=
_m
.
Called
(
p
)
var
r0
bool
if
rf
,
ok
:=
ret
.
Get
(
0
)
.
(
func
(
peer
.
ID
)
bool
);
ok
{
r0
=
rf
(
p
)
}
else
{
r0
=
ret
.
Get
(
0
)
.
(
bool
)
}
return
r0
}
// ListBlockedAddrs provides a mock function with given fields:
func
(
_m
*
ConnectionGater
)
ListBlockedAddrs
()
[]
net
.
IP
{
ret
:=
_m
.
Called
()
...
...
op-node/p2p/mocks/PeerGater.go
View file @
d37d91cc
...
...
@@ -13,6 +13,20 @@ type PeerGater struct {
mock
.
Mock
}
// IsBlocked provides a mock function with given fields: _a0
func
(
_m
*
PeerGater
)
IsBlocked
(
_a0
peer
.
ID
)
bool
{
ret
:=
_m
.
Called
(
_a0
)
var
r0
bool
if
rf
,
ok
:=
ret
.
Get
(
0
)
.
(
func
(
peer
.
ID
)
bool
);
ok
{
r0
=
rf
(
_a0
)
}
else
{
r0
=
ret
.
Get
(
0
)
.
(
bool
)
}
return
r0
}
// Update provides a mock function with given fields: _a0, _a1
func
(
_m
*
PeerGater
)
Update
(
_a0
peer
.
ID
,
_a1
float64
)
{
_m
.
Called
(
_a0
,
_a1
)
...
...
op-node/p2p/peer_gater.go
View file @
d37d91cc
...
...
@@ -3,7 +3,6 @@ package p2p
import
(
log
"github.com/ethereum/go-ethereum/log"
peer
"github.com/libp2p/go-libp2p/core/peer"
slices
"golang.org/x/exp/slices"
)
// ConnectionFactor is the factor by which we multiply the connection score.
...
...
@@ -15,6 +14,7 @@ const PeerScoreThreshold = -100
// gater is an internal implementation of the [PeerGater] interface.
type
gater
struct
{
connGater
ConnectionGater
blockedMap
map
[
peer
.
ID
]
bool
log
log
.
Logger
banEnabled
bool
}
...
...
@@ -25,30 +25,43 @@ type gater struct {
type
PeerGater
interface
{
// Update handles a peer score update and blocks/unblocks the peer if necessary.
Update
(
peer
.
ID
,
float64
)
// IsBlocked returns true if the given [peer.ID] is blocked.
IsBlocked
(
peer
.
ID
)
bool
}
// NewPeerGater returns a new peer gater.
func
NewPeerGater
(
connGater
ConnectionGater
,
log
log
.
Logger
,
banEnabled
bool
)
PeerGater
{
return
&
gater
{
connGater
:
connGater
,
blockedMap
:
make
(
map
[
peer
.
ID
]
bool
),
log
:
log
,
banEnabled
:
banEnabled
,
}
}
// IsBlocked returns true if the given [peer.ID] is blocked.
func
(
s
*
gater
)
IsBlocked
(
peerID
peer
.
ID
)
bool
{
return
s
.
blockedMap
[
peerID
]
}
// setBlocked sets the blocked status of the given [peer.ID].
func
(
s
*
gater
)
setBlocked
(
peerID
peer
.
ID
,
blocked
bool
)
{
s
.
blockedMap
[
peerID
]
=
blocked
}
// Update handles a peer score update and blocks/unblocks the peer if necessary.
func
(
s
*
gater
)
Update
(
id
peer
.
ID
,
score
float64
)
{
// Check if the peer score is below the threshold
// If so, we need to block the peer
isAlreadyBlocked
:=
slices
.
Contains
(
s
.
connGater
.
ListBlockedPeers
(),
id
)
if
score
<
PeerScoreThreshold
&&
s
.
banEnabled
{
if
!
isAlreadyBlocked
{
s
.
log
.
Warn
(
"peer blocking enabled, blocking peer"
,
"id"
,
id
.
String
(),
"score"
,
score
)
}
isAlreadyBlocked
:=
s
.
IsBlocked
(
id
)
if
score
<
PeerScoreThreshold
&&
s
.
banEnabled
&&
!
isAlreadyBlocked
{
s
.
log
.
Warn
(
"peer blocking enabled, blocking peer"
,
"id"
,
id
.
String
(),
"score"
,
score
)
err
:=
s
.
connGater
.
BlockPeer
(
id
)
if
err
!=
nil
{
s
.
log
.
Warn
(
"connection gater failed to block peer"
,
"id"
,
id
.
String
(),
"err"
,
err
)
}
// Set the peer as blocked in the blocked map
s
.
setBlocked
(
id
,
true
)
}
// Unblock peers whose score has recovered to an acceptable level
if
(
score
>
PeerScoreThreshold
)
&&
isAlreadyBlocked
{
...
...
@@ -56,5 +69,7 @@ func (s *gater) Update(id peer.ID, score float64) {
if
err
!=
nil
{
s
.
log
.
Warn
(
"connection gater failed to unblock peer"
,
"id"
,
id
.
String
(),
"err"
,
err
)
}
// Set the peer as unblocked in the blocked map
s
.
setBlocked
(
id
,
false
)
}
}
op-node/p2p/peer_gater_test.go
View file @
d37d91cc
...
...
@@ -37,7 +37,7 @@ func (testSuite *PeerGaterTestSuite) TestPeerScoreConstants() {
}
// TestPeerGaterUpdate tests the peer gater update hook.
func
(
testSuite
*
PeerGaterTestSuite
)
TestPeerGater
Update
()
{
func
(
testSuite
*
PeerGaterTestSuite
)
TestPeerGater
_UpdateBansPeers
()
{
gater
:=
p2p
.
NewPeerGater
(
testSuite
.
mockGater
,
testSuite
.
logger
,
...
...
@@ -45,19 +45,32 @@ func (testSuite *PeerGaterTestSuite) TestPeerGaterUpdate() {
)
// Return an empty list of already blocked peers
testSuite
.
mockGater
.
On
(
"ListBlockedPeers"
)
.
Return
([]
peer
.
ID
{})
testSuite
.
mockGater
.
On
(
"ListBlockedPeers"
)
.
Return
([]
peer
.
ID
{})
.
Once
()
// Mock a connection gater peer block call
// Since the peer score is below the [PeerScoreThreshold] of -100,
// the [BlockPeer] method should be called
testSuite
.
mockGater
.
On
(
"BlockPeer"
,
peer
.
ID
(
"peer1"
))
.
Return
(
nil
)
testSuite
.
mockGater
.
On
(
"BlockPeer"
,
peer
.
ID
(
"peer1"
))
.
Return
(
nil
)
.
Once
()
// The peer should initially be unblocked
testSuite
.
False
(
gater
.
IsBlocked
(
peer
.
ID
(
"peer1"
)))
// Apply the peer gater update
gater
.
Update
(
peer
.
ID
(
"peer1"
),
float64
(
-
100
))
gater
.
Update
(
peer
.
ID
(
"peer1"
),
float64
(
-
101
))
// The peer should be considered blocked
testSuite
.
True
(
gater
.
IsBlocked
(
peer
.
ID
(
"peer1"
)))
// Now let's unblock the peer
testSuite
.
mockGater
.
On
(
"UnblockPeer"
,
peer
.
ID
(
"peer1"
))
.
Return
(
nil
)
.
Once
()
gater
.
Update
(
peer
.
ID
(
"peer1"
),
float64
(
0
))
// The peer should be considered unblocked
testSuite
.
False
(
gater
.
IsBlocked
(
peer
.
ID
(
"peer1"
)))
}
// TestPeerGaterUpdateNoBanning tests the peer gater update hook without banning set
func
(
testSuite
*
PeerGaterTestSuite
)
TestPeerGaterUpdateNoBanning
()
{
func
(
testSuite
*
PeerGaterTestSuite
)
TestPeerGater
_
UpdateNoBanning
()
{
gater
:=
p2p
.
NewPeerGater
(
testSuite
.
mockGater
,
testSuite
.
logger
,
...
...
@@ -68,5 +81,15 @@ func (testSuite *PeerGaterTestSuite) TestPeerGaterUpdateNoBanning() {
testSuite
.
mockGater
.
On
(
"ListBlockedPeers"
)
.
Return
([]
peer
.
ID
{})
// Notice: [BlockPeer] should not be called since banning is not enabled
// even though the peer score is way below the [PeerScoreThreshold] of -100
gater
.
Update
(
peer
.
ID
(
"peer1"
),
float64
(
-
100000
))
// The peer should be unblocked
testSuite
.
False
(
gater
.
IsBlocked
(
peer
.
ID
(
"peer1"
)))
// Make sure that if we then "unblock" the peer, nothing happens
gater
.
Update
(
peer
.
ID
(
"peer1"
),
float64
(
0
))
// The peer should still be unblocked
testSuite
.
False
(
gater
.
IsBlocked
(
peer
.
ID
(
"peer1"
)))
}
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