Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Dec 20, 2024
1 parent 3662cd2 commit cc4e275
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 49 deletions.
9 changes: 7 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +82,9 @@ issues:
max-same-issues: 10000

linters-settings:
revive:
disable:
- var-naming
dogsled:
max-blank-identifiers: 3
maligned:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/CosmWasm/wasmvm/v2

go 1.21
go 1.23

require (
github.com/google/btree v1.0.0
Expand Down
5 changes: 2 additions & 3 deletions ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 2 additions & 3 deletions internal/api/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 12 additions & 13 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
}
}

Expand Down
13 changes: 7 additions & 6 deletions internal/api/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 **/
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib_libwasmvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib_libwasmvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 1 addition & 2 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
)

var null = "null"

//------- Results / Msgs -------------

// ContractResult is the raw response from the instantiate/execute/migrate calls.
Expand Down

0 comments on commit cc4e275

Please sign in to comment.