-
Notifications
You must be signed in to change notification settings - Fork 14
/
keeper.go
164 lines (134 loc) · 4.41 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
package community
import (
"fmt"
app "github.com/TruStory/truchain/types"
"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/params"
log "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
}
// NewKeeper creates a new keeper of the community Keeper
func NewKeeper(storeKey sdk.StoreKey, paramStore params.Subspace, codec *codec.Codec) Keeper {
return Keeper{
storeKey,
codec,
paramStore.WithKeyTable(ParamKeyTable()),
}
}
// NewCommunity creates a new community
func (k Keeper) NewCommunity(ctx sdk.Context, id string, name string, description string, creator sdk.AccAddress) (community Community, err sdk.Error) {
err = k.validateParams(ctx, id, name, description, creator)
if err != nil {
return
}
community = Community{
ID: id,
Name: name,
Description: description,
CreatedTime: ctx.BlockHeader().Time,
}
k.setCommunity(ctx, community)
logger(ctx).Info(fmt.Sprintf("Created %s", community))
return
}
// Community returns a community by its ID
func (k Keeper) Community(ctx sdk.Context, id string) (community Community, err sdk.Error) {
store := k.store(ctx)
communityBytes := store.Get(key(id))
if communityBytes == nil {
return community, ErrCommunityNotFound(community.ID)
}
k.codec.MustUnmarshalBinaryLengthPrefixed(communityBytes, &community)
return community, nil
}
// Communities gets all communities from the KVStore
func (k Keeper) Communities(ctx sdk.Context) (communities []Community) {
store := k.store(ctx)
iterator := sdk.KVStorePrefixIterator(store, CommunityKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var community Community
k.codec.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &community)
communities = append(communities, community)
}
return
}
// AddAdmin adds a new admin
func (k Keeper) AddAdmin(ctx sdk.Context, admin, creator sdk.AccAddress) (err sdk.Error) {
params := k.GetParams(ctx)
// first admin can be added without any authorisation
if len(params.CommunityAdmins) > 0 && !k.isAdmin(ctx, creator) {
err = ErrAddressNotAuthorised()
}
// if already present, don't add again
for _, currentAdmin := range params.CommunityAdmins {
if currentAdmin.Equals(admin) {
return
}
}
params.CommunityAdmins = append(params.CommunityAdmins, admin)
k.SetParams(ctx, params)
return
}
// RemoveAdmin removes an admin
func (k Keeper) RemoveAdmin(ctx sdk.Context, admin, remover sdk.AccAddress) (err sdk.Error) {
if !k.isAdmin(ctx, remover) {
err = ErrAddressNotAuthorised()
}
params := k.GetParams(ctx)
for i, currentAdmin := range params.CommunityAdmins {
if currentAdmin.Equals(admin) {
params.CommunityAdmins = append(params.CommunityAdmins[:i], params.CommunityAdmins[i+1:]...)
}
}
k.SetParams(ctx, params)
return
}
func (k Keeper) validateParams(ctx sdk.Context, id, name, description string, creator sdk.AccAddress) (err sdk.Error) {
params := k.GetParams(ctx)
if len(id) < params.MinIDLength || len(id) > params.MaxIDLength {
err = ErrInvalidCommunityMsg(
fmt.Sprintf("ID must be between %d-%d chars in length", params.MinIDLength, params.MaxIDLength),
)
}
if len(name) < params.MinNameLength || len(name) > params.MaxNameLength {
err = ErrInvalidCommunityMsg(
fmt.Sprintf("Name must be between %d-%d chars in length", params.MinNameLength, params.MaxNameLength),
)
}
if len(description) > params.MaxDescriptionLength {
err = ErrInvalidCommunityMsg(
fmt.Sprintf("Description must be less than %d chars in length", params.MaxDescriptionLength),
)
}
if !k.isAdmin(ctx, creator) {
err = ErrAddressNotAuthorised()
}
return
}
func (k Keeper) setCommunity(ctx sdk.Context, community Community) {
store := k.store(ctx)
bz := k.codec.MustMarshalBinaryLengthPrefixed(community)
store.Set(key(community.ID), bz)
}
func (k Keeper) isAdmin(ctx sdk.Context, address sdk.AccAddress) bool {
for _, admin := range k.GetParams(ctx).CommunityAdmins {
if address.Equals(admin) {
return true
}
}
return false
}
func (k Keeper) store(ctx sdk.Context) sdk.KVStore {
return gaskv.NewStore(ctx.MultiStore().GetKVStore(k.storeKey), ctx.GasMeter(), app.KVGasConfig())
}
func logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", ModuleName)
}