Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter issues fixes #252

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 4byte/4byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fourbyte
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -28,7 +28,7 @@ func get(path string) (string, error) {
}
defer req.Body.Close()

data, err := ioutil.ReadAll(req.Body)
data, err := io.ReadAll(req.Body)
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
build-artifacts:
@echo "--> Build Artifacts"
@sh -c ./scripts/build-artifacts.sh

.PHONY: lint
lint:
golangci-lint run
4 changes: 3 additions & 1 deletion abi/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (

func TestDecode_BytesBound(t *testing.T) {
typ := MustNewType("tuple(string)")
decodeTuple(typ, nil) // it should not panic
require.NotPanics(t, func() {
_, _, _ = decodeTuple(typ, nil) // it should not panic
})
}

func TestDecode_DynamicLengthOutOfBounds(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions abi/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ func encodeHex(b []byte) string {
}

func decodeHex(str string) ([]byte, error) {
if strings.HasPrefix(str, "0x") {
str = str[2:]
}
str = strings.TrimPrefix(str, "0x")
buf, err := hex.DecodeString(str)
if err != nil {
return nil, fmt.Errorf("could not decode hex: %v", err)
Expand Down
9 changes: 3 additions & 6 deletions abi/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"encoding/hex"
"fmt"
"math/big"
"math/rand"
"os"
"reflect"
"strconv"
"testing"
"time"

"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/compiler"
Expand Down Expand Up @@ -317,7 +315,8 @@ func TestEncoding(t *testing.T) {
server := testutil.NewTestServer(t)

for _, c := range cases {
t.Run("", func(t *testing.T) {
c := c
t.Run(fmt.Sprintf("Encoding type %s", c.Type), func(t *testing.T) {
t.Parallel()

tt, err := NewType(c.Type)
Expand Down Expand Up @@ -601,8 +600,6 @@ func generateRandomArgs(n int) *Type {
}

func TestRandomEncoding(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())

nStr := os.Getenv("RANDOM_TESTS")
n, err := strconv.Atoi(nStr)
if err != nil {
Expand Down Expand Up @@ -644,7 +641,7 @@ func testDecodePanic(tt *Type, input interface{}) error {
copy(buf, res1)
buf[i] = 0xff

Decode(tt, buf)
_, _ = Decode(tt, buf)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions abi/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"math/rand"
"reflect"
"strings"
"time"

"github.com/umbracle/ethgo"
)

var randomGen = rand.New(rand.NewSource(time.Now().UnixNano()))

func randomInt(min, max int) int {
return min + rand.Intn(max-min)
return min + randomGen.Intn(max-min)
}

var randomTypes = []string{
Expand Down Expand Up @@ -162,8 +165,6 @@ func generateRandomType(t *Type) interface{} {
}
}

const hexLetters = "0123456789abcdef"

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func randString(n int, dict string) string {
Expand Down
8 changes: 6 additions & 2 deletions abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ func NewTypeFromArgument(arg *ArgumentStr) (*Type, error) {
}

// fill-in the `internalType` field into the type elems
fillIn(typ, arg)
if err = fillIn(typ, arg); err != nil {
return nil, err
}

return typ, nil
}
Expand Down Expand Up @@ -326,7 +328,9 @@ func fillIn(typ *Type, arg *ArgumentStr) error {
}

for indx, i := range arg.Components {
fillIn(typ.tuple[indx].Elem, i)
if err := fillIn(typ.tuple[indx].Elem, i); err != nil {
return err
}
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions blocktracker/blocktracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func testListener(t *testing.T, server *testutil.TestServer, tracker BlockTracke
}
}

server.ProcessBlock()
assert.NoError(t, server.ProcessBlock())
recv()

server.ProcessBlock()
assert.NoError(t, server.ProcessBlock())
recv()
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func TestBlockTracker_Lifecycle(t *testing.T) {
// try to mine a block at least every 1 second
go func() {
for i := 0; i < 10; i++ {
s.ProcessBlock()
assert.NoError(t, s.ProcessBlock())
time.After(1 * time.Second)
}
}()
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestBlockTracker_Events(t *testing.T) {

// build past block history
for _, b := range c.History.ToBlocks() {
tt.AddBlockLocked(b)
assert.NoError(t, tt.AddBlockLocked(b))
}

sub := tt.Subscribe()
Expand Down
7 changes: 3 additions & 4 deletions cmd/abigen/abigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -117,7 +116,7 @@ func processAbi(sources []string, config *config) (map[string]*compiler.Artifact
artifacts := map[string]*compiler.Artifact{}

for _, abiPath := range sources {
content, err := ioutil.ReadFile(abiPath)
content, err := os.ReadFile(abiPath)
if err != nil {
return nil, fmt.Errorf("failed to read abi file (%s): %v", abiPath, err)
}
Expand All @@ -128,7 +127,7 @@ func processAbi(sources []string, config *config) (map[string]*compiler.Artifact
name = strings.TrimSuffix(name, filepath.Ext(name))
binPath := filepath.Join(path, name+".bin")

bin, err := ioutil.ReadFile(binPath)
bin, err := os.ReadFile(binPath)
if err != nil {
// bin not found
bin = []byte{}
Expand All @@ -150,7 +149,7 @@ func processJson(sources []string) (map[string]*compiler.Artifact, error) {
artifacts := map[string]*compiler.Artifact{}

for _, jsonPath := range sources {
content, err := ioutil.ReadFile(jsonPath)
content, err := os.ReadFile(jsonPath)
if err != nil {
return nil, fmt.Errorf("failed to read abi file (%s): %v", jsonPath, err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/abigen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package abigen
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -158,15 +158,15 @@ func gen(artifacts map[string]*compiler.Artifact, config *config, hash string) e
if err := tmplAbi.Execute(&b, input); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(config.Output, filename+".go"), []byte(b.Bytes()), 0644); err != nil {
if err := os.WriteFile(filepath.Join(config.Output, filename+".go"), []byte(b.Bytes()), 0644); err != nil {
return err
}

b.Reset()
if err := tmplBin.Execute(&b, input); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(config.Output, filename+"_artifacts.go"), []byte(b.Bytes()), 0644); err != nil {
if err := os.WriteFile(filepath.Join(config.Output, filename+"_artifacts.go"), []byte(b.Bytes()), 0644); err != nil {
return err
}
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -83,7 +82,7 @@ func (s *Solidity) compileImpl(code string, files ...string) (*Output, error) {
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to compile: %s", string(stderr.Bytes()))
return nil, fmt.Errorf("failed to compile: %s", stderr.String())
}

var output *Output
Expand Down Expand Up @@ -127,7 +126,7 @@ func DownloadSolidity(version string, dst string, renameDst bool) error {
}

// tmp folder to download the binary
tmpDir, err := ioutil.TempDir("/tmp", "solc-")
tmpDir, err := os.MkdirTemp("/tmp", "solc-")
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/solidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compiler

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -119,7 +118,7 @@ func existsSolidity(t *testing.T, path string) bool {
cmd.Stderr = &stderr
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
t.Fatalf("solidity version failed: %s", string(stderr.Bytes()))
t.Fatalf("solidity version failed: %s", stderr.String())
}
if len(stdout.Bytes()) == 0 {
t.Fatal("empty output")
Expand All @@ -128,7 +127,7 @@ func existsSolidity(t *testing.T, path string) bool {
}

func TestDownloadSolidityCompiler(t *testing.T) {
dst1, err := ioutil.TempDir("/tmp", "ethgo-")
dst1, err := os.MkdirTemp("/tmp", "ethgo-")
if err != nil {
t.Fatal(err)
}
Expand All @@ -144,7 +143,7 @@ func TestDownloadSolidityCompiler(t *testing.T) {
t.Fatal("it should exist")
}

dst2, err := ioutil.TempDir("/tmp", "ethgo-")
dst2, err := os.MkdirTemp("/tmp", "ethgo-")
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 10 additions & 5 deletions contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func TestContract_Deploy(t *testing.T) {

// create an address and fund it
key, _ := wallet.GenerateKey()
s.Fund(key.Address())
_, err := s.Fund(key.Address())
require.NoError(t, err)

p, _ := jsonrpc.NewClient(s.HTTPAddr())

Expand Down Expand Up @@ -137,7 +138,8 @@ func TestContract_Transaction(t *testing.T) {

// create an address and fund it
key, _ := wallet.GenerateKey()
s.Fund(key.Address())
_, err := s.Fund(key.Address())
require.NoError(t, err)

cc := &testutil.Contract{}
cc.AddEvent(testutil.NewEvent("A").Add("uint256", true))
Expand Down Expand Up @@ -170,7 +172,8 @@ func TestContract_CallAtBlock(t *testing.T) {

// create an address and fund it
key, _ := wallet.GenerateKey()
s.Fund(key.Address())
_, err := s.Fund(key.Address())
require.NoError(t, err)

cc := &testutil.Contract{}
cc.AddCallback(func() string {
Expand Down Expand Up @@ -228,7 +231,8 @@ func TestContract_SendValueContractCall(t *testing.T) {
s := testutil.NewTestServer(t)

key, _ := wallet.GenerateKey()
s.Fund(key.Address())
_, err := s.Fund(key.Address())
assert.NoError(t, err)

cc := &testutil.Contract{}
cc.AddCallback(func() string {
Expand Down Expand Up @@ -267,7 +271,8 @@ func TestContract_EIP1559(t *testing.T) {
s := testutil.NewTestServer(t)

key, _ := wallet.GenerateKey()
s.Fund(key.Address())
_, err := s.Fund(key.Address())
assert.NoError(t, err)

cc := &testutil.Contract{}
cc.AddOutputCaller("example")
Expand Down
3 changes: 2 additions & 1 deletion e2e/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func TestSendSignedTransaction(t *testing.T) {

// add value to the new key
value := big.NewInt(1000000000000000000)
s.Transfer(key.Address(), value)
_, err = s.Transfer(key.Address(), value)
assert.NoError(t, err)

c, _ := jsonrpc.NewClient(s.HTTPAddr())

Expand Down
6 changes: 3 additions & 3 deletions jsonrpc/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func TestSubscribeNewHead(t *testing.T) {
}
}

s.ProcessBlock()
assert.NoError(t, s.ProcessBlock())
recv(true)

s.ProcessBlock()
assert.NoError(t, s.ProcessBlock())
recv(true)

assert.NoError(t, cancel())

s.ProcessBlock()
assert.NoError(t, s.ProcessBlock())
recv(false)

// subscription already closed
Expand Down
1 change: 1 addition & 0 deletions jsonrpc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (a *ArgBig) Big() *big.Int {
return &b
}

//nolint:unused
func encodeUintToHex(i uint64) string {
return fmt.Sprintf("0x%x", i)
}
Expand Down
2 changes: 1 addition & 1 deletion keystore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func getRand(size int) []byte {
buf := make([]byte, size)
rand.Read(buf)
rand.Read(buf) //nolint:errcheck
return buf
}

Expand Down
2 changes: 1 addition & 1 deletion keystore/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func normalizePassword(password string) string {
if i == 0x7F {
return true
}
if 0x00 <= i && i <= 0x1F {
if i <= 0x1F {
return true
}
if 0x80 <= i && i <= 0x9F {
Expand Down
Loading