Commit 6e1d30d4 authored by Matt Joiner's avatar Matt Joiner Committed by GitHub

Apply error variable lint in op-node/p2p (#11354)

parent c14a8b2a
...@@ -61,7 +61,7 @@ func (g *ExpiryConnectionGater) UnblockPeer(p peer.ID) error { ...@@ -61,7 +61,7 @@ func (g *ExpiryConnectionGater) UnblockPeer(p peer.ID) error {
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 errors.Is(err, store.UnknownBanErr) { if errors.Is(err, store.ErrUnknownBan) {
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 {
...@@ -88,7 +88,7 @@ func (g *ExpiryConnectionGater) addrBanExpiryCheck(ma multiaddr.Multiaddr) (allo ...@@ -88,7 +88,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 errors.Is(err, store.UnknownBanErr) { if errors.Is(err, store.ErrUnknownBan) {
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 {
......
...@@ -47,7 +47,7 @@ func TestExpiryConnectionGater_InterceptPeerDial(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestExpiryConnectionGater_InterceptPeerDial(t *testing.T) {
t.Run("unknown expiring ban", func(t *testing.T) { t.Run("unknown expiring ban", func(t *testing.T) {
_, mockExpiryStore, mockGater, gater := expiryTestSetup(t) _, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptPeerDial(mallory).Return(true) mockGater.EXPECT().InterceptPeerDial(mallory).Return(true)
mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.ErrUnknownBan)
allow := gater.InterceptPeerDial(mallory) allow := gater.InterceptPeerDial(mallory)
require.True(t, allow) require.True(t, allow)
}) })
...@@ -68,7 +68,7 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) {
t.Run("expired IP ban", func(t *testing.T) { t.Run("expired IP ban", func(t *testing.T) {
cl, mockExpiryStore, mockGater, gater := expiryTestSetup(t) cl, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true) mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true)
mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.ErrUnknownBan)
mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(cl.Now().Add(-time.Second), nil) mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(cl.Now().Add(-time.Second), nil)
mockExpiryStore.EXPECT().SetIPBanExpiration(ip.To4(), time.Time{}).Return(nil) mockExpiryStore.EXPECT().SetIPBanExpiration(ip.To4(), time.Time{}).Return(nil)
allow := gater.InterceptAddrDial(mallory, addr) allow := gater.InterceptAddrDial(mallory, addr)
...@@ -77,7 +77,7 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) { ...@@ -77,7 +77,7 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) {
t.Run("active IP ban", func(t *testing.T) { t.Run("active IP ban", func(t *testing.T) {
cl, mockExpiryStore, mockGater, gater := expiryTestSetup(t) cl, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true) mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true)
mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.ErrUnknownBan)
mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(cl.Now().Add(time.Second), nil) mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(cl.Now().Add(time.Second), nil)
allow := gater.InterceptAddrDial(mallory, addr) allow := gater.InterceptAddrDial(mallory, addr)
require.False(t, allow) require.False(t, allow)
...@@ -85,8 +85,8 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) { ...@@ -85,8 +85,8 @@ func TestExpiryConnectionGater_InterceptAddrDial(t *testing.T) {
t.Run("unknown IP ban expiry", func(t *testing.T) { t.Run("unknown IP ban expiry", func(t *testing.T) {
_, mockExpiryStore, mockGater, gater := expiryTestSetup(t) _, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true) mockGater.EXPECT().InterceptAddrDial(mallory, addr).Return(true)
mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.ErrUnknownBan)
mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(time.Time{}, store.ErrUnknownBan)
allow := gater.InterceptAddrDial(mallory, addr) allow := gater.InterceptAddrDial(mallory, addr)
require.True(t, allow) require.True(t, allow)
}) })
...@@ -165,7 +165,7 @@ func TestExpiryConnectionGater_InterceptAccept(t *testing.T) { ...@@ -165,7 +165,7 @@ func TestExpiryConnectionGater_InterceptAccept(t *testing.T) {
t.Run("unknown expiry", func(t *testing.T) { t.Run("unknown expiry", func(t *testing.T) {
_, mockExpiryStore, mockGater, gater := expiryTestSetup(t) _, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptAccept(mas).Return(true) mockGater.EXPECT().InterceptAccept(mas).Return(true)
mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetIPBanExpiration(ip.To4()).Return(time.Time{}, store.ErrUnknownBan)
allow := gater.InterceptAccept(mas) allow := gater.InterceptAccept(mas)
require.True(t, allow) require.True(t, allow)
}) })
...@@ -201,7 +201,7 @@ func TestExpiryConnectionGater_InterceptSecured(t *testing.T) { ...@@ -201,7 +201,7 @@ func TestExpiryConnectionGater_InterceptSecured(t *testing.T) {
t.Run("unknown expiry", func(t *testing.T) { t.Run("unknown expiry", func(t *testing.T) {
_, mockExpiryStore, mockGater, gater := expiryTestSetup(t) _, mockExpiryStore, mockGater, gater := expiryTestSetup(t)
mockGater.EXPECT().InterceptSecured(network.DirInbound, mallory, mas).Return(true) mockGater.EXPECT().InterceptSecured(network.DirInbound, mallory, mas).Return(true)
mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.UnknownBanErr) mockExpiryStore.EXPECT().GetPeerBanExpiration(mallory).Return(time.Time{}, store.ErrUnknownBan)
allow := gater.InterceptSecured(network.DirInbound, mallory, mas) allow := gater.InterceptSecured(network.DirInbound, mallory, mas)
require.True(t, allow) require.True(t, allow)
}) })
......
...@@ -102,13 +102,13 @@ type ScoreDiff interface { ...@@ -102,13 +102,13 @@ type ScoreDiff interface {
Apply(score *scoreRecord) Apply(score *scoreRecord)
} }
var UnknownBanErr = errors.New("unknown ban") var ErrUnknownBan = errors.New("unknown ban")
type PeerBanStore interface { type PeerBanStore interface {
// SetPeerBanExpiration create the peer ban with expiration time. // SetPeerBanExpiration create the peer ban with expiration time.
// If expiry == time.Time{} then the ban is deleted. // If expiry == time.Time{} then the ban is deleted.
SetPeerBanExpiration(id peer.ID, expiry time.Time) error SetPeerBanExpiration(id peer.ID, expiry time.Time) error
// GetPeerBanExpiration gets the peer ban expiration time, or UnknownBanErr error if none exists. // GetPeerBanExpiration gets the peer ban expiration time, or ErrUnknownBan error if none exists.
GetPeerBanExpiration(id peer.ID) (time.Time, error) GetPeerBanExpiration(id peer.ID) (time.Time, error)
} }
...@@ -116,7 +116,7 @@ type IPBanStore interface { ...@@ -116,7 +116,7 @@ type IPBanStore interface {
// SetIPBanExpiration create the IP ban with expiration time. // SetIPBanExpiration create the IP ban with expiration time.
// If expiry == time.Time{} then the ban is deleted. // If expiry == time.Time{} then the ban is deleted.
SetIPBanExpiration(ip net.IP, expiry time.Time) error SetIPBanExpiration(ip net.IP, expiry time.Time) error
// GetIPBanExpiration gets the IP ban expiration time, or UnknownBanErr error if none exists. // GetIPBanExpiration gets the IP ban expiration time, or ErrUnknownBan error if none exists.
GetIPBanExpiration(ip net.IP) (time.Time, error) GetIPBanExpiration(ip net.IP) (time.Time, error)
} }
......
...@@ -71,8 +71,8 @@ func (d *ipBanBook) startGC() { ...@@ -71,8 +71,8 @@ func (d *ipBanBook) startGC() {
func (d *ipBanBook) GetIPBanExpiration(ip net.IP) (time.Time, error) { func (d *ipBanBook) GetIPBanExpiration(ip net.IP) (time.Time, error) {
rec, err := d.book.getRecord(ip.To16().String()) rec, err := d.book.getRecord(ip.To16().String())
if err == UnknownRecordErr { if err == ErrUnknownRecord {
return time.Time{}, UnknownBanErr return time.Time{}, ErrUnknownBan
} }
if err != nil { if err != nil {
return time.Time{}, err return time.Time{}, err
......
...@@ -18,7 +18,7 @@ func TestGetUnknownIPBan(t *testing.T) { ...@@ -18,7 +18,7 @@ func TestGetUnknownIPBan(t *testing.T) {
book := createMemoryIPBanBook(t) book := createMemoryIPBanBook(t)
defer book.Close() defer book.Close()
exp, err := book.GetIPBanExpiration(net.IPv4(1, 2, 3, 4)) exp, err := book.GetIPBanExpiration(net.IPv4(1, 2, 3, 4))
require.Same(t, UnknownBanErr, err) require.Same(t, ErrUnknownBan, err)
require.Equal(t, time.Time{}, exp) require.Equal(t, time.Time{}, exp)
} }
......
...@@ -69,7 +69,7 @@ func (m *metadataBook) startGC() { ...@@ -69,7 +69,7 @@ func (m *metadataBook) startGC() {
func (m *metadataBook) GetPeerMetadata(id peer.ID) (PeerMetadata, error) { func (m *metadataBook) GetPeerMetadata(id peer.ID) (PeerMetadata, error) {
record, err := m.book.getRecord(id) record, err := m.book.getRecord(id)
// If the record is not found, return an empty PeerMetadata // If the record is not found, return an empty PeerMetadata
if err == UnknownRecordErr { if err == ErrUnknownRecord {
return PeerMetadata{}, nil return PeerMetadata{}, nil
} }
if err != nil { if err != nil {
......
...@@ -67,8 +67,8 @@ func (d *peerBanBook) startGC() { ...@@ -67,8 +67,8 @@ func (d *peerBanBook) startGC() {
func (d *peerBanBook) GetPeerBanExpiration(id peer.ID) (time.Time, error) { func (d *peerBanBook) GetPeerBanExpiration(id peer.ID) (time.Time, error) {
rec, err := d.book.getRecord(id) rec, err := d.book.getRecord(id)
if err == UnknownRecordErr { if err == ErrUnknownRecord {
return time.Time{}, UnknownBanErr return time.Time{}, ErrUnknownBan
} }
if err != nil { if err != nil {
return time.Time{}, err return time.Time{}, err
......
...@@ -17,7 +17,7 @@ func TestGetUnknownPeerBan(t *testing.T) { ...@@ -17,7 +17,7 @@ func TestGetUnknownPeerBan(t *testing.T) {
book := createMemoryPeerBanBook(t) book := createMemoryPeerBanBook(t)
defer book.Close() defer book.Close()
exp, err := book.GetPeerBanExpiration("a") exp, err := book.GetPeerBanExpiration("a")
require.Same(t, UnknownBanErr, err) require.Same(t, ErrUnknownBan, err)
require.Equal(t, time.Time{}, exp) require.Equal(t, time.Time{}, exp)
} }
......
...@@ -30,7 +30,7 @@ type recordDiff[V record] interface { ...@@ -30,7 +30,7 @@ type recordDiff[V record] interface {
Apply(v V) Apply(v V)
} }
var UnknownRecordErr = errors.New("unknown record") var ErrUnknownRecord = errors.New("unknown record")
// recordsBook is a generic K-V store to embed in the extended-peerstore. // recordsBook is a generic K-V store to embed in the extended-peerstore.
// It prunes old entries to keep the store small. // It prunes old entries to keep the store small.
...@@ -103,13 +103,13 @@ func (d *recordsBook[K, V]) deleteRecord(key K) error { ...@@ -103,13 +103,13 @@ func (d *recordsBook[K, V]) deleteRecord(key K) error {
func (d *recordsBook[K, V]) getRecord(key K) (v V, err error) { func (d *recordsBook[K, V]) getRecord(key K) (v V, err error) {
if val, ok := d.cache.Get(key); ok { if val, ok := d.cache.Get(key); ok {
if d.hasExpired(val) { if d.hasExpired(val) {
return v, UnknownRecordErr return v, ErrUnknownRecord
} }
return val, nil return val, nil
} }
data, err := d.store.Get(d.ctx, d.dsKey(key)) data, err := d.store.Get(d.ctx, d.dsKey(key))
if errors.Is(err, ds.ErrNotFound) { if errors.Is(err, ds.ErrNotFound) {
return v, UnknownRecordErr return v, ErrUnknownRecord
} else if err != nil { } else if err != nil {
return v, fmt.Errorf("failed to load value of key %v: %w", key, err) return v, fmt.Errorf("failed to load value of key %v: %w", key, err)
} }
...@@ -118,7 +118,7 @@ func (d *recordsBook[K, V]) getRecord(key K) (v V, err error) { ...@@ -118,7 +118,7 @@ func (d *recordsBook[K, V]) getRecord(key K) (v V, err error) {
return v, fmt.Errorf("invalid value for key %v: %w", key, err) return v, fmt.Errorf("invalid value for key %v: %w", key, err)
} }
if d.hasExpired(v) { if d.hasExpired(v) {
return v, UnknownRecordErr return v, ErrUnknownRecord
} }
d.cache.Add(key, v) d.cache.Add(key, v)
return v, nil return v, nil
...@@ -128,7 +128,7 @@ func (d *recordsBook[K, V]) SetRecord(key K, diff recordDiff[V]) (V, error) { ...@@ -128,7 +128,7 @@ func (d *recordsBook[K, V]) SetRecord(key K, diff recordDiff[V]) (V, error) {
d.Lock() d.Lock()
defer d.Unlock() defer d.Unlock()
rec, err := d.getRecord(key) rec, err := d.getRecord(key)
if err == UnknownRecordErr { // instantiate new record if it does not exist yet if err == ErrUnknownRecord { // instantiate new record if it does not exist yet
rec = d.newRecord() rec = d.newRecord()
} else if err != nil { } else if err != nil {
return d.newRecord(), err return d.newRecord(), err
......
...@@ -71,7 +71,7 @@ func (d *scoreBook) startGC() { ...@@ -71,7 +71,7 @@ func (d *scoreBook) startGC() {
func (d *scoreBook) GetPeerScores(id peer.ID) (PeerScores, error) { func (d *scoreBook) GetPeerScores(id peer.ID) (PeerScores, error) {
record, err := d.book.getRecord(id) record, err := d.book.getRecord(id)
if err == UnknownRecordErr { if err == ErrUnknownRecord {
return PeerScores{}, nil // return zeroed scores by default return PeerScores{}, nil // return zeroed scores by default
} }
if err != nil { if err != nil {
......
...@@ -833,7 +833,7 @@ func (srv *ReqRespServer) HandleSyncRequest(ctx context.Context, log log.Logger, ...@@ -833,7 +833,7 @@ func (srv *ReqRespServer) HandleSyncRequest(ctx context.Context, log log.Logger,
log.Warn("failed to serve p2p sync request", "req", req, "err", err) log.Warn("failed to serve p2p sync request", "req", req, "err", err)
if errors.Is(err, ethereum.NotFound) { if errors.Is(err, ethereum.NotFound) {
resultCode = ResultCodeNotFoundErr resultCode = ResultCodeNotFoundErr
} else if errors.Is(err, invalidRequestErr) { } else if errors.Is(err, errInvalidRequest) {
resultCode = ResultCodeInvalidErr resultCode = ResultCodeInvalidErr
} else { } else {
resultCode = ResultCodeUnknownErr resultCode = ResultCodeUnknownErr
...@@ -846,7 +846,7 @@ func (srv *ReqRespServer) HandleSyncRequest(ctx context.Context, log log.Logger, ...@@ -846,7 +846,7 @@ func (srv *ReqRespServer) HandleSyncRequest(ctx context.Context, log log.Logger,
srv.metrics.ServerPayloadByNumberEvent(req, resultCode, time.Since(start)) srv.metrics.ServerPayloadByNumberEvent(req, resultCode, time.Since(start))
} }
var invalidRequestErr = errors.New("invalid request") var errInvalidRequest = errors.New("invalid request")
func (srv *ReqRespServer) handleSyncRequest(ctx context.Context, stream network.Stream) (uint64, error) { func (srv *ReqRespServer) handleSyncRequest(ctx context.Context, stream network.Stream) (uint64, error) {
peerId := stream.Conn().RemotePeer() peerId := stream.Conn().RemotePeer()
...@@ -892,14 +892,14 @@ func (srv *ReqRespServer) handleSyncRequest(ctx context.Context, stream network. ...@@ -892,14 +892,14 @@ func (srv *ReqRespServer) handleSyncRequest(ctx context.Context, stream network.
// Check the request is within the expected range of blocks // Check the request is within the expected range of blocks
if req < srv.cfg.Genesis.L2.Number { if req < srv.cfg.Genesis.L2.Number {
return req, fmt.Errorf("cannot serve request for L2 block %d before genesis %d: %w", req, srv.cfg.Genesis.L2.Number, invalidRequestErr) return req, fmt.Errorf("cannot serve request for L2 block %d before genesis %d: %w", req, srv.cfg.Genesis.L2.Number, errInvalidRequest)
} }
max, err := srv.cfg.TargetBlockNumber(uint64(time.Now().Unix())) max, err := srv.cfg.TargetBlockNumber(uint64(time.Now().Unix()))
if err != nil { if err != nil {
return req, fmt.Errorf("cannot determine max target block number to verify request: %w", invalidRequestErr) return req, fmt.Errorf("cannot determine max target block number to verify request: %w", errInvalidRequest)
} }
if req > max { if req > max {
return req, fmt.Errorf("cannot serve request for L2 block %d after max expected block (%v): %w", req, max, invalidRequestErr) return req, fmt.Errorf("cannot serve request for L2 block %d after max expected block (%v): %w", req, max, errInvalidRequest)
} }
envelope, err := srv.l2.PayloadByNumber(ctx, req) envelope, err := srv.l2.PayloadByNumber(ctx, req)
......
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