Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query for poa chains #1090

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
2 changes: 2 additions & 0 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/cosmos/relayer/v2/relayer"
"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/spf13/cobra"
registry "github.com/strangelove-ventures/lens/client/chain_registry"
"go.uber.org/zap"
Expand Down Expand Up @@ -476,6 +477,7 @@ func addChainsFromRegistry(ctx context.Context, a *appState, chains []string) er
OutputFormat: chainConfig.OutputFormat,
SignModeStr: chainConfig.SignModeStr,
ExtraCodecs: chainConfig.ExtraCodecs,
Broadcast: provider.BroadcastModeBatch,
Slip44: chainConfig.Slip44,
}

Expand Down
2 changes: 2 additions & 0 deletions interchaintest/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cosmos/relayer/v2/internal/relayertest"
"github.com/cosmos/relayer/v2/relayer"
"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/cosmos/relayer/v2/relayer/provider"
interchaintestcosmos "github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,6 +78,7 @@ func (r *Relayer) AddChainConfiguration(ctx context.Context, _ ibc.RelayerExecRe
Timeout: "10s",
OutputFormat: "json",
SignModeStr: "direct",
Broadcast: provider.BroadcastModeBatch,
},
})

Expand Down
5 changes: 4 additions & 1 deletion relayer/chains/cosmos/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (cc *CosmosProvider) LogFailedTx(res *provider.RelayerTxResponse, err error
}
}

if res.Code != 0 && res.Data != "" {
if res.Code != 0 {
if sdkErr := cc.sdkError(res.Codespace, res.Code); err != nil {
fields = append(fields, zap.NamedError("sdk_error", sdkErr))
}
fields = append(fields, zap.Object("response", res))
cc.log.Warn(
"Sent transaction but received failure response",
Expand Down
59 changes: 31 additions & 28 deletions relayer/chains/cosmos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ var (
const tendermintEncodingThreshold = "v0.37.0-alpha"

type CosmosProviderConfig struct {
Key string `json:"key" yaml:"key"`
ChainName string `json:"-" yaml:"-"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
GasAdjustment float64 `json:"gas-adjustment" yaml:"gas-adjustment"`
GasPrices string `json:"gas-prices" yaml:"gas-prices"`
MinGasAmount uint64 `json:"min-gas-amount" yaml:"min-gas-amount"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
OutputFormat string `json:"output-format" yaml:"output-format"`
SignModeStr string `json:"sign-mode" yaml:"sign-mode"`
ExtraCodecs []string `json:"extra-codecs" yaml:"extra-codecs"`
Slip44 int `json:"coin-type" yaml:"coin-type"`
Key string `json:"key" yaml:"key"`
ChainName string `json:"-" yaml:"-"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
GasAdjustment float64 `json:"gas-adjustment" yaml:"gas-adjustment"`
GasPrices string `json:"gas-prices" yaml:"gas-prices"`
MinGasAmount uint64 `json:"min-gas-amount" yaml:"min-gas-amount"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
OutputFormat string `json:"output-format" yaml:"output-format"`
SignModeStr string `json:"sign-mode" yaml:"sign-mode"`
ExtraCodecs []string `json:"extra-codecs" yaml:"extra-codecs"`
Slip44 int `json:"coin-type" yaml:"coin-type"`
Broadcast provider.BroadcastMode `json:"broadcast-mode" yaml:"broadcast-mode"`
}

func (pc CosmosProviderConfig) Validate() error {
Expand All @@ -55,6 +56,10 @@ func (pc CosmosProviderConfig) Validate() error {
return nil
}

func (pc CosmosProviderConfig) BroadcastMode() provider.BroadcastMode {
return pc.Broadcast
}

// NewProvider validates the CosmosProviderConfig, instantiates a ChainClient and then instantiates a CosmosProvider
func (pc CosmosProviderConfig) NewProvider(log *zap.Logger, homepath string, debug bool, chainName string) (provider.ChainProvider, error) {
if err := pc.Validate(); err != nil {
Expand All @@ -72,6 +77,10 @@ func (pc CosmosProviderConfig) NewProvider(log *zap.Logger, homepath string, deb
}
pc.ChainName = chainName

if pc.Broadcast == "" {
pc.Broadcast = provider.BroadcastModeBatch
}

return &CosmosProvider{
log: log,
ChainClient: *cc,
Expand Down Expand Up @@ -136,10 +145,14 @@ func (h CosmosIBCHeader) ConsensusState() ibcexported.ConsensusState {
return &tmclient.ConsensusState{
Timestamp: h.SignedHeader.Time,
Root: commitmenttypes.NewMerkleRoot(h.SignedHeader.AppHash),
NextValidatorsHash: h.ValidatorSet.Hash(),
NextValidatorsHash: h.SignedHeader.NextValidatorsHash,
}
}

func (h CosmosIBCHeader) NextValidatorsHash() []byte {
return h.SignedHeader.NextValidatorsHash
}

func (cc *CosmosProvider) ProviderConfig() provider.ProviderConfig {
return cc.PCfg
}
Expand Down Expand Up @@ -207,19 +220,9 @@ func (cc *CosmosProvider) Address() (string, error) {
}

func (cc *CosmosProvider) TrustingPeriod(ctx context.Context) (time.Duration, error) {
res, err := cc.QueryStakingParams(ctx)

var unbondingTime time.Duration
unbondingTime, err := cc.QueryUnbondingPeriod(ctx)
if err != nil {
// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
if consumerErr != nil {
return 0,
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
}
unbondingTime = consumerUnbondingPeriod
} else {
unbondingTime = res.UnbondingTime
return 0, err
}

// We want the trusting period to be 85% of the unbonding time.
Expand Down
38 changes: 21 additions & 17 deletions relayer/chains/cosmos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,47 +178,51 @@ func (cc *CosmosProvider) QueryBalanceWithAddress(ctx context.Context, address s
return coins, nil
}

func (cc *CosmosProvider) queryConsumerUnbondingPeriod(ctx context.Context) (time.Duration, error) {
func (cc *CosmosProvider) querySubspaceUnbondingPeriod(subspace string, ctx context.Context) (time.Duration, error) {
queryClient := proposal.NewQueryClient(cc)

params := proposal.QueryParamsRequest{Subspace: "ccvconsumer", Key: "UnbondingPeriod"}
params := proposal.QueryParamsRequest{Subspace: subspace, Key: "UnbondingPeriod"}

resICS, err := queryClient.Params(ctx, &params)

if err != nil {
return 0, fmt.Errorf("failed to make ccvconsumer params request: %w", err)
return 0, fmt.Errorf("failed to make %s params request: %w", subspace, err)
}

if resICS.Param.Value == "" {
return 0, fmt.Errorf("ccvconsumer unbonding period is empty")
return 0, fmt.Errorf("%s unbonding period is empty", subspace)
}

unbondingPeriod, err := strconv.ParseUint(strings.ReplaceAll(resICS.Param.Value, `"`, ""), 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse unbonding period from ccvconsumer param: %w", err)
return 0, fmt.Errorf("failed to parse unbonding period from %s param: %w", subspace, err)
}

return time.Duration(unbondingPeriod), nil
}

// QueryUnbondingPeriod returns the unbonding period of the chain
func (cc *CosmosProvider) QueryUnbondingPeriod(ctx context.Context) (time.Duration, error) {
req := stakingtypes.QueryParamsRequest{}
queryClient := stakingtypes.NewQueryClient(cc)

res, err := queryClient.Params(ctx, &req)
if err != nil {
// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.queryConsumerUnbondingPeriod(ctx)
if consumerErr != nil {
return 0,
fmt.Errorf("failed to query unbonding period as both standard and consumer chain: %s: %w", err.Error(), consumerErr)
}
res, err := cc.QueryStakingParams(ctx)
if err == nil {
return res.UnbondingTime, nil
}

// Attempt ICS query
consumerUnbondingPeriod, consumerErr := cc.querySubspaceUnbondingPeriod("ccvconsumer", ctx)
if consumerErr == nil {
return consumerUnbondingPeriod, nil
}

return res.Params.UnbondingTime, nil
poaUnbondingPeriod, poaErr := cc.querySubspaceUnbondingPeriod("poa", ctx)
if poaErr == nil {
return poaUnbondingPeriod, nil
}

return 0, fmt.Errorf(
"failed to query unbonding period as both standard, consumer, and poa chain: %s, %s, %s",
err.Error(), consumerErr.Error(), poaErr.Error(),
)
}

// QueryTendermintProof performs an ABCI query with the given key and returns
Expand Down
Loading