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

tweak golangci-lint conf #663

Merged
merged 2 commits into from
Feb 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
version: v1.54
# use the default if on main branch, otherwise use the pull request config
args: --timeout=30m --config=${{ github.event_name == 'pull_request' && '.golangci.pull-request.yml' || '.golangci.yml' }}
args: --timeout=30m --config=.golangci.yml
only-new-issues: true
skip-cache: true
skip-pkg-cache: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-docker-build.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Docker
name: Test Docker Buld

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Unit Tests

on: [push, pull_request]

Expand Down
20 changes: 0 additions & 20 deletions .golangci.pull-request.yml

This file was deleted.

41 changes: 32 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
# Please refer to the official golangci-lint config documentation for more details:
# https://golangci-lint.run/usage/configuration/
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
run:
timeout: 10m
tests: true
# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true

linters:
disable:
disable-all: true
enable:
- goimports
- gosimple
- govet
- ineffassign
- misspell
- unconvert
- typecheck
- unused
- errcheck
- staticcheck
- bidichk
- durationcheck
- exportloopref
- whitespace

run:
timeout: 10m
tests: false
# - structcheck # lots of false positives
# - errcheck #lot of false positives
# - contextcheck
# - errchkjson # lots of false positives
# - errorlint # this check crashes
# - exhaustive # silly check
# - makezero # false positives
# - nilerr # several intentional

linters-settings:
gofmt:
simplify: true

issues:
max-issues-per-linter: 1000
exclude-rules:
- path: vm/contracts.go
text: 'SA1019: "golang.org/x/crypto/ripemd160" is deprecated: RIPEMD-160 is a legacy hash and should not be used for new applications.'
- path: vm/bn256/cloudflare/optate.go
linters:
- deadcode
- staticcheck
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,5 @@ test-coverage:| go_version_check #@ Run the tests with coverage
lint_command_check:
@command -v golangci-lint || (echo "golangci-lint not found, please install it from https://golangci-lint.run/usage/install/" && exit 1)

lint: | go_version_check lint_command_check #@ Run 'golangci-lint' on new code changes
@golangci-lint run --new --config .golangci.pull-request.yml

lint-all: | go_version_check lint_command_check #@ Run 'golangci-lint' on the entire codebase
lint: | go_version_check lint_command_check #@ Run 'golangci-lint'
@golangci-lint run --config .golangci.yml
1 change: 0 additions & 1 deletion abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,5 @@ func TestABI(t *testing.T) {
assert.Nil(t, err)

assert.Equal(t, value, d)

}
}
1 change: 0 additions & 1 deletion api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,4 @@ func (a *Accounts) Mount(root *mux.Router, pathPrefix string) {
sub.Path("/{address}/storage/{key}").Methods("GET").HandlerFunc(utils.WrapHandlerFunc(a.handleGetStorage))
sub.Path("").Methods("POST").HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
sub.Path("/{address}").Methods("POST").HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))

}
74 changes: 36 additions & 38 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package accounts_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"math/big"
"net/http"
"net/http/httptest"
Expand All @@ -32,16 +32,16 @@ import (
"github.com/vechain/thor/v2/tx"
)

var sol = ` pragma solidity ^0.4.18;
contract Test {
uint8 value;
function add(uint8 a,uint8 b) public pure returns(uint8) {
return a+b;
}
function set(uint8 v) public {
value = v;
}
}`
// pragma solidity ^0.4.18;
// contract Test {
// uint8 value;
// function add(uint8 a,uint8 b) public pure returns(uint8) {
// return a+b;
// }
// function set(uint8 v) public {
// value = v;
// }
// }

var abiJSON = `[
{
Expand Down Expand Up @@ -111,32 +111,31 @@ func TestAccount(t *testing.T) {
}

func getAccount(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr)
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String())
res, statusCode := httpGet(t, ts.URL+"/accounts/"+addr.String())
var acc accounts.Account
if err := json.Unmarshal(res, &acc); err != nil {
t.Fatal(err)
}
assert.Equal(t, math.HexOrDecimal256(*value), acc.Balance, "balance should be equal")
assert.Equal(t, http.StatusOK, statusCode, "OK")

}

func getCode(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/code")
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/code")
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code")
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code")
var code map[string]string
if err := json.Unmarshal(res, &code); err != nil {
t.Fatal(err)
Expand All @@ -150,17 +149,17 @@ func getCode(t *testing.T) {
}

func getStorage(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/storage/"+storageKey.String())
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/storage/"+storageKey.String())
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+invalidBytes32)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+invalidBytes32)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad storage key")

res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+invalidNumberRevision)
_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+invalidNumberRevision)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String())
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String())
var value map[string]string
if err := json.Unmarshal(res, &value); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -190,9 +189,9 @@ func initAccountServer(t *testing.T) {
packTx(repo, stater, transaction, t)

method := "set"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(uint8(storageValue))
input, err := m.EncodeInput(storageValue)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -252,41 +251,40 @@ func deployContractWithCall(t *testing.T) {
Gas: 10000000,
Data: "abc",
}
res, statusCode := httpPost(t, ts.URL+"/accounts", badBody)
_, statusCode := httpPost(t, ts.URL+"/accounts", badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad data")

reqBody := &accounts.CallData{
Gas: 10000000,
Data: hexutil.Encode(bytecode),
}

res, statusCode = httpPost(t, ts.URL+"/accounts?revision="+invalidNumberRevision, reqBody)
_, statusCode = httpPost(t, ts.URL+"/accounts?revision="+invalidNumberRevision, reqBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode = httpPost(t, ts.URL+"/accounts", reqBody)
res, _ := httpPost(t, ts.URL+"/accounts", reqBody)
var output *accounts.CallResult
if err := json.Unmarshal(res, &output); err != nil {
t.Fatal(err)
}
assert.False(t, output.Reverted)

}

func callContract(t *testing.T) {
res, statusCode := httpPost(t, ts.URL+"/accounts/"+invalidAddr, nil)
_, statusCode := httpPost(t, ts.URL+"/accounts/"+invalidAddr, nil)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid address")

badBody := &accounts.CallData{
Data: "input",
}
res, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), badBody)
_, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid input data")

a := uint8(1)
b := uint8(2)
method := "add"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(a, b)
if err != nil {
Expand All @@ -295,7 +293,7 @@ func callContract(t *testing.T) {
reqBody := &accounts.CallData{
Data: hexutil.Encode(input),
}
res, statusCode = httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), reqBody)
res, statusCode := httpPost(t, ts.URL+"/accounts/"+contractAddr.String(), reqBody)
var output *accounts.CallResult
if err = json.Unmarshal(res, &output); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -327,13 +325,13 @@ func batchCall(t *testing.T) {
Value: nil,
}},
}
res, statusCode := httpPost(t, ts.URL+"/accounts/*", badBody)
_, statusCode := httpPost(t, ts.URL+"/accounts/*", badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid data")

a := uint8(1)
b := uint8(2)
method := "add"
abi, err := ABI.New([]byte(abiJSON))
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
input, err := m.EncodeInput(a, b)
if err != nil {
Expand All @@ -353,10 +351,10 @@ func batchCall(t *testing.T) {
}},
}

res, statusCode = httpPost(t, ts.URL+"/accounts/*?revision="+invalidNumberRevision, badBody)
_, statusCode = httpPost(t, ts.URL+"/accounts/*?revision="+invalidNumberRevision, badBody)
assert.Equal(t, http.StatusBadRequest, statusCode, "invalid revision")

res, statusCode = httpPost(t, ts.URL+"/accounts/*", reqBody)
res, statusCode := httpPost(t, ts.URL+"/accounts/*", reqBody)
var results accounts.BatchCallResults
if err = json.Unmarshal(res, &results); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -399,7 +397,7 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand All @@ -412,7 +410,7 @@ func httpGet(t *testing.T, url string) ([]byte, int) {
if err != nil {
t.Fatal(err)
}
r, err := ioutil.ReadAll(res.Body)
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func New(
allowCustomTracer bool,
forkConfig thor.ForkConfig,
) (http.HandlerFunc, func()) {

origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
for i, o := range origins {
origins[i] = strings.ToLower(strings.TrimSpace(o))
Expand Down
Loading
Loading