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: remove panic usage in keeper methods #18636

Merged
merged 19 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val sdk.V
) (sdk.DecCoins, error) {
// sanity check
if startingPeriod > endingPeriod {
panic("startingPeriod cannot be greater than endingPeriod")
return sdk.DecCoins{}, fmt.Errorf("startingPeriod cannot be greater than endingPeriod")
}

// sanity check
if stake.IsNegative() {
panic("stake should not be negative")
return sdk.DecCoins{}, fmt.Errorf("stake should not be negative")
}

valBz, err := k.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
return sdk.DecCoins{}, err
}

// return staking * (ending - starting)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -77,7 +77,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val sdk.V

difference := ending.CumulativeRewardRatio.Sub(starting.CumulativeRewardRatio)
if difference.IsAnyNegative() {
panic("negative rewards should not be possible")
return sdk.DecCoins{}, fmt.Errorf("negative rewards should not be possible")
}
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
rewards := difference.MulDecTruncate(stake)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (k Keeper) CalculateDelegationRewards(ctx context.Context, val sdk.Validato
if endingPeriod > startingPeriod {
delRewards, err := k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, stake)
if err != nil {
panic(err)
return true
}
rewards = rewards.Add(delRewards...)

Expand Down
8 changes: 4 additions & 4 deletions x/distribution/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,22 @@ func (k Querier) DelegationTotalRewards(ctx context.Context, req *types.QueryDel
func(_ int64, del sdk.DelegationI) (stop bool) {
valAddr, err := k.stakingKeeper.ValidatorAddressCodec().StringToBytes(del.GetValidatorAddr())
if err != nil {
panic(err)
return true
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}

val, err := k.stakingKeeper.Validator(ctx, valAddr)
if err != nil {
panic(err)
return true
}

endingPeriod, err := k.IncrementValidatorPeriod(ctx, val)
if err != nil {
panic(err)
return true
}

delReward, err := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
if err != nil {
panic(err)
return true
}

delRewards = append(delRewards, types.NewDelegationDelegatorReward(del.GetValidatorAddr(), delReward))
Expand Down
14 changes: 7 additions & 7 deletions x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
)

var (
_, _, addr = testdata.KeyTestPubAddr()
govAcct = authtypes.NewModuleAddress(types.ModuleName)
poolAcct = authtypes.NewModuleAddress(pooltypes.ModuleName)
TestProposal = getTestProposal()
_, _, addr = testdata.KeyTestPubAddr()
govAcct = authtypes.NewModuleAddress(types.ModuleName)
poolAcct = authtypes.NewModuleAddress(pooltypes.ModuleName)
TestProposal, _ = getTestProposal()
)

// mintModuleName duplicates the mint module's name to avoid a cyclic dependency with x/mint.
Expand All @@ -43,16 +43,16 @@ var (
const mintModuleName = "mint"

// getTestProposal creates and returns a test proposal message.
func getTestProposal() []sdk.Msg {
func getTestProposal() ([]sdk.Msg, error) {
legacyProposalMsg, err := v1.NewLegacyContent(v1beta1.NewTextProposal("Title", "description"), authtypes.NewModuleAddress(types.ModuleName).String())
if err != nil {
panic(err)
return nil, err
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}

return []sdk.Msg{
banktypes.NewMsgSend(govAcct, addr, sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000)))),
legacyProposalMsg,
}
}, nil
}

type mocks struct {
Expand Down
16 changes: 13 additions & 3 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"

"cosmossdk.io/collections"
Expand Down Expand Up @@ -53,7 +54,10 @@ func TestDeposits(t *testing.T) {
TestAddrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 2, sdkmath.NewInt(10000000*depositMultiplier))
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}
proposal, err := govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", TestAddrs[0], tc.expedited)
require.NoError(t, err)
proposalID := proposal.Id
Expand Down Expand Up @@ -225,7 +229,10 @@ func TestDepositAmount(t *testing.T) {
err := govKeeper.Params.Set(ctx, params)
require.NoError(t, err)

tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
proposal, err := govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", testAddrs[0], false)
require.NoError(t, err)
proposalID := proposal.Id
Expand Down Expand Up @@ -415,7 +422,10 @@ func TestChargeDeposit(t *testing.T) {
err := govKeeper.Params.Set(ctx, params)
require.NoError(t, err)

tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
proposal, err := govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", TestAddrs[0], false)
require.NoError(t, err)
proposalID := proposal.Id
Expand Down
51 changes: 42 additions & 9 deletions x/gov/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/math"
v3 "cosmossdk.io/x/gov/migrations/v3"
v1 "cosmossdk.io/x/gov/types/v1"
Expand Down Expand Up @@ -443,6 +445,10 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryProposals() {

func (suite *KeeperTestSuite) TestGRPCQueryVote() {
ctx, queryClient, addrs := suite.ctx, suite.queryClient, suite.addrs
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

var (
req *v1.QueryVoteRequest
Expand Down Expand Up @@ -496,7 +502,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryVote() {
"no votes present",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)

req = &v1.QueryVoteRequest{
Expand Down Expand Up @@ -558,7 +564,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryVote() {

func (suite *KeeperTestSuite) TestLegacyGRPCQueryVote() {
ctx, queryClient, addrs := suite.ctx, suite.legacyQueryClient, suite.addrs

var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
var (
req *v1beta1.QueryVoteRequest
expRes *v1beta1.QueryVoteResponse
Expand Down Expand Up @@ -611,7 +620,7 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryVote() {
"no votes present",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)

req = &v1beta1.QueryVoteRequest{
Expand Down Expand Up @@ -674,6 +683,10 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryVote() {
func (suite *KeeperTestSuite) TestGRPCQueryVotes() {
suite.reset()
ctx, queryClient := suite.ctx, suite.queryClient
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

addrs := simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, ctx, 2, math.NewInt(30000000))

Expand Down Expand Up @@ -718,7 +731,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryVotes() {
"create a proposal and get votes",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)

req = &v1.QueryVotesRequest{
Expand Down Expand Up @@ -778,6 +791,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryVotes() {
func (suite *KeeperTestSuite) TestLegacyGRPCQueryVotes() {
suite.reset()
ctx, queryClient := suite.ctx, suite.legacyQueryClient
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

addrs := simtestutil.AddTestAddrsIncremental(suite.bankKeeper, suite.stakingKeeper, ctx, 2, math.NewInt(30000000))

Expand Down Expand Up @@ -822,7 +839,7 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryVotes() {
"create a proposal and get votes",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)

req = &v1beta1.QueryVotesRequest{
Expand Down Expand Up @@ -1057,6 +1074,10 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryParams() {
func (suite *KeeperTestSuite) TestGRPCQueryDeposit() {
suite.reset()
ctx, queryClient, addrs := suite.ctx, suite.queryClient, suite.addrs
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

var (
req *v1.QueryDepositRequest
Expand Down Expand Up @@ -1110,7 +1131,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryDeposit() {
"no deposits proposal",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)
suite.Require().NotNil(proposal)

Expand Down Expand Up @@ -1159,6 +1180,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryDeposit() {

func (suite *KeeperTestSuite) TestLegacyGRPCQueryDeposit() {
ctx, queryClient, addrs := suite.ctx, suite.legacyQueryClient, suite.addrs
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

var (
req *v1beta1.QueryDepositRequest
Expand Down Expand Up @@ -1212,7 +1237,7 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryDeposit() {
"no deposits proposal",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)
suite.Require().NotNil(proposal)

Expand Down Expand Up @@ -1262,6 +1287,10 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryDeposit() {

func (suite *KeeperTestSuite) TestGRPCQueryDeposits() {
ctx, queryClient, addrs := suite.ctx, suite.queryClient, suite.addrs
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

var (
req *v1.QueryDepositsRequest
Expand Down Expand Up @@ -1303,7 +1332,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryDeposits() {
"create a proposal and get deposits",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], true)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], true)
suite.Require().NoError(err)

req = &v1.QueryDepositsRequest{
Expand Down Expand Up @@ -1359,6 +1388,10 @@ func (suite *KeeperTestSuite) TestGRPCQueryDeposits() {
func (suite *KeeperTestSuite) TestLegacyGRPCQueryDeposits() {
suite.reset()
ctx, queryClient, addrs := suite.ctx, suite.legacyQueryClient, suite.addrs
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

var (
req *v1beta1.QueryDepositsRequest
Expand Down Expand Up @@ -1400,7 +1433,7 @@ func (suite *KeeperTestSuite) TestLegacyGRPCQueryDeposits() {
"create a proposal and get deposits",
func() {
var err error
proposal, err = suite.govKeeper.SubmitProposal(ctx, TestProposal, "", "title", "summary", addrs[0], false)
proposal, err = suite.govKeeper.SubmitProposal(ctx, tp, "", "title", "summary", addrs[0], false)
suite.Require().NoError(err)

req = &v1beta1.QueryDepositsRequest{
Expand Down
6 changes: 5 additions & 1 deletion x/gov/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"

"cosmossdk.io/x/gov"
Expand Down Expand Up @@ -74,7 +75,10 @@ func TestHooks(t *testing.T) {
require.False(t, govHooksReceiver.AfterProposalFailedMinDepositValid)
require.False(t, govHooksReceiver.AfterProposalVotingPeriodEndedValid)

tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
_, err := govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", sdk.AccAddress("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r"), false)
require.NoError(t, err)
require.True(t, govHooksReceiver.AfterProposalSubmissionValid)
Expand Down
11 changes: 9 additions & 2 deletions x/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"testing"

"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -88,7 +89,10 @@ func TestIncrementProposalNumber(t *testing.T) {
addrBz, err := ac.StringToBytes(address1)
require.NoError(t, err)

tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
_, err = govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", addrBz, false)
require.NoError(t, err)
_, err = govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", addrBz, false)
Expand All @@ -115,7 +119,10 @@ func TestProposalQueues(t *testing.T) {
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

// create test proposals
tp := TestProposal
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}
proposal, err := govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", addrBz, false)
require.NoError(t, err)

Expand Down
9 changes: 8 additions & 1 deletion x/gov/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"time"

"github.com/cosmos/gogoproto/proto"

sdkmath "cosmossdk.io/math"
banktypes "cosmossdk.io/x/bank/types"
v1 "cosmossdk.io/x/gov/types/v1"
Expand Down Expand Up @@ -1781,7 +1783,12 @@ func (suite *KeeperTestSuite) TestSubmitProposal_InitialDeposit() {
err := govKeeper.Params.Set(ctx, params)
suite.Require().NoError(err)

msg, err := v1.NewMsgSubmitProposal(TestProposal, tc.initialDeposit, address.String(), "test", "Proposal", "description of proposal", false)
var tp []proto.Message
if TestProposal != nil {
tp = TestProposal
}

msg, err := v1.NewMsgSubmitProposal(tp, tc.initialDeposit, address.String(), "test", "Proposal", "description of proposal", false)
suite.Require().NoError(err)

// System under test
Expand Down
Loading
Loading