Skip to content

Commit

Permalink
refactor: linting (#1806)
Browse files Browse the repository at this point in the history
make format
  • Loading branch information
mpoke authored Apr 22, 2024
1 parent e266b63 commit 004e025
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 241 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,20 @@ verify-models:
###############################################################################
### Linting ###
###############################################################################

golangci_lint_cmd=golangci-lint
golangci_version=v1.54.1

lint:
@echo "--> Running linter"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
golangci-lint run ./... --config .golangci.yml
@$(golangci_lint_cmd) run ./... --config .golangci.yml

format:
@go install mvdan.cc/gofumpt@latest
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./client/docs/statik/statik.go" -not -path "./tests/mocks/*" -not -name "*.pb.go" -not -name "*.pb.gw.go" -not -name "*.pulsar.go" -not -path "./crypto/keys/secp256k1/*" | xargs gofumpt -w -l
golangci-lint run --fix --config .golangci.yml
$(golangci_lint_cmd) run --fix --config .golangci.yml

.PHONY: format

mockgen_cmd=go run github.com/golang/mock/mockgen
Expand Down
5 changes: 2 additions & 3 deletions app/consumer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"

consumerTypes "github.com/cosmos/interchain-security/v4/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v4/x/ccv/types"
)
Expand Down Expand Up @@ -149,7 +150,6 @@ func transformToV33(jsonRaw []byte, ctx client.Context) ([]byte, error) {
// to a format readable by consumer implementation of version v2.x
// Use removePreHashKey to remove prehash_key_before_comparison from result.
func transformToV2(jsonRaw []byte, ctx client.Context, removePreHashKey bool) (json.RawMessage, error) {

// populate deprecated fields of GenesisState used by version v2.x
srcConGen := consumerTypes.GenesisState{}
err := ctx.Codec.UnmarshalJSON(jsonRaw, &srcConGen)
Expand Down Expand Up @@ -252,7 +252,7 @@ func transformToV2(jsonRaw []byte, ctx client.Context, removePreHashKey bool) (j
// Returns the transformed data or an error in case the transformation failed or the format is not supported by current implementation
func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []byte) (json.RawMessage, error) {
var newConsumerGenesis json.RawMessage = nil
var err error = nil
var err error

switch targetVersion {
// v2.x, v3.0-v3.2 share same consumer genesis type
Expand Down Expand Up @@ -284,7 +284,6 @@ func transformGenesis(ctx client.Context, targetVersion IcsVersion, jsonRaw []by
//
// Result will be written to defined output.
func TransformConsumerGenesis(cmd *cobra.Command, args []string) error {

sourceFile := args[0]
jsonRaw, err := os.ReadFile(filepath.Clean(sourceFile))
if err != nil {
Expand Down
27 changes: 16 additions & 11 deletions app/consumer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"
)

const (
V4x = "v4.x"
V33x = "v3.3.x"
V2x = "v2.x"
)

// Testdata mapping consumer genesis exports to a provider module version as
// used by transformation function for consumer genesis content.
var consumerGenesisStates map[string]string = map[string]string{
Expand Down Expand Up @@ -393,6 +399,7 @@ var consumerGenesisStates map[string]string = map[string]string{
// creates ccv consumer genesis data content for a given version
// as it was exported from a provider
func createConsumerDataGenesisFile(t *testing.T, version string) string {
t.Helper()
filePath := filepath.Join(t.TempDir(), fmt.Sprintf("ConsumerGenesis_%s.json", version))
err := os.WriteFile(
filePath,
Expand Down Expand Up @@ -454,7 +461,7 @@ func transformConsumerGenesis(filePath string, version *string) ([]byte, error)
// Check transformation of a version 2 ConsumerGenesis export to
// consumer genesis json format used by current consumer implementation.
func TestConsumerGenesisTransformationFromV2ToCurrent(t *testing.T) {
version := "v2.x"
version := V2x
ctx := getClientCtx()

srcGenesis := consumerTypes.GenesisState{}
Expand Down Expand Up @@ -502,12 +509,11 @@ func TestConsumerGenesisTransformationFromV2ToCurrent(t *testing.T) {
require.Equal(t, "", resultGenesis.ProviderChannelId)
require.Equal(t, srcGenesis.InitialValSet, resultGenesis.Provider.InitialValSet)
require.Empty(t, resultGenesis.InitialValSet)

}

// Check transformation of provider v3.3.x implementation to consumer V2
func TestConsumerGenesisTransformationV330ToV2(t *testing.T) {
version := "v3.3.x"
version := V33x
filePath := createConsumerDataGenesisFile(t, version)
defer os.Remove(filePath)

Expand All @@ -516,7 +522,7 @@ func TestConsumerGenesisTransformationV330ToV2(t *testing.T) {
err := ctx.Codec.UnmarshalJSON([]byte(consumerGenesisStates[version]), &srcGenesis)
require.NoError(t, err)

targetVersion := "v2.x"
targetVersion := V2x
result, err := transformConsumerGenesis(filePath, &targetVersion)
require.NoError(t, err)

Expand All @@ -530,12 +536,11 @@ func TestConsumerGenesisTransformationV330ToV2(t *testing.T) {
require.Equal(t, srcGenesis.NewChain, resultGenesis.NewChain)
require.Equal(t, "", resultGenesis.ProviderClientId)
require.Equal(t, "", resultGenesis.ProviderChannelId)

}

// Check transformation of provider v3.3.x implementation to current consumer version
func TestConsumerGenesisTransformationV330ToCurrent(t *testing.T) {
version := "v3.3.x"
version := V33x
filePath := createConsumerDataGenesisFile(t, version)
defer os.Remove(filePath)

Expand Down Expand Up @@ -578,7 +583,7 @@ func TestConsumerGenesisTransformationV330ToCurrent(t *testing.T) {

// Check transformation of provider v4.x implementation to consumer V2
func TestConsumerGenesisTransformationV4ToV2(t *testing.T) {
version := "v4.x"
version := V4x
filePath := createConsumerDataGenesisFile(t, version)
defer os.Remove(filePath)

Expand All @@ -587,7 +592,7 @@ func TestConsumerGenesisTransformationV4ToV2(t *testing.T) {
err := ctx.Codec.UnmarshalJSON([]byte(consumerGenesisStates[version]), &srcGenesis)
require.NoError(t, err)

targetVersion := "v2.x"
targetVersion := V2x
result, err := transformConsumerGenesis(filePath, &targetVersion)
require.NoError(t, err)

Expand Down Expand Up @@ -626,7 +631,7 @@ func TestConsumerGenesisTransformationV4ToV2(t *testing.T) {

// Check transformation of provider v3.3.x implementation to consumer V2
func TestConsumerGenesisTransformationV4ToV33(t *testing.T) {
version := "v4.x"
version := V4x
filePath := createConsumerDataGenesisFile(t, version)
defer os.Remove(filePath)

Expand All @@ -635,10 +640,10 @@ func TestConsumerGenesisTransformationV4ToV33(t *testing.T) {
err := ctx.Codec.UnmarshalJSON([]byte(consumerGenesisStates[version]), &srcGenesis)
require.NoError(t, err)

targetVersion := "v3.3.x"
targetVersion := V33x
result, err := transformConsumerGenesis(filePath, &targetVersion)
require.NoError(t, err)
resultGenesis := consumerTypes.GenesisState{} //Only difference to v33 is no RetryDelayPeriod
resultGenesis := consumerTypes.GenesisState{} // Only difference to v33 is no RetryDelayPeriod
err = ctx.Codec.UnmarshalJSON(result, &resultGenesis)
require.NoError(t, err)

Expand Down
1 change: 0 additions & 1 deletion app/sovereign/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import (
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

// add mint
mint "github.com/cosmos/cosmos-sdk/x/mint"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
Expand Down
74 changes: 21 additions & 53 deletions tests/e2e/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ import (
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"
)

const (
done = "done!!!!!!!!"

VLatest = "latest"
V400 = "v4.0.0"
V330 = "v3.3.0"
V300 = "v3.0.0"
)

type SendTokensAction struct {
Chain ChainID
From ValidatorID
To ValidatorID
Amount uint
}

const done = "done!!!!!!!!"

func (tr TestConfig) sendTokens(
action SendTokensAction,
target ExecutionTarget,
Expand Down Expand Up @@ -294,7 +301,6 @@ func (tr TestConfig) submitConsumerAdditionProposal(
bz, err = target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/temp-proposal.json"),
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand All @@ -311,7 +317,6 @@ func (tr TestConfig) submitConsumerAdditionProposal(
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down Expand Up @@ -354,7 +359,6 @@ func (tr TestConfig) submitConsumerRemovalProposal(

bz, err = target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/temp-proposal.json")).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand All @@ -371,7 +375,6 @@ func (tr TestConfig) submitConsumerRemovalProposal(
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down Expand Up @@ -430,7 +433,6 @@ func (tr TestConfig) submitParamChangeProposal(
bz, err = target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/params-proposal.json"),
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down Expand Up @@ -559,20 +561,19 @@ func (tr *TestConfig) getConsumerGenesis(providerChain, consumerChain ChainID, t

// needsGenesisTransform tries to identify if a genesis transformation should be performed
func needsGenesisTransform(cfg TargetConfig) bool {

// no genesis transformation needed for same versions
if cfg.consumerVersion == cfg.providerVersion {
return false
}

// use v4.0.0 (after genesis transform breakages) for the checks if 'latest' is used
consumerVersion := cfg.consumerVersion
if cfg.consumerVersion == "latest" {
consumerVersion = "v4.0.0"
if cfg.consumerVersion == VLatest {
consumerVersion = V400
}
providerVersion := cfg.providerVersion
if cfg.providerVersion == "latest" {
providerVersion = "v4.0.0"
if cfg.providerVersion == VLatest {
providerVersion = V400
}

if !semver.IsValid(consumerVersion) || !semver.IsValid(providerVersion) {
Expand All @@ -581,7 +582,7 @@ func needsGenesisTransform(cfg TargetConfig) bool {
return false
}

breakages := []string{"v3.0.0", "v3.3.0", "v4.0.0"}
breakages := []string{V300, V330, V400}
for _, breakage := range breakages {
if (semver.Compare(consumerVersion, breakage) < 0 && semver.Compare(providerVersion, breakage) >= 0) ||
(semver.Compare(providerVersion, breakage) < 0 && semver.Compare(consumerVersion, breakage) >= 0) {
Expand All @@ -600,7 +601,7 @@ func getTransformParameter(consumerVersion string) (string, error) {
case "":
// For "" (default: local workspace) use HEAD as reference point
consumerVersion = "HEAD"
case "latest":
case VLatest:
// For 'latest' originated from latest-image use "origin/main" as ref point
consumerVersion = "origin/main"
}
Expand Down Expand Up @@ -673,9 +674,9 @@ func (tr *TestConfig) transformConsumerGenesis(consumerChain ChainID, genesis []
panic(fmt.Sprintf("failed writing ccv consumer file : %v", err))
}
defer file.Close()
err = os.WriteFile(file.Name(), genesis, 0600)
err = os.WriteFile(file.Name(), genesis, 0o600)
if err != nil {
log.Fatalf("Failed writing consumer genesis to file: %v", err)
log.Panicf("Failed writing consumer genesis to file: %v", err)
}

containerInstance := tr.containerConfig.InstanceName
Expand All @@ -686,7 +687,7 @@ func (tr *TestConfig) transformConsumerGenesis(consumerChain ChainID, genesis []
fmt.Sprintf("%s:%s", containerInstance, targetFile))
genesis, err = cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "\n", string(genesis))
log.Panic(err, "\n", string(genesis))
}

// check if genesis transform supports --to target
Expand All @@ -697,7 +698,7 @@ func (tr *TestConfig) transformConsumerGenesis(consumerChain ChainID, genesis []
cfg := target.GetTargetConfig()
targetVersion, err := getTransformParameter(cfg.consumerVersion)
if err != nil {
log.Fatal("Failed getting genesis transformation parameter: ", err)
log.Panic("Failed getting genesis transformation parameter: ", err)
}
cmd = target.ExecCommand(
"interchain-security-transformer",
Expand All @@ -710,7 +711,7 @@ func (tr *TestConfig) transformConsumerGenesis(consumerChain ChainID, genesis []

result, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err, "CCV consumer genesis transformation failed: %s", string(result))
log.Panic(err, "CCV consumer genesis transformation failed: %s", string(result))
}
return result
}
Expand Down Expand Up @@ -852,32 +853,6 @@ type AddChainToRelayerAction struct {
IsConsumer bool
}

const hermesChainConfigTemplate = `
[[chains]]
account_prefix = "%s"
clock_drift = "5s"
gas_multiplier = 1.1
grpc_addr = "%s"
id = "%s"
key_name = "%s"
max_gas = 20000000
rpc_addr = "%s"
rpc_timeout = "10s"
store_prefix = "ibc"
trusting_period = "14days"
event_source = { mode = "push", url = "%s", batch_delay = "50ms" }
ccv_consumer_chain = %v
[chains.gas_price]
denom = "stake"
price = 0.000
[chains.trust_threshold]
denominator = "3"
numerator = "1"
`

// Set up the config for a new chain for gorelayer.
// This config is added to the container as a file.
// We then add the chain to the relayer, using this config as the chain config with `rly chains add --file`
Expand Down Expand Up @@ -954,15 +929,11 @@ func (tr TestConfig) addChainToHermes(
target ExecutionTarget,
verbose bool,
) {

bz, err := target.ExecCommand("bash", "-c", "hermes", "version").CombinedOutput()
if err != nil {
log.Fatal(err, "\n error getting hermes version", string(bz))
}
re, err := regexp.Compile(`hermes\s+(\d+.\d+.\d+)`)
if err != nil {
log.Fatal(err, "error identifying hermes version")
}
re := regexp.MustCompile(`hermes\s+(\d+.\d+.\d+)`)
match := re.FindStringSubmatch(string(bz))
if match == nil {
log.Fatalln("error identifying hermes version from", string(bz))
Expand Down Expand Up @@ -999,7 +970,6 @@ func (tr TestConfig) addChainToHermes(
"--chain", string(tr.chainConfigs[action.Chain].ChainId),
"--mnemonic-file", "/root/.hermes/mnemonic.txt",
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down Expand Up @@ -1956,7 +1926,6 @@ func (tr TestConfig) submitChangeRewardDenomsProposal(action SubmitChangeRewardD

bz, err = target.ExecCommand(
"/bin/bash", "-c", fmt.Sprintf(`echo '%s' > %s`, jsonStr, "/change-reward-denoms-proposal.json")).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand All @@ -1972,7 +1941,6 @@ func (tr TestConfig) submitChangeRewardDenomsProposal(action SubmitChangeRewardD
`--keyring-backend`, `test`,
`-y`,
).CombinedOutput()

if err != nil {
log.Fatal(err, "\n", string(bz))
}
Expand Down
Loading

0 comments on commit 004e025

Please sign in to comment.