Skip to content

Commit

Permalink
chore(types): add MustAccAddressFromBech32 util func (#12201)
Browse files Browse the repository at this point in the history
(cherry picked from commit 82e13b1)

# Conflicts:
#	CHANGELOG.md
#	server/rosetta/converter.go
#	simapp/export.go
#	x/authz/keeper/genesis.go
#	x/feegrant/simulation/operations.go
#	x/gov/keeper/deposit.go
#	x/gov/keeper/msg_server.go
#	x/gov/keeper/tally.go
#	x/group/msgs.go
#	x/group/types.go
#	x/nft/keeper/genesis.go
#	x/staking/keeper/delegation.go
#	x/staking/keeper/genesis.go
  • Loading branch information
fedekunze authored and mergify[bot] committed Jun 9, 2022
1 parent 48c2f77 commit 70d04af
Show file tree
Hide file tree
Showing 26 changed files with 1,874 additions and 124 deletions.
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

<<<<<<< HEAD
=======
* (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.
* [\#11511](https://github.com/cosmos/cosmos-sdk/pull/11511) Add api server flags to start command.
* [\#11484](https://github.com/cosmos/cosmos-sdk/pull/11484) Implement getter for keyring backend option.
* [\#11449](https://github.com/cosmos/cosmos-sdk/pull/11449) Improved error messages when node isn't synced.
* [\#11349](https://github.com/cosmos/cosmos-sdk/pull/11349) Add `RegisterAminoMsg` function that checks that a msg name is <40 chars (else this would break ledger nano signing) then registers the concrete msg type with amino, it should be used for registering `sdk.Msg`s with amino instead of `cdc.RegisterConcrete`.
* [\#11089](https://github.com/cosmos/cosmos-sdk/pull/11089]) Now cosmos-sdk consumers can upgrade gRPC to its newest versions.
* [\#10439](https://github.com/cosmos/cosmos-sdk/pull/10439) Check error for `RegisterQueryHandlerClient` in all modules `RegisterGRPCGatewayRoutes`.
* [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Remove gogoproto `moretags` YAML annotations and add `sigs.k8s.io/yaml` for YAML marshalling.
* (x/bank) [\#10134](https://github.com/cosmos/cosmos-sdk/pull/10134) Add `HasDenomMetadata` function to bank `Keeper` to check if a client coin denom metadata exists in state.
* (x/bank) [\#10022](https://github.com/cosmos/cosmos-sdk/pull/10022) `BankKeeper.SendCoins` now takes less execution time.
* (deps) [\#9987](https://github.com/cosmos/cosmos-sdk/pull/9987) Bump Go version minimum requirement to `1.17`
* (cli) [\#9856](https://github.com/cosmos/cosmos-sdk/pull/9856) Overwrite `--sequence` and `--account-number` flags with default flag values when used with `offline=false` in `sign-batch` command.
* (rosetta) [\#10001](https://github.com/cosmos/cosmos-sdk/issues/10001) Add documentation for rosetta-cli dockerfile and rename folder for the rosetta-ci dockerfile
* [\#9699](https://github.com/cosmos/cosmos-sdk/pull/9699) Add `:`, `.`, `-`, and `_` as allowed characters in the default denom regular expression.
* (genesis) [\#9697](https://github.com/cosmos/cosmos-sdk/pull/9697) Ensure `InitGenesis` returns with non-empty validator set.
* [\#10341](https://github.com/cosmos/cosmos-sdk/pull/10341) Move from `io/ioutil` to `io` and `os` packages.
* [\#10468](https://github.com/cosmos/cosmos-sdk/pull/10468) Allow futureOps to queue additional operations in simulations
* [\#10625](https://github.com/cosmos/cosmos-sdk/pull/10625) Add `--fee-payer` CLI flag
* (cli) [\#10683](https://github.com/cosmos/cosmos-sdk/pull/10683) In CLI, allow 1 SIGN_MODE_DIRECT signer in transactions with multiple signers.
* (deps) [\#10210](https://github.com/cosmos/cosmos-sdk/pull/10210) Bump Tendermint to [v0.35.0](https://github.com/tendermint/tendermint/releases/tag/v0.35.0).
* (deps) [\#10706](https://github.com/cosmos/cosmos-sdk/issues/10706) Bump rosetta-sdk-go to v0.7.2 and rosetta-cli to v0.7.3
* (types/errors) [\#10779](https://github.com/cosmos/cosmos-sdk/pull/10779) Move most functionality in `types/errors` to a standalone `errors` go module, except the `RootCodespace` errors and ABCI response helpers. All functions and types that used to live in `types/errors` are now aliased so this is not a breaking change.
* (gov) [\#10854](https://github.com/cosmos/cosmos-sdk/pull/10854) v1beta2's vote doesn't include the deprecate `option VoteOption` anymore. Instead, it only uses `WeightedVoteOption`.
* (types) [\#11004](https://github.com/cosmos/cosmos-sdk/pull/11004) Added mutable versions of many of the sdk.Dec types operations. This improves performance when used by avoiding reallocating a new bigint for each operation.
* (x/auth) [\#10880](https://github.com/cosmos/cosmos-sdk/pull/10880) Added a new query to the tx query service that returns a block with transactions fully decoded.
* (types) [\#11200](https://github.com/cosmos/cosmos-sdk/pull/11200) Added `Min()` and `Max()` operations on sdk.Coins.
* (gov) [\#11287](https://github.com/cosmos/cosmos-sdk/pull/11287) Fix error message when no flags are provided while executing `submit-legacy-proposal` transaction.
* (x/auth) [\#11482](https://github.com/cosmos/cosmos-sdk/pull/11482) Improve panic message when attempting to register a method handler for a message that does not implement sdk.Msg
* (x/staking) [\#11596](https://github.com/cosmos/cosmos-sdk/pull/11596) Add (re)delegation getters
* (errors) [\#11960](https://github.com/cosmos/cosmos-sdk/pull/11960) Removed 'redacted' error message from defaultErrEncoder
* (ante) [#12013](https://github.com/cosmos/cosmos-sdk/pull/12013) Index ante events for failed tx.

### Bug Fixes

* (cli) [#12075](https://github.com/cosmos/cosmos-sdk/pull/12075) Add `p2p-port` to the `gentx` and `create-validator` CLI commands to support custom P2P ports.
* (x/slashing) [\#11973](https://github.com/cosmos/cosmos-sdk/pull/11973) Update StartHeight signing info in AfterValidatorBonded hook when validator re-bonds
* [\#11969](https://github.com/cosmos/cosmos-sdk/pull/11969) Fix the panic error in `x/upgrade` when `AppVersion` is not set.
* (tests) [\#11940](https://github.com/cosmos/cosmos-sdk/pull/11940) Fix some client tests in the `x/gov` module
* [\#11772](https://github.com/cosmos/cosmos-sdk/pull/11772) Limit types.Dec length to avoid overflow.
* [\#11724](https://github.com/cosmos/cosmos-sdk/pull/11724) Fix data race issues with api.Server
>>>>>>> 82e13b168 (chore(types): add MustAccAddressFromBech32 util func (#12201))
* [\#11693](https://github.com/cosmos/cosmos-sdk/pull/11693) Add validation for gentx cmd.
* [\#11686](https://github.com/cosmos/cosmos-sdk/pull/11686) Update the min required Golang version to `1.17`.
* (x/auth/vesting) [\#11652](https://github.com/cosmos/cosmos-sdk/pull/11652) Add util functions for `Period(s)`
Expand Down
10 changes: 10 additions & 0 deletions server/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,16 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations []
default:
return nil, false
case banktypes.EventTypeCoinSpent:
<<<<<<< HEAD
spender, err := sdk.AccAddressFromBech32((string)(event.Attributes[0].Value))
if err != nil {
panic(err)
}
coins, err := sdk.ParseCoinsNormalized((string)(event.Attributes[1].Value))
=======
spender := sdk.MustAccAddressFromBech32(event.Attributes[0].Value)
coins, err := sdk.ParseCoinsNormalized(event.Attributes[1].Value)
>>>>>>> 82e13b168 (chore(types): add MustAccAddressFromBech32 util func (#12201))
if err != nil {
panic(err)
}
Expand All @@ -359,11 +364,16 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations []
accountIdentifier = spender.String()

case banktypes.EventTypeCoinReceived:
<<<<<<< HEAD
receiver, err := sdk.AccAddressFromBech32((string)(event.Attributes[0].Value))
if err != nil {
panic(err)
}
coins, err := sdk.ParseCoinsNormalized((string)(event.Attributes[1].Value))
=======
receiver := sdk.MustAccAddressFromBech32(event.Attributes[0].Value)
coins, err := sdk.ParseCoinsNormalized(event.Attributes[1].Value)
>>>>>>> 82e13b168 (chore(types): add MustAccAddressFromBech32 util func (#12201))
if err != nil {
panic(err)
}
Expand Down
20 changes: 16 additions & 4 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,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 @@ -119,12 +117,26 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
if err != nil {
panic(err)
}
<<<<<<< HEAD
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
if err != nil {
panic(err)
}
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
=======
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)

if err := app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
}

if err := app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
}
>>>>>>> 82e13b168 (chore(types): add MustAccAddressFromBech32 util func (#12201))
}

// reset context height
Expand Down
10 changes: 10 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,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 @@ -118,10 +118,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 @@ -138,11 +135,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 @@ -151,11 +144,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 @@ -134,11 +134,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 @@ -147,11 +143,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
46 changes: 46 additions & 0 deletions x/authz/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
)

// InitGenesis new authz genesis
func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) {
now := ctx.BlockTime()
for _, entry := range data.Authorization {
// ignore expired authorizations
if entry.Expiration != nil && entry.Expiration.Before(now) {
continue
}

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)
if err != nil {
panic(err)
}
}
}

// ExportGenesis returns a GenesisState for a given context.
func (k Keeper) ExportGenesis(ctx sdk.Context) *authz.GenesisState {
var entries []authz.GrantAuthorization
k.IterateGrants(ctx, func(granter, grantee sdk.AccAddress, grant authz.Grant) bool {
entries = append(entries, authz.GrantAuthorization{
Granter: granter.String(),
Grantee: grantee.String(),
Expiration: grant.Expiration,
Authorization: grant.Authorization,
})
return false
})

return authz.NewGenesisState(entries)
}
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 @@ -328,10 +328,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 @@ -342,10 +339,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
5 changes: 5 additions & 0 deletions x/feegrant/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe
var granterAddr sdk.AccAddress
var granteeAddr sdk.AccAddress
k.IterateAllFeeAllowances(ctx, func(grant feegrant.Grant) bool {
<<<<<<< HEAD

granter, err := sdk.AccAddressFromBech32(grant.Granter)
if err != nil {
Expand All @@ -128,6 +129,10 @@ func SimulateMsgRevokeAllowance(ak feegrant.AccountKeeper, bk feegrant.BankKeepe
if err != nil {
panic(err)
}
=======
granter := sdk.MustAccAddressFromBech32(grant.Granter)
grantee := sdk.MustAccAddressFromBech32(grant.Grantee)
>>>>>>> 82e13b168 (chore(types): add MustAccAddressFromBech32 util func (#12201))
granterAddr = granter
granteeAddr = grantee
hasGrant = true
Expand Down
Loading

0 comments on commit 70d04af

Please sign in to comment.