Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions ignite/services/network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,41 @@ func (n Network) verifiedClientIDs(ctx context.Context, launchID uint64) ([]stri
return res.ClientIds, nil
}

// FindClientID find client, connection and channel id by the chain launch id
func (n Network) FindClientID(ctx context.Context, launchID uint64) (relayer networktypes.Relayer, err error) {
// RewardIBCInfo returns IBC info to relay packets for a chain to claim rewards.
func (n Network) RewardIBCInfo(ctx context.Context, launchID uint64) (networktypes.RewardIBCInfo, error) {
clientStates, err := n.verifiedClientIDs(ctx, launchID)
if err != nil {
return relayer, err
return networktypes.RewardIBCInfo{}, err
}
if len(clientStates) == 0 {
return relayer, ErrObjectNotFound
return networktypes.RewardIBCInfo{}, ErrObjectNotFound
}
relayer.ClientID = clientStates[0]

connections, err := n.node.clientConnections(ctx, relayer.ClientID)
clientID := clientStates[0]

connections, err := n.node.clientConnections(ctx, clientID)
if err != nil && err != ErrObjectNotFound {
return relayer, err
return networktypes.RewardIBCInfo{}, err
}
if err == ErrObjectNotFound || len(connections) == 0 {
return relayer, nil
return networktypes.RewardIBCInfo{}, nil
}
relayer.ConnectionID = connections[0]

channels, err := n.node.connectionChannels(ctx, relayer.ConnectionID)
connectionID := connections[0]

channels, err := n.node.connectionChannels(ctx, connectionID)
if err != nil && err != ErrObjectNotFound {
return relayer, err
return networktypes.RewardIBCInfo{}, err
}
if err == ErrObjectNotFound || len(connections) == 0 {
return relayer, nil
return networktypes.RewardIBCInfo{}, nil
}
relayer.ChannelID = channels[0]
return relayer, nil

info := networktypes.RewardIBCInfo{
ClientID: clientID,
ConnectionID: connectionID,
ChannelID: channels[0],
}

return info, nil
}
22 changes: 11 additions & 11 deletions ignite/services/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ import (
"github.com/ignite-hq/cli/ignite/pkg/events"
)

//go:generate mockery --name CosmosClient --case underscore
type CosmosClient interface {
Account(accountName string) (cosmosaccount.Account, error)
Address(accountName string) (sdktypes.AccAddress, error)
Context() client.Context
BroadcastTx(accountName string, msgs ...sdktypes.Msg) (cosmosclient.Response, error)
BroadcastTxWithProvision(accountName string, msgs ...sdktypes.Msg) (gas uint64, broadcast func() (cosmosclient.Response, error), err error)
Status(ctx context.Context) (*ctypes.ResultStatus, error)
ConsensusInfo(ctx context.Context, height int64) (cosmosclient.ConsensusInfo, error)
}

// Network is network builder.
type Network struct {
node Node
Expand All @@ -46,6 +35,17 @@ type Network struct {
monitoringProviderQuery monitoringptypes.QueryClient
}

//go:generate mockery --name CosmosClient --case underscore
type CosmosClient interface {
Account(accountName string) (cosmosaccount.Account, error)
Address(accountName string) (sdktypes.AccAddress, error)
Context() client.Context
BroadcastTx(accountName string, msgs ...sdktypes.Msg) (cosmosclient.Response, error)
BroadcastTxWithProvision(accountName string, msgs ...sdktypes.Msg) (gas uint64, broadcast func() (cosmosclient.Response, error), err error)
Status(ctx context.Context) (*ctypes.ResultStatus, error)
ConsensusInfo(ctx context.Context, height int64) (cosmosclient.ConsensusInfo, error)
}

//go:generate mockery --name Chain --case underscore
type Chain interface {
ID() (string, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type (
ValidatorSet spntypes.ValidatorSet
RevisionHeight uint64
}
// Relayer is the relayer connection info.
Relayer struct {

// RewardIBCInfo holds IBC info to relay packets to claim rewards.
RewardIBCInfo struct {
ChainID string
ClientID string
ConnectionID string
Expand Down
106 changes: 62 additions & 44 deletions ignite/services/network/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,71 @@ func NewNode(cosmos CosmosClient) Node {
}
}

// RewardsInfo Fetches the consensus state with the validator set and the unbounding time
func (n Node) RewardsInfo(ctx context.Context) (
reward networktypes.Reward,
chainID string,
unbondingTimeSeconds int64,
err error,
) {
status, err := n.cosmos.Status(ctx)
if err != nil {
return networktypes.Reward{}, "", 0, err
}
lastBlockHeight := status.SyncInfo.LatestBlockHeight

info, err := n.consensus(ctx, n.cosmos, lastBlockHeight)
if err != nil {
return networktypes.Reward{}, "", 0, err
}

stakingParams, err := n.stakingParams(ctx)
if err != nil {
return networktypes.Reward{}, "", 0, err
}

return info,
status.NodeInfo.Network,
int64(stakingParams.UnbondingTime.Seconds()),
nil
}

// RewardIBCInfo returns IBC info to relay packets to claim rewards for the chain.
func (n Node) RewardIBCInfo(ctx context.Context) (networktypes.RewardIBCInfo, error) {
clientID, err := n.consumerClientID(ctx)
if err != nil && err != ErrObjectNotFound {
return networktypes.RewardIBCInfo{}, err
}

channelID, err := n.connectionChannelID(ctx)
if err != nil && err != ErrObjectNotFound {
return networktypes.RewardIBCInfo{}, err
}

connections, err := n.clientConnections(ctx, clientID)
if err != nil && err != ErrObjectNotFound {
return networktypes.RewardIBCInfo{}, err
}

info := networktypes.RewardIBCInfo{
ClientID: clientID,
ChannelID: channelID,
}

if len(connections) > 0 {
info.ConnectionID = connections[0]
}

return info, nil
}

// consensus Fetches the consensus state with the validator set
func (n Node) consensus(ctx context.Context, client CosmosClient, height int64) (networktypes.Reward, error) {
consensusState, err := client.ConsensusInfo(ctx, height)
if err != nil {
return networktypes.Reward{}, err
}

spnConsensusState := spntypes.NewConsensusState(
consensusState.Timestamp,
consensusState.NextValidatorsHash,
Expand All @@ -58,31 +117,13 @@ func (n Node) consensus(ctx context.Context, client CosmosClient, height int64)
)
}

return networktypes.Reward{
reward := networktypes.Reward{
ConsensusState: spnConsensusState,
ValidatorSet: spntypes.NewValidatorSet(validators...),
RevisionHeight: uint64(height),
}, nil
}

// FindClientID find client, connection and channel id by the chain id
func (n Node) FindClientID(ctx context.Context) (relayer networktypes.Relayer, err error) {
relayer.ClientID, err = n.consumerClientID(ctx)
if err != nil && err != ErrObjectNotFound {
return
}
relayer.ChannelID, err = n.connectionChannelID(ctx)
if err != nil && err != ErrObjectNotFound {
return
}
connections, err := n.clientConnections(ctx, relayer.ClientID)
if err != nil && err != ErrObjectNotFound {
return
}
if len(connections) > 0 {
relayer.ConnectionID = connections[0]
}
return

return reward, nil
}

// connectionChannels fetches the chain connection channels by connection id
Expand Down Expand Up @@ -148,26 +189,3 @@ func (n Node) connectionChannelID(ctx context.Context) (string, error) {
}
return res.ConnectionChannelID.ChannelID, nil
}

// RewardsInfo Fetches the consensus state with the validator set and the unbounding time
func (n Node) RewardsInfo(ctx context.Context) (networktypes.Reward, string, int64, error) {
status, err := n.cosmos.Status(ctx)
if err != nil {
return networktypes.Reward{}, "", 0, err
}
lastBlockHeight := status.SyncInfo.LatestBlockHeight

info, err := n.consensus(ctx, n.cosmos, lastBlockHeight)
if err != nil {
return networktypes.Reward{}, "", 0, err
}

stakingParams, err := n.stakingParams(ctx)
if err != nil {
return networktypes.Reward{}, "", 0, err
}
return info,
status.NodeInfo.Network,
int64(stakingParams.UnbondingTime.Seconds()),
nil
}