From b8f6137e104ac26ffa4e8ccdc1721d0f7fcbd332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Kunze=20K=C3=BCllmer?= <31522760+fedekunze@users.noreply.github.com> Date: Thu, 9 Jun 2022 18:26:07 +0200 Subject: [PATCH] chore(types): add MustAccAddressFromBech32 util func (#12201) (cherry picked from commit 82e13b16842209b0c2e4cf952bfdcf25b5e303dd) --- CHANGELOG.md | 1 + server/rosetta/converter.go | 10 +--- simapp/export.go | 11 ++--- types/address.go | 10 ++++ types/tx/types.go | 17 ++----- x/auth/tx/builder.go | 12 +---- x/authz/keeper/genesis.go | 13 ++---- x/bank/keeper/genesis.go | 5 +- x/bank/keeper/msg_server.go | 6 +-- x/bank/simulation/operations.go | 10 +--- x/bank/types/balance.go | 8 +--- x/distribution/keeper/genesis.go | 17 ++----- x/feegrant/simulation/operations.go | 10 +--- x/gov/keeper/deposit.go | 18 ++----- x/gov/keeper/msg_server.go | 8 ++-- x/gov/keeper/tally.go | 5 +- x/gov/keeper/vote.go | 6 +-- x/group/msgs.go | 70 +++++++++------------------- x/group/types.go | 18 +++---- x/nft/keeper/genesis.go | 5 +- x/staking/keeper/delegation.go | 38 ++++----------- x/staking/keeper/genesis.go | 5 +- x/staking/keeper/querier.go | 6 +-- x/staking/keeper/slash.go | 5 +- x/staking/keeper/val_state_change.go | 12 ++--- x/staking/types/delegation.go | 6 +-- 26 files changed, 97 insertions(+), 235 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3126d53c233..b1d690edb951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -208,6 +208,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* (types) [\#12201](https://github.com/cosmos/cosmos-sdk/pull/12201) Add `MustAccAddressFromBech32` util function * [\#11696](https://github.com/cosmos/cosmos-sdk/pull/11696) Rename `helpers.GenTx` to `GenSignedMockTx` to avoid confusion with genutil's `GenTxCmd`. * (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)` * [\#11630](https://github.com/cosmos/cosmos-sdk/pull/11630) Add SafeSub method to sdk.Coin. diff --git a/server/rosetta/converter.go b/server/rosetta/converter.go index 54c154579861..1a50d06da892 100644 --- a/server/rosetta/converter.go +++ b/server/rosetta/converter.go @@ -337,10 +337,7 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations [] default: return nil, false case banktypes.EventTypeCoinSpent: - spender, err := sdk.AccAddressFromBech32(event.Attributes[0].Value) - if err != nil { - panic(err) - } + spender := sdk.MustAccAddressFromBech32(event.Attributes[0].Value) coins, err := sdk.ParseCoinsNormalized(event.Attributes[1].Value) if err != nil { panic(err) @@ -351,10 +348,7 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations [] accountIdentifier = spender.String() case banktypes.EventTypeCoinReceived: - receiver, err := sdk.AccAddressFromBech32(event.Attributes[0].Value) - if err != nil { - panic(err) - } + receiver := sdk.MustAccAddressFromBech32(event.Attributes[0].Value) coins, err := sdk.ParseCoinsNormalized(event.Attributes[1].Value) if err != nil { panic(err) diff --git a/simapp/export.go b/simapp/export.go index 19a4a64ae56c..a1587e58e83e 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -85,10 +85,8 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] panic(err) } - delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) - if err != nil { - panic(err) - } + delAddr := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) + _, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr) } @@ -122,10 +120,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] if err != nil { panic(err) } - delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress) - if err != nil { - panic(err) - } + delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress) if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil { // never called as BeforeDelegationCreated always returns nil diff --git a/types/address.go b/types/address.go index 3da2bffdfd38..c569e56dd4d1 100644 --- a/types/address.go +++ b/types/address.go @@ -163,6 +163,16 @@ func VerifyAddressFormat(bz []byte) error { return nil } +// MustAccAddressFromBech32 calls AccAddressFromBech32 and panics on error. +func MustAccAddressFromBech32(address string) AccAddress { + addr, err := AccAddressFromBech32(address) + if err != nil { + panic(err) + } + + return addr +} + // AccAddressFromBech32 creates an AccAddress from a Bech32 string. func AccAddressFromBech32(address string) (addr AccAddress, err error) { if len(strings.TrimSpace(address)) == 0 { diff --git a/types/tx/types.go b/types/tx/types.go index a4816b1283a6..1c39d6c085c3 100644 --- a/types/tx/types.go +++ b/types/tx/types.go @@ -114,10 +114,7 @@ func (t *Tx) GetSigners() []sdk.AccAddress { // ensure any specified fee payer is included in the required signers (at the end) feePayer := t.AuthInfo.Fee.Payer if feePayer != "" && !seen[feePayer] { - payerAddr, err := sdk.AccAddressFromBech32(feePayer) - if err != nil { - panic(err) - } + payerAddr := sdk.MustAccAddressFromBech32(feePayer) signers = append(signers, payerAddr) seen[feePayer] = true } @@ -136,11 +133,7 @@ func (t *Tx) GetFee() sdk.Coins { func (t *Tx) FeePayer() sdk.AccAddress { feePayer := t.AuthInfo.Fee.Payer if feePayer != "" { - payerAddr, err := sdk.AccAddressFromBech32(feePayer) - if err != nil { - panic(err) - } - return payerAddr + return sdk.MustAccAddressFromBech32(feePayer) } // use first signer as default if no payer specified return t.GetSigners()[0] @@ -149,11 +142,7 @@ func (t *Tx) FeePayer() sdk.AccAddress { func (t *Tx) FeeGranter() sdk.AccAddress { feePayer := t.AuthInfo.Fee.Granter if feePayer != "" { - granterAddr, err := sdk.AccAddressFromBech32(feePayer) - if err != nil { - panic(err) - } - return granterAddr + return sdk.MustAccAddressFromBech32(feePayer) } return nil } diff --git a/x/auth/tx/builder.go b/x/auth/tx/builder.go index 052da8785ee8..4a4d81492e9d 100644 --- a/x/auth/tx/builder.go +++ b/x/auth/tx/builder.go @@ -140,11 +140,7 @@ func (w *wrapper) GetFee() sdk.Coins { func (w *wrapper) FeePayer() sdk.AccAddress { feePayer := w.tx.AuthInfo.Fee.Payer if feePayer != "" { - payerAddr, err := sdk.AccAddressFromBech32(feePayer) - if err != nil { - panic(err) - } - return payerAddr + return sdk.MustAccAddressFromBech32(feePayer) } // use first signer as default if no payer specified return w.GetSigners()[0] @@ -153,11 +149,7 @@ func (w *wrapper) FeePayer() sdk.AccAddress { func (w *wrapper) FeeGranter() sdk.AccAddress { feePayer := w.tx.AuthInfo.Fee.Granter if feePayer != "" { - granterAddr, err := sdk.AccAddressFromBech32(feePayer) - if err != nil { - panic(err) - } - return granterAddr + return sdk.MustAccAddressFromBech32(feePayer) } return nil } diff --git a/x/authz/keeper/genesis.go b/x/authz/keeper/genesis.go index f2619cba7484..6ef975a74dfe 100644 --- a/x/authz/keeper/genesis.go +++ b/x/authz/keeper/genesis.go @@ -14,22 +14,15 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) { continue } - grantee, err := sdk.AccAddressFromBech32(entry.Grantee) - if err != nil { - panic(err) - } - - granter, err := sdk.AccAddressFromBech32(entry.Granter) - if err != nil { - panic(err) - } + grantee := sdk.MustAccAddressFromBech32(entry.Grantee) + granter := sdk.MustAccAddressFromBech32(entry.Granter) a, ok := entry.Authorization.GetCachedValue().(authz.Authorization) if !ok { panic("expected authorization") } - err = k.SaveGrant(ctx, grantee, granter, a, entry.Expiration) + err := k.SaveGrant(ctx, grantee, granter, a, entry.Expiration) if err != nil { panic(err) } diff --git a/x/bank/keeper/genesis.go b/x/bank/keeper/genesis.go index 694fa6668e61..b83316fe3d28 100644 --- a/x/bank/keeper/genesis.go +++ b/x/bank/keeper/genesis.go @@ -16,10 +16,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) { genState.Balances = types.SanitizeGenesisBalances(genState.Balances) for _, balance := range genState.Balances { - addr, err := sdk.AccAddressFromBech32(balance.Address) - if err != nil { - panic(err) - } + addr := balance.GetAddress() if err := k.initBalances(ctx, addr, balance.Coins); err != nil { panic(fmt.Errorf("error on setting balances %w", err)) diff --git a/x/bank/keeper/msg_server.go b/x/bank/keeper/msg_server.go index 5d940b459e47..4e9237631d54 100644 --- a/x/bank/keeper/msg_server.go +++ b/x/bank/keeper/msg_server.go @@ -81,10 +81,8 @@ func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*t } for _, out := range msg.Outputs { - accAddr, err := sdk.AccAddressFromBech32(out.Address) - if err != nil { - panic(err) - } + accAddr := sdk.MustAccAddressFromBech32(out.Address) + if k.BlockedAddr(accAddr) { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive transactions", out.Address) } diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index 56a9919224b7..dae8471b0edf 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -323,10 +323,7 @@ func sendMsgMultiSend( sequenceNumbers := make([]uint64, len(msg.Inputs)) for i := 0; i < len(msg.Inputs); i++ { - addr, err := sdk.AccAddressFromBech32(msg.Inputs[i].Address) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(msg.Inputs[i].Address) acc := ak.GetAccount(ctx, addr) accountNumbers[i] = acc.GetAccountNumber() sequenceNumbers[i] = acc.GetSequence() @@ -337,10 +334,7 @@ func sendMsgMultiSend( err error ) - addr, err := sdk.AccAddressFromBech32(msg.Inputs[0].Address) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(msg.Inputs[0].Address) // feePayer is the first signer, i.e. first input address feePayer := ak.GetAccount(ctx, addr) diff --git a/x/bank/types/balance.go b/x/bank/types/balance.go index 56f73ae98219..3b0decadbea0 100644 --- a/x/bank/types/balance.go +++ b/x/bank/types/balance.go @@ -3,7 +3,6 @@ package types import ( "bytes" "encoding/json" - "fmt" "sort" "github.com/cosmos/cosmos-sdk/codec" @@ -15,12 +14,7 @@ var _ exported.GenesisBalance = (*Balance)(nil) // GetAddress returns the account address of the Balance object. func (b Balance) GetAddress() sdk.AccAddress { - addr, err := sdk.AccAddressFromBech32(b.Address) - if err != nil { - panic(fmt.Errorf("couldn't convert %q to account address: %v", b.Address, err)) - } - - return addr + return sdk.MustAccAddressFromBech32(b.Address) } // GetCoins returns the account coins of the Balance object. diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index 5cb65d760c1e..6236e74902c8 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -15,15 +15,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { k.SetParams(ctx, data.Params) for _, dwi := range data.DelegatorWithdrawInfos { - delegatorAddress, err := sdk.AccAddressFromBech32(dwi.DelegatorAddress) - if err != nil { - panic(err) - } - withdrawAddress, err := sdk.AccAddressFromBech32(dwi.WithdrawAddress) - if err != nil { - panic(err) - } - + delegatorAddress := sdk.MustAccAddressFromBech32(dwi.DelegatorAddress) + withdrawAddress := sdk.MustAccAddressFromBech32(dwi.WithdrawAddress) k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress) } @@ -72,10 +65,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { if err != nil { panic(err) } - delegatorAddress, err := sdk.AccAddressFromBech32(del.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(del.DelegatorAddress) + k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo) } for _, evt := range data.ValidatorSlashEvents { diff --git a/x/feegrant/simulation/operations.go b/x/feegrant/simulation/operations.go index 021e5ee79c96..652b3b2f1acd 100644 --- a/x/feegrant/simulation/operations.go +++ b/x/feegrant/simulation/operations.go @@ -116,14 +116,8 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe var granterAddr sdk.AccAddress var granteeAddr sdk.AccAddress k.IterateAllFeeAllowances(ctx, func(grant feegrant.Grant) bool { - granter, err := sdk.AccAddressFromBech32(grant.Granter) - if err != nil { - panic(err) - } - grantee, err := sdk.AccAddressFromBech32(grant.Grantee) - if err != nil { - panic(err) - } + granter := sdk.MustAccAddressFromBech32(grant.Granter) + grantee := sdk.MustAccAddressFromBech32(grant.Grantee) granterAddr = granter granteeAddr = grantee hasGrant = true diff --git a/x/gov/keeper/deposit.go b/x/gov/keeper/deposit.go index 2dcd50f9ff6a..ee57d91bf257 100644 --- a/x/gov/keeper/deposit.go +++ b/x/gov/keeper/deposit.go @@ -26,10 +26,7 @@ func (keeper Keeper) GetDeposit(ctx sdk.Context, proposalID uint64, depositorAdd func (keeper Keeper) SetDeposit(ctx sdk.Context, deposit v1.Deposit) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshal(&deposit) - depositor, err := sdk.AccAddressFromBech32(deposit.Depositor) - if err != nil { - panic(err) - } + depositor := sdk.MustAccAddressFromBech32(deposit.Depositor) store.Set(types.DepositKey(deposit.ProposalId, depositor), bz) } @@ -64,10 +61,8 @@ func (keeper Keeper) DeleteAndBurnDeposits(ctx sdk.Context, proposalID uint64) { panic(err) } - depositor, err := sdk.AccAddressFromBech32(deposit.Depositor) - if err != nil { - panic(err) - } + depositor := sdk.MustAccAddressFromBech32(deposit.Depositor) + store.Delete(types.DepositKey(proposalID, depositor)) return false }) @@ -172,12 +167,9 @@ func (keeper Keeper) RefundAndDeleteDeposits(ctx sdk.Context, proposalID uint64) store := ctx.KVStore(keeper.storeKey) keeper.IterateDeposits(ctx, proposalID, func(deposit v1.Deposit) bool { - depositor, err := sdk.AccAddressFromBech32(deposit.Depositor) - if err != nil { - panic(err) - } + depositor := sdk.MustAccAddressFromBech32(deposit.Depositor) - err = keeper.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, depositor, deposit.Amount) + err := keeper.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, depositor, deposit.Amount) if err != nil { panic(err) } diff --git a/x/gov/keeper/msg_server.go b/x/gov/keeper/msg_server.go index 8800d2d79eca..24e809ce31be 100644 --- a/x/gov/keeper/msg_server.go +++ b/x/gov/keeper/msg_server.go @@ -109,11 +109,11 @@ func (k msgServer) ExecLegacyContent(goCtx context.Context, msg *v1.MsgExecLegac func (k msgServer) Vote(goCtx context.Context, msg *v1.MsgVote) (*v1.MsgVoteResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - accAddr, accErr := sdk.AccAddressFromBech32(msg.Voter) - if accErr != nil { - return nil, accErr + accAddr, err := sdk.AccAddressFromBech32(msg.Voter) + if err != nil { + return nil, err } - err := k.Keeper.AddVote(ctx, msg.ProposalId, accAddr, v1.NewNonSplitVoteOption(msg.Option), msg.Metadata) + err = k.Keeper.AddVote(ctx, msg.ProposalId, accAddr, v1.NewNonSplitVoteOption(msg.Option), msg.Metadata) if err != nil { return nil, err } diff --git a/x/gov/keeper/tally.go b/x/gov/keeper/tally.go index 3f1dd10ec7dd..5959989b2490 100644 --- a/x/gov/keeper/tally.go +++ b/x/gov/keeper/tally.go @@ -35,10 +35,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool, keeper.IterateVotes(ctx, proposal.Id, func(vote v1.Vote) bool { // if validator, just record it in the map - voter, err := sdk.AccAddressFromBech32(vote.Voter) - if err != nil { - panic(err) - } + voter := sdk.MustAccAddressFromBech32(vote.Voter) valAddrStr := sdk.ValAddress(voter.Bytes()).String() if val, ok := currValidators[valAddrStr]; ok { diff --git a/x/gov/keeper/vote.go b/x/gov/keeper/vote.go index c37c143ce409..4e2436ff84b5 100644 --- a/x/gov/keeper/vote.go +++ b/x/gov/keeper/vote.go @@ -81,10 +81,8 @@ func (keeper Keeper) GetVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.A func (keeper Keeper) SetVote(ctx sdk.Context, vote v1.Vote) { store := ctx.KVStore(keeper.storeKey) bz := keeper.cdc.MustMarshal(&vote) - addr, err := sdk.AccAddressFromBech32(vote.Voter) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(vote.Voter) + store.Set(types.VoteKey(vote.ProposalId, addr), bz) } diff --git a/x/group/msgs.go b/x/group/msgs.go index 04d52903240b..639bcda1de37 100644 --- a/x/group/msgs.go +++ b/x/group/msgs.go @@ -26,10 +26,8 @@ func (m MsgCreateGroup) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgCreateGroup. func (m MsgCreateGroup) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -78,10 +76,8 @@ func (m MsgUpdateGroupAdmin) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgUpdateGroupAdmin. func (m MsgUpdateGroupAdmin) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -128,10 +124,8 @@ func (m MsgUpdateGroupMetadata) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgUpdateGroupMetadata. func (m MsgUpdateGroupMetadata) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -171,10 +165,8 @@ var _ sdk.Msg = &MsgUpdateGroupMembers{} // GetSigners returns the expected signers for a MsgUpdateGroupMembers. func (m MsgUpdateGroupMembers) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -263,10 +255,7 @@ func (m MsgCreateGroupWithPolicy) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgCreateGroupWithPolicy. func (m MsgCreateGroupWithPolicy) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) return []sdk.AccAddress{admin} } @@ -349,10 +338,8 @@ func (m MsgUpdateGroupPolicyAdmin) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgUpdateGroupPolicyAdmin. func (m MsgUpdateGroupPolicyAdmin) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -427,10 +414,8 @@ func (m MsgUpdateGroupPolicyDecisionPolicy) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgUpdateGroupPolicyDecisionPolicy. func (m MsgUpdateGroupPolicyDecisionPolicy) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -490,10 +475,8 @@ func (m MsgUpdateGroupPolicyMetadata) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgUpdateGroupPolicyMetadata. func (m MsgUpdateGroupPolicyMetadata) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Admin) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Admin) + return []sdk.AccAddress{admin} } @@ -690,10 +673,8 @@ func (m MsgWithdrawProposal) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgWithdrawProposal. func (m MsgWithdrawProposal) GetSigners() []sdk.AccAddress { - admin, err := sdk.AccAddressFromBech32(m.Address) - if err != nil { - panic(err) - } + admin := sdk.MustAccAddressFromBech32(m.Address) + return []sdk.AccAddress{admin} } @@ -728,10 +709,8 @@ func (m MsgVote) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgVote. func (m MsgVote) GetSigners() []sdk.AccAddress { - addr, err := sdk.AccAddressFromBech32(m.Voter) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(m.Voter) + return []sdk.AccAddress{addr} } @@ -770,10 +749,8 @@ func (m MsgExec) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgExec. func (m MsgExec) GetSigners() []sdk.AccAddress { - signer, err := sdk.AccAddressFromBech32(m.Executor) - if err != nil { - panic(err) - } + signer := sdk.MustAccAddressFromBech32(m.Executor) + return []sdk.AccAddress{signer} } @@ -806,10 +783,7 @@ func (m MsgLeaveGroup) GetSignBytes() []byte { // GetSigners returns the expected signers for a MsgLeaveGroup func (m MsgLeaveGroup) GetSigners() []sdk.AccAddress { - signer, err := sdk.AccAddressFromBech32(m.Address) - if err != nil { - panic(err) - } + signer := sdk.MustAccAddressFromBech32(m.Address) return []sdk.AccAddress{signer} } diff --git a/x/group/types.go b/x/group/types.go index 86c0dfd9c1d7..f6b757f4d70d 100644 --- a/x/group/types.go +++ b/x/group/types.go @@ -298,10 +298,8 @@ func (g GroupInfo) ValidateBasic() error { } func (g GroupPolicyInfo) PrimaryKeyFields() []interface{} { - addr, err := sdk.AccAddressFromBech32(g.Address) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(g.Address) + return []interface{}{addr.Bytes()} } @@ -337,10 +335,8 @@ func (g GroupPolicyInfo) ValidateBasic() error { } func (g GroupMember) PrimaryKeyFields() []interface{} { - addr, err := sdk.AccAddressFromBech32(g.Member.Address) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(g.Member.Address) + return []interface{}{g.GroupId, addr.Bytes()} } @@ -402,10 +398,8 @@ func (p Proposal) ValidateBasic() error { } func (v Vote) PrimaryKeyFields() []interface{} { - addr, err := sdk.AccAddressFromBech32(v.Voter) - if err != nil { - panic(err) - } + addr := sdk.MustAccAddressFromBech32(v.Voter) + return []interface{}{v.ProposalId, addr.Bytes()} } diff --git a/x/nft/keeper/genesis.go b/x/nft/keeper/genesis.go index 6018ca2e076d..116bf0bd2494 100644 --- a/x/nft/keeper/genesis.go +++ b/x/nft/keeper/genesis.go @@ -16,10 +16,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *nft.GenesisState) { } for _, entry := range data.Entries { for _, nft := range entry.Nfts { - owner, err := sdk.AccAddressFromBech32(entry.Owner) - if err != nil { - panic(err) - } + owner := sdk.MustAccAddressFromBech32(entry.Owner) if err := k.Mint(ctx, *nft, owner); err != nil { panic(err) diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index 4c99e4adaedb..a7af75af4448 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -90,10 +90,7 @@ func (k Keeper) GetDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddres // SetDelegation sets a delegation. func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { - delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) store := ctx.KVStore(k.storeKey) b := types.MustMarshalDelegation(k.cdc, delegation) @@ -102,10 +99,8 @@ func (k Keeper) SetDelegation(ctx sdk.Context, delegation types.Delegation) { // RemoveDelegation removes a delegation func (k Keeper) RemoveDelegation(ctx sdk.Context, delegation types.Delegation) error { - delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) + // TODO: Consider calling hooks outside of the store wrapper functions, it's unobvious. if err := k.BeforeDelegationRemoved(ctx, delegatorAddress, delegation.GetValidatorAddr()); err != nil { return err @@ -275,10 +270,8 @@ func (k Keeper) HasMaxUnbondingDelegationEntries(ctx sdk.Context, delegatorAddr // SetUnbondingDelegation sets the unbonding delegation and associated index. func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { - delegatorAddress, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(ubd.DelegatorAddress) + store := ctx.KVStore(k.storeKey) bz := types.MustMarshalUBD(k.cdc, ubd) addr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress) @@ -292,10 +285,7 @@ func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDeleg // RemoveUnbondingDelegation removes the unbonding delegation object and associated index. func (k Keeper) RemoveUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) { - delegatorAddress, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(ubd.DelegatorAddress) store := ctx.KVStore(k.storeKey) addr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress) @@ -470,10 +460,7 @@ func (k Keeper) HasMaxRedelegationEntries(ctx sdk.Context, delegatorAddr sdk.Acc // SetRedelegation set a redelegation and associated index. func (k Keeper) SetRedelegation(ctx sdk.Context, red types.Redelegation) { - delegatorAddress, err := sdk.AccAddressFromBech32(red.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(red.DelegatorAddress) store := ctx.KVStore(k.storeKey) bz := types.MustMarshalRED(k.cdc, red) @@ -530,10 +517,8 @@ func (k Keeper) IterateRedelegations(ctx sdk.Context, fn func(index int64, red t // RemoveRedelegation removes a redelegation object and associated index. func (k Keeper) RemoveRedelegation(ctx sdk.Context, red types.Redelegation) { - delegatorAddress, err := sdk.AccAddressFromBech32(red.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(red.DelegatorAddress) + store := ctx.KVStore(k.storeKey) valSrcAddr, err := sdk.ValAddressFromBech32(red.ValidatorSrcAddress) if err != nil { @@ -653,10 +638,7 @@ func (k Keeper) Delegate( return sdk.ZeroDec(), err } - delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) // if subtractAccount is true then we are // performing a delegation and not a redelegation, thus the source tokens are diff --git a/x/staking/keeper/genesis.go b/x/staking/keeper/genesis.go index 86eae3ada04b..b47e32de1b3e 100644 --- a/x/staking/keeper/genesis.go +++ b/x/staking/keeper/genesis.go @@ -60,10 +60,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) (res []ab } for _, delegation := range data.Delegations { - delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress) // Call the before-creation hook if not exported if !data.Exported { diff --git a/x/staking/keeper/querier.go b/x/staking/keeper/querier.go index 95c26d49a45d..b3ec7e18d92f 100644 --- a/x/staking/keeper/querier.go +++ b/x/staking/keeper/querier.go @@ -496,10 +496,8 @@ func RedelegationsToRedelegationResponses( panic(err) } - delegatorAddress, err := sdk.AccAddressFromBech32(redel.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(redel.DelegatorAddress) + val, found := k.GetValidator(ctx, valDstAddr) if !found { return nil, types.ErrNoValidatorFound diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index d09fb56c270f..75e22ca49fb2 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -253,10 +253,7 @@ func (k Keeper) SlashRedelegation(ctx sdk.Context, srcValidator types.Validator, panic(err) } - delegatorAddress, err := sdk.AccAddressFromBech32(redelegation.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(redelegation.DelegatorAddress) delegation, found := k.GetDelegation(ctx, delegatorAddress, valDstAddr) if !found { diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index 7392936f5421..22f699366119 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -39,10 +39,8 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate { if err != nil { panic(err) } - delegatorAddress, err := sdk.AccAddressFromBech32(dvPair.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(dvPair.DelegatorAddress) + balances, err := k.CompleteUnbonding(ctx, delegatorAddress, addr) if err != nil { continue @@ -69,10 +67,8 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate { if err != nil { panic(err) } - delegatorAddress, err := sdk.AccAddressFromBech32(dvvTriplet.DelegatorAddress) - if err != nil { - panic(err) - } + delegatorAddress := sdk.MustAccAddressFromBech32(dvvTriplet.DelegatorAddress) + balances, err := k.CompleteRedelegation( ctx, delegatorAddress, diff --git a/x/staking/types/delegation.go b/x/staking/types/delegation.go index 07438c4f40af..39a45d41d67f 100644 --- a/x/staking/types/delegation.go +++ b/x/staking/types/delegation.go @@ -60,10 +60,8 @@ func UnmarshalDelegation(cdc codec.BinaryCodec, value []byte) (delegation Delega } func (d Delegation) GetDelegatorAddr() sdk.AccAddress { - delAddr, err := sdk.AccAddressFromBech32(d.DelegatorAddress) - if err != nil { - panic(err) - } + delAddr := sdk.MustAccAddressFromBech32(d.DelegatorAddress) + return delAddr }