forked from KomodoPlatform/nucleus
/
genesis.go
65 lines (54 loc) · 1.58 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
package types
import (
fmt "fmt"
time "time"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewGenesisState constructs a new GenesisState instance
func NewGenesisState(params Params, htlcs []HTLC, Supplies []AssetSupply, previousBlockTime time.Time) *GenesisState {
return &GenesisState{
Params: params,
Htlcs: htlcs,
Supplies: Supplies,
PreviousBlockTime: previousBlockTime,
}
}
// DefaultGenesisState gets the raw genesis message for testing
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
Htlcs: []HTLC{},
Supplies: DefaultAssetSupplies(),
PreviousBlockTime: DefaultPreviousBlockTime,
}
}
// ValidateGenesis validates the provided HTLC genesis state to ensure the expected invariants holds.
func ValidateGenesis(data GenesisState) error {
if err := data.Params.Validate(); err != nil {
return err
}
ids := map[string]bool{}
for _, htlc := range data.Htlcs {
if ids[htlc.Id] {
return fmt.Errorf("found duplicate htlc ID %s", htlc.Id)
}
if htlc.State != Open {
return sdkerrors.Wrap(ErrHTLCNotOpen, htlc.Id)
}
if err := htlc.Validate(); err != nil {
return err
}
ids[htlc.Id] = true
}
supplyDenoms := map[string]bool{}
for _, supply := range data.Supplies {
if err := supply.Validate(); err != nil {
return err
}
if supplyDenoms[supply.CurrentSupply.Denom] {
return fmt.Errorf("found duplicate denom in asset supplies %s", supply.CurrentSupply.Denom)
}
supplyDenoms[supply.CurrentSupply.Denom] = true
}
return nil
}