Skip to content

Commit

Permalink
feat: add type constructors/validation for genesis types.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitrisJim committed Nov 21, 2024
1 parent 5afcf1d commit 815f35c
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 38 deletions.
17 changes: 17 additions & 0 deletions modules/core/04-channel/v2/types/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,20 @@ func (c Channel) Validate() error {

return nil
}

// NewIdentifiedChannel creates a new IdentifiedChannel instance
func NewIdentifiedChannel(channelID string, channel Channel) IdentifiedChannel {
return IdentifiedChannel{
Channel: channel,
ChannelId: channelID,
}
}

// ValidateBasic performs a basic validation of the identifiers and channel fields.
func (ic IdentifiedChannel) ValidateBasic() error {
if err := host.ChannelIdentifierValidator(ic.ChannelId); err != nil {
return errorsmod.Wrap(err, "invalid channel ID")
}

return ic.Channel.Validate()
}
134 changes: 134 additions & 0 deletions modules/core/04-channel/v2/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package types

import (
"errors"
"fmt"

channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// NewPacketState creates a new PacketState instance.
func NewPacketState(channelID string, sequence uint64, data []byte) PacketState {
return PacketState{
ChannelId: channelID,
Sequence: sequence,
Data: data,
}
}

// Validate performs basic validation of fields returning an error upon any failure.
func (ps PacketState) Validate() error {
if ps.Data == nil {
return errors.New("data bytes cannot be nil")
}
return validateGenFields(ps.ChannelId, ps.Sequence)
}

// NewPacketSequence creates a new PacketSequences instance.
func NewPacketSequence(channelID string, sequence uint64) PacketSequence {
return PacketSequence{
ChannelId: channelID,
Sequence: sequence,
}
}

// Validate performs basic validation of fields returning an error upon any failure.
func (ps PacketSequence) Validate() error {
return validateGenFields(ps.ChannelId, ps.Sequence)
}

// NewGenesisState creates a GenesisState instance.
func NewGenesisState(
channels []IdentifiedChannel, acks, receipts, commitments []PacketState,
sendSeqs []PacketSequence, nextChannelSequence uint64,
) GenesisState {
return GenesisState{
Channels: channels,
Acknowledgements: acks,
Receipts: receipts,
Commitments: commitments,
SendSequences: sendSeqs,
NextChannelSequence: nextChannelSequence,
}
}

// DefaultGenesisState returns the ibc channel v2 submodule's default genesis state.
func DefaultGenesisState() GenesisState {
return GenesisState{
Channels: []IdentifiedChannel{},
Acknowledgements: []PacketState{},
Receipts: []PacketState{},
Commitments: []PacketState{},
SendSequences: []PacketSequence{},
NextChannelSequence: 0,
}
}

// 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 channel identifiers.
var maxSequence uint64

for i, channel := range gs.Channels {
sequence, err := channeltypesv1.ParseChannelSequence(channel.ChannelId)
if err != nil {
return err
}

if sequence > maxSequence {
maxSequence = sequence
}

if err := channel.ValidateBasic(); err != nil {
return fmt.Errorf("invalid channel %v channel index %d: %w", channel, i, err)
}
}

if maxSequence != 0 && maxSequence >= gs.NextChannelSequence {
return fmt.Errorf("next channel sequence %d must be greater than maximum sequence used in channel identifier %d", gs.NextChannelSequence, maxSequence)
}

for i, ack := range gs.Acknowledgements {
if err := ack.Validate(); err != nil {
return fmt.Errorf("invalid acknowledgement %v ack index %d: %w", ack, i, err)
}
if len(ack.Data) == 0 {
return fmt.Errorf("invalid acknowledgement %v ack index %d: data bytes cannot be empty", ack, i)
}
}

for i, receipt := range gs.Receipts {
if err := receipt.Validate(); err != nil {
return fmt.Errorf("invalid acknowledgement %v ack index %d: %w", receipt, i, err)
}
}

for i, commitment := range gs.Commitments {
if err := commitment.Validate(); err != nil {
return fmt.Errorf("invalid commitment %v index %d: %w", commitment, i, err)
}
if len(commitment.Data) == 0 {
return fmt.Errorf("invalid acknowledgement %v ack index %d: data bytes cannot be empty", commitment, i)
}
}

for i, ss := range gs.SendSequences {
if err := ss.Validate(); err != nil {
return fmt.Errorf("invalid send sequence %v index %d: %w", ss, i, err)
}
}

return nil
}

func validateGenFields(channelID string, sequence uint64) error {
if err := host.ChannelIdentifierValidator(channelID); err != nil {
return fmt.Errorf("invalid channel Id: %w", err)
}
if sequence == 0 {
return errors.New("sequence cannot be 0")
}
return nil
}
94 changes: 66 additions & 28 deletions modules/core/04-channel/v2/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 815f35c

Please sign in to comment.