Skip to content

Commit

Permalink
parse errors and allow project id zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Nov 6, 2024
1 parent f29c1af commit 71ff2dc
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 42 deletions.
14 changes: 6 additions & 8 deletions network/cmd/network_chain_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ the "--account-balance" flag with a list of coins.
c.Flags().String(flagGenesisURL, "", "URL to a custom Genesis")
c.Flags().String(flagGenesisConfig, "", "name of an Ignite config file in the repo for custom Genesis")
c.Flags().String(flagChainID, "", "chain ID to use for this network")
c.Flags().Uint64(flagProject, 0, "project ID to use for this network")
c.Flags().Int64(flagProject, -1, "project ID to use for this network")
c.Flags().Bool(flagNoCheck, false, "skip verifying chain's integrity")
c.Flags().String(flagMetadata, "", "add chain metadata")
c.Flags().String(flagProjectTotalSupply, "", "add a total of the mainnet of a project")
Expand Down Expand Up @@ -116,7 +116,7 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
genesisURL, _ = cmd.Flags().GetString(flagGenesisURL)
genesisConfig, _ = cmd.Flags().GetString(flagGenesisConfig)
chainID, _ = cmd.Flags().GetString(flagChainID)
project, _ = cmd.Flags().GetUint64(flagProject)
project, _ = cmd.Flags().GetInt64(flagProject)
noCheck, _ = cmd.Flags().GetBool(flagNoCheck)
metadata, _ = cmd.Flags().GetString(flagMetadata)
projectTotalSupplyStr, _ = cmd.Flags().GetString(flagProjectTotalSupply)
Expand Down Expand Up @@ -149,11 +149,11 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
return err
}

if project != 0 && projectTotalSupplyStr != "" {
if project > -1 && projectTotalSupplyStr != "" {
return fmt.Errorf("%s and %s flags cannot be set together", flagProject, flagProjectTotalSupply)
}
if isMainnet {
if project == 0 && projectTotalSupplyStr == "" {
if project < 0 && projectTotalSupplyStr == "" {
return fmt.Errorf(
"%s flag requires one of the %s or %s flags to be set",
flagMainnet,
Expand Down Expand Up @@ -248,7 +248,7 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {

}

if project != 0 {
if project > -1 {
publishOptions = append(publishOptions, network.WithProject(project))
} else if projectTotalSupplyStr != "" {
totalSupply, err := sdk.ParseCoinsNormalized(projectTotalSupplyStr)
Expand Down Expand Up @@ -358,9 +358,7 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error {
} else {
session.Printf("%s Launch ID: %d \n", icons.Bullet, launchID)
}
if projectID != 0 {
session.Printf("%s Project ID: %d \n", icons.Bullet, projectID)
}
session.Printf("%s Project ID: %d \n", icons.Bullet, projectID)

return nil
}
4 changes: 1 addition & 3 deletions network/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ignite/cli/v28/ignite/pkg/cosmoserror"
monitoringctypes "github.com/ignite/network/x/monitoringc/types"

"github.com/ignite/apps/network/network/networktypes"
Expand Down Expand Up @@ -52,7 +50,7 @@ func (n Network) verifiedClientIDs(ctx context.Context, launchID uint64) ([]stri
},
)

if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return nil, ErrObjectNotFound
} else if err != nil {
return nil, err
Expand Down
22 changes: 22 additions & 0 deletions network/network/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package network

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func isNotFoundErr(err error) bool {
s, ok := status.FromError(err)
if ok {
switch s.Code() {
case codes.NotFound:
return true
case codes.Unknown:
if s.Message() == "not found: unknown request" {
return true
}
return false
}
}
return false
}
10 changes: 4 additions & 6 deletions network/network/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"encoding/base64"
"errors"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/ignite/cli/v28/ignite/pkg/cosmoserror"
spntypes "github.com/ignite/network/pkg/types"
monitoringptypes "github.com/ignite/network/x/monitoringp/types"

Expand Down Expand Up @@ -133,7 +131,7 @@ func (n Node) connectionChannels(ctx context.Context, connectionID string) (chan
res, err := n.ibcChannelQuery.ConnectionChannels(ctx, &ibcchanneltypes.QueryConnectionChannelsRequest{
Connection: connectionID,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return channels, nil
} else if err != nil {
return nil, err
Expand All @@ -149,7 +147,7 @@ func (n Node) clientConnections(ctx context.Context, clientID string) ([]string,
res, err := n.ibcConnQuery.ClientConnections(ctx, &ibcconntypes.QueryClientConnectionsRequest{
ClientId: clientID,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return []string{}, nil
} else if err != nil {
return nil, err
Expand All @@ -171,7 +169,7 @@ func (n Node) consumerClientID(ctx context.Context) (string, error) {
res, err := n.monitoringProviderQuery.GetConsumerClientID(
ctx, &monitoringptypes.QueryGetConsumerClientIDRequest{},
)
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return "", ErrObjectNotFound
} else if err != nil {
return "", err
Expand All @@ -184,7 +182,7 @@ func (n Node) connectionChannelID(ctx context.Context) (string, error) {
res, err := n.monitoringProviderQuery.GetConnectionChannelID(
ctx, &monitoringptypes.QueryGetConnectionChannelIDRequest{},
)
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return "", ErrObjectNotFound
} else if err != nil {
return "", err
Expand Down
10 changes: 4 additions & 6 deletions network/network/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ignite/cli/v28/ignite/pkg/cosmoserror"
"github.com/ignite/cli/v28/ignite/pkg/events"
profiletypes "github.com/ignite/network/x/profile/types"
projecttypes "github.com/ignite/network/x/project/types"
Expand All @@ -24,7 +22,7 @@ func (n Network) CoordinatorIDByAddress(ctx context.Context, address string) (ui
},
)

if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return 0, ErrObjectNotFound
} else if err != nil {
return 0, err
Expand Down Expand Up @@ -88,7 +86,7 @@ func (n Network) Coordinator(ctx context.Context, address string) (networktypes.
CoordinatorId: coordinatorID,
},
)
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.Coordinator{}, ErrObjectNotFound
} else if err != nil {
return networktypes.Coordinator{}, err
Expand Down Expand Up @@ -129,7 +127,7 @@ func (n Network) Validator(ctx context.Context, address string) (networktypes.Va
res, err := n.profileQuery.GetValidator(ctx, &profiletypes.QueryGetValidatorRequest{
Address: address,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.Validator{}, ErrObjectNotFound
} else if err != nil {
return networktypes.Validator{}, err
Expand All @@ -145,7 +143,7 @@ func (n Network) Balances(ctx context.Context, address string) (sdk.Coins, error
Address: address,
},
)
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return sdk.Coins{}, ErrObjectNotFound
} else if err != nil {
return sdk.Coins{}, err
Expand Down
5 changes: 1 addition & 4 deletions network/network/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package network

import (
"context"
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ignite/cli/v28/ignite/pkg/cosmoserror"
"github.com/ignite/cli/v28/ignite/pkg/events"
projecttypes "github.com/ignite/network/x/project/types"

Expand Down Expand Up @@ -53,7 +50,7 @@ func (n Network) Project(ctx context.Context, projectID uint64) (networktypes.Pr
res, err := n.projectQuery.GetProject(ctx, &projecttypes.QueryGetProjectRequest{
ProjectId: projectID,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.Project{}, ErrObjectNotFound
} else if err != nil {
return networktypes.Project{}, err
Expand Down
21 changes: 13 additions & 8 deletions network/network/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ type publishOptions struct {
genesisURL string
genesisConfig string
chainID string
projectID uint64
projectID int64
metadata string
totalSupply sdk.Coins
sharePercentages SharePercents
mainnet bool
accountBalance sdk.Coins
}

// hasProject check if the option has a project set.
func (o publishOptions) hasProject() bool {
return o.projectID >= 0
}

// PublishOption configures chain creation.
type PublishOption func(*publishOptions)

// WithProject add a project id.
func WithProject(id uint64) PublishOption {
func WithProject(id int64) PublishOption {
return func(o *publishOptions) {
o.projectID = id
}
Expand Down Expand Up @@ -98,7 +103,7 @@ func Mainnet() PublishOption {

// Publish submits Genesis to SPN to announce a new network.
func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption) (launchID, projectID uint64, err error) {
o := publishOptions{}
o := publishOptions{projectID: -1}
for _, apply := range options {
apply(&o)
}
Expand Down Expand Up @@ -141,7 +146,7 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption)
if err != nil {
return 0, 0, err
}
projectID = o.projectID
projectID = uint64(o.projectID)

n.ev.Send("Publishing the network", events.ProgressStart())

Expand All @@ -162,9 +167,9 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption)
}

// check if a project associated to the chain is provided
if projectID != 0 {
if o.hasProject() {
_, err = n.projectQuery.GetProject(ctx, &projecttypes.QueryGetProjectRequest{
ProjectId: o.projectID,
ProjectId: projectID,
})
if err != nil {
return 0, 0, err
Expand All @@ -179,7 +184,7 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption)
}

// mint vouchers
if projectID != 0 && !o.sharePercentages.Empty() {
if o.hasProject() && !o.sharePercentages.Empty() {
totalSharesResp, err := n.projectQuery.TotalShares(ctx, &projecttypes.QueryTotalSharesRequest{})
if err != nil {
return 0, 0, err
Expand Down Expand Up @@ -251,7 +256,7 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption)
c.SourceURL(),
c.SourceHash(),
initialGenesis,
projectID != 0,
o.hasProject(),
projectID,
o.accountBalance,
metadata,
Expand Down
12 changes: 5 additions & 7 deletions network/network/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"sort"
"sync"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/ignite/cli/v28/ignite/pkg/cosmoserror"
"github.com/ignite/cli/v28/ignite/pkg/events"
launchtypes "github.com/ignite/network/x/launch/types"
projecttypes "github.com/ignite/network/x/project/types"
Expand Down Expand Up @@ -190,7 +188,7 @@ func (n Network) MainnetAccount(
ProjectId: projectID,
Address: address,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.MainnetAccount{}, ErrObjectNotFound
} else if err != nil {
return acc, err
Expand Down Expand Up @@ -222,7 +220,7 @@ func (n Network) GenesisAccount(ctx context.Context, launchID uint64, address st
LaunchId: launchID,
Address: address,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.GenesisAccount{}, ErrObjectNotFound
} else if err != nil {
return networktypes.GenesisAccount{}, err
Expand All @@ -236,7 +234,7 @@ func (n Network) VestingAccount(ctx context.Context, launchID uint64, address st
LaunchId: launchID,
Address: address,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.VestingAccount{}, ErrObjectNotFound
} else if err != nil {
return networktypes.VestingAccount{}, err
Expand All @@ -250,7 +248,7 @@ func (n Network) GenesisValidator(ctx context.Context, launchID uint64, address
LaunchId: launchID,
Address: address,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return networktypes.GenesisValidator{}, ErrObjectNotFound
} else if err != nil {
return networktypes.GenesisValidator{}, err
Expand All @@ -263,7 +261,7 @@ func (n Network) ChainReward(ctx context.Context, launchID uint64) (rewardtypes.
res, err := n.rewardQuery.GetRewardPool(ctx, &rewardtypes.QueryGetRewardPoolRequest{
LaunchId: launchID,
})
if errors.Is(cosmoserror.Unwrap(err), sdkerrors.ErrNotFound) {
if isNotFoundErr(err) {
return rewardtypes.RewardPool{}, ErrObjectNotFound
} else if err != nil {
return rewardtypes.RewardPool{}, err
Expand Down

0 comments on commit 71ff2dc

Please sign in to comment.