-
Notifications
You must be signed in to change notification settings - Fork 15
/
genesis.go
79 lines (64 loc) · 2.07 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
79
package slashing
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GenesisState defines genesis data for the module
type GenesisState struct {
Slashes []Slash `json:"slashes"`
Params Params `json:"params"`
}
// NewGenesisState creates a new genesis state.
func NewGenesisState() GenesisState {
return GenesisState{
Slashes: []Slash{},
Params: DefaultParams(),
}
}
// DefaultGenesisState returns a default genesis state
func DefaultGenesisState() GenesisState { return NewGenesisState() }
// InitGenesis initializes slashing state from genesis file
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
counter := make(map[uint64]uint64)
for _, slash := range data.Slashes {
keeper.setSlash(ctx, slash)
count, ok := counter[slash.ArgumentID]
if !ok {
count = 0
}
count = count + 1
counter[slash.ArgumentID] = count
keeper.setCreatorSlash(ctx, slash.Creator, slash.ID)
keeper.setSlashCount(ctx, slash.ArgumentID, count)
keeper.setArgumentSlash(ctx, slash.ArgumentID, slash.ID)
keeper.setArgumentSlasherSlash(ctx, slash.ArgumentID, slash.ID, slash.Creator)
}
keeper.setSlashID(ctx, uint64(len(data.Slashes)+1))
keeper.SetParams(ctx, data.Params)
}
// ExportGenesis exports the genesis state
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
return GenesisState{
Slashes: keeper.Slashes(ctx),
Params: keeper.GetParams(ctx),
}
}
// ValidateGenesis validates the genesis state data
func ValidateGenesis(data GenesisState) error {
if data.Params.MinSlashCount < 1 {
return fmt.Errorf("Param: MinSlashCount, must have a positive value")
}
if data.Params.SlashMagnitude < 1 {
return fmt.Errorf("Param: SlashMagnitude, must have a positive value")
}
if data.Params.SlashMinStake.IsNegative() {
return fmt.Errorf("Param: SlashMinStake, cannot be a negative value")
}
if len(data.Params.SlashAdmins) < 1 {
return fmt.Errorf("Param: SlashAdmins, must have atleast one admin")
}
if data.Params.CuratorShare.IsNegative() {
return fmt.Errorf("Param: CuratorShare, cannot be a negative value")
}
return nil
}