From cc4e2752512c8a7f4409bdd83d9a4b020719da00 Mon Sep 17 00:00:00 2001 From: Jacob Gadikian Date: Sat, 21 Dec 2024 02:40:31 +0700 Subject: [PATCH] lint --- .golangci.yml | 9 +++++++-- go.mod | 2 +- ibc_test.go | 5 ++--- internal/api/api_test.go | 3 +-- internal/api/iterator.go | 2 +- internal/api/iterator_test.go | 5 ++--- internal/api/lib.go | 13 ++++++------- internal/api/lib_test.go | 25 ++++++++++++------------- internal/api/mocks.go | 13 +++++++------ lib.go | 6 +++--- lib_libwasmvm.go | 2 +- lib_libwasmvm_test.go | 5 ++--- lib_test.go | 3 +-- types/msg.go | 2 -- 14 files changed, 46 insertions(+), 49 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 449da08bb..f5735c4fe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,17 +10,19 @@ linters: - dogsled - errcheck - exportloopref + - gci - goconst - - gocritic + # - gocritic - gofumpt - gosec - gosimple - govet - ineffassign + - maintidx - misspell - nakedret - nolintlint - - revive + # - revive - staticcheck - stylecheck - typecheck @@ -80,6 +82,9 @@ issues: max-same-issues: 10000 linters-settings: + revive: + disable: + - var-naming dogsled: max-blank-identifiers: 3 maligned: diff --git a/go.mod b/go.mod index b8a003356..16083455d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/CosmWasm/wasmvm/v2 -go 1.21 +go 1.23 require ( github.com/google/btree v1.0.0 diff --git a/ibc_test.go b/ibc_test.go index 18cb09bd7..c6b542c21 100644 --- a/ibc_test.go +++ b/ibc_test.go @@ -7,11 +7,10 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/internal/api" "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const IBCTestContract = "./testdata/ibc_reflect.wasm" diff --git a/internal/api/api_test.go b/internal/api/api_test.go index b9bb3e8dd..ff1d95f64 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -5,9 +5,8 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/require" ) func TestValidateAddressFailure(t *testing.T) { diff --git a/internal/api/iterator.go b/internal/api/iterator.go index 2f997e707..4ba47e792 100644 --- a/internal/api/iterator.go +++ b/internal/api/iterator.go @@ -65,7 +65,7 @@ func storeIterator(callID uint64, it types.Iterator, frameLenLimit int) (uint64, new_index := len(iteratorFrames[callID]) if new_index >= frameLenLimit { - return 0, fmt.Errorf("Reached iterator limit (%d)", frameLenLimit) + return 0, fmt.Errorf("reached iterator limit (%d)", frameLenLimit) } // store at array position `new_index` diff --git a/internal/api/iterator_test.go b/internal/api/iterator_test.go index ef9070975..31ad6b705 100644 --- a/internal/api/iterator_test.go +++ b/internal/api/iterator_test.go @@ -6,11 +6,10 @@ import ( "sync" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/internal/api/testdb" "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type queueData struct { diff --git a/internal/api/lib.go b/internal/api/lib.go index 2e1bc3cf2..a454af2f8 100644 --- a/internal/api/lib.go +++ b/internal/api/lib.go @@ -13,9 +13,8 @@ import ( "strings" "syscall" - "golang.org/x/sys/unix" - "github.com/CosmWasm/wasmvm/v2/types" + "golang.org/x/sys/unix" ) // Value types @@ -45,26 +44,26 @@ func InitCache(config types.VMConfig) (Cache, error) { // libwasmvm would create this directory too but we need it earlier for the lockfile err := os.MkdirAll(config.Cache.BaseDir, 0o755) if err != nil { - return Cache{}, fmt.Errorf("Could not create base directory") + return Cache{}, fmt.Errorf("could not create base directory") } lockfile, err := os.OpenFile(filepath.Join(config.Cache.BaseDir, "exclusive.lock"), os.O_WRONLY|os.O_CREATE, 0o666) if err != nil { - return Cache{}, fmt.Errorf("Could not open exclusive.lock") + return Cache{}, fmt.Errorf("could not open exclusive.lock") } _, err = lockfile.WriteString("This is a lockfile that prevent two VM instances to operate on the same directory in parallel.\nSee codebase at github.com/CosmWasm/wasmvm for more information.\nSafety first – brought to you by Confio ❤️\n") if err != nil { - return Cache{}, fmt.Errorf("Error writing to exclusive.lock") + return Cache{}, fmt.Errorf("error writing to exclusive.lock") } err = unix.Flock(int(lockfile.Fd()), unix.LOCK_EX|unix.LOCK_NB) if err != nil { - return Cache{}, fmt.Errorf("Could not lock exclusive.lock. Is a different VM running in the same directory already?") + return Cache{}, fmt.Errorf("could not lock exclusive.lock. is a different VM running in the same directory already?") } configBytes, err := json.Marshal(config) if err != nil { - return Cache{}, fmt.Errorf("Could not serialize config") + return Cache{}, fmt.Errorf("could not serialize config") } configView := makeView(configBytes) defer runtime.KeepAlive(configBytes) diff --git a/internal/api/lib_test.go b/internal/api/lib_test.go index ab1531e3d..eae45e310 100644 --- a/internal/api/lib_test.go +++ b/internal/api/lib_test.go @@ -13,10 +13,9 @@ import ( "testing" "time" + "github.com/CosmWasm/wasmvm/v2/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/CosmWasm/wasmvm/v2/types" ) const ( @@ -501,9 +500,9 @@ func TestGetPinnedMetrics(t *testing.T) { findMetrics := func(list []types.PerModuleEntry, checksum types.Checksum) *types.PerModuleMetrics { found := (*types.PerModuleMetrics)(nil) - for _, structure := range list { - if bytes.Equal(structure.Checksum, checksum) { - found = &structure.Metrics + for i := range list { + if bytes.Equal(list[i].Checksum, checksum) { + found = &list[i].Metrics break } } @@ -1156,7 +1155,7 @@ func TestReplyAndQuery(t *testing.T) { require.Equal(t, events, val.Events) } -func requireOkResponse(t testing.TB, res []byte, expectedMsgs int) { +func requireOkResponse(t testing.TB, res []byte, expectedMsgs int) { //nolint:unparam // expectedMsgs always receives 0 but that could change var result types.ContractResult err := json.Unmarshal(res, &result) require.NoError(t, err) @@ -1361,17 +1360,17 @@ func TestFloats(t *testing.T) { // helper to print the value in the same format as Rust's Debug trait debugStr := func(value Value) string { - if value.U32 != nil { + switch { + case value.U32 != nil: return fmt.Sprintf("U32(%d)", *value.U32) - } else if value.U64 != nil { + case value.U64 != nil: return fmt.Sprintf("U64(%d)", *value.U64) - } else if value.F32 != nil { + case value.F32 != nil: return fmt.Sprintf("F32(%d)", *value.F32) - } else if value.F64 != nil { + case value.F64 != nil: return fmt.Sprintf("F64(%d)", *value.F64) - } else { - t.FailNow() - return "" + default: + return "None" } } diff --git a/internal/api/mocks.go b/internal/api/mocks.go index d2f5e20ea..01ce06be2 100644 --- a/internal/api/mocks.go +++ b/internal/api/mocks.go @@ -8,11 +8,10 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/internal/api/testdb" "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) /** helper constructors **/ @@ -392,8 +391,8 @@ func NewMockAPI() *types.GoAPI { } } -func TestMockApi(t *testing.T) { - human := Foobar +func TestMockAPI(t *testing.T) { + human := "Foobar" canon, cost, err := MockCanonicalizeAddress(human) require.NoError(t, err) assert.Equal(t, CanonicalLength, len(canon)) @@ -428,7 +427,9 @@ func DefaultQuerier(contractAddr string, coins types.Array[types.Coin]) types.Qu } } -func (q *MockQuerier) Query(request types.QueryRequest, _gasLimit uint64) ([]byte, error) { +// Query is a mock implementation of the Querier interface. It takes a request and a gas limit. +// It returns the marshaled request and an error if the request is not supported. +func (q *MockQuerier) Query(request types.QueryRequest, _ uint64) ([]byte, error) { marshaled, err := json.Marshal(request) if err != nil { return nil, err diff --git a/lib.go b/lib.go index 458af0740..cb18a4c61 100644 --- a/lib.go +++ b/lib.go @@ -44,15 +44,15 @@ func LibwasmvmVersion() (string, error) { // to avoid accidental misusage. func CreateChecksum(wasm []byte) (Checksum, error) { if len(wasm) == 0 { - return Checksum{}, fmt.Errorf("Wasm bytes nil or empty") + return Checksum{}, fmt.Errorf("wasm bytes nil or empty") } if len(wasm) < 4 { - return Checksum{}, fmt.Errorf("Wasm bytes shorter than 4 bytes") + return Checksum{}, fmt.Errorf("wasm bytes shorter than 4 bytes") } // magic number for Wasm is "\0asm" // See https://webassembly.github.io/spec/core/binary/modules.html#binary-module if !bytes.Equal(wasm[:4], []byte("\x00\x61\x73\x6D")) { - return Checksum{}, fmt.Errorf("Wasm bytes do not start with Wasm magic number") + return Checksum{}, fmt.Errorf("wasm bytes do not start with wasm magic number") } hash := sha256.Sum256(wasm) return Checksum(hash[:]), nil diff --git a/lib_libwasmvm.go b/lib_libwasmvm.go index 3f66b71ea..66a687500 100644 --- a/lib_libwasmvm.go +++ b/lib_libwasmvm.go @@ -703,7 +703,7 @@ var ( func DeserializeResponse(gasLimit uint64, deserCost types.UFraction, gasReport *types.GasReport, data []byte, response any) error { gasForDeserialization := deserCost.Mul(uint64(len(data))).Floor() if gasLimit < gasForDeserialization+gasReport.UsedInternally { - return fmt.Errorf("Insufficient gas left to deserialize contract execution result (%d bytes)", len(data)) + return fmt.Errorf("insufficient gas left to deserialize contract execution result (%d bytes)", len(data)) } gasReport.UsedInternally += gasForDeserialization gasReport.Remaining -= gasForDeserialization diff --git a/lib_libwasmvm_test.go b/lib_libwasmvm_test.go index 7b6f3fb32..f54ca2913 100644 --- a/lib_libwasmvm_test.go +++ b/lib_libwasmvm_test.go @@ -9,11 +9,10 @@ import ( "os" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/internal/api" "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( diff --git a/lib_test.go b/lib_test.go index 35094e7df..54b2d3293 100644 --- a/lib_test.go +++ b/lib_test.go @@ -3,9 +3,8 @@ package cosmwasm import ( "testing" - "github.com/stretchr/testify/require" - "github.com/CosmWasm/wasmvm/v2/types" + "github.com/stretchr/testify/require" ) func TestCreateChecksum(t *testing.T) { diff --git a/types/msg.go b/types/msg.go index 696894e8f..369a3616b 100644 --- a/types/msg.go +++ b/types/msg.go @@ -5,8 +5,6 @@ import ( "fmt" ) -var null = "null" - //------- Results / Msgs ------------- // ContractResult is the raw response from the instantiate/execute/migrate calls.