dev: fix: http related and nilnil lint errors

This commit is contained in:
marcello33 2023-06-14 16:58:04 +02:00
parent 631efbe675
commit 4307e68288
16 changed files with 60 additions and 11 deletions

View file

@ -754,7 +754,7 @@ func authTwitter(url string, tokenV1, tokenV2 string) (string, string, string, c
func authTwitterWithTokenV1(tweetID string, token string) (string, string, string, common.Address, error) { func authTwitterWithTokenV1(tweetID string, token string) (string, string, string, common.Address, error) {
// Query the tweet details from Twitter // Query the tweet details from Twitter
url := fmt.Sprintf("https://api.twitter.com/1.1/statuses/show.json?id=%s", tweetID) url := fmt.Sprintf("https://api.twitter.com/1.1/statuses/show.json?id=%s", tweetID)
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil { if err != nil {
return "", "", "", common.Address{}, err return "", "", "", common.Address{}, err
} }
@ -791,7 +791,7 @@ func authTwitterWithTokenV1(tweetID string, token string) (string, string, strin
func authTwitterWithTokenV2(tweetID string, token string) (string, string, string, common.Address, error) { func authTwitterWithTokenV2(tweetID string, token string) (string, string, string, common.Address, error) {
// Query the tweet details from Twitter // Query the tweet details from Twitter
url := fmt.Sprintf("https://api.twitter.com/2/tweets/%s?expansions=author_id&user.fields=profile_image_url", tweetID) url := fmt.Sprintf("https://api.twitter.com/2/tweets/%s?expansions=author_id&user.fields=profile_image_url", tweetID)
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil { if err != nil {
return "", "", "", common.Address{}, err return "", "", "", common.Address{}, err
} }

View file

@ -417,6 +417,7 @@ func LoadCliqueConfig(db ethdb.Database, genesis *Genesis) (*params.CliqueConfig
// There is no stored chain config and no new config provided, // There is no stored chain config and no new config provided,
// In this case the default chain config(mainnet) will be used, // In this case the default chain config(mainnet) will be used,
// namely ethash is the specified consensus engine, return nil. // namely ethash is the specified consensus engine, return nil.
//nolint:nilnil
return nil, nil return nil, nil
} }

View file

@ -357,6 +357,7 @@ func (s *stateObject) commitTrie(db Database) (*trie.NodeSet, error) {
} }
// If nothing changed, don't bother with committing anything // If nothing changed, don't bother with committing anything
if tr == nil { if tr == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
// Track the amount of time wasted on committing the storage trie // Track the amount of time wasted on committing the storage trie

View file

@ -692,6 +692,7 @@ func (s *StateDB) Database() Database {
func (s *StateDB) StorageTrie(addr common.Address) (Trie, error) { func (s *StateDB) StorageTrie(addr common.Address) (Trie, error) {
stateObject := s.getStateObject(addr) stateObject := s.getStateObject(addr)
if stateObject == nil { if stateObject == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
cpy := stateObject.deepCopy(s) cpy := stateObject.deepCopy(s)

View file

@ -81,6 +81,7 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
hash = rawdb.ReadHeadBlockHash(b.db) hash = rawdb.ReadHeadBlockHash(b.db)
number := rawdb.ReadHeaderNumber(b.db, hash) number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil { if number == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
num = *number num = *number
@ -88,6 +89,7 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
hash = rawdb.ReadFinalizedBlockHash(b.db) hash = rawdb.ReadFinalizedBlockHash(b.db)
number := rawdb.ReadHeaderNumber(b.db, hash) number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil { if number == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
num = *number num = *number
@ -103,6 +105,7 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
func (b *testBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { func (b *testBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
number := rawdb.ReadHeaderNumber(b.db, hash) number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil { if number == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
return rawdb.ReadHeader(b.db, hash, *number), nil return rawdb.ReadHeader(b.db, hash, *number), nil

View file

@ -29,7 +29,6 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
@ -2901,6 +2900,7 @@ func (s *Syncer) onHealState(paths [][]byte, value []byte) error {
if len(paths) == 1 { if len(paths) == 1 {
var account types.StateAccount var account types.StateAccount
if err := rlp.DecodeBytes(value, &account); err != nil { if err := rlp.DecodeBytes(value, &account); err != nil {
//nolint:nilerr
return nil // Returning the error here would drop the remote peer return nil // Returning the error here would drop the remote peer
} }
blob := snapshot.SlimAccountRLP(account.Nonce, account.Balance, account.Root, account.CodeHash) blob := snapshot.SlimAccountRLP(account.Nonce, account.Balance, account.Root, account.CodeHash)

View file

@ -34,6 +34,7 @@ type Database struct {
func (db *Database) Has(key []byte) (bool, error) { func (db *Database) Has(key []byte) (bool, error) {
if _, err := db.Get(key); err != nil { if _, err := db.Get(key); err != nil {
//nolint:nilerr
return false, nil return false, nil
} }
return true, nil return true, nil
@ -50,6 +51,7 @@ func (db *Database) Get(key []byte) ([]byte, error) {
func (db *Database) HasAncient(kind string, number uint64) (bool, error) { func (db *Database) HasAncient(kind string, number uint64) (bool, error) {
if _, err := db.Ancient(kind, number); err != nil { if _, err := db.Ancient(kind, number); err != nil {
//nolint:nilerr
return false, nil return false, nil
} }
return true, nil return true, nil

View file

@ -470,6 +470,7 @@ func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) {
} }
// Pending tx // Pending tx
if block == nil { if block == nil {
//nolint:nilnil
return nil, nil return nil, nil
} }
h, err := block.Hash(ctx) h, err := block.Hash(ctx)

View file

@ -272,25 +272,32 @@ func (b *backendMock) RPCTxFeeCap() float64 { return 0 }
func (b *backendMock) UnprotectedAllowed() bool { return false } func (b *backendMock) UnprotectedAllowed() bool { return false }
func (b *backendMock) SetHead(number uint64) {} func (b *backendMock) SetHead(number uint64) {}
func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) { func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) CurrentBlock() *types.Header { return nil } func (b *backendMock) CurrentBlock() *types.Header { return nil }
func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) { func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
@ -299,11 +306,16 @@ func (b *backendMock) StateAndHeaderByNumber(ctx context.Context, number rpc.Blo
func (b *backendMock) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { func (b *backendMock) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) {
return nil, nil, nil return nil, nil, nil
} }
func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) { return nil, nil } func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) { func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) {
//nolint:nilnil
return nil, nil return nil, nil
} }
func (b *backendMock) GetTd(ctx context.Context, hash common.Hash) *big.Int { return nil } func (b *backendMock) GetTd(ctx context.Context, hash common.Hash) *big.Int { return nil }

View file

@ -2,6 +2,7 @@ package librato
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -80,7 +81,7 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
return return
} }
if req, err = http.NewRequest(http.MethodPost, MetricsPostUrl, bytes.NewBuffer(js)); err != nil { if req, err = http.NewRequestWithContext(context.Background(), http.MethodPost, MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
return return
} }

View file

@ -18,6 +18,7 @@ package node
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -167,7 +168,7 @@ func TestWebsocketOrigins(t *testing.T) {
// TestIsWebsocket tests if an incoming websocket upgrade request is handled properly. // TestIsWebsocket tests if an incoming websocket upgrade request is handled properly.
func TestIsWebsocket(t *testing.T) { func TestIsWebsocket(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil) r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
assert.False(t, isWebsocket(r)) assert.False(t, isWebsocket(r))
r.Header.Set("upgrade", "websocket") r.Header.Set("upgrade", "websocket")
@ -296,7 +297,7 @@ func baseRpcRequest(t *testing.T, url, bodyStr string, extraHeaders ...string) *
// Create the request. // Create the request.
body := bytes.NewReader([]byte(bodyStr)) body := bytes.NewReader([]byte(bodyStr))
req, err := http.NewRequest(http.MethodPost, url, body) req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, body)
if err != nil { if err != nil {
t.Fatal("could not create http request:", err) t.Fatal("could not create http request:", err)
} }
@ -530,7 +531,14 @@ func TestGzipHandler(t *testing.T) {
srv := httptest.NewServer(newGzipHandler(test.handler)) srv := httptest.NewServer(newGzipHandler(test.handler))
defer srv.Close() defer srv.Close()
resp, err := http.Get(srv.URL) cli := &http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
if err != nil {
log.Crit("Can't build request", "url", srv.URL, "err", err)
}
resp, err := cli.Do(req)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -79,6 +79,7 @@ func (n *upnp) ExternalIP() (addr net.IP, err error) {
func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, lifetime time.Duration) error { func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, lifetime time.Duration) error {
ip, err := n.internalAddress() ip, err := n.internalAddress()
if err != nil { if err != nil {
//nolint:nilerr
return nil // TODO: Shouldn't we return the error? return nil // TODO: Shouldn't we return the error?
} }
protocol = strings.ToUpper(protocol) protocol = strings.ToUpper(protocol)

View file

@ -427,8 +427,15 @@ func execP2PNode() {
} }
// Send status to the host. // Send status to the host.
cli := &http.Client{
Timeout: 10 * time.Second,
}
statusJSON, _ := json.Marshal(status) statusJSON, _ := json.Marshal(status)
resp, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)) req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, statusURL, bytes.NewReader(statusJSON))
if err != nil {
log.Crit("Can't build request", "url", statusURL, "err", err)
}
resp, err := cli.Do(req)
if err != nil { if err != nil {
log.Crit("Can't post startup info", "url", statusURL, "err", err) log.Crit("Can't post startup info", "url", statusURL, "err", err)
} }

View file

@ -102,7 +102,7 @@ type SubscribeOpts struct {
// nodes and connections and filtering message events // nodes and connections and filtering message events
func (c *Client) SubscribeNetwork(events chan *Event, opts SubscribeOpts) (event.Subscription, error) { func (c *Client) SubscribeNetwork(events chan *Event, opts SubscribeOpts) (event.Subscription, error) {
url := fmt.Sprintf("%s/events?current=%t&filter=%s", c.URL, opts.Current, opts.Filter) url := fmt.Sprintf("%s/events?current=%t&filter=%s", c.URL, opts.Current, opts.Filter)
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -19,7 +19,9 @@
package simulations package simulations
import ( import (
"context"
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -152,7 +154,15 @@ func TestMocker(t *testing.T) {
} }
//reset the network //reset the network
resp, err = http.Post(s.URL+"/reset", "", nil) cli := &http.Client{
Timeout: 10 * time.Second,
}
resetUrl := s.URL + "/reset"
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, resetUrl, nil)
if err != nil {
log.Crit("Can't build request", "url", resetUrl, "err", err)
}
resp, err = cli.Do(req)
if err != nil { if err != nil {
t.Fatalf("Could not reset network: %s", err) t.Fatalf("Could not reset network: %s", err)
} }

View file

@ -193,6 +193,7 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo
if err != nil { if err != nil {
// Here, an error exists but it was expected. // Here, an error exists but it was expected.
// We do not check the post state or logs. // We do not check the post state or logs.
//nolint:nilerr
return snaps, statedb, nil return snaps, statedb, nil
} }
post := t.json.Post[subtest.Fork][subtest.Index] post := t.json.Post[subtest.Fork][subtest.Index]