-
Notifications
You must be signed in to change notification settings - Fork 1
/
violation_report.go
72 lines (59 loc) · 2.26 KB
/
violation_report.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
package keeper
import (
"bytes"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rarimo/rarimo-core/x/rarimocore/types"
)
// SetViolationReport set a violation report in the store from its index
func (k Keeper) SetViolationReport(ctx sdk.Context, v types.ViolationReport) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ViolationReportKeyPrefix))
b := k.cdc.MustMarshal(&v)
store.Set(types.ViolationReportKey(v.Index), b)
}
// GetViolationReport returns a ViolationReport from its index
func (k Keeper) GetViolationReport(
ctx sdk.Context,
index *types.ViolationReportIndex,
) (val types.ViolationReport, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ViolationReportKeyPrefix))
b := store.Get(types.ViolationReportKey(index))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveViolationReport removes a ViolationReport from the store
func (k Keeper) RemoveViolationReport(
ctx sdk.Context,
index *types.ViolationReportIndex,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ViolationReportKeyPrefix))
store.Delete(types.ViolationReportKey(index))
}
// GetAllViolationReport returns all ViolationReport
func (k Keeper) GetAllViolationReport(ctx sdk.Context) (list []types.ViolationReport) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ViolationReportKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.ViolationReport
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) IterateViolationReports(ctx sdk.Context, sessionId, offender string, f func(report types.ViolationReport) (stop bool)) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ViolationReportKeyPrefix))
storePrefix := bytes.Join([][]byte{[]byte(sessionId), []byte(offender)}, []byte("/"))
iterator := sdk.KVStorePrefixIterator(store, storePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var report types.ViolationReport
k.cdc.MustUnmarshal(iterator.Value(), &report)
if f(report) {
break
}
}
}