Commit 3267e60e authored by Adrian Sutton's avatar Adrian Sutton

op-node: Add tests for ip and peer ban books

parent 9ad110a9
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 errors.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 errors.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 {
......
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
}
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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment