From 55b70657448b81e9b3621f4886ab152fe82231a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Mon, 26 Feb 2024 18:06:43 +0100 Subject: [PATCH] Eckelj/remove the smell (#326) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * removed some more code smell/error message aggregation * removed obsolete/duplicate tests/code * excluded TODOs (simulation) from being processed by sonarqube * removed TODO code Signed-off-by: Jürgen Eckel --------- Signed-off-by: Jürgen Eckel --- sonar-project.properties | 2 +- tests/e2e/dao/asset_distribution_suite.go | 8 ++++---- testutil/e2e/e2e.go | 6 +++--- testutil/sample/sample.go | 20 ------------------- util/issue_commands.go | 1 - util/mocks/mqtt.go | 1 + x/asset/keeper/msg_server_test.go | 3 ++- x/asset/types/message_notarize_asset_test.go | 8 +++++++- x/dao/keeper/distribution.go | 19 ------------------ x/dao/types/message_init_pop_test.go | 4 ++-- x/dao/types/message_mint_token_test.go | 2 +- x/dao/types/message_update_params_test.go | 2 +- x/dao/types/messages_redeem_claim_test.go | 6 +++--- x/machine/keeper/trust_anchor_test.go | 18 ----------------- .../types/message_attest_machine_test.go | 2 +- .../message_register_trust_anchor_test.go | 2 +- x/machine/types/message_update_params_test.go | 2 +- 17 files changed, 28 insertions(+), 78 deletions(-) diff --git a/sonar-project.properties b/sonar-project.properties index ff38c99..62cbf04 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,2 +1,2 @@ sonar.projectKey=planetmint_planetmint-go_AYjnSLNdwwdSy162QoXI -sonar.exclusions=docs/static/openapi.yml,x/**/*.pb.go,x/**/*.pb.gw.go,x/**/module.go,x/**/types/genesis_test.go,x/**/types/params.go,x/**/client/cli/query_params.go,tests/e2e/**/suite.go,app/simulation_test.go,x/**/client/cli/tx_update_params.go,x/**/types/message_update_params_test.go,x/**/keeper/message_server_update_params.go +sonar.exclusions=docs/static/openapi.yml,x/**/*.pb.go,x/**/*.pb.gw.go,x/**/module.go,x/**/types/genesis_test.go,x/**/types/params.go,x/**/client/cli/query_params.go,tests/e2e/**/suite.go,app/simulation_test.go,x/**/client/cli/tx_update_params.go,x/**/types/message_update_params_test.go,x/**/keeper/message_server_update_params.go,x/**/types/message*test.go,x/**/module_simulation.go,x/**/simulation/*.go diff --git a/tests/e2e/dao/asset_distribution_suite.go b/tests/e2e/dao/asset_distribution_suite.go index 165c7db..3aad073 100644 --- a/tests/e2e/dao/asset_distribution_suite.go +++ b/tests/e2e/dao/asset_distribution_suite.go @@ -67,7 +67,7 @@ func (s *AssetDistributionE2ETestSuite) TestAssetDistribution() { break } } - + errmsg := "rpc error: code = NotFound desc = distribution not found: key not found" testCases := []struct { name string requestHeight int64 @@ -76,17 +76,17 @@ func (s *AssetDistributionE2ETestSuite) TestAssetDistribution() { { "request height too low", s.distributionOffset, - "rpc error: code = NotFound desc = distribution not found: key not found", + errmsg, }, { "wrong request height", height, - "rpc error: code = NotFound desc = distribution not found: key not found", + errmsg, }, { "request height too high", 2*s.reissaunceEpochs + s.distributionOffset, - "rpc error: code = NotFound desc = distribution not found: key not found", + errmsg, }, { "valid distribution request", diff --git a/testutil/e2e/e2e.go b/testutil/e2e/e2e.go index c914a2b..92cb05a 100644 --- a/testutil/e2e/e2e.go +++ b/testutil/e2e/e2e.go @@ -10,7 +10,7 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/planetmint/planetmint-go/lib" clitestutil "github.com/planetmint/planetmint-go/testutil/cli" - moduleobjects "github.com/planetmint/planetmint-go/testutil/moduleobject" + "github.com/planetmint/planetmint-go/testutil/moduleobject" "github.com/planetmint/planetmint-go/testutil/network" "github.com/planetmint/planetmint-go/testutil/sample" machinetypes "github.com/planetmint/planetmint-go/x/machine/types" @@ -76,7 +76,7 @@ func AttestMachine(network *network.Network, name string, mnemonic string, num i // register Ta prvKey, pubKey := sample.KeyPair(num) - ta := moduleobjects.TrustAnchor(pubKey) + ta := moduleobject.TrustAnchor(pubKey) registerMsg := machinetypes.NewMsgRegisterTrustAnchor(val.Address.String(), &ta) _, err = lib.BroadcastTxWithFileLock(val.Address, registerMsg) if err != nil { @@ -92,7 +92,7 @@ func AttestMachine(network *network.Network, name string, mnemonic string, num i return err } - machine := moduleobjects.Machine(name, pubKey, prvKey, addr.String()) + machine := moduleobject.Machine(name, pubKey, prvKey, addr.String()) attestMsg := machinetypes.NewMsgAttestMachine(addr.String(), &machine) _, err = lib.BroadcastTxWithFileLock(addr, attestMsg) if err != nil { diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index 0bbf381..d2c26fe 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -4,13 +4,9 @@ import ( "encoding/hex" "fmt" - "github.com/btcsuite/btcd/btcutil/hdkeychain" - "github.com/btcsuite/btcd/chaincfg" - "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/go-bip39" ) // Mnemonic sample mnemonic to use in tests @@ -60,19 +56,3 @@ func Asset() string { cid := "cid0" return cid } - -func ExtendedKeyPair(cfg chaincfg.Params) (string, string) { - seed, err := bip39.NewSeedWithErrorChecking(Mnemonic, keyring.DefaultBIP39Passphrase) - if err != nil { - panic(err) - } - xprivKey, err := hdkeychain.NewMaster(seed, &cfg) - if err != nil { - panic(err) - } - xpubKey, err := xprivKey.Neuter() - if err != nil { - panic(err) - } - return xprivKey.String(), xpubKey.String() -} diff --git a/util/issue_commands.go b/util/issue_commands.go index 3fe51ee..a8147b8 100644 --- a/util/issue_commands.go +++ b/util/issue_commands.go @@ -48,7 +48,6 @@ func buildSignBroadcastTx(goCtx context.Context, loggingContext string, sendingV func SendInitReissuance(goCtx context.Context, proposerAddress string, txUnsigned string, blockHeight int64, firstIncludedPop int64, lastIncludedPop int64) { - // get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store sendingValidatorAddress := config.GetConfig().ValidatorAddress msg := daotypes.NewMsgReissueRDDLProposal(sendingValidatorAddress, proposerAddress, txUnsigned, blockHeight, firstIncludedPop, lastIncludedPop) diff --git a/util/mocks/mqtt.go b/util/mocks/mqtt.go index a4ae06e..e6fcba3 100644 --- a/util/mocks/mqtt.go +++ b/util/mocks/mqtt.go @@ -24,6 +24,7 @@ func GetConnectFunc() mqtt.Token { // GetDisconnectFunc fetches the mock client's `Disconnect` func func GetDisconnectFunc(_ uint) { + // not implemented at this point in time } // GetPublishFunc fetches the mock client's `Publish` func diff --git a/x/asset/keeper/msg_server_test.go b/x/asset/keeper/msg_server_test.go index 4e70f70..4fcb0b2 100644 --- a/x/asset/keeper/msg_server_test.go +++ b/x/asset/keeper/msg_server_test.go @@ -7,6 +7,7 @@ import ( "github.com/planetmint/planetmint-go/config" keepertest "github.com/planetmint/planetmint-go/testutil/keeper" + "github.com/planetmint/planetmint-go/testutil/moduleobject" "github.com/planetmint/planetmint-go/testutil/sample" "github.com/planetmint/planetmint-go/x/asset/keeper" "github.com/planetmint/planetmint-go/x/asset/types" @@ -31,7 +32,7 @@ func TestMsgServer(t *testing.T) { func TestMsgServerNotarizeAsset(t *testing.T) { t.Parallel() - extSk, _ := sample.ExtendedKeyPair(config.PlmntNetParams) + extSk, _ := moduleobject.ExtendedKeyPair(config.PlmntNetParams) xskKey, _ := hdkeychain.NewKeyFromString(extSk) privKey, _ := xskKey.ECPrivKey() byteKey := privKey.Serialize() diff --git a/x/asset/types/message_notarize_asset_test.go b/x/asset/types/message_notarize_asset_test.go index cf8e7ae..db5ac1e 100644 --- a/x/asset/types/message_notarize_asset_test.go +++ b/x/asset/types/message_notarize_asset_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/planetmint/planetmint-go/testutil/errormsg" + "github.com/planetmint/planetmint-go/testutil/sample" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" @@ -17,11 +18,16 @@ func TestMsgNotarizeAssetValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgNotarizeAsset{ Creator: errormsg.ErrorInvalidAddress, }, err: sdkerrors.ErrInvalidAddress, + }, { + name: "valid address", + msg: MsgNotarizeAsset{ + Creator: sample.AccAddress(), + }, }, } for _, tt := range tests { diff --git a/x/dao/keeper/distribution.go b/x/dao/keeper/distribution.go index 4bfc8f2..b21ac79 100644 --- a/x/dao/keeper/distribution.go +++ b/x/dao/keeper/distribution.go @@ -38,25 +38,6 @@ func (k Keeper) GetLastDistributionOrder(ctx sdk.Context) (val types.Distributio return val, found } -// TODO to be integrated at a later stage -// func (k Keeper) getDistributionRequestPage(ctx sdk.Context, key []byte, offset uint64, page_size uint64, all bool, reverse bool) (distribution_orders []types.DistributionOrder) { -// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DistributionKey)) - -// iterator := store.Iterator(nil, nil) -// defer iterator.Close() -// if reverse { -// iterator = store.ReverseIterator(nil, nil) -// defer iterator.Close() -// } -// for ; iterator.Valid(); iterator.Next() { -// distribution_order := iterator.Value() -// var distribution_order_org types.DistributionOrder -// k.cdc.MustUnmarshal(distribution_order, &distribution_order_org) -// distribution_orders = append(distribution_orders, distribution_order_org) -// } -// return distribution_orders -// } - func (k Keeper) ComputeDistribution(ctx sdk.Context, lastReissuance int64, blockHeight int64, amount uint64) (distribution types.DistributionOrder) { distribution.FirstPop = lastReissuance distribution.LastPop = blockHeight diff --git a/x/dao/types/message_init_pop_test.go b/x/dao/types/message_init_pop_test.go index 1b1119f..59ac7aa 100644 --- a/x/dao/types/message_init_pop_test.go +++ b/x/dao/types/message_init_pop_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestMsgInitPop_ValidateBasic(t *testing.T) { +func TestMsgInitPopValidateBasic(t *testing.T) { t.Parallel() tests := []struct { name string @@ -16,7 +16,7 @@ func TestMsgInitPop_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgInitPop{ Creator: errormsg.ErrorInvalidAddress, }, diff --git a/x/dao/types/message_mint_token_test.go b/x/dao/types/message_mint_token_test.go index 436f3f1..0be4f1a 100644 --- a/x/dao/types/message_mint_token_test.go +++ b/x/dao/types/message_mint_token_test.go @@ -16,7 +16,7 @@ func TestMsgMintToken_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgMintToken{ Creator: errormsg.ErrorInvalidAddress, }, diff --git a/x/dao/types/message_update_params_test.go b/x/dao/types/message_update_params_test.go index 90f4217..cb2c2e5 100644 --- a/x/dao/types/message_update_params_test.go +++ b/x/dao/types/message_update_params_test.go @@ -16,7 +16,7 @@ func TestMsgUpdateParams_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgUpdateParams{ Authority: errormsg.ErrorInvalidAddress, }, diff --git a/x/dao/types/messages_redeem_claim_test.go b/x/dao/types/messages_redeem_claim_test.go index 88f0dab..a43f13d 100644 --- a/x/dao/types/messages_redeem_claim_test.go +++ b/x/dao/types/messages_redeem_claim_test.go @@ -16,7 +16,7 @@ func TestMsgCreateRedeemClaim_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgCreateRedeemClaim{ Creator: errormsg.ErrorInvalidAddress, }, @@ -45,7 +45,7 @@ func TestMsgUpdateRedeemClaim_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgUpdateRedeemClaim{ Creator: errormsg.ErrorInvalidAddress, }, @@ -74,7 +74,7 @@ func TestMsgConfirmRedeemClaim_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgConfirmRedeemClaim{ Creator: errormsg.ErrorInvalidAddress, }, diff --git a/x/machine/keeper/trust_anchor_test.go b/x/machine/keeper/trust_anchor_test.go index a162144..6962bba 100644 --- a/x/machine/keeper/trust_anchor_test.go +++ b/x/machine/keeper/trust_anchor_test.go @@ -69,21 +69,3 @@ func TestUpdateTrustAnchor(t *testing.T) { assert.True(t, activated) } } - -func TestUpdateTrustAnchorInvalidPubKey(t *testing.T) { - t.Parallel() - keeper, ctx := keepertest.MachineKeeper(t) - items := createNTrustAnchor(t, keeper, ctx, 10) - for _, item := range items { - ta, activated, _ := keeper.GetTrustAnchor(ctx, item.Pubkey) - if !activated { - err := keeper.StoreTrustAnchor(ctx, ta, true) - assert.False(t, (err != nil)) - } - } - - for _, item := range items { - _, activated, _ := keeper.GetTrustAnchor(ctx, item.Pubkey) - assert.True(t, activated) - } -} diff --git a/x/machine/types/message_attest_machine_test.go b/x/machine/types/message_attest_machine_test.go index 3eb2f51..66a2427 100644 --- a/x/machine/types/message_attest_machine_test.go +++ b/x/machine/types/message_attest_machine_test.go @@ -16,7 +16,7 @@ func TestMsgAttestMachineValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgAttestMachine{ Creator: errormsg.ErrorInvalidAddress, }, diff --git a/x/machine/types/message_register_trust_anchor_test.go b/x/machine/types/message_register_trust_anchor_test.go index f348711..7ed28df 100644 --- a/x/machine/types/message_register_trust_anchor_test.go +++ b/x/machine/types/message_register_trust_anchor_test.go @@ -16,7 +16,7 @@ func TestMsgRegisterTrustAnchor_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgRegisterTrustAnchor{ Creator: errormsg.ErrorInvalidAddress, }, diff --git a/x/machine/types/message_update_params_test.go b/x/machine/types/message_update_params_test.go index 90f4217..cb2c2e5 100644 --- a/x/machine/types/message_update_params_test.go +++ b/x/machine/types/message_update_params_test.go @@ -16,7 +16,7 @@ func TestMsgUpdateParams_ValidateBasic(t *testing.T) { err error }{ { - name: "invalid address", + name: sdkerrors.ErrInvalidAddress.Error(), msg: MsgUpdateParams{ Authority: errormsg.ErrorInvalidAddress, },