Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Faddat/lint v5 (#67)
Browse files Browse the repository at this point in the history
* lint v5

* still need to fix errors

* remove uses of deprecated

* go.mod

* miss one left

* Wrap NOT Wrapf

---------

Co-authored-by: hoangdv2429 <hoangdv2429@gmail.com>
  • Loading branch information
faddat and hoangdv2429 authored Apr 12, 2023
1 parent bb0f53e commit e10c580
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest
66 changes: 66 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
run:
tests: true
timeout: 10m
sort-results: true
allow-parallel-runners: true
exclude-dir: testutil/testdata_pulsar
concurrency: 4

linters:
disable-all: true
enable:
- depguard
- dogsled
- exportloopref
- goconst
- gocritic
- gofumpt
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- revive
- stylecheck
- typecheck
- unconvert
- unused

issues:
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec
- 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
- text: "leading space"
linters:
- nolintlint

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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ go 1.18
module github.com/strangelove-ventures/packet-forward-middleware/v5

require (
cosmossdk.io/errors v1.0.0-beta.7
github.com/armon/go-metrics v0.4.0
github.com/cosmos/cosmos-sdk v0.46.4
github.com/cosmos/ibc-go/v5 v5.1.0
Expand All @@ -21,7 +22,6 @@ require (
)

require (
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/math v1.0.0-beta.3 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
3 changes: 2 additions & 1 deletion router/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -253,7 +254,7 @@ func (im IBCMiddleware) OnAcknowledgementPacket(

var ack channeltypes.Acknowledgement
if err := channeltypes.SubModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
}

inFlightPacket := im.keeper.GetAndClearInFlightPacket(ctx, packet.SourceChannel, packet.SourcePort, packet.Sequence)
Expand Down
2 changes: 2 additions & 0 deletions router/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
// Initialize store refund path for forwarded packets in genesis state that have not yet been acked.
store := ctx.KVStore(k.storeKey)
for key, value := range state.InFlightPackets {
key := key
value := value
bz := k.cdc.MustMarshal(&value)
store.Set([]byte(key), bz)
}
Expand Down
11 changes: 6 additions & 5 deletions router/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -101,7 +102,7 @@ func (k *Keeper) WriteAcknowledgementForForwardedPacket(
// Lookup module by channel capability
_, cap, err := k.channelKeeper.LookupModuleByChannel(ctx, inFlightPacket.RefundPortId, inFlightPacket.RefundChannelId)
if err != nil {
return sdkerrors.Wrap(err, "could not retrieve module from port-id")
return errorsmod.Wrap(err, "could not retrieve module from port-id")
}

// for forwarded packets, the funds were moved into an escrow account if the denom originated on this chain.
Expand Down Expand Up @@ -224,7 +225,7 @@ func (k *Keeper) ForwardTransferPacket(
k.Logger(ctx).Error("packetForwardMiddleware error funding community pool",
"error", err,
)
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
}

Expand All @@ -245,7 +246,7 @@ func (k *Keeper) ForwardTransferPacket(
k.Logger(ctx).Error("packetForwardMiddleware error marshaling next as JSON",
"error", err,
)
return sdkerrors.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
return errorsmod.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
}
msgTransfer.Memo = string(memoBz)
}
Expand All @@ -268,7 +269,7 @@ func (k *Keeper) ForwardTransferPacket(
"amount", packetCoin.Amount.String(), "denom", packetCoin.Denom,
"error", err,
)
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}

// Store the following information in keeper:
Expand Down Expand Up @@ -381,7 +382,7 @@ func (k *Keeper) RetryTimeout(

denom := transfertypes.ParseDenomTrace(data.Denom).IBCDenom()

var token = sdk.NewCoin(denom, amount)
token := sdk.NewCoin(denom, amount)

// srcPacket and srcPacketSender are empty because inFlightPacket is non-nil.
return k.ForwardTransferPacket(
Expand Down
13 changes: 6 additions & 7 deletions router/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
apptypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestOnRecvPacket_ForwardNoFee(t *testing.T) {
keeper.DefaultTransferPacketTimeoutHeight,
uint64(ctx.BlockTime().UnixNano())+uint64(keeper.DefaultForwardTransferPacketTimeoutTimestamp.Nanoseconds()),
),
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr).
Return(nil),
Expand Down Expand Up @@ -285,7 +284,7 @@ func TestOnRecvPacket_ForwardWithFee(t *testing.T) {
keeper.DefaultTransferPacketTimeoutHeight,
uint64(ctx.BlockTime().UnixNano())+uint64(keeper.DefaultForwardTransferPacketTimeoutTimestamp.Nanoseconds()),
),
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr).
Return(nil),
Expand Down Expand Up @@ -380,15 +379,15 @@ func TestOnRecvPacket_ForwardMultihopStringNext(t *testing.T) {
setup.Mocks.TransferKeeperMock.EXPECT().Transfer(
sdk.WrapSDKContext(ctx),
msgTransfer1,
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnRecvPacket(ctx, packet2, senderAccAddr2).
Return(acknowledgement),

setup.Mocks.TransferKeeperMock.EXPECT().Transfer(
sdk.WrapSDKContext(ctx),
msgTransfer2,
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr2).
Return(nil),
Expand Down Expand Up @@ -497,15 +496,15 @@ func TestOnRecvPacket_ForwardMultihopJSONNext(t *testing.T) {
setup.Mocks.TransferKeeperMock.EXPECT().Transfer(
sdk.WrapSDKContext(ctx),
msgTransfer1,
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnRecvPacket(ctx, packet2, senderAccAddr2).
Return(acknowledgement),

setup.Mocks.TransferKeeperMock.EXPECT().Transfer(
sdk.WrapSDKContext(ctx),
msgTransfer2,
).Return(&apptypes.MsgTransferResponse{Sequence: 0}, nil),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr2).
Return(nil),
Expand Down
8 changes: 5 additions & 3 deletions router/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const (
QuerierRoute = ModuleName
)

type NonrefundableKey struct{}
type DisableDenomCompositionKey struct{}
type ProcessedKey struct{}
type (
NonrefundableKey struct{}
DisableDenomCompositionKey struct{}
ProcessedKey struct{}
)

func RefundPacketKey(channelID, portID string, sequence uint64) []byte {
return []byte(fmt.Sprintf("%s/%s/%d", channelID, portID, sequence))
Expand Down
1 change: 0 additions & 1 deletion router/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func validateFeePercentage(i interface{}) error {
}
if v.IsNegative() {
return fmt.Errorf("invalid fee percentage. expected not negative, got %d", v.RoundInt64())

}
if !(v.LTE(sdk.OneDec())) {
return fmt.Errorf("invalid fee percentage. expected less than one 1 got %d", v.RoundInt64())
Expand Down
7 changes: 3 additions & 4 deletions test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
tmdb "github.com/tendermint/tm-db"
)

func NewTestSetup(t *testing.T, ctl *gomock.Controller) *TestSetup {
func NewTestSetup(t *testing.T, ctl *gomock.Controller) *Setup {
initializer := newInitializer()

transferKeeperMock := mock.NewMockTransferKeeper(ctl)
Expand All @@ -35,13 +35,12 @@ func NewTestSetup(t *testing.T, ctl *gomock.Controller) *TestSetup {

paramsKeeper := initializer.paramsKeeper()
routerKeeper := initializer.routerKeeper(paramsKeeper, transferKeeperMock, channelKeeperMock, distributionKeeperMock, bankKeeperMock, ics4WrapperMock)
//routerModule := initializer.routerModule(routerKeeper)

require.NoError(t, initializer.StateStore.LoadLatestVersion())

routerKeeper.SetParams(initializer.Ctx, types.DefaultParams())

return &TestSetup{
return &Setup{
Initializer: initializer,

Keepers: &testKeepers{
Expand All @@ -61,7 +60,7 @@ func NewTestSetup(t *testing.T, ctl *gomock.Controller) *TestSetup {
}
}

type TestSetup struct {
type Setup struct {
Initializer initializer

Keepers *testKeepers
Expand Down

0 comments on commit e10c580

Please sign in to comment.