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

Lint and format #703

Merged
merged 2 commits into from
Jan 16, 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
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 10m
tests: true

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

linters-settings:
gosec:
excludes:
- G404
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ run:
### Tools / Dependencies ###
###############################################################################

format-tools:
go install mvdan.cc/gofumpt@v0.4.0
go install github.com/client9/misspell/cmd/misspell@v0.3.4
go install golang.org/x/tools/cmd/goimports@latest

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download
Expand All @@ -112,21 +117,20 @@ go.sum: go.mod
go mod tidy -compat=1.17
.PHONY: go.sum

lint:
$(BINDIR)/golangci-lint run
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" | xargs gofmt -d -s
go mod verify
lint: format-tools
golangci-lint run --tests=false
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*_test.go" | xargs gofumpt -d
.PHONY: lint

statik:
$(GO) get -u github.com/rakyll/statik
$(GO) generate ./api/...
.PHONY: statik

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" | xargs goimports -w -local github.com/cybercongress/go-cyber
format: format-tools
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/docs/statik/statik.go" | xargs gofumpt -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/docs/statik/statik.go" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/docs/statik/statik.go" | xargs goimports -w -local github.com/cybercongress/go-cyber
.PHONY: format

###############################################################################
Expand Down
49 changes: 23 additions & 26 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package app

import (
"fmt"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

Expand All @@ -18,10 +18,10 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
"github.com/cosmos/ibc-go/v3/modules/core/keeper"

bandwidthkeeper "github.com/cybercongress/go-cyber/x/bandwidth/keeper"
bandwidthtypes "github.com/cybercongress/go-cyber/x/bandwidth/types"
graphtypes "github.com/cybercongress/go-cyber/x/graph/types"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
Expand All @@ -31,16 +31,16 @@ type HandlerBaseOptions struct {
FeegrantKeeper ante.FeegrantKeeper
BandwidthMeter *bandwidthkeeper.BandwidthMeter
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
}

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
HandlerBaseOptions

IBCKeeper *keeper.Keeper
WasmConfig *wasmTypes.WasmConfig
IBCKeeper *keeper.Keeper
WasmConfig *wasmtypes.WasmConfig
TXCounterStoreKey sdk.StoreKey
}

Expand All @@ -64,7 +64,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
}

var sigGasConsumer = options.SigGasConsumer
sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}
Expand Down Expand Up @@ -94,11 +94,10 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
}

type DeductFeeBandDecorator struct {
ak ante.AccountKeeper
bankKeeper bankkeeper.Keeper
bandMeter *bandwidthkeeper.BandwidthMeter
feegrantKeeper ante.FeegrantKeeper

ak ante.AccountKeeper
bankKeeper bankkeeper.Keeper
bandMeter *bandwidthkeeper.BandwidthMeter
feegrantKeeper ante.FeegrantKeeper
}

func NewDeductFeeBandRouterDecorator(
Expand All @@ -116,7 +115,6 @@ func NewDeductFeeBandRouterDecorator(
}

func (drd DeductFeeBandDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {

feeFlag := false
feeSplitFlag := false
bandwidthFlag := false
Expand Down Expand Up @@ -152,8 +150,8 @@ func (drd DeductFeeBandDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}

if addr := drd.ak.GetModuleAddress(types.FeeCollectorName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.FeeCollectorName))
if addr := drd.ak.GetModuleAddress(authtypes.FeeCollectorName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", authtypes.FeeCollectorName))
}

fee := feeTx.GetFee()
Expand All @@ -168,7 +166,6 @@ func (drd DeductFeeBandDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "fee grants are not enabled")
} else if !feeGranter.Equals(feePayer) {
err := drd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, fee, tx.GetMsgs())

if err != nil {
return ctx, sdkerrors.Wrapf(err, "%s not allowed to pay fees from %s", feeGranter, feePayer)
}
Expand Down Expand Up @@ -220,19 +217,19 @@ func (drd DeductFeeBandDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
currentBlockSpentBandwidth := drd.bandMeter.GetCurrentBlockSpentBandwidth(ctx)
maxBlockBandwidth := drd.bandMeter.GetMaxBlockBandwidth(ctx)

//nolint:gocritic
if !simulate {
if !accountBandwidth.HasEnoughRemained(txCost) {
return ctx, bandwidthtypes.ErrNotEnoughBandwidth
} else if (txCost + currentBlockSpentBandwidth) > maxBlockBandwidth {
} else if (txCost + currentBlockSpentBandwidth) > maxBlockBandwidth {
return ctx, bandwidthtypes.ErrExceededMaxBlockBandwidth
} else {
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
err = drd.bandMeter.ConsumeAccountBandwidth(ctx, accountBandwidth, txCost); if err != nil {
return ctx, err
}
// TODO think to add to transient store
drd.bandMeter.AddToBlockBandwidth(txCost)
} else if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
err = drd.bandMeter.ConsumeAccountBandwidth(ctx, accountBandwidth, txCost)
if err != nil {
return ctx, err
}
// TODO think to add to transient store
drd.bandMeter.AddToBlockBandwidth(txCost)
}
}
return next(ctx, tx, simulate)
Expand All @@ -248,13 +245,13 @@ func DeductFees(bankKeeper bankkeeper.Keeper, ctx sdk.Context, acc authtypes.Acc
}

if program2pay == nil {
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, fees)
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authtypes.FeeCollectorName, fees)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
} else {
feeInCYB := sdk.NewDec(fees.AmountOf(ctypes.CYB).Int64())
toProgram := feeInCYB.Mul(sdk.NewDecWithPrec(80,2))
toProgram := feeInCYB.Mul(sdk.NewDecWithPrec(80, 2))
toValidators := feeInCYB.Sub(toProgram)

toValidatorsAmount := sdk.NewCoins(sdk.NewCoin(ctypes.CYB, toValidators.RoundInt()))
Expand All @@ -265,7 +262,7 @@ func DeductFees(bankKeeper bankkeeper.Keeper, ctx sdk.Context, acc authtypes.Acc
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}

err = bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), types.FeeCollectorName, toValidatorsAmount)
err = bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authtypes.FeeCollectorName, toValidatorsAmount)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
Expand Down
Loading