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) {
// Query the tweet details from Twitter
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 {
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) {
// 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)
req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
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,
// In this case the default chain config(mainnet) will be used,
// namely ethash is the specified consensus engine, return nil.
//nolint:nilnil
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 tr == nil {
//nolint:nilnil
return nil, nil
}
// 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) {
stateObject := s.getStateObject(addr)
if stateObject == nil {
//nolint:nilnil
return nil, nil
}
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)
number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil {
//nolint:nilnil
return nil, nil
}
num = *number
@ -88,6 +89,7 @@ func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumbe
hash = rawdb.ReadFinalizedBlockHash(b.db)
number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil {
//nolint:nilnil
return nil, nil
}
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) {
number := rawdb.ReadHeaderNumber(b.db, hash)
if number == nil {
//nolint:nilnil
return nil, nil
}
return rawdb.ReadHeader(b.db, hash, *number), nil

View file

@ -29,7 +29,6 @@ import (
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/rawdb"
@ -2901,6 +2900,7 @@ func (s *Syncer) onHealState(paths [][]byte, value []byte) error {
if len(paths) == 1 {
var account types.StateAccount
if err := rlp.DecodeBytes(value, &account); err != nil {
//nolint:nilerr
return nil // Returning the error here would drop the remote peer
}
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) {
if _, err := db.Get(key); err != nil {
//nolint:nilerr
return false, 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) {
if _, err := db.Ancient(kind, number); err != nil {
//nolint:nilerr
return false, nil
}
return true, nil

View file

@ -470,6 +470,7 @@ func (t *Transaction) Logs(ctx context.Context) (*[]*Log, error) {
}
// Pending tx
if block == nil {
//nolint:nilnil
return nil, nil
}
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) SetHead(number uint64) {}
func (b *backendMock) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) HeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Header, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) CurrentBlock() *types.Header { return nil }
func (b *backendMock) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) {
//nolint:nilnil
return nil, nil
}
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) {
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) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) {
//nolint:nilnil
return nil, nil
}
func (b *backendMock) GetTd(ctx context.Context, hash common.Hash) *big.Int { return nil }

View file

@ -2,6 +2,7 @@ package librato
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@ -80,7 +81,7 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
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
}

View file

@ -18,6 +18,7 @@ package node
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
@ -167,7 +168,7 @@ func TestWebsocketOrigins(t *testing.T) {
// TestIsWebsocket tests if an incoming websocket upgrade request is handled properly.
func TestIsWebsocket(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
assert.False(t, isWebsocket(r))
r.Header.Set("upgrade", "websocket")
@ -296,7 +297,7 @@ func baseRpcRequest(t *testing.T, url, bodyStr string, extraHeaders ...string) *
// Create the request.
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 {
t.Fatal("could not create http request:", err)
}
@ -530,7 +531,14 @@ func TestGzipHandler(t *testing.T) {
srv := httptest.NewServer(newGzipHandler(test.handler))
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 {
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 {
ip, err := n.internalAddress()
if err != nil {
//nolint:nilerr
return nil // TODO: Shouldn't we return the error?
}
protocol = strings.ToUpper(protocol)

View file

@ -427,8 +427,15 @@ func execP2PNode() {
}
// Send status to the host.
cli := &http.Client{
Timeout: 10 * time.Second,
}
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 {
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
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)
req, err := http.NewRequest(http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
return nil, err
}

View file

@ -19,7 +19,9 @@
package simulations
import (
"context"
"encoding/json"
"github.com/ethereum/go-ethereum/log"
"net/http"
"net/url"
"strconv"
@ -152,7 +154,15 @@ func TestMocker(t *testing.T) {
}
//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 {
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 {
// Here, an error exists but it was expected.
// We do not check the post state or logs.
//nolint:nilerr
return snaps, statedb, nil
}
post := t.json.Post[subtest.Fork][subtest.Index]