client: add GetAccount and GetAccountWithHeight to AccountRetriever (#7558)
* client: add GetAccount and GetAccountWithHeight to AccountRetriever * update ADR * address comments from review
This commit is contained in:
parent
bf7165414d
commit
503b518efc
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -3,19 +3,67 @@ package client
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
var _ AccountRetriever = TestAccountRetriever{}
|
||||
// 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]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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue