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

client: add GetAccount and GetAccountWithHeight to AccountRetriever #7558

Merged
merged 6 commits into from
Oct 15, 2020
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
20 changes: 17 additions & 3 deletions client/account_retriever.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package client

import "github.com/cosmos/cosmos-sdk/types"
import (
"github.com/tendermint/tendermint/crypto"

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

// Account defines a read-only version of the auth module's AccountI.
type Account interface {
GetAddress() sdk.AccAddress
GetPubKey() crypto.PubKey // can return nil.
GetAccountNumber() uint64
GetSequence() uint64
}

// AccountRetriever defines the interfaces required by transactions to
// ensure an account exists and to be able to query for account fields necessary
// for signing.
type AccountRetriever interface {
EnsureExists(clientCtx Context, addr types.AccAddress) error
GetAccountNumberSequence(clientCtx Context, addr types.AccAddress) (accNum uint64, accSeq uint64, err error)
GetAccount(clientCtx Context, addr sdk.AccAddress) (Account, error)
GetAccountWithHeight(clientCtx Context, addr sdk.AccAddress) (Account, int64, error)
EnsureExists(clientCtx Context, addr sdk.AccAddress) error
GetAccountNumberSequence(clientCtx Context, addr sdk.AccAddress) (accNum uint64, accSeq uint64, err error)
}
58 changes: 53 additions & 5 deletions client/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,67 @@ package client
import (
"fmt"

"github.com/tendermint/tendermint/crypto"

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

var (
_ AccountRetriever = TestAccountRetriever{}
_ Account = TestAccount{}
)

// TestAccount represents a client Account that can be used in unit tests
type TestAccount struct {
Address sdk.AccAddress
Num uint64
Seq uint64
}

// GetAddress implements client Account.GetAddress
func (t TestAccount) GetAddress() sdk.AccAddress {
return t.Address
}

// GetPubKey implements client Account.GetPubKey
func (t TestAccount) GetPubKey() crypto.PubKey {
return nil
}

// GetAccountNumber implements client Account.GetAccountNumber
func (t TestAccount) GetAccountNumber() uint64 {
return t.Num
}

// GetSequence implements client Account.GetSequence
func (t TestAccount) GetSequence() uint64 {
return t.Seq
}

// TestAccountRetriever is an AccountRetriever that can be used in unit tests
type TestAccountRetriever struct {
Accounts map[string]struct {
Address sdk.AccAddress
Num uint64
Seq uint64
Accounts map[string]TestAccount
}

// GetAccount implements AccountRetriever.GetAccount
func (t TestAccountRetriever) GetAccount(_ Context, addr sdk.AccAddress) (Account, error) {
acc, ok := t.Accounts[addr.String()]
if !ok {
return nil, fmt.Errorf("account %s not found", addr)
}

return acc, nil
}

var _ AccountRetriever = TestAccountRetriever{}
// GetAccountWithHeight implements AccountRetriever.GetAccountWithHeight
func (t TestAccountRetriever) GetAccountWithHeight(clientCtx Context, addr sdk.AccAddress) (Account, int64, error) {
acc, err := t.GetAccount(clientCtx, addr)
if err != nil {
return nil, 0, err
}

return acc, 0, nil
}

// EnsureExists implements AccountRetriever.EnsureExists
func (t TestAccountRetriever) EnsureExists(_ Context, addr sdk.AccAddress) error {
Expand Down
3 changes: 3 additions & 0 deletions docs/architecture/adr-020-protobuf-transaction-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- 2020 August 07: Use ADR 027 for serializing `SignDoc`.
- 2020 August 19: Move sequence field from `SignDoc` to `SignerInfo`.
- 2020 September 25: Remove `PublicKey` type in favor of `secp256k1.PubKey`, `ed25519.PubKey` and `multisig.LegacyAminoPubKey`.
- 2020 October 15: Add `GetAccount` and `GetAccountWithHeight` methods to the `AccountRetriever` interface.

## Status

Expand Down Expand Up @@ -315,6 +316,8 @@ and messages.

```go
type AccountRetriever interface {
GetAccount(clientCtx Context, addr sdk.AccAddress) (client.Account, error)
GetAccountWithHeight(clientCtx Context, addr sdk.AccAddress) (client.Account, int64, error)
EnsureExists(clientCtx client.Context, addr sdk.AccAddress) error
GetAccountNumberSequence(clientCtx client.Context, addr sdk.AccAddress) (uint64, uint64, error)
}
Expand Down
9 changes: 7 additions & 2 deletions x/auth/types/account_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

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

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

// 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) (AccountI, error) {
func (ar AccountRetriever) GetAccount(clientCtx client.Context, addr sdk.AccAddress) (client.Account, error) {
account, _, err := ar.GetAccountWithHeight(clientCtx, addr)
return account, err
}
Expand All @@ -28,7 +33,7 @@ func (ar AccountRetriever) GetAccount(clientCtx client.Context, addr sdk.AccAddr
// height of the query with the account. An error is returned if the query
// or decoding fails.
//nolint:interfacer
func (ar AccountRetriever) GetAccountWithHeight(clientCtx client.Context, addr sdk.AccAddress) (AccountI, int64, error) {
func (ar AccountRetriever) GetAccountWithHeight(clientCtx client.Context, addr sdk.AccAddress) (client.Account, int64, error) {
var header metadata.MD

queryClient := NewQueryClient(clientCtx)
Expand Down