-
Notifications
You must be signed in to change notification settings - Fork 50
/
library.go
106 lines (92 loc) · 3.47 KB
/
library.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/shentufoundation/shentu/v2/x/cert/types"
)
// SetLibrary sets a new Certificate library registry.
func (k Keeper) SetLibrary(ctx sdk.Context, library sdk.AccAddress, publisher sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
libraryData := types.Library{Address: library.String(), Publisher: publisher.String()}
store.Set(types.LibraryStoreKey(library), k.cdc.MustMarshalLengthPrefixed(&libraryData))
}
// deleteLibrary deletes a Certificate library registry.
func (k Keeper) deleteLibrary(ctx sdk.Context, library sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.LibraryStoreKey(library))
}
// IsLibrary checks if an address is a Certificate library.
func (k Keeper) IsLibrary(ctx sdk.Context, library sdk.AccAddress) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.LibraryStoreKey(library))
}
// getLibraryPublisher gets the library publisher.
func (k Keeper) getLibraryPublisher(ctx sdk.Context, library sdk.AccAddress) (sdk.AccAddress, error) {
store := ctx.KVStore(k.storeKey)
if bPublisher := store.Get(types.LibraryStoreKey(library)); bPublisher != nil {
var libraryData types.Library
k.cdc.MustUnmarshalLengthPrefixed(bPublisher, &libraryData)
publisherAddr, err := sdk.AccAddressFromBech32(libraryData.Publisher)
if err != nil {
panic(err)
}
return publisherAddr, nil
}
return nil, types.ErrLibraryNotExists
}
// PublishLibrary publishes a new Certificate library.
func (k Keeper) PublishLibrary(ctx sdk.Context, library sdk.AccAddress, publisher sdk.AccAddress) error {
if k.IsLibrary(ctx, library) {
return types.ErrLibraryAlreadyExists
}
k.SetLibrary(ctx, library, publisher)
return nil
}
// InvalidateLibrary invalidate a Certificate library.
func (k Keeper) InvalidateLibrary(ctx sdk.Context, library sdk.AccAddress, invalidator sdk.AccAddress) error {
if !k.IsCertifier(ctx, invalidator) {
return types.ErrUnqualifiedCertifier
}
publisher, err := k.getLibraryPublisher(ctx, library)
if err != nil {
return err
}
// Can only be invalidate if the invalidator is the original publisher, or that the original publisher is no longer certifier.
if !(publisher.Equals(invalidator) || !k.IsCertifier(ctx, publisher)) {
return types.ErrUnqualifiedCertifier
}
k.deleteLibrary(ctx, library)
return nil
}
// IterateAllLibraries iterates over the all the stored libraries and performs a callback function.
func (k Keeper) IterateAllLibraries(ctx sdk.Context, callback func(library types.Library) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.LibrariesStoreKey())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var library types.Library
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &library)
if callback(library) {
break
}
}
}
// GetAllLibraries gets all libraries.
func (k Keeper) GetAllLibraries(ctx sdk.Context) (libraries types.Libraries) {
k.IterateAllLibraries(ctx, func(library types.Library) bool {
libraries = append(libraries, library)
return false
})
return
}
// GetAllLibraryAddresses gets all library addresses.
func (k Keeper) GetAllLibraryAddresses(ctx sdk.Context) (libraryAddresses []sdk.AccAddress) {
k.IterateAllLibraries(ctx, func(library types.Library) bool {
publisherAddr, err := sdk.AccAddressFromBech32(library.Publisher)
if err != nil {
panic(err)
}
libraryAddresses = append(libraryAddresses, publisherAddr)
return false
})
return
}