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
a5ea097f
Unverified
Commit
a5ea097f
authored
May 26, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-node: Introduce suitable constants to avoid magic numbers in PeerMonitor
parent
60b81b39
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
27 deletions
+26
-27
host.go
op-node/p2p/host.go
+6
-1
peer_monitor.go
op-node/p2p/monitor/peer_monitor.go
+16
-17
peer_monitor_test.go
op-node/p2p/monitor/peer_monitor_test.go
+2
-4
node.go
op-node/p2p/node.go
+2
-5
No files found.
op-node/p2p/host.go
View file @
a5ea097f
...
@@ -32,6 +32,11 @@ import (
...
@@ -32,6 +32,11 @@ import (
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum-optimism/optimism/op-service/clock"
)
)
const
(
staticPeerTag
=
"static"
minAcceptedPeerScore
=
-
100
)
type
ExtraHostFeatures
interface
{
type
ExtraHostFeatures
interface
{
host
.
Host
host
.
Host
ConnectionGater
()
gating
.
BlockingConnectionGater
ConnectionGater
()
gating
.
BlockingConnectionGater
...
@@ -67,7 +72,7 @@ func (e *extraHost) initStaticPeers() {
...
@@ -67,7 +72,7 @@ func (e *extraHost) initStaticPeers() {
e
.
Peerstore
()
.
AddAddrs
(
addr
.
ID
,
addr
.
Addrs
,
time
.
Hour
*
24
*
7
)
e
.
Peerstore
()
.
AddAddrs
(
addr
.
ID
,
addr
.
Addrs
,
time
.
Hour
*
24
*
7
)
// We protect the peer, so the connection manager doesn't decide to prune it.
// We protect the peer, so the connection manager doesn't decide to prune it.
// We tag it with "static" so other protects/unprotects with different tags don't affect this protection.
// We tag it with "static" so other protects/unprotects with different tags don't affect this protection.
e
.
connMgr
.
Protect
(
addr
.
ID
,
"static"
)
e
.
connMgr
.
Protect
(
addr
.
ID
,
staticPeerTag
)
// Try to dial the node in the background
// Try to dial the node in the background
go
func
(
addr
*
peer
.
AddrInfo
)
{
go
func
(
addr
*
peer
.
AddrInfo
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
30
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
30
)
...
...
op-node/p2p/monitor/peer_monitor.go
View file @
a5ea097f
...
@@ -12,8 +12,9 @@ import (
...
@@ -12,8 +12,9 @@ import (
)
)
const
(
const
(
// Time delay between checking the score of each peer to avoid
// Time delay between checking the score of each peer to avoid
activity spikes
checkInterval
=
1
*
time
.
Second
checkInterval
=
1
*
time
.
Second
banDuration
=
1
*
time
.
Hour
)
)
//go:generate mockery --name PeerManager --output mocks/ --with-expecter=true
//go:generate mockery --name PeerManager --output mocks/ --with-expecter=true
...
@@ -35,7 +36,6 @@ type PeerMonitor struct {
...
@@ -35,7 +36,6 @@ type PeerMonitor struct {
clock
clock
.
Clock
clock
clock
.
Clock
manager
PeerManager
manager
PeerManager
minScore
float64
minScore
float64
banDuration
time
.
Duration
bgTasks
sync
.
WaitGroup
bgTasks
sync
.
WaitGroup
...
@@ -44,7 +44,7 @@ type PeerMonitor struct {
...
@@ -44,7 +44,7 @@ type PeerMonitor struct {
nextPeerIdx
int
nextPeerIdx
int
}
}
func
NewPeerMonitor
(
ctx
context
.
Context
,
l
log
.
Logger
,
clock
clock
.
Clock
,
manager
PeerManager
,
minScore
float64
,
banDuration
time
.
Duration
)
*
PeerMonitor
{
func
NewPeerMonitor
(
ctx
context
.
Context
,
l
log
.
Logger
,
clock
clock
.
Clock
,
manager
PeerManager
,
minScore
float64
)
*
PeerMonitor
{
ctx
,
cancelFn
:=
context
.
WithCancel
(
ctx
)
ctx
,
cancelFn
:=
context
.
WithCancel
(
ctx
)
return
&
PeerMonitor
{
return
&
PeerMonitor
{
ctx
:
ctx
,
ctx
:
ctx
,
...
@@ -53,7 +53,6 @@ func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manage
...
@@ -53,7 +53,6 @@ func NewPeerMonitor(ctx context.Context, l log.Logger, clock clock.Clock, manage
clock
:
clock
,
clock
:
clock
,
manager
:
manager
,
manager
:
manager
,
minScore
:
minScore
,
minScore
:
minScore
,
banDuration
:
banDuration
,
}
}
}
}
func
(
k
*
PeerMonitor
)
Start
()
{
func
(
k
*
PeerMonitor
)
Start
()
{
...
@@ -92,7 +91,7 @@ func (k *PeerMonitor) checkNextPeer() error {
...
@@ -92,7 +91,7 @@ func (k *PeerMonitor) checkNextPeer() error {
if
k
.
manager
.
IsStatic
(
id
)
{
if
k
.
manager
.
IsStatic
(
id
)
{
return
nil
return
nil
}
}
if
err
:=
k
.
manager
.
BanPeer
(
id
,
k
.
clock
.
Now
()
.
Add
(
k
.
banDuration
));
err
!=
nil
{
if
err
:=
k
.
manager
.
BanPeer
(
id
,
k
.
clock
.
Now
()
.
Add
(
banDuration
));
err
!=
nil
{
return
fmt
.
Errorf
(
"banning peer %v: %w"
,
id
,
err
)
return
fmt
.
Errorf
(
"banning peer %v: %w"
,
id
,
err
)
}
}
...
...
op-node/p2p/monitor/peer_monitor_test.go
View file @
a5ea097f
...
@@ -15,13 +15,11 @@ import (
...
@@ -15,13 +15,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
)
const
monitorBanDuration
=
10
*
time
.
Minute
func
peerMonitorSetup
(
t
*
testing
.
T
)
(
*
PeerMonitor
,
*
clock2
.
DeterministicClock
,
*
mocks
.
PeerManager
)
{
func
peerMonitorSetup
(
t
*
testing
.
T
)
(
*
PeerMonitor
,
*
clock2
.
DeterministicClock
,
*
mocks
.
PeerManager
)
{
l
:=
testlog
.
Logger
(
t
,
log
.
LvlInfo
)
l
:=
testlog
.
Logger
(
t
,
log
.
LvlInfo
)
clock
:=
clock2
.
NewDeterministicClock
(
time
.
UnixMilli
(
10000
))
clock
:=
clock2
.
NewDeterministicClock
(
time
.
UnixMilli
(
10000
))
manager
:=
mocks
.
NewPeerManager
(
t
)
manager
:=
mocks
.
NewPeerManager
(
t
)
monitor
:=
NewPeerMonitor
(
context
.
Background
(),
l
,
clock
,
manager
,
-
100
,
monitorBanDuration
)
monitor
:=
NewPeerMonitor
(
context
.
Background
(),
l
,
clock
,
manager
,
-
100
)
return
monitor
,
clock
,
manager
return
monitor
,
clock
,
manager
}
}
...
@@ -97,7 +95,7 @@ func TestCheckNextPeer(t *testing.T) {
...
@@ -97,7 +95,7 @@ func TestCheckNextPeer(t *testing.T) {
manager
.
EXPECT
()
.
Peers
()
.
Return
(
peerIDs
)
.
Once
()
manager
.
EXPECT
()
.
Peers
()
.
Return
(
peerIDs
)
.
Once
()
manager
.
EXPECT
()
.
GetPeerScore
(
id
)
.
Return
(
-
101
,
nil
)
.
Once
()
manager
.
EXPECT
()
.
GetPeerScore
(
id
)
.
Return
(
-
101
,
nil
)
.
Once
()
manager
.
EXPECT
()
.
IsProtected
(
id
)
.
Return
(
false
)
.
Once
()
manager
.
EXPECT
()
.
IsProtected
(
id
)
.
Return
(
false
)
.
Once
()
manager
.
EXPECT
()
.
BanPeer
(
id
,
clock
.
Now
()
.
Add
(
monitorB
anDuration
))
.
Return
(
nil
)
.
Once
()
manager
.
EXPECT
()
.
BanPeer
(
id
,
clock
.
Now
()
.
Add
(
b
anDuration
))
.
Return
(
nil
)
.
Once
()
require
.
NoError
(
t
,
monitor
.
checkNextPeer
())
require
.
NoError
(
t
,
monitor
.
checkNextPeer
())
})
})
...
...
op-node/p2p/node.go
View file @
a5ea097f
...
@@ -126,9 +126,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
...
@@ -126,9 +126,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
n
.
scorer
.
OnDisconnect
(
conn
.
RemotePeer
())
n
.
scorer
.
OnDisconnect
(
conn
.
RemotePeer
())
},
},
})
})
// TODO: minScore shouldn't just be hard coded here
n
.
peerMonitor
=
monitor
.
NewPeerMonitor
(
resourcesCtx
,
log
,
clock
.
SystemClock
,
n
,
minAcceptedPeerScore
)
// TODO: Ban duration needs to be set sensibly and probably shouldn't be a magic number here
n
.
peerMonitor
=
monitor
.
NewPeerMonitor
(
resourcesCtx
,
log
,
clock
.
SystemClock
,
n
,
-
100
,
1
*
time
.
Hour
)
// notify of any new connections/streams/etc.
// notify of any new connections/streams/etc.
n
.
host
.
Network
()
.
Notify
(
NewNetworkNotifier
(
log
,
metrics
))
n
.
host
.
Network
()
.
Notify
(
NewNetworkNotifier
(
log
,
metrics
))
// note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled.
// note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled.
...
@@ -214,8 +212,7 @@ func (n *NodeP2P) GetPeerScore(id peer.ID) (float64, error) {
...
@@ -214,8 +212,7 @@ func (n *NodeP2P) GetPeerScore(id peer.ID) (float64, error) {
}
}
func
(
n
*
NodeP2P
)
IsStatic
(
id
peer
.
ID
)
bool
{
func
(
n
*
NodeP2P
)
IsStatic
(
id
peer
.
ID
)
bool
{
// TODO: "static" constant should be shared with host layer rather than hard-coded here
return
n
.
connMgr
!=
nil
&&
n
.
connMgr
.
IsProtected
(
id
,
staticPeerTag
)
return
n
.
connMgr
!=
nil
&&
n
.
connMgr
.
IsProtected
(
id
,
"static"
)
}
}
func
(
n
*
NodeP2P
)
BanPeer
(
id
peer
.
ID
,
expiration
time
.
Time
)
error
{
func
(
n
*
NodeP2P
)
BanPeer
(
id
peer
.
ID
,
expiration
time
.
Time
)
error
{
...
...
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