-
Notifications
You must be signed in to change notification settings - Fork 28
/
genesis.go
46 lines (37 loc) · 1.03 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
package types
import (
"encoding/hex"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewGenesisState constructs a new GenesisState instance
func NewGenesisState(pendingHtlcs map[string]HTLC) GenesisState {
return GenesisState{
PendingHtlcs: pendingHtlcs,
}
}
// DefaultGenesisState gets the raw genesis message for testing
func DefaultGenesisState() *GenesisState {
return &GenesisState{
PendingHtlcs: map[string]HTLC{},
}
}
// ValidateGenesis validates the provided HTLC genesis state to ensure the
// expected invariants holds.
func ValidateGenesis(data GenesisState) error {
for hashLockStr, htlc := range data.PendingHtlcs {
hashLock, err := hex.DecodeString(hashLockStr)
if err != nil {
return err
}
if len(hashLock) != HashLockLength {
return sdkerrors.Wrapf(ErrInvalidHashLock, "length of the hash lock must be %d in bytes", HashLockLength)
}
if htlc.State != Open {
return sdkerrors.Wrap(ErrHTLCNotOpen, hashLockStr)
}
if err := htlc.Validate(); err != nil {
return err
}
}
return nil
}