p2p, log, rpc: use errors.New to replace fmt.Errorf with no parameters (#29074)

This commit is contained in:
Qt 2024-02-26 17:25:35 +08:00 committed by GitHub
parent 32d4d6e616
commit 26724fc2aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package log
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
@ -77,7 +78,7 @@ func benchmarkLogger(b *testing.B, l Logger) {
tt = time.Now() tt = time.Now()
bigint = big.NewInt(100) bigint = big.NewInt(100)
nilbig *big.Int nilbig *big.Int
err = fmt.Errorf("Oh nooes it's crap") err = errors.New("Oh nooes it's crap")
) )
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
@ -106,7 +107,7 @@ func TestLoggerOutput(t *testing.T) {
tt = time.Time{} tt = time.Time{}
bigint = big.NewInt(100) bigint = big.NewInt(100)
nilbig *big.Int nilbig *big.Int
err = fmt.Errorf("Oh nooes it's crap") err = errors.New("Oh nooes it's crap")
smallUint = uint256.NewInt(500_000) smallUint = uint256.NewInt(500_000)
bigUint = &uint256.Int{0xff, 0xff, 0xff, 0xff} bigUint = &uint256.Int{0xff, 0xff, 0xff, 0xff}
) )

View File

@ -914,13 +914,13 @@ func (srv *Server) checkInboundConn(remoteIP net.IP) error {
} }
// Reject connections that do not match NetRestrict. // Reject connections that do not match NetRestrict.
if srv.NetRestrict != nil && !srv.NetRestrict.Contains(remoteIP) { if srv.NetRestrict != nil && !srv.NetRestrict.Contains(remoteIP) {
return fmt.Errorf("not in netrestrict list") return errors.New("not in netrestrict list")
} }
// Reject Internet peers that try too often. // Reject Internet peers that try too often.
now := srv.clock.Now() now := srv.clock.Now()
srv.inboundHistory.expire(now, nil) srv.inboundHistory.expire(now, nil)
if !netutil.IsLAN(remoteIP) && srv.inboundHistory.contains(remoteIP.String()) { if !netutil.IsLAN(remoteIP) && srv.inboundHistory.contains(remoteIP.String()) {
return fmt.Errorf("too many attempts") return errors.New("too many attempts")
} }
srv.inboundHistory.add(remoteIP.String(), now.Add(inboundThrottleTime)) srv.inboundHistory.add(remoteIP.String(), now.Add(inboundThrottleTime))
return nil return nil

View File

@ -19,6 +19,7 @@ package p2p
import ( import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -157,7 +158,7 @@ func readProtocolHandshake(rw MsgReader) (*protoHandshake, error) {
return nil, err return nil, err
} }
if msg.Size > baseProtocolMaxMsgSize { if msg.Size > baseProtocolMaxMsgSize {
return nil, fmt.Errorf("message too big") return nil, errors.New("message too big")
} }
if msg.Code == discMsg { if msg.Code == discMsg {
// Disconnect before protocol handshake is valid according to the // Disconnect before protocol handshake is valid according to the

View File

@ -19,6 +19,7 @@ package rpc
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math" "math"
"strings" "strings"
@ -104,7 +105,7 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
return err return err
} }
if blckNum > math.MaxInt64 { if blckNum > math.MaxInt64 {
return fmt.Errorf("block number larger than int64") return errors.New("block number larger than int64")
} }
*bn = BlockNumber(blckNum) *bn = BlockNumber(blckNum)
return nil return nil
@ -154,7 +155,7 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &e) err := json.Unmarshal(data, &e)
if err == nil { if err == nil {
if e.BlockNumber != nil && e.BlockHash != nil { if e.BlockNumber != nil && e.BlockHash != nil {
return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other") return errors.New("cannot specify both BlockHash and BlockNumber, choose one or the other")
} }
bnh.BlockNumber = e.BlockNumber bnh.BlockNumber = e.BlockNumber
bnh.BlockHash = e.BlockHash bnh.BlockHash = e.BlockHash
@ -202,7 +203,7 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
return err return err
} }
if blckNum > math.MaxInt64 { if blckNum > math.MaxInt64 {
return fmt.Errorf("blocknumber too high") return errors.New("blocknumber too high")
} }
bn := BlockNumber(blckNum) bn := BlockNumber(blckNum)
bnh.BlockNumber = &bn bnh.BlockNumber = &bn