Skip to content

Commit

Permalink
fix: lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Nov 21, 2024
1 parent 41b4906 commit 8d907eb
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ linters:
- misspell
- nakedret
- nilerr
# - nlreturn # Style wise I personally like this one, todo(lazar): unlax at somepoint, good practice
# - nlreturn # Style wise I personally like this one, todo(lazar): unlax at somepoint, good practice
- noctx
- nonamedreturns
# - nonamedreturns # Named returns in functions help understand what they do
- nosprintfhostport
- paralleltest
- reassign
Expand Down
2 changes: 1 addition & 1 deletion x/btccheckpoint/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
func GetQueryCmd() *cobra.Command {
// Group btccheckpoint queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Expand Down
2 changes: 2 additions & 0 deletions x/btccheckpoint/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestExportGenesis(t *testing.T) {
t.Parallel()
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false)

Expand All @@ -23,6 +24,7 @@ func TestExportGenesis(t *testing.T) {
}

func TestInitGenesis(t *testing.T) {
t.Parallel()
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false)

Expand Down
3 changes: 2 additions & 1 deletion x/btccheckpoint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

func TestBtcCheckpointInfo(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
raw, btcRaw := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand All @@ -40,7 +41,7 @@ func TestBtcCheckpointInfo(t *testing.T) {

// gather info for verifying with response.
btcInfo := resp.Info
blkHeight, err := tk.BTCLightClient.BlockHeight(tk.Ctx, nil)
blkHeight, err := tk.BTCLightClient.BlockHeight(tk.SdkCtx, nil)
require.NoErrorf(t, err, "Unexpected message processing error: %v", err)

rawSubmission, err := types.ParseSubmission(msg, tk.BTCCheckpoint.GetPowLimit(), tk.BTCCheckpoint.GetExpectedTag(tk.SdkCtx))
Expand Down
4 changes: 3 additions & 1 deletion x/btccheckpoint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestKeeper_GetSubmissionBtcInfo(t *testing.T) {
t.Parallel()
type TxKeyDesc struct {
TxIdx uint32
Depth uint32
Expand All @@ -37,6 +38,7 @@ func TestKeeper_GetSubmissionBtcInfo(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))

k := InitTestKeepers(t)
Expand Down Expand Up @@ -77,7 +79,7 @@ func FuzzGetSubmissionBtcInfo(f *testing.F) {
if txidx1 == txidx2 {
// transaction indexes must be different to cover the case where transactions are
// in the same block.
txidx1 = txidx1 + 1
txidx1++
}

k := InitTestKeepers(t)
Expand Down
5 changes: 0 additions & 5 deletions x/btccheckpoint/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ func (ms msgServer) InsertBTCSpvProof(ctx context.Context, req *types.MsgInsertB
sdkCtx := sdk.UnwrapSDKContext(ctx)

rawSubmission, err := types.ParseSubmission(req, ms.k.GetPowLimit(), ms.k.GetExpectedTag(sdkCtx))

if err != nil {
return nil, types.ErrInvalidCheckpointProof.Wrap(err.Error())
}

submissionKey := rawSubmission.GetSubmissionKey()

if ms.k.HasSubmission(sdkCtx, submissionKey) {
return nil, types.ErrDuplicatedSubmission
}

newSubmissionOldestHeaderDepth, err := ms.k.GetSubmissionBtcInfo(sdkCtx, submissionKey)

if err != nil {
return nil, types.ErrInvalidHeader.Wrap(err.Error())
}
Expand All @@ -54,7 +51,6 @@ func (ms msgServer) InsertBTCSpvProof(ctx context.Context, req *types.MsgInsertB
// - this is new checkpoint submission
// Verify if this is expected checkpoint
err = ms.k.checkpointingKeeper.VerifyCheckpoint(sdkCtx, rawSubmission.CheckpointData)

if err != nil {
return nil, err
}
Expand All @@ -64,7 +60,6 @@ func (ms msgServer) InsertBTCSpvProof(ctx context.Context, req *types.MsgInsertB
epochNum := rawSubmission.CheckpointData.Epoch

err = ms.k.checkAncestors(sdkCtx, epochNum, newSubmissionOldestHeaderDepth)

if err != nil {
return nil, err
}
Expand Down
35 changes: 20 additions & 15 deletions x/btccheckpoint/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package keeper_test

import (
"bytes"
"context"
"errors"
"math"
"math/rand"
"testing"
Expand All @@ -23,7 +23,6 @@ import (

type TestKeepers struct {
SdkCtx sdk.Context
Ctx context.Context
BTCLightClient *btcctypes.MockBTCLightClientKeeper
Checkpointing *btcctypes.MockCheckpointingKeeper
Incentive *btcctypes.MockIncentiveKeeper
Expand Down Expand Up @@ -51,12 +50,10 @@ func InitTestKeepers(
ic := btcctypes.NewMockIncentiveKeeper()

k, ctx := keepertest.NewBTCCheckpointKeeper(t, lc, cc, ic, chaincfg.SimNetParams.PowLimit)

srv := bkeeper.NewMsgServerImpl(*k)

return &TestKeepers{
SdkCtx: ctx,
Ctx: ctx,
BTCLightClient: lc,
Checkpointing: cc,
Incentive: ic,
Expand All @@ -66,7 +63,7 @@ func InitTestKeepers(
}

func (k *TestKeepers) insertProofMsg(msg *btcctypes.MsgInsertBTCSpvProof) (*btcctypes.MsgInsertBTCSpvProofResponse, error) {
return k.MsgSrv.InsertBTCSpvProof(k.Ctx, msg)
return k.MsgSrv.InsertBTCSpvProof(k.SdkCtx, msg)
}

func (k *TestKeepers) GetEpochData(e uint64) *btcctypes.EpochData {
Expand All @@ -82,6 +79,7 @@ func (k *TestKeepers) onTipChange() {
}

func TestUpdateParams(t *testing.T) {
t.Parallel()
tk := InitTestKeepers(t)

// Try to update params with a different checkpoint finalization timeout
Expand All @@ -92,7 +90,7 @@ func TestUpdateParams(t *testing.T) {
},
}

_, err := tk.MsgSrv.UpdateParams(tk.Ctx, msg)
_, err := tk.MsgSrv.UpdateParams(tk.SdkCtx, msg)
require.ErrorIs(t, err, govtypes.ErrInvalidProposalMsg,
"should not be able to change CheckpointFinalizationTimeout parameter")

Expand All @@ -103,6 +101,7 @@ func TestUpdateParams(t *testing.T) {
}

func TestRejectDuplicatedSubmission(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
raw, _ := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand Down Expand Up @@ -132,12 +131,13 @@ func TestRejectDuplicatedSubmission(t *testing.T) {
t.Fatalf("Submission should have failed due to duplicated submission")
}

if err != btcctypes.ErrDuplicatedSubmission {
if !errors.Is(err, btcctypes.ErrDuplicatedSubmission) {
t.Fatalf("Error should indicate duplicated submissions")
}
}

func TestRejectUnknownToBtcLightClient(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
raw, _ := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand All @@ -162,6 +162,7 @@ func TestRejectUnknownToBtcLightClient(t *testing.T) {
}

func TestSubmitValidNewCheckpoint(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
raw, rawBtcCheckpoint := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand Down Expand Up @@ -228,6 +229,7 @@ func TestSubmitValidNewCheckpoint(t *testing.T) {
}

func TestRejectSubmissionWithoutSubmissionsForPreviousEpoch(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(2)
raw, _ := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand All @@ -254,6 +256,7 @@ func TestRejectSubmissionWithoutSubmissionsForPreviousEpoch(t *testing.T) {
}

func TestRejectSubmissionWithoutAncestorsOnMainchainInPreviousEpoch(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
raw, _ := dg.RandomRawCheckpointDataForEpoch(r, epoch)
Expand Down Expand Up @@ -322,12 +325,11 @@ func TestRejectSubmissionWithoutAncestorsOnMainchainInPreviousEpoch(t *testing.T
tk.BTCLightClient.SetDepth(epoch2Block2.HeaderBytes.Hash(), uint32(2))

_, err = tk.insertProofMsg(msg2)

require.NoErrorf(t, err, "Unexpected message processing error: %v", err)

}

func TestClearChildEpochsWhenNoParenNotOnMainChain(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tk := InitTestKeepers(t)

Expand Down Expand Up @@ -412,6 +414,7 @@ func TestClearChildEpochsWhenNoParenNotOnMainChain(t *testing.T) {
}

func TestLeaveOnlyBestSubmissionWhenEpochFinalized(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tk := InitTestKeepers(t)
defaultParams := btcctypes.DefaultParams()
Expand Down Expand Up @@ -461,6 +464,7 @@ func TestLeaveOnlyBestSubmissionWhenEpochFinalized(t *testing.T) {
}

func TestTxIdxShouldBreakTies(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tk := InitTestKeepers(t)
defaultParams := btcctypes.DefaultParams()
Expand Down Expand Up @@ -510,6 +514,7 @@ func TestTxIdxShouldBreakTies(t *testing.T) {
}

func TestStateTransitionOfValidSubmission(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
epoch := uint64(1)
defaultParams := btcctypes.DefaultParams()
Expand Down Expand Up @@ -612,32 +617,32 @@ func FuzzConfirmAndDinalizeManyEpochs(f *testing.F) {

if epoch <= uint64(numFinalizedEpochs) {
tk.BTCLightClient.SetDepth(blck1.HeaderBytes.Hash(), uint32(finalizationDepth))
finalizationDepth = finalizationDepth - 1
finalizationDepth--
tk.BTCLightClient.SetDepth(blck2.HeaderBytes.Hash(), uint32(finalizationDepth))

// first submission is always deepest one, and second block is the most recent one
if j == 1 {
bestSumbissionInfos[epoch] = uint32(finalizationDepth)
}
finalizationDepth = finalizationDepth - 1
finalizationDepth--
} else if epoch <= uint64(numFinalizedEpochs+numConfirmedEpochs) {
tk.BTCLightClient.SetDepth(blck1.HeaderBytes.Hash(), confirmationDepth)
confirmationDepth = confirmationDepth - 1
confirmationDepth--
tk.BTCLightClient.SetDepth(blck2.HeaderBytes.Hash(), confirmationDepth)
// first submission is always deepest one, and second block is the most recent one
if j == 1 {
bestSumbissionInfos[epoch] = confirmationDepth
}
confirmationDepth = confirmationDepth - 1
confirmationDepth--
} else {
tk.BTCLightClient.SetDepth(blck1.HeaderBytes.Hash(), sumbissionDepth)
sumbissionDepth = sumbissionDepth - 1
sumbissionDepth--
tk.BTCLightClient.SetDepth(blck2.HeaderBytes.Hash(), sumbissionDepth)
// first submission is always deepest one, and second block is the most recent one
if j == 1 {
bestSumbissionInfos[epoch] = sumbissionDepth
}
sumbissionDepth = sumbissionDepth - 1
sumbissionDepth--
}

_, err := tk.insertProofMsg(msg)
Expand Down
1 change: 1 addition & 0 deletions x/btccheckpoint/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestGetParams(t *testing.T) {
t.Parallel()
k, ctx := testkeeper.NewBTCCheckpointKeeper(t, nil, nil, nil, nil)

params := types.DefaultParams()
Expand Down
9 changes: 5 additions & 4 deletions x/btccheckpoint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package btccheckpoint

import (
"context"
"cosmossdk.io/core/appmodule"
"encoding/json"
"fmt"

"cosmossdk.io/core/appmodule"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
}

// ValidateGenesis performs genesis state validation for the capability module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var genState types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
Expand All @@ -75,7 +76,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod
}

// RegisterRESTRoutes registers the capability module's REST service handlers.
func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module.
Expand All @@ -92,7 +93,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {

// GetQueryCmd returns the capability module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(types.StoreKey)
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 8d907eb

Please sign in to comment.