From c06b2fe66d1da84e0e00e8e2d6c91736bf832b78 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Mon, 13 Jan 2025 16:45:45 +1100 Subject: [PATCH] fix lint --- .../configurer/chain/commands_btcstaking.go | 2 +- wasmbinding/test/custom_query_test.go | 9 +++++++ wasmbinding/test/testutils.go | 4 ++-- x/btcstaking/keeper/btc_delegators.go | 7 +++--- x/btcstaking/keeper/grpc_query.go | 10 ++++---- x/finality/keeper/msg_server_test.go | 2 +- .../keeper/canonical_chain_indexer_test.go | 24 +++++++++---------- x/zoneconcierge/keeper/chain_info_indexer.go | 9 +++---- .../keeper/epoch_chain_info_indexer_test.go | 2 -- x/zoneconcierge/keeper/fork_indexer_test.go | 1 - x/zoneconcierge/keeper/grpc_query_test.go | 4 ---- .../keeper/ibc_packet_btc_timestamp_test.go | 2 +- x/zoneconcierge/module.go | 5 ++-- x/zoneconcierge/types/btc_timestamp.go | 21 +++++++++------- x/zoneconcierge/types/zoneconcierge.go | 16 +++++++------ 15 files changed, 65 insertions(+), 53 deletions(-) diff --git a/test/e2e/configurer/chain/commands_btcstaking.go b/test/e2e/configurer/chain/commands_btcstaking.go index eefe27383..bd7aede2a 100644 --- a/test/e2e/configurer/chain/commands_btcstaking.go +++ b/test/e2e/configurer/chain/commands_btcstaking.go @@ -129,7 +129,7 @@ func (n *NodeConfig) CreateBTCDelegation( // broadcast stuff cmd = append(cmd, "-b=sync", "--yes") } - //cmd = append(cmd, fmt.Sprintf("--chain-id=%s", n.chainId), "-b=sync", "--yes", "--keyring-backend=test", "--log_format=json", "--home=/home/babylon/babylondata") + // cmd = append(cmd, fmt.Sprintf("--chain-id=%s", n.chainId), "-b=sync", "--yes", "--keyring-backend=test", "--log_format=json", "--home=/home/babylon/babylondata") cmd = append(cmd, fmt.Sprintf("--chain-id=%s", n.chainId), "-b=sync", "--yes") outBuff, _, err := n.containerManager.ExecCmd(n.t, n.Name, append(cmd, overallFlags...), "") diff --git a/wasmbinding/test/custom_query_test.go b/wasmbinding/test/custom_query_test.go index 4e52a7428..667b4950b 100644 --- a/wasmbinding/test/custom_query_test.go +++ b/wasmbinding/test/custom_query_test.go @@ -199,10 +199,12 @@ func TestQueryNonExistingHeader(t *testing.T) { require.Nil(t, resp1.HeaderInfo) } +//nolint:unused func setupAppWithContext(t *testing.T) (*app.BabylonApp, sdk.Context) { return setupAppWithContextAndCustomHeight(t, 1) } +//nolint:unused func setupAppWithContextAndCustomHeight(t *testing.T, height int64) (*app.BabylonApp, sdk.Context) { babylonApp := app.Setup(t, false) ctx := babylonApp.BaseApp.NewContext(false). @@ -210,6 +212,7 @@ func setupAppWithContextAndCustomHeight(t *testing.T, height int64) (*app.Babylo return babylonApp, ctx } +//nolint:unused func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) { key := ed25519.GenPrivKey() pub := key.PubKey() @@ -217,11 +220,13 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) { return key, pub, addr } +//nolint:unused func randomAccountAddress() sdk.AccAddress { _, _, addr := keyPubAddr() return addr } +//nolint:unused func mintCoinsTo( bankKeeper bankkeeper.Keeper, ctx sdk.Context, @@ -234,6 +239,7 @@ func mintCoinsTo( return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts) } +//nolint:unused func fundAccount( t *testing.T, ctx sdk.Context, @@ -245,6 +251,7 @@ func fundAccount( require.NoError(t, err) } +//nolint:unused func storeTestCodeCode( t *testing.T, ctx sdk.Context, @@ -261,6 +268,7 @@ func storeTestCodeCode( return id, checksum } +//nolint:unused func instantiateExampleContract( t *testing.T, ctx sdk.Context, @@ -275,6 +283,7 @@ func instantiateExampleContract( return addr } +//nolint:unused func deployTestContract( t *testing.T, ctx sdk.Context, diff --git a/wasmbinding/test/testutils.go b/wasmbinding/test/testutils.go index 47afb0d31..00e574f8b 100644 --- a/wasmbinding/test/testutils.go +++ b/wasmbinding/test/testutils.go @@ -45,8 +45,8 @@ func FundAccount( t *testing.T, ctx sdk.Context, bbn *app.BabylonApp, - acc sdk.AccAddress) { - + acc sdk.AccAddress, +) { err := MintCoinsTo(bbn.BankKeeper, ctx, acc, sdk.NewCoins( sdk.NewCoin("ubbn", math.NewInt(10000000000)), )) diff --git a/x/btcstaking/keeper/btc_delegators.go b/x/btcstaking/keeper/btc_delegators.go index 8e732e411..8429a0fb7 100644 --- a/x/btcstaking/keeper/btc_delegators.go +++ b/x/btcstaking/keeper/btc_delegators.go @@ -75,13 +75,14 @@ func (k Keeper) getBTCDelegatorDelegations(ctx context.Context, fpBTCPK *bbn.BIP func (k Keeper) GetFPBTCDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) ([]*types.BTCDelegation, error) { var store prefix.Store // Determine which store to use based on the finality provider type - if k.HasFinalityProvider(ctx, *fpBTCPK) { + switch { + case k.HasFinalityProvider(ctx, *fpBTCPK): // Babylon finality provider store = k.btcDelegatorFpStore(ctx, fpBTCPK) - } else if k.BscKeeper.HasConsumerFinalityProvider(ctx, fpBTCPK) { + case k.BscKeeper.HasConsumerFinalityProvider(ctx, fpBTCPK): // Consumer finality provider store = k.btcConsumerDelegatorStore(ctx, fpBTCPK) - } else { + default: // if not found in either store, return error return nil, types.ErrFpNotFound } diff --git a/x/btcstaking/keeper/grpc_query.go b/x/btcstaking/keeper/grpc_query.go index 145c4cfb7..00d21eb0c 100644 --- a/x/btcstaking/keeper/grpc_query.go +++ b/x/btcstaking/keeper/grpc_query.go @@ -156,8 +156,8 @@ func (k Keeper) FinalityProviderDelegations(ctx context.Context, req *types.Quer btcDels []*types.BTCDelegatorDelegationsResponse pageRes *query.PageResponse ) - - if k.HasFinalityProvider(ctx, *fpPK) { + switch { + case k.HasFinalityProvider(ctx, *fpPK): // this is a Babylon finality provider btcDelStore := k.btcDelegatorFpStore(sdkCtx, fpPK) pageRes, err = query.Paginate(btcDelStore, req.Pagination, func(key, value []byte) error { @@ -185,13 +185,15 @@ func (k Keeper) FinalityProviderDelegations(ctx context.Context, req *types.Quer if err != nil { return nil, err } - } else if k.BscKeeper.HasConsumerFinalityProvider(ctx, fpPK) { + + case k.BscKeeper.HasConsumerFinalityProvider(ctx, fpPK): // this is a consumer finality provider btcDels, pageRes, err = k.GetBTCConsumerDelegatorDelegationsResponses(sdkCtx, fpPK, req.Pagination, currentWValue, btcHeight, covenantQuorum) if err != nil { return nil, err } - } else { + + default: // the given finality provider is not found return nil, types.ErrFpNotFound } diff --git a/x/finality/keeper/msg_server_test.go b/x/finality/keeper/msg_server_test.go index f2a082781..e5b830945 100644 --- a/x/finality/keeper/msg_server_test.go +++ b/x/finality/keeper/msg_server_test.go @@ -557,7 +557,7 @@ func FuzzEquivocationEvidence(f *testing.F) { require.ErrorContains(t, err, "empty PubRand") // test valid case - blockHeight := activationHeight + uint64(datagen.RandomInt(r, 100)) + blockHeight := activationHeight + datagen.RandomInt(r, 100) // generate proper pub rand data startHeight := blockHeight diff --git a/x/zoneconcierge/keeper/canonical_chain_indexer_test.go b/x/zoneconcierge/keeper/canonical_chain_indexer_test.go index c13a7bed5..45615ca28 100644 --- a/x/zoneconcierge/keeper/canonical_chain_indexer_test.go +++ b/x/zoneconcierge/keeper/canonical_chain_indexer_test.go @@ -9,6 +9,8 @@ import ( "github.com/stretchr/testify/require" ) +const consumerID = "test-consumerID" + func FuzzCanonicalChainIndexer(f *testing.F) { datagen.AddRandomSeedsToFuzzer(f, 10) @@ -18,27 +20,26 @@ func FuzzCanonicalChainIndexer(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - czConsumerId := "test-consumerid" // simulate a random number of blocks numHeaders := datagen.RandomInt(r, 100) + 1 - headers := SimulateNewHeaders(ctx, r, &zcKeeper, czConsumerId, 0, numHeaders) + headers := SimulateNewHeaders(ctx, r, &zcKeeper, consumerID, 0, numHeaders) // check if the canonical chain index is correct or not for i := uint64(0); i < numHeaders; i++ { - header, err := zcKeeper.GetHeader(ctx, czConsumerId, i) + header, err := zcKeeper.GetHeader(ctx, consumerID, i) require.NoError(t, err) require.NotNil(t, header) - require.Equal(t, czConsumerId, header.ConsumerId) + require.Equal(t, consumerID, header.ConsumerId) require.Equal(t, i, header.Height) require.Equal(t, headers[i].Header.AppHash, header.Hash) } // check if the chain info is updated or not - chainInfo, err := zcKeeper.GetChainInfo(ctx, czConsumerId) + chainInfo, err := zcKeeper.GetChainInfo(ctx, consumerID) require.NoError(t, err) require.NotNil(t, chainInfo.LatestHeader) - require.Equal(t, czConsumerId, chainInfo.LatestHeader.ConsumerId) + require.Equal(t, consumerID, chainInfo.LatestHeader.ConsumerId) require.Equal(t, numHeaders-1, chainInfo.LatestHeader.Height) require.Equal(t, headers[numHeaders-1].Header.AppHash, chainInfo.LatestHeader.Hash) }) @@ -53,17 +54,16 @@ func FuzzFindClosestHeader(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - czConsumerId := "test-consumerid" // no header at the moment, FindClosestHeader invocation should give error - _, err := zcKeeper.FindClosestHeader(ctx, czConsumerId, 100) + _, err := zcKeeper.FindClosestHeader(ctx, consumerID, 100) require.Error(t, err) // simulate a random number of blocks numHeaders := datagen.RandomInt(r, 100) + 1 - headers := SimulateNewHeaders(ctx, r, &zcKeeper, czConsumerId, 0, numHeaders) + headers := SimulateNewHeaders(ctx, r, &zcKeeper, consumerID, 0, numHeaders) - header, err := zcKeeper.FindClosestHeader(ctx, czConsumerId, numHeaders) + header, err := zcKeeper.FindClosestHeader(ctx, consumerID, numHeaders) require.NoError(t, err) require.Equal(t, headers[len(headers)-1].Header.AppHash, header.Hash) @@ -72,12 +72,12 @@ func FuzzFindClosestHeader(f *testing.F) { // simulate a random number of blocks // where the new batch of headers has a gap with the previous batch - SimulateNewHeaders(ctx, r, &zcKeeper, czConsumerId, numHeaders+gap+1, numHeaders) + SimulateNewHeaders(ctx, r, &zcKeeper, consumerID, numHeaders+gap+1, numHeaders) // get a random height that is in this gap randomHeightInGap := datagen.RandomInt(r, int(gap+1)) + numHeaders // find the closest header with the given randomHeightInGap - header, err = zcKeeper.FindClosestHeader(ctx, czConsumerId, randomHeightInGap) + header, err = zcKeeper.FindClosestHeader(ctx, consumerID, randomHeightInGap) require.NoError(t, err) // the header should be the same as the last header in the last batch require.Equal(t, headers[len(headers)-1].Header.AppHash, header.Hash) diff --git a/x/zoneconcierge/keeper/chain_info_indexer.go b/x/zoneconcierge/keeper/chain_info_indexer.go index 29deeea92..7c73bb05c 100644 --- a/x/zoneconcierge/keeper/chain_info_indexer.go +++ b/x/zoneconcierge/keeper/chain_info_indexer.go @@ -98,18 +98,19 @@ func (k Keeper) tryToUpdateLatestForkHeader(ctx context.Context, consumerId stri return errorsmod.Wrapf(types.ErrChainInfoNotFound, "cannot insert fork header when chain info is not initialized") } - if len(chainInfo.LatestForks.Headers) == 0 { + switch { + case len(chainInfo.LatestForks.Headers) == 0: // no fork at the moment, add this fork header as the latest one chainInfo.LatestForks.Headers = append(chainInfo.LatestForks.Headers, header) - } else if chainInfo.LatestForks.Headers[0].Height == header.Height { + case chainInfo.LatestForks.Headers[0].Height == header.Height: // there exists fork headers at the same height, add this fork header to the set of latest fork headers chainInfo.LatestForks.Headers = append(chainInfo.LatestForks.Headers, header) - } else if chainInfo.LatestForks.Headers[0].Height < header.Height { + case chainInfo.LatestForks.Headers[0].Height < header.Height: // this fork header is newer than the previous one, replace the old fork headers with this fork header chainInfo.LatestForks = &types.Forks{ Headers: []*types.IndexedHeader{header}, } - } else { + default: // this fork header is older than the current latest fork, don't record this fork header in chain info return nil } diff --git a/x/zoneconcierge/keeper/epoch_chain_info_indexer_test.go b/x/zoneconcierge/keeper/epoch_chain_info_indexer_test.go index 472c76dd5..d42190bc2 100644 --- a/x/zoneconcierge/keeper/epoch_chain_info_indexer_test.go +++ b/x/zoneconcierge/keeper/epoch_chain_info_indexer_test.go @@ -20,7 +20,6 @@ func FuzzEpochChainInfoIndexer(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" hooks := zcKeeper.Hooks() @@ -57,7 +56,6 @@ func FuzzGetEpochHeaders(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" hooks := zcKeeper.Hooks() diff --git a/x/zoneconcierge/keeper/fork_indexer_test.go b/x/zoneconcierge/keeper/fork_indexer_test.go index e1fb2a954..132ecbbc1 100644 --- a/x/zoneconcierge/keeper/fork_indexer_test.go +++ b/x/zoneconcierge/keeper/fork_indexer_test.go @@ -18,7 +18,6 @@ func FuzzForkIndexer(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" // invoke the hook a random number of times to simulate a random number of blocks numHeaders := datagen.RandomInt(r, 100) + 1 diff --git a/x/zoneconcierge/keeper/grpc_query_test.go b/x/zoneconcierge/keeper/grpc_query_test.go index bb59582d4..ab8a7836b 100644 --- a/x/zoneconcierge/keeper/grpc_query_test.go +++ b/x/zoneconcierge/keeper/grpc_query_test.go @@ -121,7 +121,6 @@ func FuzzHeader(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" // invoke the hook a random number of times to simulate a random number of blocks numHeaders := datagen.RandomInt(r, 100) + 2 @@ -214,7 +213,6 @@ func FuzzEpochChainsInfo(f *testing.F) { actualHeight := epochToChainInfo[epochNum][info.ConsumerId].headerStartHeight + (epochToChainInfo[epochNum][info.ConsumerId].numHeaders - 1) require.Equal(t, actualHeight, info.LatestHeader.Height) - } } @@ -253,7 +251,6 @@ func FuzzListHeaders(f *testing.F) { babylonApp := app.Setup(t, false) zcKeeper := babylonApp.ZoneConciergeKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" // invoke the hook a random number of times to simulate a random number of blocks numHeaders := datagen.RandomInt(r, 100) + 1 @@ -287,7 +284,6 @@ func FuzzListEpochHeaders(f *testing.F) { zcKeeper := babylonApp.ZoneConciergeKeeper epochingKeeper := babylonApp.EpochingKeeper ctx := babylonApp.NewContext(false) - consumerID := "test-consumerid" hooks := zcKeeper.Hooks() diff --git a/x/zoneconcierge/keeper/ibc_packet_btc_timestamp_test.go b/x/zoneconcierge/keeper/ibc_packet_btc_timestamp_test.go index f38f3dab1..e4a79cd75 100644 --- a/x/zoneconcierge/keeper/ibc_packet_btc_timestamp_test.go +++ b/x/zoneconcierge/keeper/ibc_packet_btc_timestamp_test.go @@ -25,7 +25,7 @@ func genRandomChain( ctx context.Context, initialHeight uint32, chainLength uint32, -) *datagen.BTCHeaderPartialChain { +) *datagen.BTCHeaderPartialChain { //nolint:unparam // randomChain is used for test setup initHeader := k.GetHeaderByHeight(ctx, initialHeight) randomChain := datagen.NewBTCHeaderChainFromParentInfo( r, diff --git a/x/zoneconcierge/module.go b/x/zoneconcierge/module.go index 5fcc64a16..53c38ed84 100644 --- a/x/zoneconcierge/module.go +++ b/x/zoneconcierge/module.go @@ -2,10 +2,11 @@ package zoneconcierge import ( "context" - "cosmossdk.io/core/appmodule" "encoding/json" "fmt" + "cosmossdk.io/core/appmodule" + "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -74,7 +75,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { - types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck // either we propogate the error up the stack, or don't check here. + types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck // either we propagate the error up the stack, or don't check here. } // GetTxCmd returns the root Tx command for the module. The subcommands of this root command are used by end-users to generate new transactions containing messages defined in the module diff --git a/x/zoneconcierge/types/btc_timestamp.go b/x/zoneconcierge/types/btc_timestamp.go index 7865494e2..dc10fa53a 100644 --- a/x/zoneconcierge/types/btc_timestamp.go +++ b/x/zoneconcierge/types/btc_timestamp.go @@ -80,11 +80,12 @@ func VerifyValSet(epoch *epochingtypes.Epoch, valSet *checkpointingtypes.Validat // - The validator set is committed to the sealer_app_hash of the sealed epoch func VerifyEpochSealed(epoch *epochingtypes.Epoch, rawCkpt *checkpointingtypes.RawCheckpoint, proof *ProofEpochSealed) error { // nil check - if epoch == nil { + switch { + case epoch == nil: return fmt.Errorf("epoch is nil") - } else if rawCkpt == nil { + case rawCkpt == nil: return fmt.Errorf("rawCkpt is nil") - } else if proof == nil { + case proof == nil: return fmt.Errorf("proof is nil") } @@ -151,11 +152,12 @@ func VerifyEpochSealed(epoch *epochingtypes.Epoch, rawCkpt *checkpointingtypes.R func VerifyCZHeaderInEpoch(header *IndexedHeader, epoch *epochingtypes.Epoch, proof *cmtcrypto.ProofOps) error { // nil check - if header == nil { + switch { + case header == nil: return fmt.Errorf("header is nil") - } else if epoch == nil { + case epoch == nil: return fmt.Errorf("epoch is nil") - } else if proof == nil { + case proof == nil: return fmt.Errorf("proof is nil") } @@ -194,11 +196,12 @@ func VerifyCZHeaderInEpoch(header *IndexedHeader, epoch *epochingtypes.Epoch, pr // - the raw ckpt decoded from txsInfo is same as the expected rawCkpt func VerifyEpochSubmitted(rawCkpt *checkpointingtypes.RawCheckpoint, txsInfo []*btcctypes.TransactionInfo, btcHeaders []*wire.BlockHeader, powLimit *big.Int, babylonTag txformat.BabylonTag) error { // basic sanity check - if rawCkpt == nil { + switch { + case rawCkpt == nil: return fmt.Errorf("rawCkpt is nil") - } else if len(txsInfo) != txformat.NumberOfParts { + case len(txsInfo) != txformat.NumberOfParts: return fmt.Errorf("txsInfo contains %d parts rather than %d", len(txsInfo), txformat.NumberOfParts) - } else if len(btcHeaders) != txformat.NumberOfParts { + case len(btcHeaders) != txformat.NumberOfParts: return fmt.Errorf("btcHeaders contains %d parts rather than %d", len(btcHeaders), txformat.NumberOfParts) } diff --git a/x/zoneconcierge/types/zoneconcierge.go b/x/zoneconcierge/types/zoneconcierge.go index 91c970ebe..d59c45e2a 100644 --- a/x/zoneconcierge/types/zoneconcierge.go +++ b/x/zoneconcierge/types/zoneconcierge.go @@ -33,13 +33,14 @@ func VerifyStore(root []byte, moduleStoreKey string, key []byte, value []byte, p } func (p *ProofEpochSealed) ValidateBasic() error { - if p.ValidatorSet == nil { + switch { + case p.ValidatorSet == nil: return ErrInvalidProofEpochSealed.Wrap("ValidatorSet is nil") - } else if len(p.ValidatorSet) == 0 { + case len(p.ValidatorSet) == 0: return ErrInvalidProofEpochSealed.Wrap("ValidatorSet is empty") - } else if p.ProofEpochInfo == nil { + case p.ProofEpochInfo == nil: return ErrInvalidProofEpochSealed.Wrap("ProofEpochInfo is nil") - } else if p.ProofEpochValSet == nil { + case p.ProofEpochValSet == nil: return ErrInvalidProofEpochSealed.Wrap("ProofEpochValSet is nil") } return nil @@ -110,11 +111,12 @@ func (ci *ChainInfo) Equal(ci2 *ChainInfo) bool { } func (ci *ChainInfo) ValidateBasic() error { - if len(ci.ConsumerId) == 0 { + switch { + case len(ci.ConsumerId) == 0: return ErrInvalidChainInfo.Wrap("ConsumerId is empty") - } else if ci.LatestHeader == nil { + case ci.LatestHeader == nil: return ErrInvalidChainInfo.Wrap("LatestHeader is nil") - } else if ci.LatestForks == nil { + case ci.LatestForks == nil: return ErrInvalidChainInfo.Wrap("LatestForks is nil") } if err := ci.LatestHeader.ValidateBasic(); err != nil {