Skip to content

Commit

Permalink
Merge pull request #703 from cybercongress/lint-format
Browse files Browse the repository at this point in the history
Lint and format
  • Loading branch information
cyborgshead authored Jan 16, 2024
2 parents d2de6cc + 120e8ca commit ecda66c
Show file tree
Hide file tree
Showing 177 changed files with 2,459 additions and 1,837 deletions.
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

0 comments on commit ecda66c

Please sign in to comment.