-
Notifications
You must be signed in to change notification settings - Fork 375
/
keeper.go
162 lines (143 loc) · 4.19 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
160
161
162
package auth
import (
"fmt"
"log/slog"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
)
// Concrete implementation of AccountKeeper.
type AccountKeeper struct {
// The (unexposed) key used to access the store from the Context.
key store.StoreKey
// The prototypical Account constructor.
proto func() std.Account
}
// NewAccountKeeper returns a new AccountKeeper that uses go-amino to
// (binary) encode and decode concrete std.Accounts.
func NewAccountKeeper(
key store.StoreKey, proto func() std.Account,
) AccountKeeper {
return AccountKeeper{
key: key,
proto: proto,
}
}
// NewAccountWithAddress implements AccountKeeper.
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr crypto.Address) std.Account {
acc := ak.proto()
// acc.SetSequence(0) // start with 0.
err := acc.SetAddress(addr)
if err != nil {
// Handle w/ #870
panic(err)
}
err = acc.SetAccountNumber(ak.GetNextAccountNumber(ctx))
if err != nil {
// Handle w/ #870
panic(err)
}
return acc
}
// Logger returns a module-specific logger.
func (ak AccountKeeper) Logger(ctx sdk.Context) *slog.Logger {
return ctx.Logger().With("module", ModuleName)
}
// GetAccount returns a specific account in the AccountKeeper.
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr crypto.Address) std.Account {
stor := ctx.Store(ak.key)
bz := stor.Get(AddressStoreKey(addr))
if bz == nil {
return nil
}
acc := ak.decodeAccount(bz)
return acc
}
// GetAllAccounts returns all accounts in the AccountKeeper.
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) []std.Account {
accounts := []std.Account{}
appendAccount := func(acc std.Account) (stop bool) {
accounts = append(accounts, acc)
return false
}
ak.IterateAccounts(ctx, appendAccount)
return accounts
}
// SetAccount implements AccountKeeper.
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc std.Account) {
addr := acc.GetAddress()
stor := ctx.Store(ak.key)
bz, err := amino.MarshalAny(acc)
if err != nil {
panic(err)
}
stor.Set(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.Context, acc std.Account) {
addr := acc.GetAddress()
stor := ctx.Store(ak.key)
stor.Delete(AddressStoreKey(addr))
}
// IterateAccounts implements AccountKeeper.
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, process func(std.Account) (stop bool)) {
stor := ctx.Store(ak.key)
iter := store.PrefixIterator(stor, []byte(AddressStoreKeyPrefix))
defer iter.Close()
for {
if !iter.Valid() {
return
}
val := iter.Value()
acc := ak.decodeAccount(val)
if process(acc) {
return
}
iter.Next()
}
}
// GetPubKey Returns the PubKey of the account at address
func (ak AccountKeeper) GetPubKey(ctx sdk.Context, addr crypto.Address) (crypto.PubKey, error) {
acc := ak.GetAccount(ctx, addr)
if acc == nil {
return nil, std.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", addr))
}
return acc.GetPubKey(), nil
}
// GetSequence Returns the Sequence of the account at address
func (ak AccountKeeper) GetSequence(ctx sdk.Context, addr crypto.Address) (uint64, error) {
acc := ak.GetAccount(ctx, addr)
if acc == nil {
return 0, std.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", addr))
}
return acc.GetSequence(), nil
}
// GetNextAccountNumber Returns and increments the global account number counter
func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 {
var accNumber uint64
stor := ctx.Store(ak.key)
bz := stor.Get([]byte(GlobalAccountNumberKey))
if bz == nil {
accNumber = 0 // start with 0.
} else {
err := amino.Unmarshal(bz, &accNumber)
if err != nil {
panic(err)
}
}
bz = amino.MustMarshal(accNumber + 1)
stor.Set([]byte(GlobalAccountNumberKey), bz)
return accNumber
}
// -----------------------------------------------------------------------------
// Misc.
func (ak AccountKeeper) decodeAccount(bz []byte) (acc std.Account) {
err := amino.Unmarshal(bz, &acc)
if err != nil {
panic(err)
}
return
}