Skip to content

Commit

Permalink
feat: replace broadcaster's account retriever to use chain's bech32 p…
Browse files Browse the repository at this point in the history
…refix when converting AccAddress to string (#1009)

* feat: replace broadcaster's account retriever to use chain's bech32 prefix when converting AccAddress to string

* refactor: enhance AccountRetriever

(cherry picked from commit 1d1b837)

# Conflicts:
#	chain/cosmos/broadcaster.go
  • Loading branch information
madrezaz authored and mergify[bot] committed Mar 7, 2024
1 parent 8be1782 commit 30bc2b7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
88 changes: 88 additions & 0 deletions chain/cosmos/account_retriever.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cosmos

import (
"context"
"fmt"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"strconv"

grpc "google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

var (
_ client.Account = sdk.AccountI(nil)
_ client.AccountRetriever = AccountRetriever{}
)

// AccountRetriever defines the properties of a type that can be used to
// retrieve accounts.
type AccountRetriever struct {
chain *CosmosChain
}

// GetAccount queries for an account given an address and a block height. An
// error is returned if the query or decoding fails.
func (ar AccountRetriever) GetAccount(clientCtx client.Context, addr sdk.AccAddress) (client.Account, error) {
account, _, err := ar.GetAccountWithHeight(clientCtx, addr)
return account, err
}

// GetAccountWithHeight queries for an account given an address. Returns the
// height of the query with the account. An error is returned if the query
// or decoding fails.
func (ar AccountRetriever) GetAccountWithHeight(clientCtx client.Context, addr sdk.AccAddress) (client.Account, int64, error) {
var header metadata.MD

bech32Address, err := ar.chain.AccAddressToBech32(addr)
if err != nil {
return nil, 0, err
}

queryClient := authtypes.NewQueryClient(clientCtx)
res, err := queryClient.Account(context.Background(), &authtypes.QueryAccountRequest{Address: bech32Address}, grpc.Header(&header))
if err != nil {
return nil, 0, err
}

blockHeight := header.Get(grpctypes.GRPCBlockHeightHeader)
if l := len(blockHeight); l != 1 {
return nil, 0, fmt.Errorf("unexpected '%s' header length; got %d, expected: %d", grpctypes.GRPCBlockHeightHeader, l, 1)
}

nBlockHeight, err := strconv.Atoi(blockHeight[0])
if err != nil {
return nil, 0, fmt.Errorf("failed to parse block height: %w", err)
}

var acc sdk.AccountI
if err := clientCtx.InterfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
return nil, 0, err
}

return acc, int64(nBlockHeight), nil
}

// EnsureExists returns an error if no account exists for the given address else nil.
func (ar AccountRetriever) EnsureExists(clientCtx client.Context, addr sdk.AccAddress) error {
if _, err := ar.GetAccount(clientCtx, addr); err != nil {
return err
}

return nil
}

// GetAccountNumberSequence returns sequence and account number for the given address.
// It returns an error if the account couldn't be retrieved from the state.
func (ar AccountRetriever) GetAccountNumberSequence(clientCtx client.Context, addr sdk.AccAddress) (uint64, uint64, error) {
acc, err := ar.GetAccount(clientCtx, addr)
if err != nil {
return 0, 0, err
}

return acc.GetAccountNumber(), acc.GetSequence(), nil
}
5 changes: 5 additions & 0 deletions chain/cosmos/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmos

import (
"errors"
"github.com/cosmos/cosmos-sdk/types/bech32"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -26,3 +27,7 @@ func (c *CosmosChain) AccAddressFromBech32(address string) (addr sdk.AccAddress,

return sdk.AccAddress(bz), nil
}

func (c *CosmosChain) AccAddressToBech32(addr sdk.AccAddress) (string, error) {
return bech32.ConvertAndEncode(c.Config().Bech32Prefix, addr)
}
8 changes: 7 additions & 1 deletion chain/cosmos/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
<<<<<<< HEAD

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / unit-tests

expected 'STRING', found '<<'

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / unit-tests

expected 'STRING', found '<<'

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / test-cosmos-examples

expected 'STRING', found '<<'

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / test-conformance

expected 'STRING', found '<<'

Check failure on line 17 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

expected 'STRING', found '<<'
authTx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/strangelove-ventures/interchaintest/v7/internal/dockerutil"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
=======

Check failure on line 22 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/strangelove-ventures/interchaintest/v8/internal/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
>>>>>>> 1d1b837 (feat: replace broadcaster's account retriever to use chain's bech32 prefix when converting AccAddress to string (#1009))

Check failure on line 26 in chain/cosmos/broadcaster.go

View workflow job for this annotation

GitHub Actions / lint

missing import path (typecheck)
)

type ClientContextOpt func(clientContext client.Context) client.Context
Expand Down Expand Up @@ -165,7 +171,7 @@ func (b *Broadcaster) defaultClientContext(fromUser User, sdkAdd sdk.AccAddress)
WithFromAddress(sdkAdd).
WithFromName(fromUser.KeyName()).
WithSkipConfirmation(true).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithAccountRetriever(AccountRetriever{chain: b.chain}).
WithKeyring(kr).
WithBroadcastMode(flags.BroadcastSync).
WithCodec(b.chain.cfg.EncodingConfig.Codec)
Expand Down

0 comments on commit 30bc2b7

Please sign in to comment.