Skip to content

Commit

Permalink
PRT-117 tech go linter (lavanet#181)
Browse files Browse the repository at this point in the history
* added linter and fixedadded linter and fixed issues problems

* added go fumpt

* adding some more options and fixes

* add comment

* whitespace linter

* goconst linter

* gocritic linter

* stylecheck linter

* gosimple linter

* ineffassign linter

* misspell linter

* nakedret

* stylecheck

* unconvert

* go simple

* linter configurations

* PRT-117 Client linter fix

* PRT-117 space fix

* PRT-117 fixing some nil issues.

Co-authored-by: Yarom Swisa <[email protected] git config --global user.name Yarom>
Co-authored-by: Ran Mishael <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2022
1 parent da57d1a commit a55f598
Show file tree
Hide file tree
Showing 124 changed files with 1,210 additions and 1,092 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
on:
push:
branches:
- main
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.18.x

- name: Checkout code
uses: actions/checkout@v3

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
config: .golangci.yml
72 changes: 72 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
run:
tests: false
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

linters:
disable-all: true
#for list of linters and what they do: https://golangci-lint.run/usage/linters/
enable:
- depguard
- dogsled
- exportloopref
- goconst
- gocritic
- gofumpt
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- stylecheck
- typecheck
- unconvert
#- unused we have a lot of unused code that we dont want to delete
- forcetypeassert
- goconst
- gofmt
- goimports
- importas
- ineffassign
- nilnil
- typecheck
- unconvert
- whitespace

issues:
exclude-rules:
- text: "singleCaseSwitch"
linters:
- gocritic
- text: "ifElseChain"
linters:
- gocritic
- text: "ST1003:"
linters:
- stylecheck
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
# https://github.com/dominikh/go-tools/issues/389
- text: "ST1016:"
linters:
- stylecheck
- path: "migrations"
text: "SA1019:"
linters:
- staticcheck

max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ BUILD_TARGETS := build install

build: BUILD_ARGS=-o $(BUILDDIR)/

.PHONY: lint
lint:
golangci-lint run --config .golangci.yml

.PHONY: $(BUILD_TARGETS)
$(BUILD_TARGETS): go.sum $(BUILDDIR)/
go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./...
Expand Down
17 changes: 7 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ import (
pairingmodulekeeper "github.com/lavanet/lava/x/pairing/keeper"
pairingmoduletypes "github.com/lavanet/lava/x/pairing/types"
"github.com/lavanet/lava/x/spec"
specmodule "github.com/lavanet/lava/x/spec"
specmoduleclient "github.com/lavanet/lava/x/spec/client"
specmodulekeeper "github.com/lavanet/lava/x/spec/keeper"
specmoduletypes "github.com/lavanet/lava/x/spec/types"
Expand All @@ -115,10 +114,8 @@ const (
Name = "lava"
)

var (
// add here future upgrades (upgrades.Upgrade)
Upgrades = []upgrades.Upgrade{}
)
// Upgrades add here future upgrades (upgrades.Upgrade)
var Upgrades = []upgrades.Upgrade{}

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals

Expand Down Expand Up @@ -166,7 +163,7 @@ var (
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
vesting.AppModuleBasic{},
specmodule.AppModuleBasic{},
spec.AppModuleBasic{},
epochstoragemodule.AppModuleBasic{},
pairingmodule.AppModuleBasic{},
conflictmodule.AppModuleBasic{},
Expand Down Expand Up @@ -348,7 +345,7 @@ func New(
keys[specmoduletypes.MemStoreKey],
app.GetSubspace(specmoduletypes.ModuleName),
)
specModule := specmodule.NewAppModule(appCodec, app.SpecKeeper, app.AccountKeeper, app.BankKeeper)
specModule := spec.NewAppModule(appCodec, app.SpecKeeper, app.AccountKeeper, app.BankKeeper)

// register the proposal types
govRouter := govtypes.NewRouter()
Expand Down Expand Up @@ -438,7 +435,7 @@ func New(

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
var skipGenesisInvariants = cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
Expand Down Expand Up @@ -491,7 +488,7 @@ func New(
ibctransfertypes.ModuleName,
specmoduletypes.ModuleName,
epochstoragemoduletypes.ModuleName,
conflictmoduletypes.ModuleName, //conflict needs to change state before pairing changes stakes
conflictmoduletypes.ModuleName, // conflict needs to change state before pairing changes stakes
pairingmoduletypes.ModuleName,
vestingtypes.ModuleName,
upgradetypes.ModuleName,
Expand Down Expand Up @@ -547,7 +544,7 @@ func New(
upgradetypes.ModuleName,
feegrant.ModuleName,
paramstypes.ModuleName,
conflictmoduletypes.ModuleName, //NOTICE: the last module to initgenesis needs to push fixation in epoch storage
conflictmoduletypes.ModuleName, // NOTICE: the last module to initgenesis needs to push fixation in epoch storage
// this line is used by starport scaffolding # stargate/app/initGenesis
)

Expand Down
4 changes: 2 additions & 2 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
func (app *LavaApp) ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (servertypes.ExportedApp, error) {

// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})

Expand Down Expand Up @@ -50,7 +49,8 @@ func (app *LavaApp) ExportAppStateAndValidators(

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
// in favour of export at a block height
//
// in favour of export at a block height
func (app *LavaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

Expand Down
1 change: 0 additions & 1 deletion app/keepers/lavaKeepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

type LavaKeepers struct {

// Standard Keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v5/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func CreateUpgradeHandler(
log.Println("# STARTING UPGRADE #")
log.Println("########################")

//delete all existing conflicts
// delete all existing conflicts
migrator := keeper.NewMigrator(keepers.ConflictKeeper)
migrator.MigrateToV5(ctx)

Expand Down
6 changes: 3 additions & 3 deletions cmd/lavad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
// this line is used by starport scaffolding # root/arguments
)

var cmdServer = &cobra.Command{
cmdServer := &cobra.Command{
Use: "server [listen-ip] [listen-port] [node-url] [node-chain-id] [api-interface]",
Short: "server",
Long: `server`,
Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {
},
}

var cmdPortalServer = &cobra.Command{
cmdPortalServer := &cobra.Command{
Use: "portal_server [listen-ip] [listen-port] [relayer-chain-id] [api-interface]",
Short: "portal server",
Long: `portal server`,
Expand Down Expand Up @@ -103,7 +103,7 @@ func main() {
},
}

var cmdTestClient = &cobra.Command{
cmdTestClient := &cobra.Command{
Use: "test_client [chain-id] [api-interface] [duration-seconds]",
Short: "test client",
Long: `test client`,
Expand Down
23 changes: 11 additions & 12 deletions relayer/chainproxy/chainproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func GetChainProxy(nodeUrl string, nConns uint, sentry *sentry.Sentry, pLogs *Po
}

func VerifyRelayReply(reply *pairingtypes.RelayReply, relayRequest *pairingtypes.RelayRequest, addr string, comparesHashes bool) error {

serverKey, err := sigs.RecoverPubKeyFromRelayReply(reply, relayRequest)
if err != nil {
return err
Expand Down Expand Up @@ -103,14 +102,13 @@ func SendRelay(
connectionType string,
dappID string,
) (*pairingtypes.RelayReply, *pairingtypes.Relayer_RelaySubscribeClient, error) {

// Unmarshal request
nodeMsg, err := cp.ParseMsg(url, []byte(req), connectionType)
if err != nil {
return nil, nil, err
}
isSubscription := nodeMsg.GetServiceApi().Category.Subscription
blockHeight := int64(-1) //to sync reliability blockHeight in case it changes
blockHeight := int64(-1) // to sync reliability blockHeight in case it changes
requestedBlock := int64(0)

// Get Session. we get session here so we can use the epoch in the callbacks
Expand All @@ -121,7 +119,7 @@ func SendRelay(
// consumerSession is locked here.

callback_send_relay := func(consumerSession *lavasession.SingleConsumerSession) (*pairingtypes.RelayReply, *pairingtypes.Relayer_RelaySubscribeClient, *pairingtypes.RelayRequest, time.Duration, error) {
//client session is locked here
// client session is locked here
blockHeight = int64(epoch) // epochs heights only

// we need to apply CuSum and relay number that we plan to add in the relay request. even if we didn't yet apply them to the consumerSession.
Expand Down Expand Up @@ -172,7 +170,7 @@ func SendRelay(
}

if !isSubscription {
//update relay request requestedBlock to the provided one in case it was arbitrary
// update relay request requestedBlock to the provided one in case it was arbitrary
sentry.UpdateRequestedBlock(relayRequest, reply)
finalized := cp.GetSentry().IsFinalizedBlock(relayRequest.RequestBlock, reply.LatestBlock)
err = VerifyRelayReply(reply, relayRequest, providerPublicAddress, cp.GetSentry().GetSpecComparesHashes())
Expand All @@ -189,7 +187,7 @@ func SendRelay(
}

callback_send_reliability := func(consumerSession *lavasession.SingleConsumerSession, dataReliability *pairingtypes.VRFData, providerAddress string) (*pairingtypes.RelayReply, *pairingtypes.RelayRequest, error) {
//client session is locked here
// client session is locked here
sentry := cp.GetSentry()
if blockHeight < 0 {
return nil, nil, fmt.Errorf("expected callback_send_relay to be called first and set blockHeight")
Expand All @@ -199,7 +197,7 @@ func SendRelay(
Provider: providerAddress,
ApiUrl: url,
Data: []byte(req),
SessionId: lavasession.DataReliabilitySessionId, //sessionID for reliability is 0
SessionId: lavasession.DataReliabilitySessionId, // sessionID for reliability is 0
ChainID: sentry.ChainID,
CuSum: lavasession.DataReliabilityCuSum, // consumerSession.CuSum == 0
BlockHeight: blockHeight,
Expand Down Expand Up @@ -289,17 +287,18 @@ func ConstructFiberCallbackWithDappIDExtraction(callbackToBeCalled fiber.Handler
dappID := ""
if len(c.Route().Params) > 1 {
dappID = c.Route().Params[1]
dappID = strings.Replace(dappID, "*", "", -1)
dappID = strings.ReplaceAll(dappID, "*", "")
}
c.Context().SetUserValue(ContextUserValueKeyDappID, dappID) //this sets a user value in context and this is given to the callback
return webSocketCallback(c) //uses external dappID
c.Context().SetUserValue(ContextUserValueKeyDappID, dappID) // this sets a user value in context and this is given to the callback
return webSocketCallback(c) // uses external dappID
}
return handler
}

func ExtractDappIDFromWebsocketConnection(c *websocket.Conn) string {
dappIDLocal := c.Locals(ContextUserValueKeyDappID)
if dappID, ok := dappIDLocal.(string); ok {
//zeroallocation policy for fiber.Ctx
// zeroallocation policy for fiber.Ctx
buffer := make([]byte, len(dappID))
copy(buffer, dappID)
return string(buffer)
Expand All @@ -310,7 +309,7 @@ func ExtractDappIDFromWebsocketConnection(c *websocket.Conn) string {
func ExtractDappIDFromFiberContext(c *fiber.Ctx) (dappID string) {
if len(c.Route().Params) > 1 {
dappID = c.Route().Params[1]
dappID = strings.Replace(dappID, "*", "", -1)
dappID = strings.ReplaceAll(dappID, "*", "")
return
}
return "NoDappID"
Expand Down
4 changes: 1 addition & 3 deletions relayer/chainproxy/chainproxyErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

var (
ErrFailedToConvertMessage = sdkerrors.New("RPC error", 1000, "failed to convert a message")
)
var ErrFailedToConvertMessage = sdkerrors.New("RPC error", 1000, "failed to convert a message")
Loading

0 comments on commit a55f598

Please sign in to comment.