/
keeper.go
159 lines (137 loc) · 4.35 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package auth
import (
"fmt"
"github.com/pokt-network/posmint/crypto"
"github.com/tendermint/tendermint/libs/log"
"github.com/pokt-network/posmint/codec"
sdk "github.com/pokt-network/posmint/types"
"github.com/pokt-network/posmint/x/auth/exported"
"github.com/pokt-network/posmint/x/auth/types"
)
// AccountKeeper encodes/decodes accounts using the go-amino (binary)
// encoding/decoding library.
type AccountKeeper struct {
// The (unexposed) key used to access the store from the Context.
key sdk.StoreKey
// The prototypical Account constructor.
proto func() exported.Account
// The codec codec for binary encoding/decoding of accounts.
cdc *codec.Codec
paramSubspace sdk.Subspace
}
// NewAccountKeeper returns a new sdk.AccountKeeper that uses go-amino to
// (binary) encode and decode concrete sdk.Accounts.
// nolint
func NewAccountKeeper(
cdc *codec.Codec, key sdk.StoreKey, paramstore sdk.Subspace, proto func() exported.Account,
) AccountKeeper {
return AccountKeeper{
key: key,
proto: proto,
cdc: cdc,
paramSubspace: paramstore.WithKeyTable(types.ParamKeyTable()),
}
}
// Logger returns a module-specific logger.
func (ak AccountKeeper) Logger(ctx sdk.Ctx) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// NewAccountWithAddress implements sdk.AccountKeeper.
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Ctx, addr sdk.Address) exported.Account {
acc := ak.proto()
err := acc.SetAddress(addr)
if err != nil {
// Handle w/ #870
panic(err)
}
return acc
}
// NewAccount creates a new account
func (ak AccountKeeper) NewAccount(ctx sdk.Ctx, acc exported.Account) exported.Account {
return acc
}
// GetAccount implements sdk.AccountKeeper.
func (ak AccountKeeper) GetAccount(ctx sdk.Ctx, addr sdk.Address) exported.Account {
store := ctx.KVStore(ak.key)
bz := store.Get(types.AddressStoreKey(addr))
if bz == nil {
return nil
}
acc, err := ak.decodeAccount(bz)
if err != nil {
return nil // Could not decode account
}
return acc
}
// GetAllAccounts returns all accounts in the accountKeeper.
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Ctx) []exported.Account {
accounts := []exported.Account{}
appendAccount := func(acc exported.Account) (stop bool) {
accounts = append(accounts, acc)
return false
}
ak.IterateAccounts(ctx, appendAccount)
return accounts
}
// SetAccount implements sdk.AccountKeeper.
func (ak AccountKeeper) SetAccount(ctx sdk.Ctx, acc exported.Account) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.key)
bz, err := ak.cdc.MarshalBinaryBare(acc)
if err != nil {
panic(err)
}
store.Set(types.AddressStoreKey(addr), bz)
}
// RemoveAccount removes an account for the account mapper store.
// NOTE: this will cause supply invariant violation if called
func (ak AccountKeeper) RemoveAccount(ctx sdk.Ctx, acc exported.Account) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.key)
store.Delete(types.AddressStoreKey(addr))
}
// IterateAccounts implements sdk.AccountKeeper.
func (ak AccountKeeper) IterateAccounts(ctx sdk.Ctx, process func(exported.Account) (stop bool)) {
store := ctx.KVStore(ak.key)
iter := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
defer iter.Close()
for {
if !iter.Valid() {
return
}
val := iter.Value()
acc, err := ak.decodeAccount(val)
if err != nil {
panic(err)
}
if process(acc) {
return
}
iter.Next()
}
}
// GetPubKey Returns the PublicKey of the account at address
func (ak AccountKeeper) GetPubKey(ctx sdk.Ctx, addr sdk.Address) (crypto.PublicKey, sdk.Error) {
acc := ak.GetAccount(ctx, addr)
if acc == nil {
return nil, sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", addr))
}
return acc.GetPubKey(), nil
}
// -----------------------------------------------------------------------------
// Params
// SetParams sets the auth module's parameters.
func (ak AccountKeeper) SetParams(ctx sdk.Ctx, params types.Params) {
ak.paramSubspace.SetParamSet(ctx, ¶ms)
}
// GetParams gets the auth module's parameters.
func (ak AccountKeeper) GetParams(ctx sdk.Ctx) (params types.Params) {
ak.paramSubspace.GetParamSet(ctx, ¶ms)
return
}
// -----------------------------------------------------------------------------
// Misc.
func (ak AccountKeeper) decodeAccount(bz []byte) (acc exported.Account, err error) {
err = ak.cdc.UnmarshalBinaryBare(bz, &acc)
return
}