-
Notifications
You must be signed in to change notification settings - Fork 15
/
keeper.go
243 lines (201 loc) · 6.95 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package account
import (
"fmt"
"time"
app "github.com/TruStory/truchain/types"
"github.com/TruStory/truchain/x/distribution"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/gaskv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
)
// Keeper data type storing keys to the KVStore
type Keeper struct {
storeKey sdk.StoreKey
codec *codec.Codec
paramStore params.Subspace
bankKeeper BankKeeper
accountKeeper auth.AccountKeeper
supplyKeeper supply.Keeper
}
// NewKeeper creates a new keeper of the auth Keeper
func NewKeeper(storeKey sdk.StoreKey, paramStore params.Subspace, codec *codec.Codec, bankKeeper BankKeeper, accountKeeper auth.AccountKeeper, supplyKeeper supply.Keeper) Keeper {
return Keeper{
storeKey,
codec,
paramStore.WithKeyTable(ParamKeyTable()),
bankKeeper,
accountKeeper,
supplyKeeper,
}
}
// CreateAppAccount creates a new account on chain for a user
func (k Keeper) CreateAppAccount(ctx sdk.Context, address sdk.AccAddress,
coins sdk.Coins, pubKey crypto.PubKey) (appAccnt AppAccount, sdkErr sdk.Error) {
// first create a base account
baseAccount := auth.NewBaseAccountWithAddress(address)
err := baseAccount.SetPubKey(pubKey)
if err != nil {
return appAccnt, ErrAppAccountCreateFailed(address)
}
k.accountKeeper.SetAccount(ctx, &baseAccount)
// then create an app account
appAccnt = NewAppAccount(address, ctx.BlockHeader().Time)
k.setAppAccount(ctx, appAccnt)
// set initial coins
initialCoinAmount := coins.AmountOf(app.StakeDenom)
if initialCoinAmount.IsPositive() {
coin := sdk.NewCoin(app.StakeDenom, initialCoinAmount)
_, sdkErr := k.bankKeeper.AddCoin(ctx, address, coin, 0, TransactionGift, FromModuleAccount(distribution.UserGrowthPoolName))
if sdkErr != nil {
return appAccnt, sdkErr
}
} else {
return appAccnt, sdk.ErrInvalidCoins("Invalid initial coins")
}
k.Logger(ctx).Info(fmt.Sprintf("Created %s", appAccnt.String()))
return appAccnt, nil
}
// AppAccounts returns all app accounts
func (k Keeper) AppAccounts(ctx sdk.Context) (appAccounts []AppAccount) {
iterator := sdk.KVStorePrefixIterator(k.store(ctx), AppAccountKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var appAccount AppAccount
k.codec.MustUnmarshalBinaryBare(iterator.Value(), &appAccount)
appAccounts = append(appAccounts, appAccount)
}
return appAccounts
}
// PrimaryAccount gets the primary base account
func (k Keeper) PrimaryAccount(ctx sdk.Context, addr sdk.AccAddress) (pAcc PrimaryAccount, err sdk.Error) {
appAcc, ok := k.getAppAccount(ctx, addr)
if !ok {
return pAcc, ErrAppAccountNotFound(addr)
}
acc := k.accountKeeper.GetAccount(ctx, addr)
pAcc = PrimaryAccount{
BaseAccount: auth.BaseAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
PubKey: acc.GetPubKey(),
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
},
SlashCount: appAcc.SlashCount,
IsJailed: appAcc.IsJailed,
JailEndTime: appAcc.JailEndTime,
CreatedTime: appAcc.CreatedTime,
}
return pAcc, nil
}
// JailedAccountsBefore returns all jailed accounts before jailEndTime
func (k Keeper) JailedAccountsBefore(ctx sdk.Context, jailEndTime time.Time) (accounts AppAccounts, err sdk.Error) {
store := k.store(ctx)
iterator := store.Iterator(JailEndTimeAccountPrefix, jailEndTimeAccountsKey(jailEndTime))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
addr := iterator.Value()
user, ok := k.getAppAccount(ctx, addr)
if ok {
accounts = append(accounts, user)
}
}
return accounts, nil
}
// JailUntil puts an AppAccount in jail until a time
func (k Keeper) JailUntil(ctx sdk.Context, address sdk.AccAddress, until time.Time) sdk.Error {
user, ok := k.getAppAccount(ctx, address)
if !ok {
return ErrAppAccountNotFound(address)
}
// delete previous jail time
if user.IsJailed {
k.deleteJailEndTimeAccount(ctx, user.JailEndTime, user.Addresses[0])
}
user.IsJailed = true
user.JailEndTime = until
k.setAppAccount(ctx, user)
// persist in jail list (sorted by jail end time)
k.setJailEndTimeAccount(ctx, until, address)
return nil
}
// UnJail unjails an AppAccount.
func (k Keeper) UnJail(ctx sdk.Context, address sdk.AccAddress) sdk.Error {
user, ok := k.getAppAccount(ctx, address)
if !ok {
return ErrAppAccountNotFound(address)
}
user.IsJailed = false
k.deleteJailEndTimeAccount(ctx, user.JailEndTime, user.Addresses[0])
k.setAppAccount(ctx, user)
return nil
}
// IsJailed tells whether an AppAccount is jailed by its address
func (k Keeper) IsJailed(ctx sdk.Context, address sdk.AccAddress) (bool, sdk.Error) {
user, ok := k.getAppAccount(ctx, address)
if !ok {
return false, ErrAppAccountNotFound(address)
}
return user.IsJailed, nil
}
// IncrementSlashCount increments the slash count of the user
func (k Keeper) IncrementSlashCount(ctx sdk.Context, address sdk.AccAddress) (jailed bool, err sdk.Error) {
user, ok := k.getAppAccount(ctx, address)
if !ok {
return false, ErrAppAccountNotFound(address)
}
user.SlashCount++
k.setAppAccount(ctx, user)
if user.SlashCount >= k.GetParams(ctx).MaxSlashCount {
jailEndTime := ctx.BlockHeader().Time.Add(k.GetParams(ctx).JailDuration)
err := k.JailUntil(ctx, user.Addresses[0], jailEndTime)
if err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// IterateAppAccounts iterates over all the stored app accounts and performs a callback function
func (k Keeper) IterateAppAccounts(ctx sdk.Context, cb func(acc AppAccount) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(k.store(ctx), AppAccountKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var account AppAccount
k.codec.MustUnmarshalBinaryBare(iterator.Value(), &account)
if cb(account) {
break
}
}
}
func (k Keeper) getAppAccount(ctx sdk.Context, addr sdk.AccAddress) (acc AppAccount, ok bool) {
accBytes := k.store(ctx).Get(key(addr))
if accBytes == nil {
return
}
k.codec.MustUnmarshalBinaryBare(accBytes, &acc)
return acc, true
}
func (k Keeper) setAppAccount(ctx sdk.Context, acc AppAccount) {
accBytes := k.codec.MustMarshalBinaryBare(acc)
k.store(ctx).Set(key(acc.PrimaryAddress()), accBytes)
}
func (k Keeper) setJailEndTimeAccount(ctx sdk.Context, jailEndTime time.Time, addr sdk.AccAddress) {
store := k.store(ctx)
store.Set(jailEndTimeAccountKey(jailEndTime, addr), addr)
}
func (k Keeper) deleteJailEndTimeAccount(ctx sdk.Context, jailEndTime time.Time, addr sdk.AccAddress) {
store := k.store(ctx)
store.Delete(jailEndTimeAccountKey(jailEndTime, addr))
}
func (k Keeper) store(ctx sdk.Context) sdk.KVStore {
return gaskv.NewStore(ctx.MultiStore().GetKVStore(k.storeKey), ctx.GasMeter(), app.KVGasConfig())
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", ModuleName)
}