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

chore(types): add MustAccAddressFromBech32 util func (backport #12201) #12204

Merged
merged 1 commit into from
Jun 9, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 2 additions & 8 deletions server/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 3 additions & 14 deletions types/tx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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]
Expand All @@ -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
}
Expand Down
12 changes: 2 additions & 10 deletions x/auth/tx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
}
Expand Down
13 changes: 3 additions & 10 deletions x/authz/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 1 addition & 4 deletions x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions x/bank/types/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"bytes"
"encoding/json"
"fmt"
"sort"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -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.
Expand Down
17 changes: 4 additions & 13 deletions x/distribution/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions x/feegrant/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 5 additions & 13 deletions x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions x/gov/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions x/gov/keeper/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading