-
Notifications
You must be signed in to change notification settings - Fork 568
/
genesis.go
71 lines (59 loc) · 1.75 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
package types
import (
"encoding/json"
"errors"
"fmt"
time "time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func NewGenesisState(params Params, lockableDurations []time.Duration, distrInfo *DistrInfo) *GenesisState {
return &GenesisState{
Params: params,
LockableDurations: lockableDurations,
DistrInfo: distrInfo,
}
}
// DefaultGenesisState gets the raw genesis raw message for testing.
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
LockableDurations: []time.Duration{
time.Hour,
time.Hour * 3,
time.Hour * 7,
},
DistrInfo: &DistrInfo{
TotalWeight: sdk.ZeroInt(),
Records: nil,
},
}
}
// GetGenesisStateFromAppState returns x/pool-yield GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
}
return &genesisState
}
// ValidateGenesis validates the provided pool-yield genesis state to ensure the
// expected invariants holds. (i.e. params in correct bounds).
func ValidateGenesis(data *GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
if data.DistrInfo.TotalWeight.LT(sdk.NewInt(0)) {
return errors.New("distrinfo weight should not be negative")
}
return validateLockableDurations(data.LockableDurations)
}
// validateLockableDurations ensures the provided lockable duration is a valid parameter.
func validateLockableDurations(i interface{}) error {
_, ok := i.([]time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}