From 60e8c807c7dc25265c7f6b4448d2efb89fa04d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Sun, 15 May 2022 19:45:50 +0200 Subject: [PATCH 1/5] p2p: use errors.Is for error comparison --- p2p/discover/lookup.go | 3 ++- p2p/discover/v4_udp.go | 4 ++-- p2p/discover/v4_udp_test.go | 10 +++++----- p2p/discover/v5_udp.go | 4 ++-- p2p/discover/v5_udp_test.go | 13 +++++++------ p2p/discover/v5wire/encoding.go | 2 +- p2p/dnsdisc/client.go | 5 +++-- p2p/dnsdisc/client_test.go | 2 +- p2p/dnsdisc/tree_test.go | 5 +++-- p2p/enr/enr.go | 8 ++++---- p2p/enr/enr_test.go | 9 +++++---- p2p/enr/entries.go | 5 +++-- p2p/message_test.go | 7 ++++--- p2p/netutil/net_test.go | 3 ++- p2p/peer.go | 2 +- p2p/peer_error.go | 2 +- p2p/peer_test.go | 2 +- p2p/server_test.go | 14 +++++++------- p2p/simulations/http.go | 3 ++- 19 files changed, 56 insertions(+), 47 deletions(-) diff --git a/p2p/discover/lookup.go b/p2p/discover/lookup.go index 9ab4a71ce7b4..b8d97b44e1cc 100644 --- a/p2p/discover/lookup.go +++ b/p2p/discover/lookup.go @@ -18,6 +18,7 @@ package discover import ( "context" + "errors" "time" "github.com/ethereum/go-ethereum/p2p/enode" @@ -141,7 +142,7 @@ func (it *lookup) slowdown() { func (it *lookup) query(n *node, reply chan<- []*node) { fails := it.tab.db.FindFails(n.ID(), n.IP()) r, err := it.queryfunc(n) - if err == errClosed { + if errors.Is(err, errClosed) { // Avoid recording failures on shutdown. reply <- nil return diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index 334716aebed0..a3e1549075b3 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -328,7 +328,7 @@ func (t *UDPv4) findnode(toid enode.ID, toaddr *net.UDPAddr, target v4wire.Pubke // enough nodes the reply matcher will time out waiting for the second reply, but // there's no need for an error in that case. err := <-rm.errc - if err == errTimeout && rm.reply != nil { + if errors.Is(err, errTimeout) && rm.reply != nil { err = nil } return nodes, err @@ -526,7 +526,7 @@ func (t *UDPv4) readLoop(unhandled chan<- ReadPacket) { continue } else if err != nil { // Shut down the loop for permament errors. - if err != io.EOF { + if !errors.Is(err, io.EOF) { t.log.Debug("UDP read error", "err", err) } return diff --git a/p2p/discover/v4_udp_test.go b/p2p/discover/v4_udp_test.go index e36912f010ae..b114ecbb27fc 100644 --- a/p2p/discover/v4_udp_test.go +++ b/p2p/discover/v4_udp_test.go @@ -100,7 +100,7 @@ func (test *udpTest) packetInFrom(wantError error, key *ecdsa.PrivateKey, addr * test.t.Errorf("%s encode error: %v", data.Name(), err) } test.sent = append(test.sent, enc) - if err = test.udp.handlePacket(addr, enc); err != wantError { + if err = test.udp.handlePacket(addr, enc); !errors.Is(err, wantError) { test.t.Errorf("error mismatch: got %q, want %q", err, wantError) } } @@ -111,7 +111,7 @@ func (test *udpTest) waitPacketOut(validate interface{}) (closed bool) { test.t.Helper() dgram, err := test.pipe.receive() - if err == errClosed { + if errors.Is(err, errClosed) { return true } else if err != nil { test.t.Error("packet receive error:", err) @@ -150,7 +150,7 @@ func TestUDPv4_pingTimeout(t *testing.T) { key := newkey() toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222} node := enode.NewV4(&key.PublicKey, toaddr.IP, 0, toaddr.Port) - if _, err := test.udp.ping(node); err != errTimeout { + if _, err := test.udp.ping(node); !errors.Is(err, errTimeout) { t.Error("expected timeout error, got", err) } } @@ -211,7 +211,7 @@ func TestUDPv4_responseTimeouts(t *testing.T) { for i := 0; i < nReqs; i++ { select { case err := <-timeoutErr: - if err != errTimeout { + if !errors.Is(err, errTimeout) { t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err) } nTimeoutsRecv++ @@ -241,7 +241,7 @@ func TestUDPv4_findnodeTimeout(t *testing.T) { toid := enode.ID{1, 2, 3, 4} target := v4wire.Pubkey{4, 5, 6, 7} result, err := test.udp.findnode(toid, toaddr, target) - if err != errTimeout { + if !errors.Is(err, errTimeout) { t.Error("expected timeout error, got", err) } if len(result) > 0 { diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index dc63382fc901..22fab7243501 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -305,7 +305,7 @@ func (t *UDPv5) lookupWorker(destNode *node, target enode.ID) ([]*node, error) { ) var r []*enode.Node r, err = t.findnode(unwrapNode(destNode), dists) - if err == errClosed { + if errors.Is(err, errClosed) { return nil, err } for _, n := range r { @@ -623,7 +623,7 @@ func (t *UDPv5) readLoop() { continue } else if err != nil { // Shut down the loop for permament errors. - if err != io.EOF { + if !errors.Is(err, io.EOF) { t.log.Debug("UDP read error", "err", err) } return diff --git a/p2p/discover/v5_udp_test.go b/p2p/discover/v5_udp_test.go index 30d610a4dd8c..a4d74974ba40 100644 --- a/p2p/discover/v5_udp_test.go +++ b/p2p/discover/v5_udp_test.go @@ -20,6 +20,7 @@ import ( "bytes" "crypto/ecdsa" "encoding/binary" + "errors" "fmt" "math/rand" "net" @@ -246,7 +247,7 @@ func TestUDPv5_pingCall(t *testing.T) { done <- err }() test.waitPacketOut(func(p *v5wire.Ping, addr *net.UDPAddr, _ v5wire.Nonce) {}) - if err := <-done; err != errTimeout { + if err := <-done; !errors.Is(err, errTimeout) { t.Fatalf("want errTimeout, got %q", err) } @@ -271,7 +272,7 @@ func TestUDPv5_pingCall(t *testing.T) { wrongAddr := &net.UDPAddr{IP: net.IP{33, 44, 55, 22}, Port: 10101} test.packetInFrom(test.remotekey, wrongAddr, &v5wire.Pong{ReqID: p.ReqID}) }) - if err := <-done; err != errTimeout { + if err := <-done; !errors.Is(err, errTimeout) { t.Fatalf("want errTimeout for reply from wrong IP, got %q", err) } } @@ -384,7 +385,7 @@ func TestUDPv5_multipleHandshakeRounds(t *testing.T) { test.waitPacketOut(func(p *v5wire.Ping, addr *net.UDPAddr, nonce v5wire.Nonce) { test.packetIn(&v5wire.Whoareyou{Nonce: nonce}) }) - if err := <-done; err != errTimeout { + if err := <-done; !errors.Is(err, errTimeout) { t.Fatalf("unexpected ping error: %q", err) } } @@ -493,7 +494,7 @@ func TestUDPv5_talkRequest(t *testing.T) { done <- err }() test.waitPacketOut(func(p *v5wire.TalkRequest, addr *net.UDPAddr, _ v5wire.Nonce) {}) - if err := <-done; err != errTimeout { + if err := <-done; !errors.Is(err, errTimeout) { t.Fatalf("want errTimeout, got %q", err) } @@ -767,10 +768,10 @@ func (test *udpV5Test) waitPacketOut(validate interface{}) (closed bool) { exptype := fn.Type().In(0) dgram, err := test.pipe.receive() - if err == errClosed { + if errors.Is(err, errClosed) { return true } - if err == errTimeout { + if errors.Is(err, errTimeout) { test.t.Fatalf("timed out waiting for %v", exptype) return false } diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index 7d17281ef969..8dc64de6dfd6 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -596,7 +596,7 @@ func (c *Codec) decodeMessage(fromAddr string, head *Header, headerData, msgData // Try decrypting the message. key := c.sc.readKey(auth.SrcID, fromAddr) msg, err := c.decryptMessage(msgData, head.Nonce[:], headerData, key) - if err == errMessageDecrypt { + if errors.Is(err, errMessageDecrypt) { // It didn't work. Start the handshake since this is an ordinary message packet. return &Unknown{Nonce: head.Nonce}, nil } diff --git a/p2p/dnsdisc/client.go b/p2p/dnsdisc/client.go index 93868b39a8d4..3f914d6e9416 100644 --- a/p2p/dnsdisc/client.go +++ b/p2p/dnsdisc/client.go @@ -19,6 +19,7 @@ package dnsdisc import ( "bytes" "context" + "errors" "fmt" "math/rand" "net" @@ -204,7 +205,7 @@ func (c *Client) doResolveEntry(ctx context.Context, domain, hash string) (entry } for _, txt := range txts { e, err := parseEntry(txt, c.cfg.ValidSchemes) - if err == errUnknownEntry { + if errors.Is(err, errUnknownEntry) { continue } if !bytes.HasPrefix(crypto.Keccak256([]byte(txt)), wantHash) { @@ -281,7 +282,7 @@ func (it *randomIterator) nextNode() *enode.Node { } n, err := ct.syncRandom(it.ctx) if err != nil { - if err == it.ctx.Err() { + if errors.Is(err, it.ctx.Err()) { return nil // context canceled. } it.c.cfg.Logger.Debug("Error in DNS random node sync", "tree", ct.loc.domain, "err", err) diff --git a/p2p/dnsdisc/client_test.go b/p2p/dnsdisc/client_test.go index 0a9a96e62167..b88f6be9e0e8 100644 --- a/p2p/dnsdisc/client_test.go +++ b/p2p/dnsdisc/client_test.go @@ -92,7 +92,7 @@ func TestClientSyncTreeBadNode(t *testing.T) { c := NewClient(Config{Resolver: r, Logger: testlog.Logger(t, log.LvlTrace)}) _, err := c.SyncTree("enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@n") wantErr := nameError{name: "INDMVBZEEQ4ESVYAKGIYU74EAA.n", err: entryError{typ: "enr", err: errInvalidENR}} - if err != wantErr { + if !errors.Is(err, wantErr) { t.Fatalf("expected sync error %q, got %q", wantErr, err) } } diff --git a/p2p/dnsdisc/tree_test.go b/p2p/dnsdisc/tree_test.go index 461b9ec4fd43..579692b888c9 100644 --- a/p2p/dnsdisc/tree_test.go +++ b/p2p/dnsdisc/tree_test.go @@ -17,6 +17,7 @@ package dnsdisc import ( + "errors" "reflect" "testing" @@ -54,7 +55,7 @@ func TestParseRoot(t *testing.T) { if !reflect.DeepEqual(e, test.e) { t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e)) } - if err != test.err { + if !errors.Is(err, test.err) { t.Errorf("test %d: wrong error %q, want %q", i, err, test.err) } } @@ -125,7 +126,7 @@ func TestParseEntry(t *testing.T) { if !reflect.DeepEqual(e, test.e) { t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e)) } - if err != test.err { + if !errors.Is(err, test.err) { t.Errorf("test %d: wrong error %q, want %q", i, err, test.err) } } diff --git a/p2p/enr/enr.go b/p2p/enr/enr.go index 15891813b41a..f9a2f0f10523 100644 --- a/p2p/enr/enr.go +++ b/p2p/enr/enr.go @@ -210,13 +210,13 @@ func decodeRecord(s *rlp.Stream) (dec Record, raw []byte, err error) { return dec, raw, err } if err = s.Decode(&dec.signature); err != nil { - if err == rlp.EOL { + if errors.Is(err, rlp.EOL) { err = errIncompleteList } return dec, raw, err } if err = s.Decode(&dec.seq); err != nil { - if err == rlp.EOL { + if errors.Is(err, rlp.EOL) { err = errIncompleteList } return dec, raw, err @@ -226,13 +226,13 @@ func decodeRecord(s *rlp.Stream) (dec Record, raw []byte, err error) { for i := 0; ; i++ { var kv pair if err := s.Decode(&kv.k); err != nil { - if err == rlp.EOL { + if errors.Is(err, rlp.EOL) { break } return dec, raw, err } if err := s.Decode(&kv.v); err != nil { - if err == rlp.EOL { + if errors.Is(err, rlp.EOL) { return dec, raw, errIncompletePair } return dec, raw, err diff --git a/p2p/enr/enr_test.go b/p2p/enr/enr_test.go index bf3f1047440e..fc3f6cc836d8 100644 --- a/p2p/enr/enr_test.go +++ b/p2p/enr/enr_test.go @@ -19,6 +19,7 @@ package enr import ( "bytes" "encoding/binary" + "errors" "fmt" "math/rand" "testing" @@ -149,7 +150,7 @@ func TestSortedGetAndSet(t *testing.T) { func TestDirty(t *testing.T) { var r Record - if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned { + if _, err := rlp.EncodeToBytes(r); !errors.Is(err, errEncodeUnsigned) { t.Errorf("expected errEncodeUnsigned, got %#v", err) } @@ -164,7 +165,7 @@ func TestDirty(t *testing.T) { if len(r.signature) != 0 { t.Error("signature still set after modification") } - if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned { + if _, err := rlp.EncodeToBytes(r); !errors.Is(err, errEncodeUnsigned) { t.Errorf("expected errEncodeUnsigned, got %#v", err) } } @@ -222,7 +223,7 @@ func TestRecordTooBig(t *testing.T) { // set a big value for random key, expect error r.Set(WithEntry(key, randomString(SizeLimit))) - if err := signTest([]byte{5}, &r); err != errTooBig { + if err := signTest([]byte{5}, &r); !errors.Is(err, errTooBig) { t.Fatalf("expected to get errTooBig, got %#v", err) } @@ -248,7 +249,7 @@ func TestDecodeIncomplete(t *testing.T) { for _, test := range tests { var r Record err := rlp.DecodeBytes(test.input, &r) - if err != test.err { + if !errors.Is(err, test.err) { t.Errorf("wrong error for %X: %v", test.input, err) } } diff --git a/p2p/enr/entries.go b/p2p/enr/entries.go index f2118401afb8..6b571a078f22 100644 --- a/p2p/enr/entries.go +++ b/p2p/enr/entries.go @@ -17,6 +17,7 @@ package enr import ( + "errors" "fmt" "io" "net" @@ -174,7 +175,7 @@ type KeyError struct { // Error implements error. func (err *KeyError) Error() string { - if err.Err == errNotFound { + if errors.Is(err.Err, errNotFound) { return fmt.Sprintf("missing ENR key %q", err.Key) } return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err) @@ -184,5 +185,5 @@ func (err *KeyError) Error() string { // missing from a record. func IsNotFound(err error) bool { kerr, ok := err.(*KeyError) - return ok && kerr.Err == errNotFound + return ok && errors.Is(kerr.Err, errNotFound) } diff --git a/p2p/message_test.go b/p2p/message_test.go index e575c5d96e66..292c23071daf 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -18,6 +18,7 @@ package p2p import ( "bytes" + "errors" "fmt" "io" "runtime" @@ -55,7 +56,7 @@ loop: go func() { if err := SendItems(rw1, 1); err == nil { t.Error("EncodeMsg returned nil error") - } else if err != ErrPipeClosed { + } else if !errors.Is(err, ErrPipeClosed) { t.Errorf("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed) } close(done) @@ -91,7 +92,7 @@ func TestEOFSignal(t *testing.T) { // empty reader eof := make(chan struct{}, 1) sig := &eofSignal{new(bytes.Buffer), 0, eof} - if n, err := sig.Read(rb); n != 0 || err != io.EOF { + if n, err := sig.Read(rb); n != 0 || !errors.Is(err, io.EOF) { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } select { @@ -118,7 +119,7 @@ func TestEOFSignal(t *testing.T) { if n, err := sig.Read(rb); n != 4 || err != nil { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } - if n, err := sig.Read(rb); n != 0 || err != io.EOF { + if n, err := sig.Read(rb); n != 0 || !errors.Is(err, io.EOF) { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } select { diff --git a/p2p/netutil/net_test.go b/p2p/netutil/net_test.go index 3a6aa081f2c2..71f4ad10eac9 100644 --- a/p2p/netutil/net_test.go +++ b/p2p/netutil/net_test.go @@ -17,6 +17,7 @@ package netutil import ( + "errors" "fmt" "net" "reflect" @@ -160,7 +161,7 @@ func TestCheckRelayIP(t *testing.T) { for _, test := range tests { err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr)) - if err != test.want { + if !errors.Is(err, test.want) { t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want) } } diff --git a/p2p/peer.go b/p2p/peer.go index 257027a5b74d..469a1b797416 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -416,7 +416,7 @@ func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error) if err == nil { p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version)) err = errProtocolReturned - } else if err != io.EOF { + } else if !errors.Is(err, io.EOF) { p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err) } p.protoErr <- err diff --git a/p2p/peer_error.go b/p2p/peer_error.go index 3028685041fe..ebc59de251a8 100644 --- a/p2p/peer_error.go +++ b/p2p/peer_error.go @@ -103,7 +103,7 @@ func discReasonForError(err error) DiscReason { if reason, ok := err.(DiscReason); ok { return reason } - if err == errProtocolReturned { + if errors.Is(err, errProtocolReturned) { return DiscQuitting } peerError, ok := err.(*peerError) diff --git a/p2p/peer_test.go b/p2p/peer_test.go index 4308bbd2eb4b..91862f14a1bf 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -138,7 +138,7 @@ func TestPeerProtoReadMsg(t *testing.T) { select { case err := <-errc: - if err != errProtocolReturned { + if !errors.Is(err, errProtocolReturned) { t.Errorf("peer returned error: %v", err) } case <-time.After(2 * time.Second): diff --git a/p2p/server_test.go b/p2p/server_test.go index f6f5700c5efc..fdcf82ba6f53 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -270,7 +270,7 @@ func TestServerAtCap(t *testing.T) { // Try inserting a non-trusted connection. anotherID := randomID() c := newconn(anotherID) - if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != DiscTooManyPeers { + if err := srv.checkpoint(c, srv.checkpointPostHandshake); !errors.Is(err, DiscTooManyPeers) { t.Error("wrong error for insert:", err) } // Try inserting a trusted connection. @@ -285,7 +285,7 @@ func TestServerAtCap(t *testing.T) { // Remove from trusted set and try again srv.RemoveTrustedPeer(newNode(trustedID, "")) c = newconn(trustedID) - if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != DiscTooManyPeers { + if err := srv.checkpoint(c, srv.checkpointPostHandshake); !errors.Is(err, DiscTooManyPeers) { t.Error("wrong error for insert:", err) } @@ -335,7 +335,7 @@ func TestServerPeerLimits(t *testing.T) { dialDest := clientnode conn, _ := net.Pipe() srv.SetupConn(conn, flags, dialDest) - if tp.closeErr != DiscTooManyPeers { + if !errors.Is(tp.closeErr, DiscTooManyPeers) { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -345,11 +345,11 @@ func TestServerPeerLimits(t *testing.T) { // Check that server allows a trusted peer despite being full. conn, _ = net.Pipe() srv.SetupConn(conn, flags, dialDest) - if tp.closeErr == DiscTooManyPeers { + if errors.Is(tp.closeErr, DiscTooManyPeers) { t.Errorf("failed to bypass MaxPeers with trusted node: %q", tp.closeErr) } - if tp.closeErr != DiscUselessPeer { + if !errors.Is(tp.closeErr, DiscUselessPeer) { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -359,7 +359,7 @@ func TestServerPeerLimits(t *testing.T) { // Check that server is full again. conn, _ = net.Pipe() srv.SetupConn(conn, flags, dialDest) - if tp.closeErr != DiscTooManyPeers { + if !errors.Is(tp.closeErr, DiscTooManyPeers) { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -556,7 +556,7 @@ func TestServerInboundThrottle(t *testing.T) { go func() { conn.SetDeadline(time.Now().Add(timeout)) buf := make([]byte, 10) - if n, err := conn.Read(buf); err != io.EOF || n != 0 { + if n, err := conn.Read(buf); !errors.Is(err, io.EOF) || n != 0 { t.Errorf("expected io.EOF and n == 0, got error %q and n == %d", err, n) } connClosed <- struct{}{} diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index 341ff8718b7d..66cdc13109d1 100644 --- a/p2p/simulations/http.go +++ b/p2p/simulations/http.go @@ -21,6 +21,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "html" "io" @@ -559,7 +560,7 @@ func (s *Server) CreateNode(w http.ResponseWriter, req *http.Request) { config := &adapters.NodeConfig{} err := json.NewDecoder(req.Body).Decode(config) - if err != nil && err != io.EOF { + if err != nil && !errors.Is(err, io.EOF) { http.Error(w, err.Error(), http.StatusBadRequest) return } From a7a5b7b21d955374ea39be323dbaf694b920e9e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Tue, 17 May 2022 18:09:24 +0200 Subject: [PATCH 2/5] Don't use errors.Is in tests --- p2p/discover/v4_udp_test.go | 10 +++++----- p2p/discover/v5_udp_test.go | 13 ++++++------- p2p/dnsdisc/client_test.go | 2 +- p2p/dnsdisc/tree_test.go | 5 ++--- p2p/enr/enr_test.go | 9 ++++----- p2p/message_test.go | 7 +++---- p2p/netutil/net_test.go | 3 +-- p2p/peer_test.go | 2 +- p2p/server_test.go | 14 +++++++------- 9 files changed, 30 insertions(+), 35 deletions(-) diff --git a/p2p/discover/v4_udp_test.go b/p2p/discover/v4_udp_test.go index b114ecbb27fc..e36912f010ae 100644 --- a/p2p/discover/v4_udp_test.go +++ b/p2p/discover/v4_udp_test.go @@ -100,7 +100,7 @@ func (test *udpTest) packetInFrom(wantError error, key *ecdsa.PrivateKey, addr * test.t.Errorf("%s encode error: %v", data.Name(), err) } test.sent = append(test.sent, enc) - if err = test.udp.handlePacket(addr, enc); !errors.Is(err, wantError) { + if err = test.udp.handlePacket(addr, enc); err != wantError { test.t.Errorf("error mismatch: got %q, want %q", err, wantError) } } @@ -111,7 +111,7 @@ func (test *udpTest) waitPacketOut(validate interface{}) (closed bool) { test.t.Helper() dgram, err := test.pipe.receive() - if errors.Is(err, errClosed) { + if err == errClosed { return true } else if err != nil { test.t.Error("packet receive error:", err) @@ -150,7 +150,7 @@ func TestUDPv4_pingTimeout(t *testing.T) { key := newkey() toaddr := &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 2222} node := enode.NewV4(&key.PublicKey, toaddr.IP, 0, toaddr.Port) - if _, err := test.udp.ping(node); !errors.Is(err, errTimeout) { + if _, err := test.udp.ping(node); err != errTimeout { t.Error("expected timeout error, got", err) } } @@ -211,7 +211,7 @@ func TestUDPv4_responseTimeouts(t *testing.T) { for i := 0; i < nReqs; i++ { select { case err := <-timeoutErr: - if !errors.Is(err, errTimeout) { + if err != errTimeout { t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err) } nTimeoutsRecv++ @@ -241,7 +241,7 @@ func TestUDPv4_findnodeTimeout(t *testing.T) { toid := enode.ID{1, 2, 3, 4} target := v4wire.Pubkey{4, 5, 6, 7} result, err := test.udp.findnode(toid, toaddr, target) - if !errors.Is(err, errTimeout) { + if err != errTimeout { t.Error("expected timeout error, got", err) } if len(result) > 0 { diff --git a/p2p/discover/v5_udp_test.go b/p2p/discover/v5_udp_test.go index a4d74974ba40..30d610a4dd8c 100644 --- a/p2p/discover/v5_udp_test.go +++ b/p2p/discover/v5_udp_test.go @@ -20,7 +20,6 @@ import ( "bytes" "crypto/ecdsa" "encoding/binary" - "errors" "fmt" "math/rand" "net" @@ -247,7 +246,7 @@ func TestUDPv5_pingCall(t *testing.T) { done <- err }() test.waitPacketOut(func(p *v5wire.Ping, addr *net.UDPAddr, _ v5wire.Nonce) {}) - if err := <-done; !errors.Is(err, errTimeout) { + if err := <-done; err != errTimeout { t.Fatalf("want errTimeout, got %q", err) } @@ -272,7 +271,7 @@ func TestUDPv5_pingCall(t *testing.T) { wrongAddr := &net.UDPAddr{IP: net.IP{33, 44, 55, 22}, Port: 10101} test.packetInFrom(test.remotekey, wrongAddr, &v5wire.Pong{ReqID: p.ReqID}) }) - if err := <-done; !errors.Is(err, errTimeout) { + if err := <-done; err != errTimeout { t.Fatalf("want errTimeout for reply from wrong IP, got %q", err) } } @@ -385,7 +384,7 @@ func TestUDPv5_multipleHandshakeRounds(t *testing.T) { test.waitPacketOut(func(p *v5wire.Ping, addr *net.UDPAddr, nonce v5wire.Nonce) { test.packetIn(&v5wire.Whoareyou{Nonce: nonce}) }) - if err := <-done; !errors.Is(err, errTimeout) { + if err := <-done; err != errTimeout { t.Fatalf("unexpected ping error: %q", err) } } @@ -494,7 +493,7 @@ func TestUDPv5_talkRequest(t *testing.T) { done <- err }() test.waitPacketOut(func(p *v5wire.TalkRequest, addr *net.UDPAddr, _ v5wire.Nonce) {}) - if err := <-done; !errors.Is(err, errTimeout) { + if err := <-done; err != errTimeout { t.Fatalf("want errTimeout, got %q", err) } @@ -768,10 +767,10 @@ func (test *udpV5Test) waitPacketOut(validate interface{}) (closed bool) { exptype := fn.Type().In(0) dgram, err := test.pipe.receive() - if errors.Is(err, errClosed) { + if err == errClosed { return true } - if errors.Is(err, errTimeout) { + if err == errTimeout { test.t.Fatalf("timed out waiting for %v", exptype) return false } diff --git a/p2p/dnsdisc/client_test.go b/p2p/dnsdisc/client_test.go index b88f6be9e0e8..0a9a96e62167 100644 --- a/p2p/dnsdisc/client_test.go +++ b/p2p/dnsdisc/client_test.go @@ -92,7 +92,7 @@ func TestClientSyncTreeBadNode(t *testing.T) { c := NewClient(Config{Resolver: r, Logger: testlog.Logger(t, log.LvlTrace)}) _, err := c.SyncTree("enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@n") wantErr := nameError{name: "INDMVBZEEQ4ESVYAKGIYU74EAA.n", err: entryError{typ: "enr", err: errInvalidENR}} - if !errors.Is(err, wantErr) { + if err != wantErr { t.Fatalf("expected sync error %q, got %q", wantErr, err) } } diff --git a/p2p/dnsdisc/tree_test.go b/p2p/dnsdisc/tree_test.go index 579692b888c9..461b9ec4fd43 100644 --- a/p2p/dnsdisc/tree_test.go +++ b/p2p/dnsdisc/tree_test.go @@ -17,7 +17,6 @@ package dnsdisc import ( - "errors" "reflect" "testing" @@ -55,7 +54,7 @@ func TestParseRoot(t *testing.T) { if !reflect.DeepEqual(e, test.e) { t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e)) } - if !errors.Is(err, test.err) { + if err != test.err { t.Errorf("test %d: wrong error %q, want %q", i, err, test.err) } } @@ -126,7 +125,7 @@ func TestParseEntry(t *testing.T) { if !reflect.DeepEqual(e, test.e) { t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e)) } - if !errors.Is(err, test.err) { + if err != test.err { t.Errorf("test %d: wrong error %q, want %q", i, err, test.err) } } diff --git a/p2p/enr/enr_test.go b/p2p/enr/enr_test.go index fc3f6cc836d8..bf3f1047440e 100644 --- a/p2p/enr/enr_test.go +++ b/p2p/enr/enr_test.go @@ -19,7 +19,6 @@ package enr import ( "bytes" "encoding/binary" - "errors" "fmt" "math/rand" "testing" @@ -150,7 +149,7 @@ func TestSortedGetAndSet(t *testing.T) { func TestDirty(t *testing.T) { var r Record - if _, err := rlp.EncodeToBytes(r); !errors.Is(err, errEncodeUnsigned) { + if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned { t.Errorf("expected errEncodeUnsigned, got %#v", err) } @@ -165,7 +164,7 @@ func TestDirty(t *testing.T) { if len(r.signature) != 0 { t.Error("signature still set after modification") } - if _, err := rlp.EncodeToBytes(r); !errors.Is(err, errEncodeUnsigned) { + if _, err := rlp.EncodeToBytes(r); err != errEncodeUnsigned { t.Errorf("expected errEncodeUnsigned, got %#v", err) } } @@ -223,7 +222,7 @@ func TestRecordTooBig(t *testing.T) { // set a big value for random key, expect error r.Set(WithEntry(key, randomString(SizeLimit))) - if err := signTest([]byte{5}, &r); !errors.Is(err, errTooBig) { + if err := signTest([]byte{5}, &r); err != errTooBig { t.Fatalf("expected to get errTooBig, got %#v", err) } @@ -249,7 +248,7 @@ func TestDecodeIncomplete(t *testing.T) { for _, test := range tests { var r Record err := rlp.DecodeBytes(test.input, &r) - if !errors.Is(err, test.err) { + if err != test.err { t.Errorf("wrong error for %X: %v", test.input, err) } } diff --git a/p2p/message_test.go b/p2p/message_test.go index 292c23071daf..e575c5d96e66 100644 --- a/p2p/message_test.go +++ b/p2p/message_test.go @@ -18,7 +18,6 @@ package p2p import ( "bytes" - "errors" "fmt" "io" "runtime" @@ -56,7 +55,7 @@ loop: go func() { if err := SendItems(rw1, 1); err == nil { t.Error("EncodeMsg returned nil error") - } else if !errors.Is(err, ErrPipeClosed) { + } else if err != ErrPipeClosed { t.Errorf("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed) } close(done) @@ -92,7 +91,7 @@ func TestEOFSignal(t *testing.T) { // empty reader eof := make(chan struct{}, 1) sig := &eofSignal{new(bytes.Buffer), 0, eof} - if n, err := sig.Read(rb); n != 0 || !errors.Is(err, io.EOF) { + if n, err := sig.Read(rb); n != 0 || err != io.EOF { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } select { @@ -119,7 +118,7 @@ func TestEOFSignal(t *testing.T) { if n, err := sig.Read(rb); n != 4 || err != nil { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } - if n, err := sig.Read(rb); n != 0 || !errors.Is(err, io.EOF) { + if n, err := sig.Read(rb); n != 0 || err != io.EOF { t.Errorf("Read returned unexpected values: (%v, %v)", n, err) } select { diff --git a/p2p/netutil/net_test.go b/p2p/netutil/net_test.go index 71f4ad10eac9..3a6aa081f2c2 100644 --- a/p2p/netutil/net_test.go +++ b/p2p/netutil/net_test.go @@ -17,7 +17,6 @@ package netutil import ( - "errors" "fmt" "net" "reflect" @@ -161,7 +160,7 @@ func TestCheckRelayIP(t *testing.T) { for _, test := range tests { err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr)) - if !errors.Is(err, test.want) { + if err != test.want { t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want) } } diff --git a/p2p/peer_test.go b/p2p/peer_test.go index 91862f14a1bf..4308bbd2eb4b 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -138,7 +138,7 @@ func TestPeerProtoReadMsg(t *testing.T) { select { case err := <-errc: - if !errors.Is(err, errProtocolReturned) { + if err != errProtocolReturned { t.Errorf("peer returned error: %v", err) } case <-time.After(2 * time.Second): diff --git a/p2p/server_test.go b/p2p/server_test.go index fdcf82ba6f53..f6f5700c5efc 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -270,7 +270,7 @@ func TestServerAtCap(t *testing.T) { // Try inserting a non-trusted connection. anotherID := randomID() c := newconn(anotherID) - if err := srv.checkpoint(c, srv.checkpointPostHandshake); !errors.Is(err, DiscTooManyPeers) { + if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != DiscTooManyPeers { t.Error("wrong error for insert:", err) } // Try inserting a trusted connection. @@ -285,7 +285,7 @@ func TestServerAtCap(t *testing.T) { // Remove from trusted set and try again srv.RemoveTrustedPeer(newNode(trustedID, "")) c = newconn(trustedID) - if err := srv.checkpoint(c, srv.checkpointPostHandshake); !errors.Is(err, DiscTooManyPeers) { + if err := srv.checkpoint(c, srv.checkpointPostHandshake); err != DiscTooManyPeers { t.Error("wrong error for insert:", err) } @@ -335,7 +335,7 @@ func TestServerPeerLimits(t *testing.T) { dialDest := clientnode conn, _ := net.Pipe() srv.SetupConn(conn, flags, dialDest) - if !errors.Is(tp.closeErr, DiscTooManyPeers) { + if tp.closeErr != DiscTooManyPeers { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -345,11 +345,11 @@ func TestServerPeerLimits(t *testing.T) { // Check that server allows a trusted peer despite being full. conn, _ = net.Pipe() srv.SetupConn(conn, flags, dialDest) - if errors.Is(tp.closeErr, DiscTooManyPeers) { + if tp.closeErr == DiscTooManyPeers { t.Errorf("failed to bypass MaxPeers with trusted node: %q", tp.closeErr) } - if !errors.Is(tp.closeErr, DiscUselessPeer) { + if tp.closeErr != DiscUselessPeer { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -359,7 +359,7 @@ func TestServerPeerLimits(t *testing.T) { // Check that server is full again. conn, _ = net.Pipe() srv.SetupConn(conn, flags, dialDest) - if !errors.Is(tp.closeErr, DiscTooManyPeers) { + if tp.closeErr != DiscTooManyPeers { t.Errorf("unexpected close error: %q", tp.closeErr) } conn.Close() @@ -556,7 +556,7 @@ func TestServerInboundThrottle(t *testing.T) { go func() { conn.SetDeadline(time.Now().Add(timeout)) buf := make([]byte, 10) - if n, err := conn.Read(buf); !errors.Is(err, io.EOF) || n != 0 { + if n, err := conn.Read(buf); err != io.EOF || n != 0 { t.Errorf("expected io.EOF and n == 0, got error %q and n == %d", err, n) } connClosed <- struct{}{} From 7907abbbce8d12b2db995b2b20dd5a1833094d38 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 17 May 2022 22:19:48 +0200 Subject: [PATCH 3/5] p2p/enr: check for rlp.EOL directly This error is a special sentinel value and is guaranteed not to be wrapped in this case. --- p2p/enr/enr.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/p2p/enr/enr.go b/p2p/enr/enr.go index f9a2f0f10523..15891813b41a 100644 --- a/p2p/enr/enr.go +++ b/p2p/enr/enr.go @@ -210,13 +210,13 @@ func decodeRecord(s *rlp.Stream) (dec Record, raw []byte, err error) { return dec, raw, err } if err = s.Decode(&dec.signature); err != nil { - if errors.Is(err, rlp.EOL) { + if err == rlp.EOL { err = errIncompleteList } return dec, raw, err } if err = s.Decode(&dec.seq); err != nil { - if errors.Is(err, rlp.EOL) { + if err == rlp.EOL { err = errIncompleteList } return dec, raw, err @@ -226,13 +226,13 @@ func decodeRecord(s *rlp.Stream) (dec Record, raw []byte, err error) { for i := 0; ; i++ { var kv pair if err := s.Decode(&kv.k); err != nil { - if errors.Is(err, rlp.EOL) { + if err == rlp.EOL { break } return dec, raw, err } if err := s.Decode(&kv.v); err != nil { - if errors.Is(err, rlp.EOL) { + if err == rlp.EOL { return dec, raw, errIncompletePair } return dec, raw, err From e6a020a658d4300804e5fd1cde6ae83d81a9cff4 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 18 May 2022 16:04:33 +0200 Subject: [PATCH 4/5] Update entries.go --- p2p/enr/entries.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/p2p/enr/entries.go b/p2p/enr/entries.go index 6b571a078f22..73bb547a8ed6 100644 --- a/p2p/enr/entries.go +++ b/p2p/enr/entries.go @@ -175,15 +175,21 @@ type KeyError struct { // Error implements error. func (err *KeyError) Error() string { - if errors.Is(err.Err, errNotFound) { + if err.Err == errNotFound { return fmt.Sprintf("missing ENR key %q", err.Key) } return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err) } +func (err *KeyError) Unwrap() error { + return err.Err +} + // IsNotFound reports whether the given error means that a key/value pair is // missing from a record. func IsNotFound(err error) bool { - kerr, ok := err.(*KeyError) - return ok && errors.Is(kerr.Err, errNotFound) + var keyErr *KeyError + if errors.As(err, keyErr) { + return keyErr.Err == errNotFound + } } From 518aa996614a5b59966218ccbcf65a639d8735aa Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 7 Jun 2022 15:47:32 +0200 Subject: [PATCH 5/5] p2p/enr: fix issues in IsNotFound --- p2p/enr/entries.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/p2p/enr/entries.go b/p2p/enr/entries.go index 73bb547a8ed6..a8b0a3839bda 100644 --- a/p2p/enr/entries.go +++ b/p2p/enr/entries.go @@ -182,14 +182,15 @@ func (err *KeyError) Error() string { } func (err *KeyError) Unwrap() error { - return err.Err + return err.Err } // IsNotFound reports whether the given error means that a key/value pair is // missing from a record. func IsNotFound(err error) bool { - var keyErr *KeyError - if errors.As(err, keyErr) { - return keyErr.Err == errNotFound + var ke *KeyError + if errors.As(err, &ke) { + return ke.Err == errNotFound } + return false }