Merge pull request #496 from sputn1ck/dockerize_linter

Dockerize linter, add gosimports
pull/500/head
Oliver Gugger 2 years ago committed by GitHub
commit 6d1708de68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,37 +2,64 @@ run:
# timeout for analysis
deadline: 4m
# Linting uses a lot of memory. Keep it under control by only running a single
# worker.
concurrency: 1
skip-files:
- "\\.pb\\.go$"
- "\\.pb\\.gw\\.go$"
linters-settings:
govet:
# Don't report about shadowed variables
check-shadowing: false
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
tagliatelle:
case:
rules:
json: snake
whitespace:
multi-func: true
multi-if: true
gosec:
excludes:
- G402 # Look for bad TLS connection settings.
- G306 # Poor file permissions used when writing to a new file.
staticcheck:
go: "1.18"
checks: ["-SA1019"]
linters:
enable-all: true
disable:
# Global variables are used in many places throughout the code base.
# Global variables are used in many places throughout the code base.
- gochecknoglobals
# Some lines are over 80 characters on purpose and we don't want to make them
# even longer by marking them as 'nolint'.
- lll
# We don't care (enough) about misaligned structs to lint that.
- maligned
# We want to allow short variable names.
- varnamelen
# We want to allow TODOs.
- godox
# We have long functions, especially in tests. Moving or renaming those would
# trigger funlen problems that we may not want to solve at that time.
- funlen
# Disable for now as we haven't yet tuned the sensitivity to our codebase
# yet. Enabling by default for example, would also force new contributors to
# yet. Enabling by default for example, would also force new contributors to
# potentially extensively refactor code, when they want to smaller change to
# land.
- gocyclo
- gocognit
- cyclop
# Instances of table driven tests that don't pre-allocate shouldn't trigger
# the linter.
@ -40,7 +67,69 @@ linters:
# Init functions are used by loggers throughout the codebase.
- gochecknoinits
# Causes stack overflow, see https://github.com/polyfloyd/go-errorlint/issues/19.
- errorlint
# Deprecated linters. See https://golangci-lint.run/usage/linters/.
- interfacer
- golint
- maligned
- scopelint
# New linters that need a code adjustment first.
- wrapcheck
- nolintlint
- paralleltest
- tparallel
- testpackage
- gofumpt
- gomoddirectives
- ireturn
- maintidx
- nlreturn
- dogsled
- gci
- containedctx
- contextcheck
- errname
- exhaustivestruct
- goerr113
- gomnd
- ifshort
- noctx
- nestif
- wsl
- exhaustive
- forcetypeassert
- nilerr
- nilnil
- stylecheck
- thelper
# Additions compared to LND
- exhaustruct
issues:
# Only show newly introduced problems.
new-from-rev: 36838cf7f464cf73b0201798063b2caffeae4250
exclude-rules:
# Allow fmt.Printf() in test files
- path: _test\.go
linters:
- forbidigo
# Allow fmt.Printf() in loopd
- path: cmd/loopd/*
linters:
- forbidigo
- path: loopd/*
linters:
- forbidigo
# Allow fmt.Printf() in loop
- path: cmd/loop/*
linters:
- forbidigo

@ -1,10 +1,16 @@
.DEFAULT_GOAL := build
PKG := github.com/lightninglabs/loop
TOOLS_DIR := tools
GOTEST := GO111MODULE=on go test -v
GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports
GO_BIN := ${GOPATH}/bin
GOIMPORTS_BIN := $(GO_BIN)/gosimports
GOBUILD := GO111MODULE=on go build -v
GOINSTALL := GO111MODULE=on go install -v
GOMOD := GO111MODULE=on go mod
@ -13,51 +19,37 @@ COMMIT := $(shell git describe --abbrev=40 --dirty)
LDFLAGS := -ldflags "-X $(PKG).Commit=$(COMMIT)"
DEV_TAGS = dev
GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -name "*pb.go" -not -name "*pb.gw.go" -not -name "*.pb.json.go")
GOLIST := go list $(PKG)/... | grep -v '/vendor/'
LINT_BIN := $(GO_BIN)/golangci-lint
LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
LINT_COMMIT := v1.18.0
LINT = $(LINT_BIN) run -v
DEPGET := cd /tmp && GO111MODULE=on go get -v
XARGS := xargs -L 1
TEST_FLAGS = -test.timeout=20m
UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS)
# Linting uses a lot of memory, so keep it under control by limiting the number
# of workers if requested.
ifneq ($(workers),)
LINT_WORKERS = --concurrency=$(workers)
endif
DOCKER_TOOLS = docker run -v $$(pwd):/build loop-tools
GREEN := "\\033[0;32m"
NC := "\\033[0m"
define print
echo $(GREEN)$1$(NC)
endef
$(LINT_BIN):
@$(call print, "Fetching linter")
$(DEPGET) $(LINT_PKG)@$(LINT_COMMIT)
unit:
@$(call print, "Running unit tests.")
$(UNIT)
fmt:
@$(call print, "Formatting source.")
gofmt -l -w -s $(GOFILES_NOVENDOR)
lint: $(LINT_BIN)
@$(call print, "Linting source.")
$(LINT)
# ============
# DEPENDENCIES
# ============
mod-tidy:
@$(call print, "Tidying modules.")
$(GOMOD) tidy
$(GOIMPORTS_BIN):
@$(call print, "Installing goimports.")
cd $(TOOLS_DIR); go install -trimpath $(GOIMPORTS_PKG)
mod-check:
@$(call print, "Checking modules.")
$(GOMOD) tidy
if test -n "$$(git status | grep -e "go.mod\|go.sum")"; then echo "Running go mod tidy changes go.mod/go.sum"; git status; git diff; exit 1; fi
# ============
# INSTALLATION
@ -94,3 +86,40 @@ clean:
@$(call print, "Cleaning up.")
rm -f ./loop-debug ./loopd-debug
rm -rf ./vendor
# =======
# TESTING
# =======
unit:
@$(call print, "Running unit tests.")
$(UNIT)
# =========
# UTILITIES
# =========
fmt: $(GOIMPORTS_BIN)
@$(call print, "Fixing imports.")
gosimports -w $(GOFILES_NOVENDOR)
@$(call print, "Formatting source.")
gofmt -l -w -s $(GOFILES_NOVENDOR)
lint: docker-tools
@$(call print, "Linting source.")
$(DOCKER_TOOLS) golangci-lint run -v $(LINT_WORKERS)
docker-tools:
@$(call print, "Building tools docker image.")
docker build -q -t loop-tools $(TOOLS_DIR)
mod-tidy:
@$(call print, "Tidying modules.")
$(GOMOD) tidy
mod-check:
@$(call print, "Checking modules.")
$(GOMOD) tidy
if test -n "$$(git status | grep -e "go.mod\|go.sum")"; then echo "Running go mod tidy changes go.mod/go.sum"; git status; git diff; exit 1; fi

@ -476,7 +476,6 @@ func setParams(ctx *cli.Context) error {
// so that it does not need to be manually updated.
case categoriesSet:
params.FeePpm = 0
}
// Update our parameters to our mutated values.
_, err = client.SetLiquidityParams(

@ -161,7 +161,7 @@ func loopOut(ctx *cli.Context) error {
}
// Show a warning if a slow swap was requested.
warning := ""
var warning string
if fast {
warning = "Fast swap requested."
} else {

@ -49,7 +49,6 @@ func listAuth(ctx *cli.Context) error {
tokens := make([]*printableToken, len(resp.Tokens))
for i, t := range resp.Tokens {
mac := &macaroon.Macaroon{}
err := mac.UnmarshalBinary(t.BaseMacaroon)
if err != nil {

@ -11,6 +11,7 @@ import (
"strings"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/loopd"
@ -21,9 +22,6 @@ import (
"github.com/lightninglabs/protobuf-hex-display/proto"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/btcsuite/btcd/btcutil"
"github.com/urfave/cli"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

@ -60,7 +60,7 @@ func swapInfo(ctx *cli.Context) error {
id = ctx.String("id")
case ctx.NArg() > 0:
id = args[0]
args = args.Tail()
args = args.Tail() // nolint:wastedassign
default:
// Show command help if no arguments and flags were provided.
return cli.ShowCommandHelp(ctx, "swapinfo")

@ -305,7 +305,7 @@ type LoopInSwapInfo struct { // nolint
// LoopOutSwapInfo contains essential information of a loop-out swap after the
// swap is initiated.
type LoopOutSwapInfo struct { // nolint:golint
type LoopOutSwapInfo struct { // nolint:revive
// SwapHash contains the sha256 hash of the swap preimage.
SwapHash lntypes.Hash

@ -262,7 +262,6 @@ func (p Parameters) String() string {
ruleList = append(
ruleList, fmt.Sprintf("Peer: %v: %v", peer, rule),
)
}
return fmt.Sprintf("rules: %v, failure backoff: %v, sweep "+

@ -1409,7 +1409,6 @@ func TestSizeRestrictions(t *testing.T) {
Maximum: 6000,
}, nil,
).Once()
},
suggestions: nil,
expectedError: ErrMaxExceedsServer,
@ -1582,7 +1581,6 @@ func TestFeePercentage(t *testing.T) {
error) {
return testCase.quote, nil
}
lnd.Channels = []lndclient.ChannelInfo{

@ -152,7 +152,6 @@ func calculateSwapAmount(targetAmount, reserveAmount,
// cannot take any further action.
case reserveAmount <= reserveMinimum:
return 0
}
// Express our minimum reserve amount as a maximum target amount.

@ -103,6 +103,7 @@ func NewListenerConfig(config *Config, rpcCfg RPCConfig) *ListenerCfg {
if rpcCfg.LndConn != nil {
svcCfg.Dialer = func(context.Context, string) (
net.Conn, error) {
return rpcCfg.LndConn, nil
}
}

@ -314,7 +314,7 @@ func (s *swapClientServer) Monitor(in *clientrpc.MonitorRequest,
}
// Concatenate both sets.
filteredSwaps := append(pendingSwaps, completedSwaps...)
filteredSwaps := append(pendingSwaps, completedSwaps...) // nolint: gocritic
// Sort again, but this time old to new.
sort.Slice(filteredSwaps, func(i, j int) bool {
@ -936,7 +936,6 @@ func rpcToRule(rule *clientrpc.LiquidityRule) (*liquidity.SwapRule, error) {
default:
return nil, fmt.Errorf("unknown rule: %T", rule)
}
}
// SuggestSwaps provides a list of suggested swaps based on lnd's current

@ -7,10 +7,9 @@ import (
"context"
"fmt"
"gopkg.in/macaroon-bakery.v2/bakery"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/looprpc"
"gopkg.in/macaroon-bakery.v2/bakery"
)
var (

@ -19,7 +19,7 @@ import (
// "human-readable": Hex("102030"),
// Hex("1111"): Hex("5783492373"),
// },
// }
// } .
func DumpDB(tx *bbolt.Tx) error { // nolint: unused
return tx.ForEach(func(k []byte, bucket *bbolt.Bucket) error {
key := toString(k)
@ -95,9 +95,7 @@ func restoreDB(bucket *bbolt.Bucket, data map[string]interface{}) error {
}
continue
}
switch value := v.(type) {
// Key contains value.
case string:
err := bucket.Put(key, []byte(value))

@ -90,7 +90,6 @@ func TestLoopOutStore(t *testing.T) {
t.Run("labelled swap", func(t *testing.T) {
testLoopOutStore(t, &labelledSwap)
})
}
// testLoopOutStore tests the basic functionality of the current bbolt

@ -257,7 +257,6 @@ func testLoopInTimeout(t *testing.T,
// Expect a signing request for the htlc tx output value.
signReq := <-ctx.lnd.SignOutputRawChannel
if signReq.SignDescriptors[0].Output.Value != htlcTx.TxOut[0].Value {
t.Fatal("invalid signing amount")
}

@ -114,6 +114,7 @@ func TestLoopOutPaymentParameters(t *testing.T) {
if !reflect.DeepEqual(
[]uint64(req.OutgoingChanSet), swapPayment.OutgoingChanIds,
) {
t.Fatalf("Unexpected outgoing channel set")
}

@ -4,7 +4,6 @@ import (
"context"
"errors"
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"

@ -503,7 +503,7 @@ type HtlcScriptV2 struct {
// OP_ELSE
// OP_SIZE <20> OP_EQUALVERIFY OP_HASH160 <ripemd(swapHash)> OP_EQUALVERIFY 1
// OP_CHECKSEQUENCEVERIFY
// OP_ENDIF
// OP_ENDIF .
func newHTLCScriptV2(cltvExpiry int32, senderHtlcKey,
receiverHtlcKey [33]byte, swapHash lntypes.Hash) (*HtlcScriptV2, error) {

@ -26,7 +26,7 @@ import (
// asserting the result matches the validity expectation. In the case where it
// doesn't match the expectation, it executes the script step-by-step and
// prints debug information to stdout.
// This code is adopted from: lnd/input/script_utils_test.go
// This code is adopted from: lnd/input/script_utils_test.go .
func assertEngineExecution(t *testing.T, valid bool,
newEngine func() (*txscript.Engine, error)) {
@ -200,7 +200,6 @@ func TestHtlcV2(t *testing.T) {
require.NoError(t, err)
return witness
}, true,
},
{

@ -41,7 +41,6 @@ func (s *PrefixLog) Errorf(format string, params ...interface{}) {
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
params...,
)
}
// ShortHash returns a shortened version of the hash suitable for use in

@ -185,7 +185,7 @@ func (h *mockLightningClient) ListTransactions(
}
// GetNodeInfo retrieves info on the node, and if includeChannels is True,
// will return other channels the node may have with other peers
// will return other channels the node may have with other peers.
func (h *mockLightningClient) GetNodeInfo(ctx context.Context,
pubKeyBytes route.Vertex, includeChannels bool) (*lndclient.NodeInfo, error) {
@ -224,7 +224,7 @@ func (h *mockLightningClient) GetNodeInfo(ctx context.Context,
return nodeInfo, nil
}
// GetChanInfo retrieves all the info the node has on the given channel
// GetChanInfo retrieves all the info the node has on the given channel.
func (h *mockLightningClient) GetChanInfo(ctx context.Context,
channelID uint64) (*lndclient.ChannelEdge, error) {

@ -0,0 +1,16 @@
FROM golang:1.18.0-buster
RUN apt-get update && apt-get install -y git
ENV GOCACHE=/tmp/build/.cache
ENV GOMODCACHE=/tmp/build/.modcache
COPY . /tmp/tools
RUN cd /tmp \
&& mkdir -p /tmp/build/.cache \
&& mkdir -p /tmp/build/.modcache \
&& cd /tmp/tools \
&& go install -trimpath -tags=tools github.com/golangci/golangci-lint/cmd/golangci-lint \
&& chmod -R 777 /tmp/build/
WORKDIR /build

@ -0,0 +1,8 @@
module github.com/lightninglabs/loop/tools
go 1.16
require (
github.com/golangci/golangci-lint v1.46.2 // indirect
github.com/rinchsan/gosimports v0.1.5 // indirect
)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,9 @@
//go:build tools
// +build tools
package loop
import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/rinchsan/gosimports/cmd/gosimports"
)
Loading…
Cancel
Save