-
Notifications
You must be signed in to change notification settings - Fork 580
/
genesis.go
82 lines (69 loc) · 2.3 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
80
81
82
package types
import (
"fmt"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
)
// NewConnectionPaths creates a ConnectionPaths instance.
func NewConnectionPaths(id string, paths []string) ConnectionPaths {
return ConnectionPaths{
ClientId: id,
Paths: paths,
}
}
// NewGenesisState creates a GenesisState instance.
func NewGenesisState(
connections []IdentifiedConnection, connPaths []ConnectionPaths,
nextConnectionSequence uint64, params Params,
) GenesisState {
return GenesisState{
Connections: connections,
ClientConnectionPaths: connPaths,
NextConnectionSequence: nextConnectionSequence,
Params: params,
}
}
// DefaultGenesisState returns the ibc connection submodule's default genesis state.
func DefaultGenesisState() GenesisState {
return GenesisState{
Connections: []IdentifiedConnection{},
ClientConnectionPaths: []ConnectionPaths{},
NextConnectionSequence: 0,
Params: DefaultParams(),
}
}
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
// keep track of the max sequence to ensure it is less than
// the next sequence used in creating connection identifers.
var maxSequence uint64
for i, conn := range gs.Connections {
sequence, err := ParseConnectionSequence(conn.Id)
if err != nil {
return err
}
if sequence > maxSequence {
maxSequence = sequence
}
if err := conn.ValidateBasic(); err != nil {
return fmt.Errorf("invalid connection %v index %d: %w", conn, i, err)
}
}
for i, conPaths := range gs.ClientConnectionPaths {
if err := host.ClientIdentifierValidator(conPaths.ClientId); err != nil {
return fmt.Errorf("invalid client connection path %d: %w", i, err)
}
for _, connectionID := range conPaths.Paths {
if err := host.ConnectionIdentifierValidator(connectionID); err != nil {
return fmt.Errorf("invalid client connection ID (%s) in connection paths %d: %w", connectionID, i, err)
}
}
}
if maxSequence != 0 && maxSequence >= gs.NextConnectionSequence {
return fmt.Errorf("next connection sequence %d must be greater than maximum sequence used in connection identifier %d", gs.NextConnectionSequence, maxSequence)
}
if err := gs.Params.Validate(); err != nil {
return err
}
return nil
}