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

auth: Update AccountRetriever #7006

Merged
merged 13 commits into from
Aug 13, 2020
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
ports:
- "26659-26660:26656-26657"
- "1318:1317"
- "9090:9090"
- "9091:9090"
environment:
- ID=1
- LOG=${LOG:-simd.log}
Expand All @@ -42,7 +42,7 @@ services:
ports:
- "26661-26662:26656-26657"
- "1319:1317"
- "9090:9090"
- "9092:9090"
volumes:
- ./build:/simd:Z
networks:
Expand All @@ -58,7 +58,7 @@ services:
ports:
- "26663-26664:26656-26657"
- "1320:1317"
- "9090:9090"
- "9093:9090"
volumes:
- ./build:/simd:Z
networks:
Expand Down
44 changes: 21 additions & 23 deletions x/auth/types/account_retriever.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
package types

import (
"fmt"
context "context"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
// AccountRetriever defines the properties of a type that can be used to
// retrieve accounts.
type AccountRetriever struct {
codec codec.JSONMarshaler
ir types.InterfaceRegistry
}

// NewAccountRetriever initialises a new AccountRetriever instance.
func NewAccountRetriever(codec codec.JSONMarshaler) AccountRetriever {
return AccountRetriever{codec: codec}
// NewAccountRetriever initializes a new AccountRetriever instance.
func NewAccountRetriever(ir types.InterfaceRegistry) AccountRetriever {
return AccountRetriever{ir: ir}
}

// 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(querier client.NodeQuerier, addr sdk.AccAddress) (AccountI, error) {
account, _, err := ar.GetAccountWithHeight(querier, addr)
func (ar AccountRetriever) GetAccount(clientCtx client.Context, addr sdk.AccAddress) (AccountI, error) {
account, _, err := ar.GetAccountWithHeight(clientCtx, addr)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
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(querier client.NodeQuerier, addr sdk.AccAddress) (AccountI, int64, error) {
bs, err := ar.codec.MarshalJSON(QueryAccountRequest{Address: addr})
func (ar AccountRetriever) GetAccountWithHeight(clientCtx client.Context, addr sdk.AccAddress) (AccountI, int64, error) {
queryClient := NewQueryClient(clientCtx)
res, err := queryClient.Account(context.Background(), &QueryAccountRequest{Address: addr})
if err != nil {
return nil, 0, err
}

bz, height, err := querier.QueryWithData(fmt.Sprintf("custom/%s/%s", QuerierRoute, QueryAccount), bs)
if err != nil {
return nil, height, err
}

var account AccountI
if err := ar.codec.UnmarshalJSON(bz, &account); err != nil {
return nil, height, err
var acc AccountI
if err := ar.ir.UnpackAny(res.Account, &acc); err != nil {
return nil, 0, err
}

return account, height, nil
return acc, 0, nil
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

// EnsureExists returns an error if no account exists for the given address else nil.
func (ar AccountRetriever) EnsureExists(querier client.NodeQuerier, addr sdk.AccAddress) error {
if _, err := ar.GetAccount(querier, addr); err != 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(nodeQuerier client.NodeQuerier, addr sdk.AccAddress) (uint64, uint64, error) {
acc, err := ar.GetAccount(nodeQuerier, addr)
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
}