-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
54 lines (42 loc) · 1.5 KB
/
genesis.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elesto-dao/elesto/v4/x/credential"
)
func InitGenesis(ctx sdk.Context, k Keeper, genState *credential.GenesisState) {
for i := range genState.CredentialDefinitions {
k.SetCredentialDefinition(ctx, &genState.CredentialDefinitions[i])
}
for _, id := range genState.PublicCredentialDefinitionsIDs {
_, found := k.GetCredentialDefinition(ctx, id)
if !found {
panic(fmt.Sprintf("credential id %s not found", id))
}
allowed := k.IsPublicCredentialDefinitionAllowed(ctx, id)
if allowed {
panic(fmt.Sprintf("credential id %s already allowed", id))
}
k.AllowPublicCredential(ctx, id)
}
}
func ExportGenesis(ctx sdk.Context, k Keeper) *credential.GenesisState {
var genState credential.GenesisState
cdsIterator := k.GetAll(ctx, credential.CredentialDefinitionKey)
defer cdsIterator.Close()
for ; cdsIterator.Valid(); cdsIterator.Next() {
var cd credential.CredentialDefinition
ok := k.Unmarshal(cdsIterator.Value(), &cd)
if !ok {
panic(fmt.Errorf("cannot unmarshal %s", cdsIterator.Value()))
}
genState.CredentialDefinitions = append(genState.CredentialDefinitions, cd)
}
allowedCdIterator := k.GetAll(ctx, credential.PublicCredentialAllowKey)
defer allowedCdIterator.Close()
for ; allowedCdIterator.Valid(); allowedCdIterator.Next() {
id := string(allowedCdIterator.Value())
genState.PublicCredentialDefinitionsIDs = append(genState.PublicCredentialDefinitionsIDs, id)
}
return &genState
}