Skip to content

Commit

Permalink
staticcheck && linter
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Dec 26, 2023
1 parent ab41e36 commit c9a9fb4
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 46 deletions.
2 changes: 1 addition & 1 deletion x/opchild/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func WriteValidators(ctx context.Context, keeper *keeper.Keeper) (vals []tmtypes
if err != nil {
return true, err
}
tmPk, err := cryptocodec.ToTmPubKeyInterface(pk)
tmPk, err := cryptocodec.ToCmtPubKeyInterface(pk)
if err != nil {
return true, err
}
Expand Down
17 changes: 13 additions & 4 deletions x/opchild/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
}

for _, validator := range data.Validators {
k.SetValidator(ctx, validator)
if err := k.SetValidator(ctx, validator); err != nil {
panic(err)

Check warning on line 34 in x/opchild/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/genesis.go#L33-L34

Added lines #L33 - L34 were not covered by tests
}

// Manually set indices for the first time
if err := k.SetValidatorByConsAddr(ctx, validator); err != nil {
Expand All @@ -46,7 +48,10 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
panic(err)
}

k.SetLastValidatorPower(ctx, valAddr, lv.Power)
if err := k.SetLastValidatorPower(ctx, valAddr, lv.Power); err != nil {
panic(err)

Check warning on line 52 in x/opchild/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/genesis.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

validator, found := k.GetValidator(ctx, valAddr)

if !found {
Expand All @@ -67,10 +72,14 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
}

for _, finalizedL1Sequence := range data.FinalizedL1Sequences {
k.RecordFinalizedL1Sequence(ctx, finalizedL1Sequence)
if err := k.RecordFinalizedL1Sequence(ctx, finalizedL1Sequence); err != nil {
panic(err)

Check warning on line 76 in x/opchild/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/genesis.go#L76

Added line #L76 was not covered by tests
}
}

k.SetNextL2Sequence(ctx, data.NextL2Sequence)
if err := k.SetNextL2Sequence(ctx, data.NextL2Sequence); err != nil {
panic(err)

Check warning on line 81 in x/opchild/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/genesis.go#L81

Added line #L81 was not covered by tests
}

return res
}
Expand Down
4 changes: 3 additions & 1 deletion x/opchild/keeper/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (k Keeper) TrackHistoricalInfo(ctx context.Context) error {
if err != nil {
return err

Check warning on line 56 in x/opchild/keeper/historical_info.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/historical_info.go#L56

Added line #L56 was not covered by tests
} else if found {
k.DeleteHistoricalInfo(ctx, i)
if err := k.DeleteHistoricalInfo(ctx, i); err != nil {
return err
}

Check warning on line 60 in x/opchild/keeper/historical_info.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/historical_info.go#L59-L60

Added lines #L59 - L60 were not covered by tests
} else {
break
}
Expand Down
12 changes: 9 additions & 3 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ func (ms MsgServer) AddValidator(ctx context.Context, req *types.MsgAddValidator
return nil, err
}

ms.SetValidator(ctx, validator)
if err := ms.SetValidator(ctx, validator); err != nil {
return nil, err
}

Check warning on line 193 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L192-L193

Added lines #L192 - L193 were not covered by tests
if err = ms.SetValidatorByConsAddr(ctx, validator); err != nil {
return nil, err
}
Expand Down Expand Up @@ -227,7 +229,9 @@ func (ms MsgServer) RemoveValidator(ctx context.Context, req *types.MsgRemoveVal

// set validator consensus power `0`,
// then `val_state_change` will execute `k.RemoveValidator`.
ms.Keeper.SetValidator(ctx, val)
if err := ms.Keeper.SetValidator(ctx, val); err != nil {
return nil, err
}

Check warning on line 234 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L233-L234

Added lines #L233 - L234 were not covered by tests

sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
Expand Down Expand Up @@ -323,7 +327,9 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
return nil, err
}

ms.RecordFinalizedL1Sequence(ctx, req.Sequence)
if err := ms.RecordFinalizedL1Sequence(ctx, req.Sequence); err != nil {
return nil, err
}

Check warning on line 332 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L331-L332

Added lines #L331 - L332 were not covered by tests

event := sdk.NewEvent(
types.EventTypeFinalizeTokenDeposit,
Expand Down
18 changes: 13 additions & 5 deletions x/opchild/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) ([]abci.V
if !found || oldPower != newPower {
updates = append(updates, validator.ABCIValidatorUpdate())

k.SetLastValidatorPower(ctx, valAddr, newPower)
if err := k.SetLastValidatorPower(ctx, valAddr, newPower); err != nil {
return nil, err
}

Check warning on line 62 in x/opchild/keeper/val_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/val_state_change.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

delete(last, validator.GetOperator())
Expand All @@ -75,8 +77,14 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) ([]abci.V
return nil, err
}

Check warning on line 78 in x/opchild/keeper/val_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/val_state_change.go#L77-L78

Added lines #L77 - L78 were not covered by tests

k.RemoveValidator(ctx, valAddr)
k.DeleteLastValidatorPower(ctx, valAddr)
if err := k.RemoveValidator(ctx, valAddr); err != nil {
return nil, err
}

Check warning on line 82 in x/opchild/keeper/val_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/val_state_change.go#L81-L82

Added lines #L81 - L82 were not covered by tests

if err := k.DeleteLastValidatorPower(ctx, valAddr); err != nil {
return nil, err
}

Check warning on line 86 in x/opchild/keeper/val_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/val_state_change.go#L85-L86

Added lines #L85 - L86 were not covered by tests

updates = append(updates, validator.ABCIValidatorUpdateZero())
}

Expand All @@ -91,12 +99,12 @@ type validatorsByAddr map[string]int64
func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr, error) {
last := make(validatorsByAddr)

k.IterateLastValidators(ctx, func(validator types.ValidatorI, power int64) (stop bool, err error) {
err := k.IterateLastValidators(ctx, func(validator types.ValidatorI, power int64) (stop bool, err error) {
last[validator.GetOperator()] = power
return false, nil
})

return last, nil
return last, err
}

// given a map of remaining validators to previous bonded power
Expand Down
12 changes: 6 additions & 6 deletions x/opchild/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err

// get the set of all validators with no limits, used during genesis dump
func (k Keeper) GetAllValidators(ctx context.Context) (validators []types.Validator, err error) {
k.Validators.Walk(ctx, nil, func(key []byte, validator types.Validator) (stop bool, err error) {
err = k.Validators.Walk(ctx, nil, func(key []byte, validator types.Validator) (stop bool, err error) {
validators = append(validators, validator)
return false, nil
})

return validators, nil
return validators, err
}

// return a given amount of all the validators
func (k Keeper) GetValidators(ctx context.Context, maxRetrieve uint32) (validators []types.Validator, err error) {
validators = make([]types.Validator, 0, maxRetrieve)
k.Validators.Walk(ctx, nil, func(key []byte, validator types.Validator) (stop bool, err error) {
err = k.Validators.Walk(ctx, nil, func(key []byte, validator types.Validator) (stop bool, err error) {
validators = append(validators, validator)
return len(validators) == int(maxRetrieve), nil
})

return validators, nil
return validators, err
}

// Last Validator Index
Expand Down Expand Up @@ -154,7 +154,7 @@ func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Valid

validators = make([]types.Validator, 0, maxValidators)

k.IterateLastValidatorPowers(ctx, func(operator []byte, power int64) (stop bool, err error) {
err = k.IterateLastValidatorPowers(ctx, func(operator []byte, power int64) (stop bool, err error) {
validators = append(validators, k.mustGetValidator(ctx, operator))

// sanity check
Expand All @@ -165,5 +165,5 @@ func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Valid
return false, nil
})

return validators, nil
return validators, err
}
12 changes: 6 additions & 6 deletions x/opchild/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

abci "github.com/cometbft/cometbft/abci/types"
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
"sigs.k8s.io/yaml"

"cosmossdk.io/core/address"
Expand Down Expand Up @@ -227,16 +227,16 @@ func (v Validator) ConsPubKey() (cryptotypes.PubKey, error) {
return pk, nil
}

// TmConsPublicKey casts Validator.ConsensusPubkey to tmprotocrypto.PubKey.
func (v Validator) TmConsPublicKey() (tmprotocrypto.PublicKey, error) {
// TmConsPublicKey casts Validator.ConsensusPubkey to cmtprotocrypto.PubKey.
func (v Validator) TmConsPublicKey() (cmtprotocrypto.PublicKey, error) {
pk, err := v.ConsPubKey()
if err != nil {
return tmprotocrypto.PublicKey{}, err
return cmtprotocrypto.PublicKey{}, err
}

tmPk, err := cryptocodec.ToTmProtoPublicKey(pk)
tmPk, err := cryptocodec.ToCmtProtoPublicKey(pk)
if err != nil {
return tmprotocrypto.PublicKey{}, err
return cmtprotocrypto.PublicKey{}, err
}

return tmPk, nil
Expand Down
25 changes: 19 additions & 6 deletions x/ophost/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
panic(err)
}

k.SetNextL1Sequence(ctx, bridgeId, bridge.NextL1Sequence)
if err := k.SetNextL1Sequence(ctx, bridgeId, bridge.NextL1Sequence); err != nil {
panic(err)

Check warning on line 25 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L25

Added line #L25 was not covered by tests
}

for _, proposal := range bridge.Proposals {
if err := k.SetOutputProposal(ctx, bridgeId, proposal.OutputIndex, proposal.OutputProposal); err != nil {
panic(err)
}
}

k.SetNextOutputIndex(ctx, bridgeId, bridge.NextOutputIndex)
if err := k.SetNextOutputIndex(ctx, bridgeId, bridge.NextOutputIndex); err != nil {
panic(err)

Check warning on line 35 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L35

Added line #L35 was not covered by tests
}

for _, provenWithdrawal := range bridge.ProvenWithdrawals {
withdrawalHash := [32]byte{}
copy(withdrawalHash[:], provenWithdrawal)
k.RecordProvenWithdrawal(ctx, bridgeId, withdrawalHash)
if err := k.RecordProvenWithdrawal(ctx, bridgeId, withdrawalHash); err != nil {
panic(err)

Check warning on line 42 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L42

Added line #L42 was not covered by tests
}
}

for _, tokenPair := range bridge.TokenPairs {
k.SetTokenPair(ctx, bridgeId, tokenPair.L2Denom, tokenPair.L1Denom)
if err := k.SetTokenPair(ctx, bridgeId, tokenPair.L2Denom, tokenPair.L1Denom); err != nil {
panic(err)

Check warning on line 48 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L48

Added line #L48 was not covered by tests
}
}
}

k.SetNextBridgeId(ctx, data.NextBridgeId)
if err := k.SetNextBridgeId(ctx, data.NextBridgeId); err != nil {
panic(err)

Check warning on line 54 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L54

Added line #L54 was not covered by tests
}
}

// ExportGenesis returns a GenesisState for a given context and keeper. The
Expand All @@ -51,7 +61,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) {
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {

var bridges []types.Bridge
k.IterateBridgeConfig(ctx, func(bridgeId uint64, bridgeConfig types.BridgeConfig) (stop bool, err error) {
err := k.IterateBridgeConfig(ctx, func(bridgeId uint64, bridgeConfig types.BridgeConfig) (stop bool, err error) {
nextL1Sequence, err := k.GetNextL1Sequence(ctx, bridgeId)
if err != nil {
return true, err
Expand Down Expand Up @@ -102,6 +112,9 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {

return false, nil
})
if err != nil {
panic(err)

Check warning on line 116 in x/ophost/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/genesis.go#L116

Added line #L116 was not covered by tests
}

nextBridgeId, err := k.GetNextBridgeId(ctx)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func (ms MsgServer) DeleteOutput(ctx context.Context, req *types.MsgDeleteOutput
}

// delete output proposal
ms.DeleteOutputProposal(ctx, bridgeId, outputIndex)
if err := ms.DeleteOutputProposal(ctx, bridgeId, outputIndex); err != nil {
return nil, err
}

Check warning on line 176 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L175-L176

Added lines #L175 - L176 were not covered by tests

sdk.UnwrapSDKContext(ctx).EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeDeleteOutput,
Expand Down Expand Up @@ -211,7 +213,9 @@ func (ms MsgServer) InitiateTokenDeposit(ctx context.Context, req *types.MsgInit
if ok, err := ms.HasTokenPair(ctx, bridgeId, l2Denom); err != nil {
return nil, err

Check warning on line 214 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L214

Added line #L214 was not covered by tests
} else if !ok {
ms.SetTokenPair(ctx, bridgeId, l2Denom, coin.Denom)
if err := ms.SetTokenPair(ctx, bridgeId, l2Denom, coin.Denom); err != nil {
return nil, err
}

Check warning on line 218 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L217-L218

Added lines #L217 - L218 were not covered by tests
}

// emit events for bridge executor
Expand Down Expand Up @@ -314,7 +318,9 @@ func (ms MsgServer) FinalizeTokenWithdrawal(ctx context.Context, req *types.MsgF
return nil, types.ErrFailedToVerifyWithdrawal.Wrap("invalid storage root proofs")
}

ms.RecordProvenWithdrawal(ctx, bridgeId, withdrawalHash)
if err := ms.RecordProvenWithdrawal(ctx, bridgeId, withdrawalHash); err != nil {
return nil, err
}

Check warning on line 323 in x/ophost/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/msg_server.go#L322-L323

Added lines #L322 - L323 were not covered by tests
}

// transfer asset to a user from the bridge account
Expand Down
4 changes: 2 additions & 2 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func Test_UpdateProposal(t *testing.T) {

// current proposer signer
msg = types.NewMsgUpdateProposer(addrs[1], 1, addrs[2])
_, err = ms.UpdateProposer(sdk.WrapSDKContext(ctx), msg)
_, err = ms.UpdateProposer(ctx, msg)
require.NoError(t, err)
_config, err = ms.GetBridgeConfig(ctx, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -270,7 +270,7 @@ func Test_UpdateChallenger(t *testing.T) {

// current challenger
msg = types.NewMsgUpdateChallenger(addrs[2], 1, addrs[3])
_, err = ms.UpdateChallenger(sdk.WrapSDKContext(ctx), msg)
_, err = ms.UpdateChallenger(ctx, msg)
require.NoError(t, err)
_config, err = ms.GetBridgeConfig(ctx, 1)
require.NoError(t, err)
Expand Down
16 changes: 7 additions & 9 deletions x/ophost/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/initia-labs/OPinit/x/ophost/keeper"
"github.com/initia-labs/OPinit/x/ophost/types"
)
Expand All @@ -26,7 +24,7 @@ func Test_QueryBridge(t *testing.T) {
require.NoError(t, err)

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.Bridge(sdk.WrapSDKContext(ctx), &types.QueryBridgeRequest{
res, err := q.Bridge(ctx, &types.QueryBridgeRequest{
BridgeId: 1,
})

Expand Down Expand Up @@ -60,7 +58,7 @@ func Test_QueryBridges(t *testing.T) {
require.NoError(t, input.OPHostKeeper.SetBridgeConfig(ctx, 2, config2))

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.Bridges(sdk.WrapSDKContext(ctx), &types.QueryBridgesRequest{})
res, err := q.Bridges(ctx, &types.QueryBridgesRequest{})

require.NoError(t, err)
require.Equal(t, []types.QueryBridgeResponse{
Expand All @@ -85,14 +83,14 @@ func Test_QueryTokenPair(t *testing.T) {
input.OPHostKeeper.SetTokenPair(ctx, 1, pair.L2Denom, pair.L1Denom)

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.TokenPairByL1Denom(sdk.WrapSDKContext(ctx), &types.QueryTokenPairByL1DenomRequest{
res, err := q.TokenPairByL1Denom(ctx, &types.QueryTokenPairByL1DenomRequest{
BridgeId: 1,
L1Denom: pair.L1Denom,
})
require.NoError(t, err)
require.Equal(t, pair, res.TokenPair)

res2, err := q.TokenPairByL2Denom(sdk.WrapSDKContext(ctx), &types.QueryTokenPairByL2DenomRequest{
res2, err := q.TokenPairByL2Denom(ctx, &types.QueryTokenPairByL2DenomRequest{
BridgeId: 1,
L2Denom: pair.L2Denom,
})
Expand All @@ -114,7 +112,7 @@ func Test_QueryTokenPairs(t *testing.T) {
input.OPHostKeeper.SetTokenPair(ctx, 1, pair2.L2Denom, pair2.L1Denom)

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.TokenPairs(sdk.WrapSDKContext(ctx), &types.QueryTokenPairsRequest{
res, err := q.TokenPairs(ctx, &types.QueryTokenPairsRequest{
BridgeId: 1,
})

Expand All @@ -134,7 +132,7 @@ func Test_QueryOutputProposal(t *testing.T) {
require.NoError(t, input.OPHostKeeper.SetOutputProposal(ctx, 1, 1, output))

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.OutputProposal(sdk.WrapSDKContext(ctx), &types.QueryOutputProposalRequest{
res, err := q.OutputProposal(ctx, &types.QueryOutputProposalRequest{
BridgeId: 1,
OutputIndex: 1,
})
Expand All @@ -158,7 +156,7 @@ func Test_QueryOutputProposals(t *testing.T) {
require.NoError(t, input.OPHostKeeper.SetOutputProposal(ctx, 1, 2, output2))

q := keeper.NewQuerier(input.OPHostKeeper)
res, err := q.OutputProposals(sdk.WrapSDKContext(ctx), &types.QueryOutputProposalsRequest{
res, err := q.OutputProposals(ctx, &types.QueryOutputProposalsRequest{
BridgeId: 1,
})

Expand Down

0 comments on commit c9a9fb4

Please sign in to comment.