-
Notifications
You must be signed in to change notification settings - Fork 28
/
genesis.go
78 lines (68 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package types
import (
"encoding/hex"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewGenesisState constructs a new GenesisState instance
func NewGenesisState(
params Params,
definitions []ServiceDefinition,
bindings []ServiceBinding,
withdrawAddresses map[string]string,
requestContexts map[string]*RequestContext,
) *GenesisState {
return &GenesisState{
Params: params,
Definitions: definitions,
Bindings: bindings,
WithdrawAddresses: withdrawAddresses,
RequestContexts: requestContexts,
}
}
// DefaultGenesisState gets the default genesis state for testing
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
}
}
// ValidateGenesis validates the provided service genesis state to ensure the
// expected invariants holds.
func ValidateGenesis(data GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
for _, definition := range data.Definitions {
if err := definition.Validate(); err != nil {
return err
}
}
for _, binding := range data.Bindings {
if err := binding.Validate(); err != nil {
return err
}
}
for providerAddressStr, withdrawAddress := range data.WithdrawAddresses {
if _, err := sdk.AccAddressFromBech32(providerAddressStr); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(withdrawAddress); err != nil {
return err
}
}
for requestContextID, requestContext := range data.RequestContexts {
if _, err := hex.DecodeString(requestContextID); err != nil {
return err
}
if err := requestContext.Validate(); err != nil {
return err
}
if requestContext.State != PAUSED {
return fmt.Errorf("invalid request context state, ID:%s, State:%s", requestContextID, requestContext.State)
}
if requestContext.BatchState != BATCHCOMPLETED {
return fmt.Errorf("invalid request context batch state, ID:%s, BatchState:%s", requestContextID, requestContext.BatchState)
}
}
return nil
}