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

Enable linter for every module #3605

Merged
merged 7 commits into from
Oct 11, 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
27 changes: 26 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,33 @@ on:

jobs:
lint:
name: Lint
name: 'Lint: NeoGo'
uses: nspcc-dev/.github/.github/workflows/go-linter.yml@master

lint_examples:
name: 'Lint: examples (${{ matrix.contract }})'
uses: nspcc-dev/.github/.github/workflows/go-linter.yml@master

strategy:
fail-fast: false
matrix:
contract: [ 'engine', 'events', 'iterator', 'nft-d', 'nft-nd', 'nft-nd-nns', 'oracle',
'runtime', 'storage', 'timer', 'token', 'zkp/cubic_circuit', 'zkp/xor_compat']
with:
workdir: examples/${{ matrix.contract }}

lint_scripts:
name: 'Lint: scripts'
uses: nspcc-dev/.github/.github/workflows/go-linter.yml@master
with:
workdir: scripts

lint_interops:
name: 'Lint: interop'
uses: nspcc-dev/.github/.github/workflows/go-linter.yml@master
with:
workdir: pkg/interop

gomodcheck:
name: Check internal dependencies
runs-on: ubuntu-latest
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ IMAGE_REPO=nspccdev/neo-go

DISABLE_NEOTEST_COVER=1

ROOT_DIR:=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))
GOMODDIRS=$(dir $(shell find $(ROOT_DIR) -name go.mod))

# All of the targets are phony here because we don't really use make dependency
# tracking for files
.PHONY: build $(BINARY) deps image docker/$(BINARY) image-latest image-push image-push-latest clean-cluster \
Expand Down Expand Up @@ -113,7 +116,9 @@ vet:
curl -L -o $@ https://github.com/nspcc-dev/.github/raw/master/.golangci.yml

lint: .golangci.yml
@golangci-lint run
@for dir in $(GOMODDIRS); do \
(cd "$$dir" && golangci-lint run --config $(ROOT_DIR)/$< | sed -r "s,^,$$dir," | sed -r "s,^$(ROOT_DIR),,") \
done

fmt:
@gofmt -l -w -s $$(find . -type f -name '*.go'| grep -v "/vendor/")
Expand Down
8 changes: 4 additions & 4 deletions examples/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
)

// NotifyScriptContainer sends runtime notification with script container hash
// NotifyScriptContainer sends runtime notification with script container hash.
func NotifyScriptContainer() {
tx := runtime.GetScriptContainer()
runtime.Notify("Tx", tx.Hash)
}

// NotifyCallingScriptHash sends runtime notification with calling script hash
// NotifyCallingScriptHash sends runtime notification with calling script hash.
func NotifyCallingScriptHash() {
callingScriptHash := runtime.GetCallingScriptHash()
runtime.Notify("Calling", callingScriptHash)
}

// NotifyExecutingScriptHash sends runtime notification about executing script hash
// NotifyExecutingScriptHash sends runtime notification about executing script hash.
func NotifyExecutingScriptHash() {
execScriptHash := runtime.GetExecutingScriptHash()
runtime.Notify("Executing", execScriptHash)
}

// NotifyEntryScriptHash sends notification about entry script hash
// NotifyEntryScriptHash sends notification about entry script hash.
func NotifyEntryScriptHash() {
entryScriptHash := runtime.GetEntryScriptHash()
runtime.Notify("Entry", entryScriptHash)
Expand Down
2 changes: 1 addition & 1 deletion examples/iterator/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// _deploy primes contract's storage with some data to be used later.
func _deploy(_ any, _ bool) {
func _deploy(_ any, _ bool) { // nolint: unused
ctx := storage.GetContext() // RW context.
storage.Put(ctx, "foo1", "1")
storage.Put(ctx, "foo2", "2")
Expand Down
8 changes: 4 additions & 4 deletions examples/nft-nd-nns/nns.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const (
// Other constants.
const (
// defaultRegisterPrice is the default price for new domain registration.
defaultRegisterPrice = 10_0000_0000
defaultRegisterPrice = 10_0000_0000 // nolint: unused
// millisecondsInYear is amount of milliseconds per year.
millisecondsInYear = 365 * 24 * 3600 * 1000
)
Expand All @@ -79,7 +79,7 @@ func Update(nef []byte, manifest string) {
}

// _deploy initializes defaults (total supply and registration price) on contract deploy.
func _deploy(data any, isUpdate bool) {
func _deploy(data any, isUpdate bool) { // nolint: unused
if isUpdate {
return
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func getRecordKey(tokenId []byte, name string, typ RecordType) []byte {

// isValid returns true if the provided address is a valid Uint160.
func isValid(address interop.Hash160) bool {
return address != nil && len(address) == 20
return address != nil && len(address) == 20 // nolint: gosimple
}

// checkCommittee panics if the script container is not signed by the committee.
Expand Down Expand Up @@ -555,7 +555,7 @@ func splitAndCheck(name string, allowMultipleFragments bool) []string {
if l > 2 && !allowMultipleFragments {
return nil
}
for i := 0; i < l; i++ {
for i := range fragments {
if !checkFragment(fragments[i], i == l-1) {
return nil
}
Expand Down
2 changes: 0 additions & 2 deletions examples/nft-nd-nns/nns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,9 @@ func testTokensOf(t *testing.T, c *neotest.ContractInvoker, result [][]byte, arg
}
require.NoError(t, err)
iter := s.Pop().Interop().Value().(*storage.Iterator)
arr := make([]stackitem.Item, 0, len(result))
for i := range result {
require.True(t, iter.Next())
require.Equal(t, result[i], iter.Value().Value())
arr = append(arr, stackitem.Make(result[i]))
}
require.False(t, iter.Next())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nft-nd/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
totalSupplyPrefix = "s"
// balancePrefix contains map from addresses to balances.
balancePrefix = "b"
// accountPrefix contains map from address + token id to tokens
// accountPrefix contains map from address + token id to tokens.
accountPrefix = "a"
// tokenPrefix contains map from token id to it's owner.
tokenPrefix = "t"
Expand Down
4 changes: 2 additions & 2 deletions examples/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
// Check if the invoker of the contract is the specified owner
// Check if the invoker of the contract is the specified owner.
owner = address.ToHash160("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
)

Expand All @@ -22,7 +22,7 @@ func init() {

// _deploy is called after contract deployment or update, it'll be called
// in deployment transaction and if call update method of this contract.
func _deploy(_ any, isUpdate bool) {
func _deploy(_ any, isUpdate bool) { // nolint: unused
if isUpdate {
Log("_deploy method called after contract update")
return
Expand Down
4 changes: 2 additions & 2 deletions examples/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)

// ctx holds storage context for contract methods
// ctx holds storage context for contract methods.
var ctx storage.Context

// defaultKey represents the default key.
var defaultKey = []byte("default")

// init inits storage context before any other contract method is called
// init inits storage context before any other contract method is called.
func init() {
ctx = storage.GetContext()
}
Expand Down
10 changes: 5 additions & 5 deletions examples/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)

const defaultTicks = 3
const defaultTicks = 3 // nolint: unused
const mgmtKey = "mgmt"

var (
// ctx holds storage context for contract methods
// ctx holds storage context for contract methods.
ctx storage.Context
// Check if the invoker of the contract is the specified owner
// Check if the invoker of the contract is the specified owner.
owner = address.ToHash160("NbrUYaZgyhSkNoRo9ugRyEMdUZxrhkNaWB")
// ticksKey is a storage key for ticks counter
// ticksKey is a storage key for ticks counter.
ticksKey = []byte("ticks")
)

func init() {
ctx = storage.GetContext()
}

func _deploy(_ any, isUpdate bool) {
func _deploy(_ any, isUpdate bool) { // nolint: unused
if isUpdate {
ticksLeft := storage.Get(ctx, ticksKey).(int) + 1
storage.Put(ctx, ticksKey, ticksLeft)
Expand Down
17 changes: 8 additions & 9 deletions examples/token/nep17/nep17.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)

// Token holds all token info
// Token holds all token info.
type Token struct {
// Token name
Name string
Expand All @@ -35,17 +35,17 @@ func getIntFromDB(ctx storage.Context, key []byte) int {
return res
}

// GetSupply gets the token totalSupply value from VM storage
// GetSupply gets the token totalSupply value from VM storage.
func (t Token) GetSupply(ctx storage.Context) int {
return getIntFromDB(ctx, []byte(t.CirculationKey))
}

// BalanceOf gets the token balance of a specific address
// BalanceOf gets the token balance of a specific address.
func (t Token) BalanceOf(ctx storage.Context, holder []byte) int {
return getIntFromDB(ctx, holder)
}

// Transfer token from one user to another
// Transfer token from one user to another.
func (t Token) Transfer(ctx storage.Context, from, to interop.Hash160, amount int, data any) bool {
amountFrom := t.CanTransfer(ctx, from, to, amount)
if amountFrom == -1 {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (t Token) Transfer(ctx storage.Context, from, to interop.Hash160, amount in
return true
}

// CanTransfer returns the amount it can transfer
// CanTransfer returns the amount it can transfer.
func (t Token) CanTransfer(ctx storage.Context, from []byte, to []byte, amount int) int {
if len(to) != 20 || !IsUsableAddress(from) {
return -1
Expand All @@ -94,10 +94,9 @@ func (t Token) CanTransfer(ctx storage.Context, from []byte, to []byte, amount i
return amountFrom
}

// IsUsableAddress checks if the sender is either the correct Neo address or SC address
// IsUsableAddress checks if the sender is either the correct Neo address or SC address.
func IsUsableAddress(addr []byte) bool {
if len(addr) == 20 {

if runtime.CheckWitness(addr) {
return true
}
Expand All @@ -112,13 +111,13 @@ func IsUsableAddress(addr []byte) bool {
return false
}

// Mint initial supply of tokens
// Mint initial supply of tokens.
func (t Token) Mint(ctx storage.Context, to interop.Hash160) bool {
if !IsUsableAddress(t.Owner) {
return false
}
minted := storage.Get(ctx, []byte("minted"))
if minted != nil && minted.(bool) == true {
if minted != nil && minted.(bool) {
return false
}

Expand Down
14 changes: 7 additions & 7 deletions examples/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
)

// init initializes Token Interface and storage context for the Smart
// Contract to operate with
// Contract to operate with.
func init() {
token = nep17.Token{
Name: "Awesome NEO Token",
Expand All @@ -32,32 +32,32 @@ func init() {
ctx = storage.GetContext()
}

// Symbol returns the token symbol
// Symbol returns the token symbol.
func Symbol() string {
return token.Symbol
}

// Decimals returns the token decimals
// Decimals returns the token decimals.
func Decimals() int {
return token.Decimals
}

// TotalSupply returns the token total supply value
// TotalSupply returns the token total supply value.
func TotalSupply() int {
return token.GetSupply(ctx)
}

// BalanceOf returns the amount of token on the specified address
// BalanceOf returns the amount of token on the specified address.
func BalanceOf(holder interop.Hash160) int {
return token.BalanceOf(ctx, holder)
}

// Transfer token from one user to another
// Transfer token from one user to another.
func Transfer(from interop.Hash160, to interop.Hash160, amount int, data any) bool {
return token.Transfer(ctx, from, to, amount, data)
}

// Mint initial supply of tokens
// Mint initial supply of tokens.
func Mint(to interop.Hash160) bool {
return token.Mint(ctx, to)
}
2 changes: 1 addition & 1 deletion examples/zkp/cubic_circuit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (circuit *CubicCircuit) Define(api frontend.API) error {

// main demonstrates how to build the proof and verify it with the help of gnark
// library. Error handling omitted intentionally to simplify the example.
func main() {
func main() { // nolint: unused
var (
circuit CubicCircuit
assignment = CubicCircuit{X: 3, Y: 35}
Expand Down
2 changes: 1 addition & 1 deletion examples/zkp/cubic_circuit/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func setup(t *testing.T, ccs constraint.ConstraintSystem, phase1ResponsePath str
// receive a []byte, deserialize it, add his contribution and send back to
// coordinator, like it is done in https://github.com/bnb-chain/zkbnb-setup
// for BN254 elliptic curve.
for i := range nContributionsPhase2 {
for range nContributionsPhase2 {
srs2.Contribute()
}

Expand Down
6 changes: 3 additions & 3 deletions examples/zkp/xor_compat/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
// proving system and is taken from the
// https://github.com/neo-project/neo/issues/2647#issuecomment-1002893109 without
// changes. The verification process checks the following equality:
// A * B = alpha * beta + sum(pub_input[i] * (beta * u_i(x) + alpha * v_i(x) + w_i(x)) / gamma) * gamma + C * delta
// A * B = alpha * beta + sum(pub_input[i] * (beta * u_i(x) + alpha * v_i(x) + w_i(x)) / gamma) * gamma + C * delta.
func VerifyProof(a []byte, b []byte, c []byte, publicInput [][]byte) bool {
alphaPoint := crypto.Bls12381Deserialize(alpha)
betaPoint := crypto.Bls12381Deserialize(beta)
Expand All @@ -68,11 +68,11 @@ func VerifyProof(a []byte, b []byte, c []byte, publicInput [][]byte) bool {
panic("error: inputlen or iclen")
}
icPoints := make([]crypto.Bls12381Point, iclen)
for i := 0; i < iclen; i++ {
for i := range icPoints {
icPoints[i] = crypto.Bls12381Deserialize(ic[i])
}
acc := icPoints[0]
for i := 0; i < inputlen; i++ {
for i := range publicInput {
scalar := publicInput[i] // 32-bytes LE field element.
temp := crypto.Bls12381Mul(icPoints[i+1], scalar, false)
acc = crypto.Bls12381Add(acc, temp)
Expand Down
2 changes: 1 addition & 1 deletion pkg/interop/native/ledger/transaction_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type WitnessCondition struct {
// WitnessConditionType represents the type of rule-based witness condition.
type WitnessConditionType byte

// Various witness condition types
// Various witness condition types.
const (
// WitnessBoolean is a generic boolean condition.
WitnessBoolean WitnessConditionType = 0x00
Expand Down
1 change: 0 additions & 1 deletion pkg/interop/native/neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,4 @@ func GetAccountState(addr interop.Hash160) *AccountState {
// GetCommitteeAddress represents `getCommitteeAddress` method of NEO native contract.
func GetCommitteeAddress() interop.Hash160 {
return neogointernal.CallWithToken(Hash, "getCommitteeAddress", int(contract.ReadStates)).(interop.Hash160)

}
Loading
Loading