Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply Cosmos SDK v0.50 conventions #208

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: refactor
module: none
pull_request: 208
description: Apply Cosmos SDK v0.50 conventions
backward_compatible: true
date: 2024-12-10T08:38:13.018999Z
6 changes: 2 additions & 4 deletions x/assets/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

type Keeper struct {
cdc codec.Codec
storeService corestoretypes.KVStoreService
cdc codec.Codec

Schema collections.Schema
Assets collections.Map[string, types.Asset] // denom => types.Asset
Expand All @@ -30,8 +29,7 @@ func NewKeeper(
) *Keeper {
sb := collections.NewSchemaBuilder(storeService)
k := &Keeper{
cdc: cdc,
storeService: storeService,
cdc: cdc,

Assets: collections.NewMap(
sb,
Expand Down
9 changes: 3 additions & 6 deletions x/liquidvesting/keeper/send_restriction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ func (suite *KeeperTestSuite) TestKeeper_BankSend() {
lockedDenom, err := types.GetLockedRepresentationDenom("stake")
suite.Require().NoError(err)

pool, found, err := suite.pk.GetPool(ctx, 1)
pool, err := suite.pk.GetPool(ctx, 1)
suite.Require().NoError(err)
suite.Require().True(found)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add back these tests by changing them to make sure the returned error is ErrNotFound? We might need to create a new testutils method to do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err == nil means the pool was found, so we don't need further tests after suite.Require().NoError(err). I added suite.Require().ErrorIs(err, collections.ErrNotFound) to where we originally had suite.Require().False(found) tests.


poolCoins := suite.bk.GetAllBalances(ctx, sdk.MustAccAddressFromBech32(pool.GetAddress()))
suite.Require().Equal(sdk.NewCoins(sdk.NewInt64Coin(lockedDenom, 1000)), poolCoins)
Expand Down Expand Up @@ -126,9 +125,8 @@ func (suite *KeeperTestSuite) TestKeeper_BankSend() {
lockedDenom, err := types.GetLockedRepresentationDenom("stake")
suite.Require().NoError(err)

operator, found, err := suite.ok.GetOperator(ctx, 1)
operator, err := suite.ok.GetOperator(ctx, 1)
suite.Require().NoError(err)
suite.Require().True(found)

poolCoins := suite.bk.GetAllBalances(ctx, sdk.MustAccAddressFromBech32(operator.GetAddress()))
suite.Require().Equal(sdk.NewCoins(sdk.NewInt64Coin(lockedDenom, 1000)), poolCoins)
Expand Down Expand Up @@ -173,9 +171,8 @@ func (suite *KeeperTestSuite) TestKeeper_BankSend() {
lockedDenom, err := types.GetLockedRepresentationDenom("stake")
suite.Require().NoError(err)

service, found, err := suite.sk.GetService(ctx, 1)
service, err := suite.sk.GetService(ctx, 1)
suite.Require().NoError(err)
suite.Require().True(found)

poolCoins := suite.bk.GetAllBalances(ctx, sdk.MustAccAddressFromBech32(service.GetAddress()))
suite.Require().Equal(sdk.NewCoins(sdk.NewInt64Coin(lockedDenom, 1000)), poolCoins)
Expand Down
6 changes: 2 additions & 4 deletions x/operators/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ func TestBeginBlocker(t *testing.T) {
},
check: func(ctx sdk.Context) {
// Make sure the operator is still inactivating
operator, found, err := operatorsKeeper.GetOperator(ctx, 1)
operator, err := operatorsKeeper.GetOperator(ctx, 1)
require.NoError(t, err)
require.True(t, found)
require.Equal(t, types.OPERATOR_STATUS_INACTIVATING, operator.Status)

// Make sure the operator is still in the inactivating queue
Expand Down Expand Up @@ -116,9 +115,8 @@ func TestBeginBlocker(t *testing.T) {
},
check: func(ctx sdk.Context) {
// Make sure the operator is inactive
operator, found, err := operatorsKeeper.GetOperator(ctx, 1)
operator, err := operatorsKeeper.GetOperator(ctx, 1)
require.NoError(t, err)
require.True(t, found)
require.Equal(t, types.OPERATOR_STATUS_INACTIVE, operator.Status)

// Make sure the operator is not in the inactivating queue
Expand Down
63 changes: 15 additions & 48 deletions x/operators/keeper/alias_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package keeper

import (
"context"
"fmt"
"errors"
"time"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -23,29 +24,10 @@ func (k *Keeper) createAccountIfNotExists(ctx context.Context, address sdk.AccAd

// IterateOperators iterates over the operators in the store and performs a callback function
func (k *Keeper) IterateOperators(ctx context.Context, cb func(operator types.Operator) (stop bool, err error)) error {
iterator, err := k.operators.Iterate(ctx, nil)
if err != nil {
return err
}
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
operator, err := iterator.Value()
if err != nil {
return err
}

stop, err := cb(operator)
if err != nil {
return err
}

if stop {
break
}
}

return nil
err := k.operators.Walk(ctx, nil, func(_ uint32, operator types.Operator) (stop bool, err error) {
return cb(operator)
})
return err
}

// GetOperators returns the operators stored in the KVStore
Expand All @@ -63,15 +45,13 @@ func (k *Keeper) GetOperators(ctx context.Context) ([]types.Operator, error) {
func (k *Keeper) IterateInactivatingOperatorQueue(ctx context.Context, endTime time.Time, fn func(operator types.Operator) (stop bool, err error)) error {
return k.iterateInactivatingOperatorsKeys(ctx, endTime, func(key, value []byte) (stop bool, err error) {
operatorID, _ := types.SplitInactivatingOperatorQueueKey(key)
operator, found, err := k.GetOperator(ctx, operatorID)
operator, err := k.GetOperator(ctx, operatorID)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return true, types.ErrOperatorNotFound
}
return true, err
}

if !found {
return true, fmt.Errorf("operator %d does not exist", operatorID)
}

return fn(operator)
})
}
Expand Down Expand Up @@ -126,26 +106,13 @@ func (k *Keeper) IsOperatorAddress(ctx context.Context, address string) (bool, e

// GetAllOperatorParamsRecords returns all the operator params records
func (k *Keeper) GetAllOperatorParamsRecords(ctx context.Context) ([]types.OperatorParamsRecord, error) {
iterator, err := k.operatorParams.Iterate(ctx, nil)
if err != nil {
return nil, err
}
defer iterator.Close()

var records []types.OperatorParamsRecord
for ; iterator.Valid(); iterator.Next() {
// Get the operator params
params, err := iterator.Value()
if err != nil {
return nil, err
}
// Get the operator id from the map key
operatorID, err := iterator.Key()
if err != nil {
return nil, err
}
err := k.operatorParams.Walk(ctx, nil, func(operatorID uint32, params types.OperatorParams) (stop bool, err error) {
records = append(records, types.NewOperatorParamsRecord(operatorID, params))
return false, nil
})
if err != nil {
return nil, err
}

return records, nil
}
13 changes: 7 additions & 6 deletions x/operators/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package keeper

import (
"fmt"
"errors"

"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/milkyway-labs/milkyway/v3/x/operators/types"
Expand Down Expand Up @@ -65,15 +67,14 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, state *types.GenesisState) error {
// Store the operator params
for _, operatorParams := range state.OperatorsParams {
// Ensure that the operator is present
_, found, err := k.GetOperator(ctx, operatorParams.OperatorID)
_, err := k.GetOperator(ctx, operatorParams.OperatorID)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return errorsmod.Wrapf(types.ErrOperatorNotFound, "operator %d not found", operatorParams.OperatorID)
}
return err
}

if !found {
return fmt.Errorf("can't set operator params for %d, operator not found", operatorParams.OperatorID)
}

err = k.SaveOperatorParams(ctx, operatorParams.OperatorID, operatorParams.Params)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions x/operators/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,8 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() {
Params: types.DefaultParams(),
},
check: func(ctx sdk.Context) {
operator, found, err := suite.k.GetOperator(ctx, 1)
operator, err := suite.k.GetOperator(ctx, 1)
suite.Require().NoError(err)
suite.Require().True(found)
suite.Require().Equal(types.NewOperator(
1,
types.OPERATOR_STATUS_ACTIVE,
Expand All @@ -305,9 +304,8 @@ func (suite *KeeperTestSuite) TestKeeper_InitGenesis() {
params, err := suite.k.GetOperatorParams(ctx, 1)
suite.Require().Equal(types.DefaultOperatorParams(), params)

operator, found, err = suite.k.GetOperator(ctx, 2)
operator, err = suite.k.GetOperator(ctx, 2)
suite.Require().NoError(err)
suite.Require().True(found)
suite.Require().Equal(types.NewOperator(
2,
types.OPERATOR_STATUS_INACTIVATING,
Expand Down
37 changes: 12 additions & 25 deletions x/operators/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package keeper

import (
"context"
"errors"

"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/runtime"
"cosmossdk.io/collections"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -16,53 +16,40 @@ var _ types.QueryServer = &Keeper{}

// Operator implements the Query/Operator gRPC method
func (k *Keeper) Operator(ctx context.Context, request *types.QueryOperatorRequest) (*types.QueryOperatorResponse, error) {
operator, found, err := k.GetOperator(ctx, request.OperatorId)
operator, err := k.GetOperator(ctx, request.OperatorId)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "operator not found")
}
return nil, status.Error(codes.Internal, err.Error())
}

if !found {
return nil, status.Error(codes.NotFound, "operator not found")
}

return &types.QueryOperatorResponse{Operator: operator}, nil
}

// Operators implements the Query/Operators gRPC method
func (k *Keeper) Operators(ctx context.Context, request *types.QueryOperatorsRequest) (*types.QueryOperatorsResponse, error) {
store := k.storeService.OpenKVStore(ctx)
operatorsStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.OperatorPrefix)

var operators []types.Operator
pageRes, err := query.Paginate(operatorsStore, request.Pagination, func(key []byte, value []byte) error {
var operator types.Operator
if err := k.cdc.Unmarshal(value, &operator); err != nil {
return status.Error(codes.Internal, err.Error())
}

operators = append(operators, operator)
return nil
operators, pageRes, err := query.CollectionPaginate(ctx, k.operators, request.Pagination, func(_ uint32, operator types.Operator) (types.Operator, error) {
return operator, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryOperatorsResponse{
Operators: operators,
Pagination: pageRes,
}, nil
}

func (k *Keeper) OperatorParams(ctx context.Context, request *types.QueryOperatorParamsRequest) (*types.QueryOperatorParamsResponse, error) {
_, found, err := k.GetOperator(ctx, request.OperatorId)
_, err := k.GetOperator(ctx, request.OperatorId)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, status.Error(codes.NotFound, "operator not found")
}
return nil, status.Error(codes.Internal, err.Error())
}

if !found {
return nil, types.ErrOperatorNotFound
}

params, err := k.GetOperatorParams(ctx, request.OperatorId)
if err != nil {
return nil, err
Expand Down
Loading
Loading