/
privileged.go
67 lines (60 loc) · 2.23 KB
/
privileged.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
package keeper
import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/public-awesome/stargaze/v12/x/cron/types"
)
// SetPrivileged checks if the given contract exists and adds it to the list of privilege contracts
func (k Keeper) SetPrivileged(ctx sdk.Context, contractAddr sdk.AccAddress) error {
if k.wasmKeeper.HasContractInfo(ctx, contractAddr) {
if !k.IsPrivileged(ctx, contractAddr) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PrivilegedContractsKey(contractAddr), []byte{1})
}
event := sdk.NewEvent(
types.EventTypeSetContractPriviledge,
sdk.NewAttribute(wasmtypes.AttributeKeyContractAddr, contractAddr.String()),
)
ctx.EventManager().EmitEvent(event)
} else {
return types.ErrContractDoesNotExist
}
return nil
}
// UnsetPrivileged checks if the given contract exists and if it has privilege and remove it from the list of privileg contracts
func (k Keeper) UnsetPrivileged(ctx sdk.Context, contractAddr sdk.AccAddress) error {
if k.wasmKeeper.HasContractInfo(ctx, contractAddr) {
if k.IsPrivileged(ctx, contractAddr) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.PrivilegedContractsKey(contractAddr))
event := sdk.NewEvent(
types.EventTypeUnsetContractPriviledge,
sdk.NewAttribute(wasmtypes.AttributeKeyContractAddr, contractAddr.String()),
)
ctx.EventManager().EmitEvent(event)
} else {
return types.ErrContractPrivilegeNotSet
}
} else {
return types.ErrContractDoesNotExist
}
return nil
}
// IsPrivileged returns if the given contract is part of the privilege contract list
func (k Keeper) IsPrivileged(ctx sdk.Context, contractAddr sdk.AccAddress) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.PrivilegedContractsKey(contractAddr))
}
// IteratePrivileged executes the given func on all the privilege contracts
func (k Keeper) IteratePrivileged(ctx sdk.Context, cb func(sdk.AccAddress) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrivilegedContractsPrefix)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
// cb returns true to stop early
if cb(iter.Key()) {
return
}
}
}