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
3267e60e
Unverified
Commit
3267e60e
authored
May 26, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-node: Add tests for ip and peer ban books
parent
9ad110a9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
2 deletions
+87
-2
expiry.go
op-node/p2p/gating/expiry.go
+3
-2
ip_ban_book_test.go
op-node/p2p/store/ip_ban_book_test.go
+43
-0
peer_ban_book_test.go
op-node/p2p/store/peer_ban_book_test.go
+41
-0
No files found.
op-node/p2p/gating/expiry.go
View file @
3267e60e
package
gating
package
gating
import
(
import
(
"errors"
"time"
"time"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/network"
...
@@ -47,7 +48,7 @@ func AddBanExpiry(gater BlockingConnectionGater, store ExpiryStore, log log.Logg
...
@@ -47,7 +48,7 @@ func AddBanExpiry(gater BlockingConnectionGater, store ExpiryStore, log log.Logg
func
(
g
*
ExpiryConnectionGater
)
peerBanExpiryCheck
(
p
peer
.
ID
)
(
allow
bool
)
{
func
(
g
*
ExpiryConnectionGater
)
peerBanExpiryCheck
(
p
peer
.
ID
)
(
allow
bool
)
{
// if the peer is blocked, check if it's time to unblock
// if the peer is blocked, check if it's time to unblock
expiry
,
err
:=
g
.
store
.
GetPeerBanExpiration
(
p
)
expiry
,
err
:=
g
.
store
.
GetPeerBanExpiration
(
p
)
if
err
==
store
.
UnknownBanErr
{
if
err
ors
.
Is
(
err
,
store
.
UnknownBanErr
)
{
return
true
// peer is allowed if it has not been banned
return
true
// peer is allowed if it has not been banned
}
}
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -74,7 +75,7 @@ func (g *ExpiryConnectionGater) addrBanExpiryCheck(ma multiaddr.Multiaddr) (allo
...
@@ -74,7 +75,7 @@ func (g *ExpiryConnectionGater) addrBanExpiryCheck(ma multiaddr.Multiaddr) (allo
}
}
// if just the IP is blocked, check if it's time to unblock
// if just the IP is blocked, check if it's time to unblock
expiry
,
err
:=
g
.
store
.
GetIPBanExpiration
(
ip
)
expiry
,
err
:=
g
.
store
.
GetIPBanExpiration
(
ip
)
if
err
==
store
.
UnknownBanErr
{
if
err
ors
.
Is
(
err
,
store
.
UnknownBanErr
)
{
return
true
// IP is allowed if it has not been banned
return
true
// IP is allowed if it has not been banned
}
}
if
err
!=
nil
{
if
err
!=
nil
{
...
...
op-node/p2p/store/ip_ban_book_test.go
0 → 100644
View file @
3267e60e
package
store
import
(
"context"
"net"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum/go-ethereum/log"
ds
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
)
func
TestGetUnknownIPBan
(
t
*
testing
.
T
)
{
book
:=
createMemoryIPBanBook
(
t
)
defer
book
.
Close
()
exp
,
err
:=
book
.
GetIPBanExpiration
(
net
.
IPv4
(
1
,
2
,
3
,
4
))
require
.
Same
(
t
,
UnknownBanErr
,
err
)
require
.
Equal
(
t
,
time
.
Time
{},
exp
)
}
func
TestRoundTripIPBan
(
t
*
testing
.
T
)
{
book
:=
createMemoryIPBanBook
(
t
)
defer
book
.
Close
()
expiry
:=
time
.
Unix
(
2484924
,
0
)
ip
:=
net
.
IPv4
(
1
,
2
,
3
,
4
)
require
.
NoError
(
t
,
book
.
SetIPBanExpiration
(
ip
,
expiry
))
result
,
err
:=
book
.
GetIPBanExpiration
(
ip
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
result
,
expiry
)
}
func
createMemoryIPBanBook
(
t
*
testing
.
T
)
*
ipBanBook
{
store
:=
sync
.
MutexWrap
(
ds
.
NewMapDatastore
())
logger
:=
testlog
.
Logger
(
t
,
log
.
LvlInfo
)
c
:=
clock
.
NewDeterministicClock
(
time
.
UnixMilli
(
100
))
book
,
err
:=
newIPBanBook
(
context
.
Background
(),
logger
,
c
,
store
)
require
.
NoError
(
t
,
err
)
return
book
}
op-node/p2p/store/peer_ban_book_test.go
0 → 100644
View file @
3267e60e
package
store
import
(
"context"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum/go-ethereum/log"
ds
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
)
func
TestGetUnknownPeerBan
(
t
*
testing
.
T
)
{
book
:=
createMemoryPeerBanBook
(
t
)
defer
book
.
Close
()
exp
,
err
:=
book
.
GetPeerBanExpiration
(
"a"
)
require
.
Same
(
t
,
UnknownBanErr
,
err
)
require
.
Equal
(
t
,
time
.
Time
{},
exp
)
}
func
TestRoundTripPeerBan
(
t
*
testing
.
T
)
{
book
:=
createMemoryPeerBanBook
(
t
)
defer
book
.
Close
()
expiry
:=
time
.
Unix
(
2484924
,
0
)
require
.
NoError
(
t
,
book
.
SetPeerBanExpiration
(
"a"
,
expiry
))
result
,
err
:=
book
.
GetPeerBanExpiration
(
"a"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
result
,
expiry
)
}
func
createMemoryPeerBanBook
(
t
*
testing
.
T
)
*
peerBanBook
{
store
:=
sync
.
MutexWrap
(
ds
.
NewMapDatastore
())
logger
:=
testlog
.
Logger
(
t
,
log
.
LvlInfo
)
c
:=
clock
.
NewDeterministicClock
(
time
.
UnixMilli
(
100
))
book
,
err
:=
newPeerBanBook
(
context
.
Background
(),
logger
,
c
,
store
)
require
.
NoError
(
t
,
err
)
return
book
}
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